From 09ef106a80a891a7e638c63e9ad330c5d2303040 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 13 Jul 2022 16:11:33 +0200 Subject: [PATCH] Refactor to create PayPal order first --- modules/ppcp-wc-gateway/resources/js/oxxo.js | 31 ++++ modules/ppcp-wc-gateway/services.php | 10 ++ .../ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php | 18 ++- .../src/Gateway/OXXO/OXXOEndpoint.php | 135 ++++++++++++++++++ .../src/Gateway/OXXO/OXXOGateway.php | 53 +------ .../ppcp-wc-gateway/src/WCGatewayModule.php | 8 ++ 6 files changed, 204 insertions(+), 51 deletions(-) create mode 100644 modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOEndpoint.php diff --git a/modules/ppcp-wc-gateway/resources/js/oxxo.js b/modules/ppcp-wc-gateway/resources/js/oxxo.js index e2b9cb254..76ecc83c3 100644 --- a/modules/ppcp-wc-gateway/resources/js/oxxo.js +++ b/modules/ppcp-wc-gateway/resources/js/oxxo.js @@ -1,4 +1,33 @@ window.addEventListener('load', function() { + + const oxxoButton = document.getElementById('ppcp-oxxo'); + oxxoButton?.addEventListener('click', (event) => { + event.preventDefault(); + + fetch(OXXOConfig.oxxo_endpoint, { + method: 'POST', + body: JSON.stringify({ + nonce: OXXOConfig.oxxo_nonce, + }) + }).then((res)=>{ + return res.json(); + }).then((data)=>{ + if (!data.success) { + alert('Could not update signup buttons: ' + JSON.stringify(data)); + return; + } + + window.open( + data.data.payer_action, + '_blank', + 'popup' + ); + + document.querySelector('#place_order').click() + }); + }); + + /* const oxxoButton = document.getElementById('ppcp-oxxo-payer-action'); if(oxxoButton) { oxxoButton.addEventListener('click', (event) => { @@ -12,4 +41,6 @@ window.addEventListener('load', function() { window.open(oxxoButton.href); } + + */ }); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 1f865d484..80198ff2f 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -31,6 +31,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXOEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXOGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\FraudNet; @@ -2245,6 +2246,15 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, + 'wcgateway.endpoint.oxxo' => static function (ContainerInterface $container): OXXOEndpoint { + return new OXXOEndpoint( + $container->get( 'button.request-data' ), + $container->get( 'api.endpoint.order' ), + $container->get( 'api.factory.purchase-unit' ), + $container->get( 'api.factory.shipping-preference' ), + $container->get( 'woocommerce.logger.woocommerce' ) + ); + }, 'wcgateway.logging.is-enabled' => function ( ContainerInterface $container ) : bool { $settings = $container->get( 'wcgateway.settings' ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php index a1c37d959..9d158a998 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php @@ -93,6 +93,11 @@ class OXXO { 'wp_enqueue_scripts', array( $this, 'register_assets' ) ); + + add_action('woocommerce_review_order_after_payment', function () { + + echo ''; + }); } /** @@ -123,14 +128,23 @@ class OXXO { public function register_assets(): void { $gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' ); $gateway_enabled = $gateway_settings['enabled'] ?? ''; - if ( $gateway_enabled === 'yes' && is_checkout() && ! empty( is_wc_endpoint_url( 'order-received' ) ) ) { + if ( $gateway_enabled === 'yes' && is_checkout() ) { // && ! empty( is_wc_endpoint_url( 'order-received' ) ) wp_enqueue_script( - 'ppcp-pay-upon-invoice', + 'ppcp-oxxo', trailingslashit( $this->module_url ) . 'assets/js/oxxo.js', array(), $this->asset_version, true ); } + + wp_localize_script( + 'ppcp-oxxo', + 'OXXOConfig', + array( + 'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ), + 'oxxo_nonce' => wp_create_nonce( 'ppc-oxxo' ), + ) + ); } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOEndpoint.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOEndpoint.php new file mode 100644 index 000000000..1fc1833d2 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOEndpoint.php @@ -0,0 +1,135 @@ +request_data = $request_data; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->shipping_preference_factory = $shipping_preference_factory; + $this->order_endpoint = $order_endpoint; + $this->logger = $logger; + } + + public static function nonce(): string + { + return 'ppc-oxxo'; + } + + public function handle_request(): bool + { + $data = $this->request_data->read_request( $this->nonce() ); + + $purchase_unit = $this->purchase_unit_factory->from_wc_cart(); + + $payer_action = ''; + try { + $shipping_preference = $this->shipping_preference_factory->from_state( + $purchase_unit, + 'checkout' + ); + + $order = $this->order_endpoint->create(array($purchase_unit), $shipping_preference); + + $payment_source = array( + 'oxxo' => array( + 'name' => 'John Doe', + 'email' => 'foo@bar.com', + 'country_code' => 'MX', + ), + ); + + $payment_method = $this->order_endpoint->confirm_payment_source( $order->id(), $payment_source ); + + foreach ( $payment_method->links as $link ) { + if ( $link->rel === 'payer-action' ) { + $payer_action = $link->href; + } + } + + } catch ( RuntimeException $exception ) { + $error = $exception->getMessage(); + + if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) { + $details = ''; + foreach ( $exception->details() as $detail ) { + $issue = $detail->issue ?? ''; + $field = $detail->field ?? ''; + $description = $detail->description ?? ''; + $details .= $issue . ' ' . $field . ' ' . $description . '
'; + } + + $error = $details; + } + + + $this->logger->error( $error ); + wc_add_notice( $error, 'error' ); + } + + WC()->session->set( 'ppcp_payer_action', $payer_action ); + + wp_send_json_success( + array('payer_action' => $payer_action,) + ); + + return true; + } +} diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php index 7c5a4e1e8..ff5548e51 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXOGateway.php @@ -130,56 +130,11 @@ class OXXOGateway extends WC_Payment_Gateway { public function process_payment( $order_id ) { $wc_order = wc_get_order( $order_id ); $wc_order->update_status( 'on-hold', __( 'Awaiting OXXO payment.', 'woocommerce-paypal-payments' ) ); - $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); - try { - $shipping_preference = $this->shipping_preference_factory->from_state( - $purchase_unit, - 'checkout' - ); - - $order = $this->order_endpoint->create( array( $purchase_unit ), $shipping_preference ); - $payment_source = array( - 'oxxo' => array( - 'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(), - 'email' => $wc_order->get_billing_email(), - 'country_code' => $wc_order->get_billing_country(), - ), - ); - $payment_method = $this->order_endpoint->confirm_payment_source( $order->id(), $payment_source ); - foreach ( $payment_method->links as $link ) { - if ( $link->rel === 'payer-action' ) { - $wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $link->href ); - $wc_order->save_meta_data(); - } - } - } catch ( RuntimeException $exception ) { - $error = $exception->getMessage(); - - if ( is_a( $exception, PayPalApiException::class ) && is_array( $exception->details() ) ) { - $details = ''; - foreach ( $exception->details() as $detail ) { - $issue = $detail->issue ?? ''; - $field = $detail->field ?? ''; - $description = $detail->description ?? ''; - $details .= $issue . ' ' . $field . ' ' . $description . '
'; - } - - $error = $details; - } - - $this->logger->error( $error ); - wc_add_notice( $error, 'error' ); - - $wc_order->update_status( - 'failed', - $error - ); - - return array( - 'result' => 'failure', - 'redirect' => wc_get_checkout_url(), - ); + $payer_action = WC()->session->get( 'ppcp_payer_action' ); + if($payer_action) { + $wc_order->add_meta_data( 'ppcp_oxxo_payer_action', $payer_action ); + $wc_order->save_meta_data(); } WC()->cart->empty_cart(); diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 46bbf9491..12136089c 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -262,6 +262,14 @@ class WCGatewayModule implements ModuleInterface { 10, 2 ); + + add_action( + 'wc_ajax_ppc-oxxo', + static function () use ( $c ) { + $endpoint = $c->get( 'wcgateway.endpoint.oxxo' ); + $endpoint->handle_request(); + } + ); } /**