Skip to content

Commit

Permalink
Merge pull request #740 from woocommerce/issue_739
Browse files Browse the repository at this point in the history
Fixes `paypal.getFundingSources is not a function` console errors
  • Loading branch information
mattallan authored May 25, 2020
2 parents d958f60 + a0ba855 commit b7310f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions assets/js/wc-gateway-ppec-smart-payment-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
19 changes: 17 additions & 2 deletions includes/class-wc-gateway-ppec-cart-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -493,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',
Expand Down Expand Up @@ -542,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 ) {
Expand All @@ -553,13 +555,26 @@ 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 );
wp_localize_script( 'wc-gateway-ppec-smart-payment-buttons', 'wc_ppec_context', $data );
}
}

/**
* Adds the data-namespace attribute when enqueuing the PayPal SDK script
*
* @since 2.0.1
* @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.
*/
Expand Down

0 comments on commit b7310f5

Please sign in to comment.