-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathindex.js
336 lines (305 loc) · 9.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import jQuery from 'jquery';
import WCStripeAPI from '../../api';
import { getStripeServerData, getUPETerms } from '../../stripe-utils';
import { legacyHashchangeHandler } from './legacy-support';
import './style.scss';
import './deferred-intent.js';
import {
maybeShowCashAppLimitNotice,
removeCashAppLimitNotice,
} from 'wcstripe/stripe-utils/cash-app-limit-notice-handler';
import {
PAYMENT_METHOD_BOLETO,
PAYMENT_METHOD_MULTIBANCO,
PAYMENT_METHOD_OXXO,
} from 'wcstripe/stripe-utils/constants';
jQuery( function ( $ ) {
const key = getStripeServerData()?.key;
const isUPEEnabled = getStripeServerData()?.isUPEEnabled;
if ( ! key ) {
// If no configuration is present, probably this is not the checkout page.
return;
}
// Create an API object, which will be used throughout the checkout.
const api = new WCStripeAPI(
getStripeServerData(),
// A promise-based interface to jQuery.post.
( url, args ) => {
return new Promise( ( resolve, reject ) => {
jQuery.post( url, args ).then( resolve ).fail( reject );
} );
}
);
const elements = null;
const upeElement = null;
const paymentIntentId = null;
const isUPEComplete = false;
/**
* Block UI to indicate processing and avoid duplicate submission.
*
* @param {Object} $form The jQuery object for the form.
*/
const blockUI = ( $form ) => {
$form.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6,
},
} );
};
/**
* Show error notice at top of checkout form.
* Will try to use a translatable message using the message code if available
*
* @param {string} errorMessage
*/
const showError = ( errorMessage ) => {
if (
typeof errorMessage !== 'string' &&
! ( errorMessage instanceof String )
) {
if (
errorMessage.code &&
getStripeServerData()[ errorMessage.code ]
) {
errorMessage = getStripeServerData()[ errorMessage.code ];
} else {
errorMessage = errorMessage.message;
}
}
let messageWrapper = '';
if ( errorMessage.includes( 'woocommerce-error' ) ) {
messageWrapper = errorMessage;
} else {
messageWrapper =
'<ul class="woocommerce-error" role="alert"><li>' +
errorMessage +
'</li></ul>';
}
const $container = $( '.woocommerce-notices-wrapper' ).first();
if ( ! $container.length ) {
return;
}
// Adapted from WooCommerce core @ ea9aa8c, assets/js/frontend/checkout.js#L514-L529
$(
'.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message'
).remove();
$container.prepend( messageWrapper );
$( 'form.checkout' )
.find( '.input-text, select, input:checkbox' )
.trigger( 'validate' )
.blur();
$.scroll_to_notices( $container );
$( document.body ).trigger( 'checkout_error' );
};
/**
* Converts form fields object into Stripe `billing_details` object.
*
* @param {Object} fields Object mapping checkout billing fields to values.
* @return {Object} Stripe formatted `billing_details` object.
*/
const getBillingDetails = ( fields ) => {
return {
name:
`${ fields.billing_first_name } ${ fields.billing_last_name }`.trim() ||
'-',
email: fields.billing_email || '-',
phone: fields.billing_phone || '-',
address: {
country: fields.billing_country || '-',
line1: fields.billing_address_1 || '-',
line2: fields.billing_address_2 || '-',
city: fields.billing_city || '-',
state: fields.billing_state || '-',
postal_code: fields.billing_postcode || '-',
},
};
};
/**
* Checks if UPE form is filled out. Displays errors if not.
*
* @param {Object} $form The jQuery object for the form.
* @return {boolean} false if incomplete.
*/
const checkUPEForm = async ( $form ) => {
if ( ! upeElement ) {
showError( 'Your payment information is incomplete.' );
return false;
}
if ( ! isUPEComplete ) {
// If UPE fields are not filled, confirm payment to trigger validation errors
const { error } = await api.getStripe().confirmPayment( {
elements,
confirmParams: {
return_url: '#',
},
} );
$form.removeClass( 'processing' ).unblock();
showError( error.message );
return false;
}
return true;
};
/**
* Submits checkout form via AJAX to create order and uses custom
* redirect URL in AJAX response to request payment confirmation from UPE
*
* @param {Object} $form The jQuery object for the form.
* @return {boolean} A flag for the event handler.
*/
const handleUPECheckout = async ( $form ) => {
const isUPEFormValid = await checkUPEForm( $form );
if ( ! isUPEFormValid ) {
return;
}
blockUI( $form );
// Create object where keys are form field names and values are form field values
const formFields = $form.serializeArray().reduce( ( obj, field ) => {
obj[ field.name ] = field.value;
return obj;
}, {} );
try {
const response = await api.processCheckout(
paymentIntentId,
formFields
);
const redirectUrl = response.redirect_url;
const upeConfig = {
elements,
confirmParams: {
return_url: redirectUrl,
payment_method_data: {
billing_details: getBillingDetails( formFields ),
},
},
};
let error;
if ( response.payment_needed ) {
( { error } = await api
.getStripe()
.confirmPayment( upeConfig ) );
} else {
( { error } = await api.getStripe().confirmSetup( upeConfig ) );
}
if ( error ) {
const upeType = formFields.wc_stripe_selected_upe_payment_type;
if (
upeType !== PAYMENT_METHOD_BOLETO &&
upeType !== PAYMENT_METHOD_OXXO &&
upeType !== PAYMENT_METHOD_MULTIBANCO
) {
await api.updateFailedOrder(
paymentIntentId,
response.order_id
);
}
throw error;
}
} catch ( error ) {
$form.removeClass( 'processing' ).unblock();
showError( error );
}
};
/**
* Displays the authentication modal to the user if needed.
*/
const maybeShowAuthenticationModal = () => {
const paymentMethodId = $( '#wc-stripe-payment-method' ).val();
const savePaymentMethod = $( '#wc-stripe-new-payment-method' ).is(
':checked'
);
const confirmation = api.confirmIntent(
window.location.href,
savePaymentMethod ? paymentMethodId : null
);
// Boolean `true` means that there is nothing to confirm.
if ( confirmation === true ) {
return;
}
const { request, isOrderPage } = confirmation;
if ( isOrderPage ) {
blockUI( $( '#order_review' ) );
$( '#payment' ).hide( 500 );
}
// Cleanup the URL.
// https://stackoverflow.com/a/5298684
// eslint-disable-next-line no-undef
history.replaceState(
'',
document.title,
window.location.pathname + window.location.search
);
request
.then( ( redirectUrl ) => {
window.location = redirectUrl;
} )
.catch( ( error ) => {
$( 'form.checkout' ).removeClass( 'processing' ).unblock();
$( '#order_review' ).removeClass( 'processing' ).unblock();
$( '#payment' ).show( 500 );
let errorMessage = error.message;
// If this is a generic error, we probably don't want to display the error message to the user,
// so display a generic message instead.
if ( error instanceof Error ) {
errorMessage = getStripeServerData()?.genericErrorMessage;
}
showError( errorMessage );
} );
};
/**
* Checks if the customer is using a saved payment method.
*
* @return {boolean} Boolean indicating whether or not a saved payment method is being used.
*/
function isUsingSavedPaymentMethod() {
return (
$( '#wc-stripe-payment-token-new' ).length &&
! $( '#wc-stripe-payment-token-new' ).is( ':checked' )
);
}
// Handle the checkout form when WooCommerce Gateway Stripe is chosen.
$( 'form.checkout' )
.on( 'checkout_place_order_stripe', function () {
if ( ! isUsingSavedPaymentMethod() ) {
if ( isUPEEnabled && paymentIntentId ) {
handleUPECheckout( $( this ) );
return false;
}
}
} )
.on( 'change', '.wc_payment_methods', () => {
// Check to see whether we should display the Cash App limit notice.
if ( $( 'input#payment_method_stripe_cashapp' ).is( ':checked' ) ) {
maybeShowCashAppLimitNotice(
'.woocommerce-checkout-payment',
Number( getStripeServerData()?.cartTotal )
);
} else {
removeCashAppLimitNotice();
}
} );
// Add terms parameter to UPE if save payment information checkbox is checked.
// This shows required legal mandates when customer elects to save payment method during checkout.
$( document ).on( 'change', '#wc-stripe-new-payment-method', () => {
const value = $( '#wc-stripe-new-payment-method' ).is( ':checked' )
? 'always'
: 'never';
if ( isUPEEnabled && upeElement ) {
upeElement.update( {
terms: getUPETerms( value ),
} );
}
} );
// On every page load, check to see whether we should display the authentication
// modal and display it if it should be displayed.
maybeShowAuthenticationModal();
// Handle hash change - used when authenticating payment with SCA on checkout page.
$( window ).on( 'hashchange', () => {
if ( window.location.hash.startsWith( '#wc-stripe-confirm-' ) ) {
maybeShowAuthenticationModal();
} else if ( window.location.hash.startsWith( '#confirm-' ) ) {
legacyHashchangeHandler( api, showError );
}
} );
} );