From f2f3633df4d2ca7644ec0a30ba7f90aa152fe411 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 15 Jun 2021 14:44:47 +0200 Subject: [PATCH] Refactoring --- .../Endpoint/class-createorderendpoint.php | 124 ++++++++++-------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php b/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php index 932ca01a7..cadc0e0fd 100644 --- a/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php +++ b/modules/ppcp-button/src/Endpoint/class-createorderendpoint.php @@ -175,36 +175,10 @@ class CreateOrderEndpoint implements EndpointInterface { if ( 'checkout' === $data['context'] ) { if ( '1' === $data['createaccount'] ) { - $form_values = explode( '&', $data['form'] ); - $parsed_values = array(); - foreach ( $form_values as $field ) { - $field = explode( '=', $field ); - - if ( count( $field ) !== 2 ) { - continue; - } - $parsed_values[ $field[0] ] = $field[1]; - } - $_POST = $parsed_values; - $_REQUEST = $parsed_values; - - add_action( - 'woocommerce_after_checkout_validation', - function( array $data, \WP_Error $errors ) use ( $wc_order ) { - if ( ! $errors->errors ) { - $order = $this->create_paypal_order( $wc_order ); - wp_send_json_success( $order->to_array() ); - return true; - } - }, - 10, - 2 - ); - - $checkout = \WC()->checkout(); - $checkout->process_checkout(); + $this->process_checkout_form_when_creating_account( $data['form'], $wc_order ); } - $this->process_checkout_form( $data['form'] ); + + $this->process_checkout_form( $data['form'] ); } if ( 'pay-now' === $data['context'] && get_option( 'woocommerce_terms_page_id', '' ) !== '' ) { $this->validate_paynow_form( $data['form'] ); @@ -231,6 +205,43 @@ class CreateOrderEndpoint implements EndpointInterface { return false; } + /** + * Once the checkout has been validated we execute this method. + * + * @param array $data The data. + * @param \WP_Error $errors The errors, which occurred. + * + * @return array + */ + public function after_checkout_validation( array $data, \WP_Error $errors ): array { + if ( ! $errors->errors ) { + + $order = $this->create_paypal_order(); + + /** + * In case we are onboarded and everything is fine with the \WC_Order + * we want this order to be created. We will intercept it and leave it + * in the "Pending payment" status though, which than later will change + * during the "onApprove"-JS callback or the webhook listener. + */ + if ( ! $this->early_order_handler->should_create_early_order() ) { + wp_send_json_success( $order->to_array() ); + } + $this->early_order_handler->register_for_order( $order ); + return $data; + } + + wp_send_json_error( + array( + 'name' => '', + 'message' => $errors->get_error_message(), + 'code' => (int) $errors->get_error_code(), + 'details' => array(), + ) + ); + return $data; + } + /** * Creates the order in the PayPal, uses data from WC order if provided. * @@ -371,39 +382,40 @@ class CreateOrderEndpoint implements EndpointInterface { } /** - * Once the checkout has been validated we execute this method. + * Processes checkout and creates the PayPal order after success form validation. * - * @param array $data The data. - * @param \WP_Error $errors The errors, which occurred. - * - * @return array + * @param string $form_values The values of the form. + * @param \WC_Order|null $wc_order WC order to get data from. + * @throws \Exception On Error. */ - public function after_checkout_validation( array $data, \WP_Error $errors ): array { - if ( ! $errors->errors ) { + private function process_checkout_form_when_creating_account( string $form_values, \WC_Order $wc_order = null ) { + $form_values = explode( '&', $form_values ); + $parsed_values = array(); + foreach ( $form_values as $field ) { + $field = explode( '=', $field ); - $order = $this->create_paypal_order(); - - /** - * In case we are onboarded and everything is fine with the \WC_Order - * we want this order to be created. We will intercept it and leave it - * in the "Pending payment" status though, which than later will change - * during the "onApprove"-JS callback or the webhook listener. - */ - if ( ! $this->early_order_handler->should_create_early_order() ) { - wp_send_json_success( $order->to_array() ); + if ( count( $field ) !== 2 ) { + continue; } - $this->early_order_handler->register_for_order( $order ); - return $data; + $parsed_values[ $field[0] ] = $field[1]; } + $_POST = $parsed_values; + $_REQUEST = $parsed_values; - wp_send_json_error( - array( - 'name' => '', - 'message' => $errors->get_error_message(), - 'code' => (int) $errors->get_error_code(), - 'details' => array(), - ) + add_action( + 'woocommerce_after_checkout_validation', + function ( array $data, \WP_Error $errors ) use ( $wc_order ) { + if ( ! $errors->errors ) { + $order = $this->create_paypal_order( $wc_order ); + wp_send_json_success( $order->to_array() ); + return true; + } + }, + 10, + 2 ); - return $data; + + $checkout = \WC()->checkout(); + $checkout->process_checkout(); } }