diff --git a/assets/frontend/js/globalpayments-applepay.js b/assets/frontend/js/globalpayments-applepay.js index 28d3fe9..5fb0f5d 100644 --- a/assets/frontend/js/globalpayments-applepay.js +++ b/assets/frontend/js/globalpayments-applepay.js @@ -1,10 +1,9 @@ ( function ( $, globalpayments_applepay_params, - globalpayments_order, helper ) { - function ApplePayWoocommerce ( options, order ) { + function ApplePayWoocommerce ( options ) { /** * Payment gateway id * @@ -17,7 +16,7 @@ * * @type {object} */ - this.order = order; + this.order = {}; /** * Payment gateway options @@ -78,6 +77,7 @@ createApplePaySession: function () { var self = this; + this.order = helper.order; try { var applePaySession = new ApplePaySession( 1, self.getPaymentRequest() ); @@ -153,7 +153,7 @@ supportedNetworks: this.getAllowedCardNetworks(), total: { label: this.getDisplayName(), - amount: this.order.amount + amount: this.order.amount.toString() }, }; }, @@ -185,10 +185,9 @@ }, } - new ApplePayWoocommerce( globalpayments_applepay_params, globalpayments_order ); + new ApplePayWoocommerce( globalpayments_applepay_params ); }( ( window ).jQuery, ( window ).globalpayments_applepay_params || {}, - ( window ).globalpayments_order || {}, ( window ).GlobalPaymentsHelper )); diff --git a/assets/frontend/js/globalpayments-googlepay.js b/assets/frontend/js/globalpayments-googlepay.js index 415fda9..4437263 100644 --- a/assets/frontend/js/globalpayments-googlepay.js +++ b/assets/frontend/js/globalpayments-googlepay.js @@ -1,10 +1,9 @@ ( function ( $, globalpayments_googlepay_params, - globalpayments_order, helper ) { - function GooglePayWoocommerce ( options, order ) { + function GooglePayWoocommerce ( options ) { /** * Payment gateway id * @@ -17,7 +16,7 @@ * * @type {object} */ - this.order = order; + this.order = {}; /** * Payment gateway options @@ -156,7 +155,7 @@ getGoogleTransactionInfo: function () { return { totalPriceStatus: 'FINAL', - totalPrice: this.order.amount, + totalPrice: this.order.amount.toString(), currencyCode: this.order.currency }; }, @@ -191,7 +190,8 @@ }, onGooglePaymentButtonClicked: function () { - var self = this + var self = this; + this.order = helper.order; var paymentDataRequest = this.getGooglePaymentDataRequest(); paymentDataRequest.transactionInfo = this.getGoogleTransactionInfo(); @@ -211,10 +211,9 @@ }, } - new GooglePayWoocommerce( globalpayments_googlepay_params, globalpayments_order ); + new GooglePayWoocommerce( globalpayments_googlepay_params ); }( ( window ).jQuery, ( window ).globalpayments_googlepay_params || {}, - ( window ).globalpayments_order || {}, ( window ).GlobalPaymentsHelper ) ); diff --git a/assets/frontend/js/globalpayments-helper.js b/assets/frontend/js/globalpayments-helper.js index 75853af..bcb58d5 100644 --- a/assets/frontend/js/globalpayments-helper.js +++ b/assets/frontend/js/globalpayments-helper.js @@ -1,9 +1,52 @@ (function ( - $ + $, + globalpayments_helper_params ) { - function Helper() {}; + function Helper( options ) { + /** + * Helper options + * + * @type {object} + */ + this.helperOptions = options; + + /** + * The current order + * + * @type {object} + */ + this.order = options.order; + + this.attachEventHandlers(); + }; Helper.prototype = { + /** + * Add important event handlers for controlling the payment experience during checkout + * + * @returns + */ + attachEventHandlers: function () { + var self = this; + + $( document.body ).on( + 'applied_coupon_in_checkout removed_coupon_in_checkout', + function () { + self.blockOnSubmit(); + + $.get( self.helperOptions.orderInfoUrl ) + .done( function( result ) { + self.order = result.message; + }) + .fail( function( jqXHR, textStatus, errorThrown ) { + console.log(errorThrown); + }) + .always( function() { + self.unblockOnError(); + }); + } + ); + }, /** * Convenience function to get CSS selector for the built-in 'Place Order' button * @@ -147,11 +190,45 @@ } $( this.getForm() ).submit(); }, + + /** + * Blocks checkout UI + * + * Implementation pulled from `woocommerce/assets/js/frontend/checkout.js` + * + * @returns + */ + blockOnSubmit: function () { + var $form = $( this.getForm() ); + var form_data = $form.data(); + if ( 1 !== form_data['blockUI.isBlocked'] ) { + $form.block( + { + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + } + ); + } + }, + + /** + * Unblocks checkout UI + * + * @returns + */ + unblockOnError: function () { + var $form = $( this.getForm() ); + $form.unblock(); + } }; if ( ! window.GlobalPaymentsHelper ) { - window.GlobalPaymentsHelper = new Helper(); + window.GlobalPaymentsHelper = new Helper( globalpayments_helper_params ); } } ( - ( window ).jQuery -) ); \ No newline at end of file + ( window ).jQuery, + ( window ).globalpayments_helper_params || {} +) ); diff --git a/assets/frontend/js/globalpayments-secure-payment-fields.js b/assets/frontend/js/globalpayments-secure-payment-fields.js index 124a211..05499c8 100644 --- a/assets/frontend/js/globalpayments-secure-payment-fields.js +++ b/assets/frontend/js/globalpayments-secure-payment-fields.js @@ -57,7 +57,7 @@ /** * Order info */ - this.order = threeDSecureOptions.order; + this.order = {}; /** * @@ -122,7 +122,7 @@ */ initThreeDSecure: function ( e ) { e.preventDefault; - this.blockOnSubmit(); + helper.blockOnSubmit(); var self = this; @@ -134,6 +134,7 @@ .done( function( result ) { if ( -1 !== result.messages.indexOf( self.id + '_checkout_validated' ) ) { helper.createInputElement( self.id, 'checkout_validated', 1 ); + self.order = helper.order; self.threeDSecure(); } else { self.showPaymentError( result.messages ); @@ -217,7 +218,7 @@ styles: this.getStyleConfiguration() } ); - this.cardForm.on( 'submit', 'click', this.blockOnSubmit.bind( this ) ); + this.cardForm.on( 'submit', 'click', helper.blockOnSubmit.bind( this ) ); this.cardForm.on( 'token-success', this.handleResponse.bind( this ) ); this.cardForm.on( 'token-error', this.handleErrors.bind( this ) ); this.cardForm.on( 'error', this.handleErrors.bind( this ) ); @@ -284,7 +285,7 @@ * 3DS Process */ threeDSecure: function () { - this.blockOnSubmit(); + helper.blockOnSubmit(); var self = this; var _form = helper.getForm(); @@ -425,7 +426,7 @@ showValidationError: function (fieldType) { $( '.' + this.id + '.' + fieldType + ' .woocommerce-globalpayments-validation-error' ).show(); - this.unblockOnError(); + helper.unblockOnError(); }, /** @@ -450,7 +451,7 @@ scrollTop: ( $form.offset().top - 100 ) }, 1000 ); - this.unblockOnError(); + helper.unblockOnError(); $( document.body ).trigger( 'checkout_error' ); }, @@ -589,39 +590,6 @@ */ getSubmitButtonText: function () { return $( '#place_order' ).data( 'value' ) || $( '#place_order' ).attr( 'value' ); - }, - - /** - * Blocks checkout UI - * - * Implementation pulled from `woocommerce/assets/js/frontend/checkout.js` - * - * @returns - */ - blockOnSubmit: function () { - var $form = $( helper.getForm() ); - var form_data = $form.data(); - if ( 1 !== form_data['blockUI.isBlocked'] ) { - $form.block( - { - message: null, - overlayCSS: { - background: '#fff', - opacity: 0.6 - } - } - ); - } - }, - - /** - * Unblocks checkout UI - * - * @returns - */ - unblockOnError: function () { - var $form = $( helper.getForm() ); - $form.unblock(); } }; diff --git a/globalpayments-gateway-provider-for-woocommerce.php b/globalpayments-gateway-provider-for-woocommerce.php index 6ab9a79..5ef85dc 100644 --- a/globalpayments-gateway-provider-for-woocommerce.php +++ b/globalpayments-gateway-provider-for-woocommerce.php @@ -3,7 +3,7 @@ * Plugin Name: GlobalPayments WooCommerce * Plugin URI: https://github.com/globalpayments/globalpayments-woocommerce * Description: This extension allows WooCommerce to use the available Global Payments payment gateways. All card data is tokenized using the respective gateway's tokenization service. - * Version: 1.2.1 + * Version: 1.2.2 * Requires PHP: 5.5.9 * WC tested up to: 6.3.1 * Author: Global Payments diff --git a/readme.txt b/readme.txt index aace708..5a01aef 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: globalpayments Tags: woocommerce, woo, unified, commerce, platform, global, payments, heartland, payment, systems, tsys, genius, 3DS, gateway, token, tokenize, save cards Requires at least: 5.4 Tested up to: 5.9.3 -Stable tag: 1.2.1 +Stable tag: 1.2.2 License: MIT License URI: https://github.com/globalpayments/globalpayments-woocommerce/blob/main/LICENSE @@ -47,6 +47,9 @@ Access to our Unified Payments requires sandbox credentials which you can retrie == Changelog == += 1.2.2 = +* Bug fix - 3DS/Digital Wallets amount not updated when a customer added/removed a coupon + = 1.2.1 = * Renamed Unified Commerce Platform (UCP) to Unified Payments * Bug fix - Live mode toggle in Digital Wallets javascript diff --git a/src/Gateways/AbstractGateway.php b/src/Gateways/AbstractGateway.php index 8e4eb8d..4586eae 100644 --- a/src/Gateways/AbstractGateway.php +++ b/src/Gateways/AbstractGateway.php @@ -315,6 +315,18 @@ public function tokenization_script() { WC()->version, true ); + + wp_localize_script( + 'globalpayments-helper', + 'globalpayments_helper_params', + array( + 'orderInfoUrl' => WC()->api_request_url( 'globalpayments_order_info' ), + 'order' => array( + 'amount' => $this->get_session_amount(), + 'currency' => get_woocommerce_currency(), + ) + ) + ); // Global Payments scripts for handling client-side tokenization wp_enqueue_script( @@ -366,11 +378,7 @@ public function tokenization_script() { 'checkEnrollmentUrl' => WC()->api_request_url( 'globalpayments_threedsecure_checkenrollment' ), 'initiateAuthenticationUrl' => WC()->api_request_url( 'globalpayments_threedsecure_initiateauthentication' ), 'ajaxCheckoutUrl' => \WC_AJAX::get_endpoint( 'checkout' ), - ), - 'order' => array( - 'amount' => $this->get_session_amount(), - 'currency' => get_woocommerce_currency(), - ), + ) ) ); } @@ -747,6 +755,11 @@ protected function add_hooks() { add_filter( 'woocommerce_credit_card_form_fields', array( $this, 'woocommerce_credit_card_form_fields' ) ); } + add_action( 'woocommerce_api_globalpayments_order_info', array( + $this, + 'get_order_info' + ) ); + if ( is_add_payment_method_page() ) { if ( ! $this->is_digital_wallet ) { add_action( 'wp_enqueue_scripts', array( $this, 'tokenization_script' ) ); @@ -1054,6 +1067,27 @@ public static function addCaptureOrderAction( $actions ) { return $actions; } + public function get_order_info() { + if ( ( 'GET' !== $_SERVER['REQUEST_METHOD'] ) ) { + return; + } + + $response = new \stdClass(); + $response->amount = $this->get_session_amount(); + $response->currency = get_woocommerce_currency(); + + wp_send_json( [ + 'error' => false, + 'message' => $response, + ] ); + } + + public function get_session_amount() { + $cart_totals = WC()->session->get( 'cart_totals' ); + + return round( $cart_totals['total'], 2 ); + } + /** * Disable adding new cards via 'My Account', if a Digital Wallet or "Allow Card Saving" option not checked in admin. * diff --git a/src/Gateways/ApplePayGateway.php b/src/Gateways/ApplePayGateway.php index a62f986..bb3302c 100644 --- a/src/Gateways/ApplePayGateway.php +++ b/src/Gateways/ApplePayGateway.php @@ -210,12 +210,6 @@ public function payment_fields() { } } - protected function get_session_amount() { - $cart_totals = WC()->session->get( 'cart_totals' ); - - return round( $cart_totals['total'], 2 ); - } - public function validate_merchant() { $responseValidationUrl = json_decode( file_get_contents( 'php://input' ) ); if ( empty($responseValidationUrl) ) { @@ -298,6 +292,18 @@ public function tokenization_script() { true ); + wp_localize_script( + 'globalpayments-helper', + 'globalpayments_helper_params', + array( + 'orderInfoUrl' => WC()->api_request_url( 'globalpayments_order_info' ), + 'order' => array( + 'amount' => $this->get_session_amount(), + 'currency' => get_woocommerce_currency(), + ) + ) + ); + wp_enqueue_script( 'globalpayments-wc-applepay', Plugin::get_url( '/assets/frontend/js/globalpayments-applepay.js' ), @@ -315,15 +321,6 @@ public function tokenization_script() { ) ); - wp_localize_script( - 'globalpayments-wc-applepay', - 'globalpayments_order', - array( - 'amount' => (string) $this->get_session_amount(), - 'currency' => get_woocommerce_currency(), - ) - ); - wp_enqueue_style( 'globalpayments-applepay', Plugin::get_url( '/assets/frontend/css/globalpayments-applepay.css' ), diff --git a/src/Gateways/GooglePayGateway.php b/src/Gateways/GooglePayGateway.php index 326045c..dbca869 100644 --- a/src/Gateways/GooglePayGateway.php +++ b/src/Gateways/GooglePayGateway.php @@ -190,6 +190,18 @@ public function tokenization_script() { true ); + wp_localize_script( + 'globalpayments-helper', + 'globalpayments_helper_params', + array( + 'orderInfoUrl' => WC()->api_request_url( 'globalpayments_order_info' ), + 'order' => array( + 'amount' => $this->get_session_amount(), + 'currency' => get_woocommerce_currency(), + ) + ) + ); + wp_enqueue_script( 'globalpayments-wc-googlepay', Plugin::get_url( '/assets/frontend/js/globalpayments-googlepay.js' ), @@ -206,15 +218,6 @@ public function tokenization_script() { 'gateway_options' => $this->get_frontend_gateway_options(), ) ); - - wp_localize_script( - 'globalpayments-wc-googlepay', - 'globalpayments_order', - array( - 'amount' => (string) $this->get_session_amount(), - 'currency' => get_woocommerce_currency(), - ) - ); } public function payment_fields() { @@ -224,12 +227,6 @@ public function payment_fields() { } } - protected function get_session_amount() { - $cart_totals = WC()->session->get( 'cart_totals' ); - - return round( $cart_totals['total'], 2 ); - } - public function mapResponseCodeToFriendlyMessage( $responseCode ) { if ( 'DECLINED' === $responseCode ) { return __( 'Your card has been declined by the bank.', 'globalpayments-gateway-provider-for-woocommerce' ); diff --git a/src/Gateways/GpApiGateway.php b/src/Gateways/GpApiGateway.php index e49067c..8d86778 100644 --- a/src/Gateways/GpApiGateway.php +++ b/src/Gateways/GpApiGateway.php @@ -381,10 +381,4 @@ public function process_threeDSecure_challengeNotification() { ] ); } } - - protected function get_session_amount() { - $cart_totals = WC()->session->get( 'cart_totals' ); - - return round( $cart_totals['total'], 2 ); - } } diff --git a/src/Plugin.php b/src/Plugin.php index 8d6d00c..dd5aeb1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -18,7 +18,7 @@ class Plugin { * * @var string */ - const VERSION = '1.2.1'; + const VERSION = '1.2.2'; /** * Init the package.