From a96f9f3c62d909878670d0e249e7b49c2e849ded Mon Sep 17 00:00:00 2001 From: mattallan Date: Thu, 21 May 2020 16:40:13 +1000 Subject: [PATCH 1/3] Use a script namespace so that we can load both paypal and paypal_sdk This commit updates the payment-buttons.js so that it will use paypal when using the checkout.js and paypal_sdk when using the SDK/js --- assets/js/wc-gateway-ppec-smart-payment-buttons.js | 10 +++++----- includes/class-wc-gateway-ppec-cart-handler.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/assets/js/wc-gateway-ppec-smart-payment-buttons.js b/assets/js/wc-gateway-ppec-smart-payment-buttons.js index c2659fc3..9feab349 100644 --- a/assets/js/wc-gateway-ppec-smart-payment-buttons.js +++ b/assets/js/wc-gateway-ppec-smart-payment-buttons.js @@ -43,7 +43,7 @@ var paypal_funding_methods = []; for ( var i = 0; i < methods.length; i++ ) { - var method = paypal.FUNDING[ methods[ i ].toUpperCase() ]; + var method = ( wc_ppec_context.use_checkout_js ? paypal : paypal_sdk ).FUNDING[ methods[ i ].toUpperCase() ]; if ( method ) { paypal_funding_methods.push( method ); } @@ -202,10 +202,10 @@ var disabledFundingSources = getFundingMethods( disallowed ); if ( 'undefined' === typeof( disabledFundingSources ) || ! disabledFundingSources || 0 === disabledFundingSources.length ) { - paypal.Buttons( button_args ).render( selector ); + paypal_sdk.Buttons( button_args ).render( selector ); } else { // Render context specific buttons. - paypal.getFundingSources().forEach( function( fundingSource ) { + paypal_sdk.getFundingSources().forEach( function( fundingSource ) { if ( -1 !== disabledFundingSources.indexOf( fundingSource ) ) { return; } @@ -216,10 +216,10 @@ onError: button_args.onError, onCancel: button_args.onCancel, fundingSource: fundingSource, - style: ( paypal.FUNDING.PAYPAL === fundingSource ) ? button_args.style : {} + style: ( paypal_sdk.FUNDING.PAYPAL === fundingSource ) ? button_args.style : {} }; - var button = paypal.Buttons( buttonSettings ); + var button = paypal_sdk.Buttons( buttonSettings ); if ( button.isEligible() ) { button.render( selector ); diff --git a/includes/class-wc-gateway-ppec-cart-handler.php b/includes/class-wc-gateway-ppec-cart-handler.php index 7c12c562..d1de148f 100644 --- a/includes/class-wc-gateway-ppec-cart-handler.php +++ b/includes/class-wc-gateway-ppec-cart-handler.php @@ -22,6 +22,7 @@ public function __construct() { add_action( 'woocommerce_before_cart_totals', array( $this, 'before_cart_totals' ) ); add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_paypal_button' ), 20 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + add_filter( 'script_loader_tag', array( $this, 'add_paypal_sdk_namespace_attribute' ), 10, 2 ); if ( 'yes' === wc_gateway_ppec()->settings->use_spb ) { add_action( 'woocommerce_after_mini_cart', array( $this, 'display_mini_paypal_button' ), 20 ); @@ -560,6 +561,18 @@ public function enqueue_scripts() { } } + /** + * Adds the data-namespace attribute when enqueuing the PayPal SDK script + * + * @since 2.0 + * @param string $tag + * @param string $handle + * @return string + */ + public function add_paypal_sdk_namespace_attribute( $tag, $handle ) { + return ( 'paypal-checkout-sdk' === $handle ) ? str_replace( ' src', ' data-namespace="paypal_sdk" src', $tag ) : $tag; + } + /** * Creates a customer session if one is not already active. */ From ff75fc502aef1b4c99df9b7ef1c6d87f39df7c18 Mon Sep 17 00:00:00 2001 From: mattallan Date: Tue, 26 May 2020 01:24:49 +1000 Subject: [PATCH 2/3] Use different script handle for SDK and checkout.js --- includes/class-wc-gateway-ppec-cart-handler.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-gateway-ppec-cart-handler.php b/includes/class-wc-gateway-ppec-cart-handler.php index d1de148f..bdc8f8fc 100644 --- a/includes/class-wc-gateway-ppec-cart-handler.php +++ b/includes/class-wc-gateway-ppec-cart-handler.php @@ -494,7 +494,7 @@ public function enqueue_scripts() { ); } elseif ( 'yes' === $settings->use_spb ) { - $spb_script_dependencies = array( 'jquery', 'paypal-checkout-js' ); + $spb_script_dependencies = array( 'jquery' ); $data = array( 'use_checkout_js' => $settings->use_legacy_checkout_js(), 'environment' => 'sandbox' === $settings->get_environment() ? 'sandbox' : 'production', @@ -543,7 +543,8 @@ public function enqueue_scripts() { 'currency' => get_woocommerce_currency(), ); - wp_register_script( 'paypal-checkout-js', add_query_arg( $script_args, 'https://www.paypal.com/sdk/js' ), array(), null, true ); + wp_register_script( 'paypal-checkout-sdk', add_query_arg( $script_args, 'https://www.paypal.com/sdk/js' ), array(), null, true ); + $spb_script_dependencies[] = 'paypal-checkout-sdk'; // register the fetch/promise polyfills files so the new PayPal Checkout SDK works with IE if ( $is_IE ) { @@ -554,6 +555,7 @@ public function enqueue_scripts() { } } else { wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true ); + $spb_script_dependencies[] = 'paypal-checkout-js'; } wp_register_script( 'wc-gateway-ppec-smart-payment-buttons', wc_gateway_ppec()->plugin_url . 'assets/js/wc-gateway-ppec-smart-payment-buttons.js', $spb_script_dependencies, wc_gateway_ppec()->version, true ); @@ -564,7 +566,7 @@ public function enqueue_scripts() { /** * Adds the data-namespace attribute when enqueuing the PayPal SDK script * - * @since 2.0 + * @since 2.0.1 * @param string $tag * @param string $handle * @return string From a0ba85513203655dd052fd4dc5f20ab5b0aa0360 Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Mon, 25 May 2020 16:44:40 -0300 Subject: [PATCH 3/3] Simplify use of namespaced PayPal object in JS --- assets/js/wc-gateway-ppec-smart-payment-buttons.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/assets/js/wc-gateway-ppec-smart-payment-buttons.js b/assets/js/wc-gateway-ppec-smart-payment-buttons.js index 9feab349..298d178e 100644 --- a/assets/js/wc-gateway-ppec-smart-payment-buttons.js +++ b/assets/js/wc-gateway-ppec-smart-payment-buttons.js @@ -2,6 +2,9 @@ ;( function ( $, window, document ) { 'use strict'; + // Use global 'paypal' object or namespaced 'paypal_sdk' as PayPal API (depends on legacy/SDK mode). + var paypal = wc_ppec_context.use_checkout_js ? window.paypal : window.paypal_sdk; + // Show error notice at top of checkout form, or else within button container var showError = function( errorMessage, selector ) { var $container = $( '.woocommerce-notices-wrapper, form.checkout' ); @@ -43,7 +46,7 @@ var paypal_funding_methods = []; for ( var i = 0; i < methods.length; i++ ) { - var method = ( wc_ppec_context.use_checkout_js ? paypal : paypal_sdk ).FUNDING[ methods[ i ].toUpperCase() ]; + var method = paypal.FUNDING[ methods[ i ].toUpperCase() ]; if ( method ) { paypal_funding_methods.push( method ); } @@ -202,10 +205,10 @@ var disabledFundingSources = getFundingMethods( disallowed ); if ( 'undefined' === typeof( disabledFundingSources ) || ! disabledFundingSources || 0 === disabledFundingSources.length ) { - paypal_sdk.Buttons( button_args ).render( selector ); + paypal.Buttons( button_args ).render( selector ); } else { // Render context specific buttons. - paypal_sdk.getFundingSources().forEach( function( fundingSource ) { + paypal.getFundingSources().forEach( function( fundingSource ) { if ( -1 !== disabledFundingSources.indexOf( fundingSource ) ) { return; } @@ -216,10 +219,10 @@ onError: button_args.onError, onCancel: button_args.onCancel, fundingSource: fundingSource, - style: ( paypal_sdk.FUNDING.PAYPAL === fundingSource ) ? button_args.style : {} + style: ( paypal.FUNDING.PAYPAL === fundingSource ) ? button_args.style : {} }; - var button = paypal_sdk.Buttons( buttonSettings ); + var button = paypal.Buttons( buttonSettings ); if ( button.isEligible() ) { button.render( selector );