-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathdeferred-intent.js
160 lines (144 loc) · 4.85 KB
/
deferred-intent.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
import jQuery from 'jquery';
import WCStripeAPI from '../../api';
import {
generateCheckoutEventNames,
getSelectedUPEGatewayPaymentMethod,
getStripeServerData,
isPaymentMethodRestrictedToLocation,
isUsingSavedPaymentMethod,
paymentMethodSupportsDeferredIntent,
togglePaymentMethodForCountry,
} from '../../stripe-utils';
import './style.scss';
import {
confirmVoucherPayment,
confirmWalletPayment,
createAndConfirmSetupIntent,
mountStripePaymentElement,
processPayment,
} from './payment-processing';
jQuery( function ( $ ) {
// 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 );
} );
}
);
// Only attempt to mount the card element once that section of the page has loaded.
// We can use the updated_checkout event for this.
$( document.body ).on( 'updated_checkout', () => {
maybeMountStripePaymentElement();
} );
$( 'form.checkout' ).on( generateCheckoutEventNames(), function () {
return processPaymentIfNotUsingSavedMethod( $( this ) );
} );
function processPaymentIfNotUsingSavedMethod( $form ) {
const paymentMethodType = getSelectedUPEGatewayPaymentMethod();
if ( ! isUsingSavedPaymentMethod( paymentMethodType ) ) {
return processPayment( api, $form, paymentMethodType );
}
}
// Mount the Stripe Payment Elements onto the Add Payment Method page and Pay for Order page.
if (
$( 'form#add_payment_method' ).length ||
$( 'form#order_review' ).length
) {
maybeMountStripePaymentElement();
}
// For payment methods that don't support deferred intents, we mount the Payment Element only when it's selected.
$( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', () => {
maybeMountStripePaymentElement();
} );
// My Account > Payment Methods page submit.
$( 'form#add_payment_method' ).on( 'submit', function () {
return processPayment(
api,
$( 'form#add_payment_method' ),
getSelectedUPEGatewayPaymentMethod(),
createAndConfirmSetupIntent
);
} );
// Pay for Order page submit.
$( '#order_review' ).on( 'submit', () => {
const paymentMethodType = getSelectedUPEGatewayPaymentMethod();
if ( ! isUsingSavedPaymentMethod( paymentMethodType ) ) {
return processPayment(
api,
$( '#order_review' ),
paymentMethodType
);
}
} );
async function maybeMountStripePaymentElement() {
// If the card element selector doesn't exist, do nothing.
// For example, when a 100% discount coupon is applied.
if ( ! $( '.wc-stripe-upe-element' ).length ) {
return;
}
const selectedMethod = getSelectedUPEGatewayPaymentMethod();
for ( const upeElement of $( '.wc-stripe-upe-element' ).toArray() ) {
// Don't mount if it's already mounted.
if ( $( upeElement ).children().length ) {
continue;
}
// Payment methods that don't support deferred intents don't need to be mounted unless they are selected.
if (
upeElement.dataset.paymentMethodType !== selectedMethod &&
! paymentMethodSupportsDeferredIntent( upeElement )
) {
continue;
}
await mountStripePaymentElement( api, upeElement );
restrictPaymentMethodToLocation( upeElement );
}
}
function restrictPaymentMethodToLocation( upeElement ) {
if ( isPaymentMethodRestrictedToLocation( upeElement ) ) {
togglePaymentMethodForCountry( upeElement );
// This event only applies to the checkout form, but not "pay for order" or "add payment method" pages.
$( '#billing_country' ).on( 'change', function () {
togglePaymentMethodForCountry( upeElement );
} );
}
}
/**
* Checks if the URL hash starts with #wc-stripe-voucher- or #wc-stripe-wallet- and whether we
* should display the relevant confirmation modal.
*/
function maybeConfirmVoucherOrWalletPayment() {
if (
getStripeServerData()?.isOrderPay ||
getStripeServerData()?.isCheckout ||
getStripeServerData()?.isChangingPayment
) {
if ( window.location.hash.startsWith( '#wc-stripe-voucher-' ) ) {
confirmVoucherPayment(
api,
getStripeServerData()?.isOrderPay
? $( '#order_review' )
: $( 'form.checkout' )
);
} else if (
window.location.hash.startsWith( '#wc-stripe-wallet-' )
) {
confirmWalletPayment(
api,
getStripeServerData()?.isOrderPay ||
getStripeServerData()?.isChangingPayment
? $( '#order_review' )
: $( 'form.checkout' )
);
}
}
}
// On every page load and on hash change, check to see whether we should display the Voucher (Boleto/Oxxo/Multibanco) or Wallet (CashApp/WeChat Pay) modal.
// Every page load is needed for the Pay for Order page which doesn't trigger the hash change.
maybeConfirmVoucherOrWalletPayment();
$( window ).on( 'hashchange', () => {
maybeConfirmVoucherOrWalletPayment();
} );
} );