diff --git a/assets/js/cko-paypal-integration.js b/assets/js/cko-paypal-integration.js index 4ab967f5..8af840ae 100644 --- a/assets/js/cko-paypal-integration.js +++ b/assets/js/cko-paypal-integration.js @@ -124,6 +124,12 @@ jQuery( function ( $ ) { }); }, onCancel: function (data, actions) { + fetch( cko_paypal_vars.clear_session_url, { + method: 'GET', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + }); jQuery('.woocommerce').unblock(); }, onError: function (err) { @@ -162,49 +168,4 @@ jQuery( function ( $ ) { } ); return; - - // Initialise PayPal when page is ready. - jQuery( document ).ready(function() { - - let paypalButtonProps = { - onApprove: async function (data) { - - jQuery('.woocommerce').block({message: null, overlayCSS: {background: '#fff', opacity: 0.6}}); - - jQuery.post(cko_paypal_vars.cc_capture + "&paypal_order_id=" + data.orderID + "&woocommerce-process-checkout-nonce=" + cko_paypal_vars.woocommerce_process_checkout, function (data) { - if (typeof data.success !== 'undefined' && data.success !== true ) { - var messages = data.data.messages ? data.data.messages : data.data; - - if ( 'string' === typeof messages || Array.isArray( messages ) ) { - showError( messages ); - } - } else { - window.location.href = data.data.redirect; - } - }); - }, - onCancel: function (data, actions) { - jQuery('.woocommerce').unblock(); - }, - onError: function (err) { - console.log(err); - jQuery('.woocommerce').unblock(); - }, - }; - - if ( cko_paypal_vars.is_cart_contains_subscription ) { - paypalButtonProps.createBillingAgreement = function( data, actions ) { - return cko_create_order_id(); - }; - } else { - paypalButtonProps.createOrder = function( data, actions ) { - return cko_create_order_id(); - }; - } - - paypal.Buttons({ paypalButtonProps }).render( cko_paypal_vars.paypal_button_selector ); - }); - - - }); \ No newline at end of file diff --git a/includes/class-wc-gateway-checkout-com-paypal.php b/includes/class-wc-gateway-checkout-com-paypal.php index dd452b8a..299972a7 100644 --- a/includes/class-wc-gateway-checkout-com-paypal.php +++ b/includes/class-wc-gateway-checkout-com-paypal.php @@ -85,6 +85,10 @@ public function handle_wc_api() { exit(); } break; + + case 'empty_session': + WC_Checkoutcom_Utility::cko_set_session( 'cko_paypal_order_id', '' ); + WC_Checkoutcom_Utility::cko_set_session( 'cko_pc_id', '' ); case 'cc_capture': WC_Checkoutcom_Utility::cko_set_session( 'cko_paypal_order_id', wc_clean( $_GET['paypal_order_id'] ) ); @@ -535,6 +539,7 @@ public function payment_scripts() { $vars = [ 'create_order_url' => add_query_arg( [ 'cko_paypal_action' => 'create_order' ], WC()->api_request_url( 'CKO_Paypal_Woocommerce' ) ), + 'clear_session_url' => add_query_arg( [ 'cko_paypal_action' => 'empty_session' ], WC()->api_request_url( 'CKO_Paypal_Woocommerce' ) ), 'cc_capture' => add_query_arg( [ 'cko_paypal_action' => 'cc_capture' ], WC()->api_request_url( 'CKO_Paypal_Woocommerce' ) ), 'woocommerce_process_checkout' => wp_create_nonce( 'woocommerce-process_checkout' ), 'is_cart_contains_subscription' => WC_Checkoutcom_Utility::is_cart_contains_subscription(),