|
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
|
|
|
* Get WC AJAX endpoint URL. |
|
13
|
|
|
* |
|
14
|
|
|
* @param {String} endpoint Endpoint. |
|
15
|
|
|
* @return {String} |
|
16
|
|
|
*/ |
|
17
|
|
|
getAjaxURL: function( endpoint ) { |
|
18
|
|
|
return wc_stripe_apple_pay_params.ajaxurl |
|
19
|
|
|
.toString() |
|
20
|
|
|
.replace( '%%endpoint%%', 'wc_stripe_' + endpoint ); |
|
21
|
|
|
}, |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Initialize event handlers and UI state. |
|
25
|
|
|
*/ |
|
26
|
|
|
init: function() { |
|
27
|
|
|
Stripe.applePay.checkAvailability( function( available ) { |
|
28
|
|
|
if ( available ) { |
|
29
|
|
|
$( '.apple-pay-button' ).show(); |
|
30
|
|
|
// This is so it is centered on the checkout page. |
|
31
|
|
|
$( '.woocommerce-checkout .apple-pay-button' ).css( 'visibility', 'visible' ); |
|
32
|
|
|
$( '.apple-pay-button-checkout-separator' ).show(); |
|
33
|
|
|
|
|
34
|
|
|
wc_stripe_apple_pay.generate_cart(); |
|
35
|
|
|
} |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
$( document.body ).on( 'click', '.apple-pay-button', function( e ) { |
|
39
|
|
|
e.preventDefault(); |
|
40
|
|
|
|
|
41
|
|
|
var paymentRequest = { |
|
42
|
|
|
countryCode: wc_stripe_apple_pay_params.country_code, |
|
43
|
|
|
currencyCode: wc_stripe_apple_pay_params.currency_code, |
|
44
|
|
|
total: { |
|
45
|
|
|
label: wc_stripe_apple_pay_params.label, |
|
46
|
|
|
amount: wc_stripe_apple_pay_params.total |
|
47
|
|
|
}, |
|
48
|
|
|
lineItems: wc_stripe_apple_pay_params.line_items, |
|
49
|
|
|
requiredBillingContactFields: ['postalAddress'], |
|
50
|
|
|
requiredShippingContactFields: 'yes' === wc_stripe_apple_pay_params.needs_shipping ? ['postalAddress', 'phone', 'email', 'name'] : ['phone', 'email', 'name'] |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
var applePaySession = Stripe.applePay.buildSession( paymentRequest, function( result, completion ) { |
|
54
|
|
|
var data = { |
|
55
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_nonce, |
|
56
|
|
|
'result': result |
|
57
|
|
|
}; |
|
58
|
|
|
|
|
59
|
|
|
$.ajax({ |
|
60
|
|
|
type: 'POST', |
|
61
|
|
|
data: data, |
|
62
|
|
|
url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay' ), |
|
63
|
|
|
success: function( response ) { |
|
64
|
|
|
if ( 'true' === response.success ) { |
|
65
|
|
|
completion( ApplePaySession.STATUS_SUCCESS ); |
|
|
|
|
|
|
66
|
|
|
window.location.href = response.redirect; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ( 'false' === response.success ) { |
|
70
|
|
|
completion( ApplePaySession.STATUS_FAILURE ); |
|
71
|
|
|
|
|
72
|
|
|
$( '.apple-pay-button' ).before( '<p class="woocommerce-error wc-stripe-apple-pay-error">' + response.msg + '</p>' ); |
|
73
|
|
|
|
|
74
|
|
|
// Scroll to error so user can see it. |
|
75
|
|
|
$( document.body ).animate({ scrollTop: $( '.wc-stripe-apple-pay-error' ).offset().top }, 500 ); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
}); |
|
79
|
|
|
}, function( error ) { |
|
80
|
|
|
var data = { |
|
81
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_cart_nonce, |
|
82
|
|
|
'errors': error.message |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
$.ajax({ |
|
86
|
|
|
type: 'POST', |
|
87
|
|
|
data: data, |
|
88
|
|
|
url: wc_stripe_apple_pay.getAjaxURL( 'log_apple_pay_errors' ) |
|
89
|
|
|
}); |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
// If shipping is needed -- get shipping methods. |
|
93
|
|
|
if ( 'yes' === wc_stripe_apple_pay_params.needs_shipping ) { |
|
94
|
|
|
// After the shipping contact/address has been selected |
|
95
|
|
|
applePaySession.onshippingcontactselected = function( shipping ) { |
|
96
|
|
|
var data = { |
|
97
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_get_shipping_methods_nonce, |
|
98
|
|
|
'address': shipping.shippingContact |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
$.ajax({ |
|
102
|
|
|
type: 'POST', |
|
103
|
|
|
data: data, |
|
104
|
|
|
url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay_get_shipping_methods' ), |
|
105
|
|
|
success: function( response ) { |
|
106
|
|
|
var total = { |
|
107
|
|
|
'label': wc_stripe_apple_pay_params.label, |
|
108
|
|
|
'amount': response.total |
|
109
|
|
|
}; |
|
110
|
|
|
|
|
111
|
|
|
if ( 'true' === response.success ) { |
|
112
|
|
|
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_SUCCESS, response.shipping_methods, total, response.line_items ); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( 'false' === response.success ) { |
|
116
|
|
|
applePaySession.completeShippingContactSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, response.shipping_methods, total, response.line_items ); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
}); |
|
120
|
|
|
}; |
|
121
|
|
|
|
|
122
|
|
|
// After the shipping method has been selected |
|
123
|
|
|
applePaySession.onshippingmethodselected = function( event ) { |
|
124
|
|
|
var data = { |
|
125
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_update_shipping_method_nonce, |
|
126
|
|
|
'selected_shipping_method': event.shippingMethod |
|
127
|
|
|
}; |
|
128
|
|
|
|
|
129
|
|
|
$.ajax({ |
|
130
|
|
|
type: 'POST', |
|
131
|
|
|
data: data, |
|
132
|
|
|
url: wc_stripe_apple_pay.getAjaxURL( 'apple_pay_update_shipping_method' ), |
|
133
|
|
|
success: function( response ) { |
|
134
|
|
|
var newTotal = { |
|
135
|
|
|
'label': wc_stripe_apple_pay_params.label, |
|
136
|
|
|
'amount': parseFloat( response.total ).toFixed(2) |
|
137
|
|
|
}; |
|
138
|
|
|
|
|
139
|
|
|
if ( 'true' === response.success ) { |
|
140
|
|
|
applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_SUCCESS, newTotal, response.line_items ); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( 'false' === response.success ) { |
|
144
|
|
|
applePaySession.completeShippingMethodSelection( ApplePaySession.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS, newTotal, response.line_items ); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
}); |
|
148
|
|
|
}; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
applePaySession.begin(); |
|
152
|
|
|
}); |
|
153
|
|
|
}, |
|
154
|
|
|
|
|
155
|
|
|
generate_cart: function() { |
|
156
|
|
|
var data = { |
|
157
|
|
|
'nonce': wc_stripe_apple_pay_params.stripe_apple_pay_cart_nonce |
|
158
|
|
|
}; |
|
159
|
|
|
|
|
160
|
|
|
$.ajax({ |
|
161
|
|
|
type: 'POST', |
|
162
|
|
|
data: data, |
|
163
|
|
|
url: wc_stripe_apple_pay.getAjaxURL( 'generate_apple_pay_cart' ), |
|
164
|
|
|
success: function( response ) { |
|
165
|
|
|
wc_stripe_apple_pay_params.total = response.total; |
|
166
|
|
|
wc_stripe_apple_pay_params.line_items = response.line_items; |
|
167
|
|
|
} |
|
168
|
|
|
}); |
|
169
|
|
|
} |
|
170
|
|
|
}; |
|
171
|
|
|
|
|
172
|
|
|
wc_stripe_apple_pay.init(); |
|
173
|
|
|
|
|
174
|
|
|
// We need to refresh Apple Pay data when total is updated. |
|
175
|
|
|
$( document.body ).on( 'updated_cart_totals', function() { |
|
176
|
|
|
wc_stripe_apple_pay.init(); |
|
177
|
|
|
}); |
|
178
|
|
|
|
|
179
|
|
|
// We need to refresh Apple Pay data when total is updated. |
|
180
|
|
|
$( document.body ).on( 'updated_checkout', function() { |
|
181
|
|
|
wc_stripe_apple_pay.init(); |
|
182
|
|
|
}); |
|
183
|
|
|
}); |
|
184
|
|
|
|