From 6694c79617f6b7c4dac6008c5c59aaf20968664f Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 13 Jul 2023 14:44:27 +0300 Subject: [PATCH] Refactor form usage --- .../src/Endpoint/CreateOrderEndpoint.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index 686b87dd8..184ec40b7 100644 --- a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -166,6 +166,13 @@ class CreateOrderEndpoint implements EndpointInterface { */ protected $logger; + /** + * The form data, or empty if not available. + * + * @var array + */ + private $form = array(); + /** * CreateOrderEndpoint constructor. * @@ -276,18 +283,20 @@ class CreateOrderEndpoint implements EndpointInterface { $this->set_bn_code( $data ); - $form_fields = $data['form'] ?? null; + if ( isset( $data['form'] ) ) { + $this->form = $data['form']; + } if ( $this->early_validation_enabled - && is_array( $form_fields ) + && $this->form && 'checkout' === $data['context'] && in_array( $payment_method, array( PayPalGateway::ID, CardButtonGateway::ID ), true ) ) { - $this->validate_form( $form_fields ); + $this->validate_form( $this->form ); } - if ( 'pay-now' === $data['context'] && is_array( $form_fields ) && get_option( 'woocommerce_terms_page_id', '' ) !== '' ) { - $this->validate_paynow_form( $form_fields ); + if ( 'pay-now' === $data['context'] && $this->form && get_option( 'woocommerce_terms_page_id', '' ) !== '' ) { + $this->validate_paynow_form( $this->form ); } try { @@ -510,11 +519,9 @@ class CreateOrderEndpoint implements EndpointInterface { $payer = $this->payer_factory->from_paypal_response( json_decode( wp_json_encode( $data['payer'] ) ) ); } - if ( ! $payer && isset( $data['form'] ) ) { - $form_fields = $data['form']; - - if ( is_array( $form_fields ) && isset( $form_fields['billing_email'] ) && '' !== $form_fields['billing_email'] ) { - return $this->payer_factory->from_checkout_form( $form_fields ); + if ( ! $payer && $this->form ) { + if ( isset( $this->form['billing_email'] ) && '' !== $this->form['billing_email'] ) { + return $this->payer_factory->from_checkout_form( $this->form ); } }