|
1
|
|
|
/* global wc_stripe_apple_pay_params, Stripe */ |
|
2
|
|
|
Stripe.setPublishableKey( wc_stripe_apple_pay_params.key ); |
|
3
|
|
|
|
|
4
|
|
|
jQuery( function( $ ) { |
|
5
|
|
|
'use strict'; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Object to handle Stripe payment forms. |
|
9
|
|
|
*/ |
|
10
|
|
|
var wc_stripe_apple_pay = { |
|
11
|
|
|
/** |
|
12
|
|
|
* Initialize event handlers and UI state. |
|
13
|
|
|
*/ |
|
14
|
|
|
init: function() { |
|
15
|
|
|
Stripe.applePay.checkAvailability( function( available ) { |
|
16
|
|
|
if ( available ) { |
|
17
|
|
|
$( '.apple-pay-button' ).show(); |
|
18
|
|
|
|
|
19
|
|
|
wc_stripe_apple_pay.generate_cart(); |
|
20
|
|
|
} |
|
21
|
|
|
}); |
|
22
|
|
|
|
|
23
|
|
|
$( document.body ).on( 'click', '.apple-pay-button', function( e ) { |
|
24
|
|
|
e.preventDefault(); |
|
25
|
|
|
|
|
26
|
|
|
// Clear any errors first. |
|
27
|
|
|
$( '.wc-stripe-apple-pay-error' ).remove(); |
|
28
|
|
|
|
|
29
|
|
|
// If shipping is needed, we need to force customer to calculate shipping on cart page. |
|
30
|
|
|
if ( 'yes' === wc_stripe_apple_pay_params.is_cart_page && |
|
31
|
|
|
'yes' === wc_stripe_apple_pay_params.needs_shipping && |
|
32
|
|
|
( $( '#shipping_method input[type="radio"]' ).length && ( ! $( '#shipping_method input[type="radio"]' ).is( ':checked' ) ) || |
|
33
|
|
|
0 === $( wc_stripe_apple_pay_params.chosen_shipping ).length ) |
|
34
|
|
|
) { |
|
35
|
|
|
$( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + wc_stripe_apple_pay_params.needs_shipping_msg + '</p>' ); |
|
36
|
|
|
|
|
37
|
|
|
// Scroll to error so user can see it. |
|
38
|
|
|
$( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
var paymentRequest = { |
|
43
|
|
|
countryCode: wc_stripe_apple_pay_params.country_code, |
|
44
|
|
|
currencyCode: wc_stripe_apple_pay_params.currency_code, |
|
45
|
|
|
total: { |
|
46
|
|
|
label: wc_stripe_apple_pay_params.label, |
|
47
|
|
|
amount: wc_stripe_apple_pay_params.total |
|
48
|
|
|
}, |
|
49
|
|
|
lineItems: wc_stripe_apple_pay_params.line_items, |
|
50
|
|
|
requiredBillingContactFields: ['postalAddress'], |
|
51
|
|
|
requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name'] |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) { |
|
55
|
|
|
var data = { |
|
56
|
|
|
'action': 'wc_stripe_apple_pay', |
|
57
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce, |
|
58
|
|
|
'result': result |
|
59
|
|
|
}; |
|
60
|
|
|
|
|
61
|
|
|
$.post( wc_stripe_apple_pay_params.ajaxurl, data ).done( function( response ) { |
|
62
|
|
|
if ( 'true' === response.success ) { |
|
63
|
|
|
completion( ApplePaySession.STATUS_SUCCESS ); |
|
|
|
|
|
|
64
|
|
|
window.location.href = response.redirect; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ( 'false' === response.success ) { |
|
68
|
|
|
completion( ApplePaySession.STATUS_FAILURE ); |
|
69
|
|
|
|
|
70
|
|
|
$( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' ); |
|
71
|
|
|
|
|
72
|
|
|
// Scroll to error so user can see it. |
|
73
|
|
|
$( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
}).fail( function( response ) { |
|
|
|
|
|
|
77
|
|
|
completion( ApplePaySession.STATUS_FAILURE ); |
|
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
}, function(error) { |
|
81
|
|
|
console.log(error.message); |
|
|
|
|
|
|
82
|
|
|
}); |
|
83
|
|
|
|
|
84
|
|
|
applePaySession.begin(); |
|
85
|
|
|
}); |
|
86
|
|
|
}, |
|
87
|
|
|
|
|
88
|
|
|
generate_cart: function() { |
|
89
|
|
|
var data = { |
|
90
|
|
|
'action': 'wc_stripe_generate_apple_pay_cart', |
|
91
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_cart_nonce |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
$.post( wc_stripe_apple_pay_params.ajaxurl, data, function( response ) { |
|
95
|
|
|
wc_stripe_apple_pay_params.total = response.total; |
|
96
|
|
|
wc_stripe_apple_pay_params.line_items = response.line_items; |
|
97
|
|
|
wc_stripe_apple_pay_params.chosen_shipping = response.chosen_shipping; |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
}; |
|
101
|
|
|
|
|
102
|
|
|
wc_stripe_apple_pay.init(); |
|
103
|
|
|
|
|
104
|
|
|
// We need to refresh Apple Pay data when total is updated. |
|
105
|
|
|
$( document.body ).on( 'updated_cart_totals', function() { |
|
106
|
|
|
wc_stripe_apple_pay.init(); |
|
107
|
|
|
}); |
|
108
|
|
|
|
|
109
|
|
|
// We need to refresh Apple Pay data when total is updated. |
|
110
|
|
|
$( document.body ).on( 'updated_checkout', function() { |
|
111
|
|
|
wc_stripe_apple_pay.init(); |
|
112
|
|
|
}); |
|
113
|
|
|
}); |
|
114
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.