This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 546
/
payments.js
1039 lines (965 loc) Β· 32.8 KB
/
payments.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* payments.js
* Stripe Payments Demo. Created by Romain Huet (@romainhuet)
* and Thorsten Schaeff (@thorwebdev).
*
* This modern JavaScript file handles the checkout process using Stripe.
*
* 1. It shows how to accept card payments with the `card` Element, and
* the `paymentRequestButton` Element for Payment Request and Apple Pay.
* 2. It shows how to use the Stripe Sources API to accept non-card payments,
* such as iDEAL, SOFORT, SEPA Direct Debit, and more.
*/
(async () => {
'use strict';
// Retrieve the configuration for the store.
const config = await store.getConfig();
let activeCurrency = config.currency;
// Create references to the main form and its submit button.
const form = document.getElementById('payment-form');
const submitButton = form.querySelector('button[type=submit]');
// Global variable to store the submit button text.
let submitButtonPayText = 'Pay';
const updateSubmitButtonPayText = (newText) => {
submitButton.textContent = newText;
submitButtonPayText = newText;
};
// Global variable to store the PaymentIntent object.
let paymentIntent;
/**
* Setup Stripe Elements.
*/
// Create a Stripe client.
const stripe = Stripe(config.stripePublishableKey);
// Create an instance of Elements.
const elements = stripe.elements();
// Prepare the styles for Elements.
const style = {
base: {
iconColor: '#666ee8',
color: '#31325f',
fontWeight: 400,
fontFamily:
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif',
fontSmoothing: 'antialiased',
fontSize: '15px',
'::placeholder': {
color: '#aab7c4',
},
':-webkit-autofill': {
color: '#666ee8',
},
},
};
/**
* Implement a Stripe Card Element that matches the look-and-feel of the app.
*
* This makes it easy to collect debit and credit card payments information.
*/
// Create a Card Element and pass some custom styles to it.
const card = elements.create('card', {style, hidePostalCode: true});
// Mount the Card Element on the page.
card.mount('#card-element');
// Monitor change events on the Card Element to display any errors.
card.on('change', ({error}) => {
const cardErrors = document.getElementById('card-errors');
if (error) {
cardErrors.textContent = error.message;
cardErrors.classList.add('visible');
} else {
cardErrors.classList.remove('visible');
}
// Re-enable the Pay button.
submitButton.disabled = false;
});
/**
* Implement a Stripe IBAN Element that matches the look-and-feel of the app.
*
* This makes it easy to collect bank account information.
*/
// Create a IBAN Element and pass the right options for styles and supported countries.
const ibanOptions = {
style,
supportedCountries: ['SEPA'],
};
const iban = elements.create('iban', ibanOptions);
// Mount the IBAN Element on the page.
iban.mount('#iban-element');
// Monitor change events on the IBAN Element to display any errors.
iban.on('change', ({error, bankName}) => {
const ibanErrors = document.getElementById('iban-errors');
if (error) {
ibanErrors.textContent = error.message;
ibanErrors.classList.add('visible');
} else {
ibanErrors.classList.remove('visible');
if (bankName) {
updateButtonLabel('sepa_debit', bankName);
}
}
// Re-enable the Pay button.
submitButton.disabled = false;
});
/**
* Add an iDEAL Bank selection Element that matches the look-and-feel of the app.
*
* This allows you to send the customer directly to their iDEAL enabled bank.
*/
// Create a iDEAL Bank Element and pass the style options, along with an extra `padding` property.
const idealBank = elements.create('idealBank', {
style: {base: Object.assign({padding: '10px 15px'}, style.base)},
});
// Mount the iDEAL Bank Element on the page.
idealBank.mount('#ideal-bank-element');
/**
* Add a BECS element that matches the look-and-feel of the app.
*
* This allows you to collect australian bank account details
*/
const becsBank = elements.create('auBankAccount', {style});
// Mount the BECS element on the page.
becsBank.mount('#becs-bank-element');
// Monitor change events on the BECS Element to display any errors.
becsBank.on('change', ({error, bankName}) => {
const becsBankErrors = document.getElementById('becs-errors');
if (error) {
becsBankErrors.textContent = error.message;
becsBankErrors.classList.add('visible');
} else {
becsBankErrors.classList.remove('visible');
if (bankName) {
updateButtonLabel('au_becs_debit', bankName);
}
}
// Re-enable the Pay button.
submitButton.disabled = false;
});
/**
* Implement a Stripe Payment Request Button Element.
*
* This automatically supports the Payment Request API (already live on Chrome),
* as well as Apple Pay on the Web on Safari, Google Pay, and Microsoft Pay.
* When of these two options is available, this element adds a βPayβ button on top
* of the page to let users pay in just a click (or a tap on mobile).
*/
// Make sure all data is loaded from the store to compute the payment amount.
await store.loadProducts();
// Create the payment request.
const paymentRequest = stripe.paymentRequest({
country: config.stripeCountry,
currency: config.currency,
total: {
label: 'Total',
amount: store.getPaymentTotal(),
},
requestShipping: true,
requestPayerEmail: true,
shippingOptions: config.shippingOptions,
});
// Callback when a payment method is created.
paymentRequest.on('paymentmethod', async (event) => {
// Confirm the PaymentIntent with the payment method returned from the payment request.
const {error} = await stripe.confirmCardPayment(
paymentIntent.client_secret,
{
payment_method: event.paymentMethod.id,
shipping: {
name: event.shippingAddress.recipient,
phone: event.shippingAddress.phone,
address: {
line1: event.shippingAddress.addressLine[0],
city: event.shippingAddress.city,
postal_code: event.shippingAddress.postalCode,
state: event.shippingAddress.region,
country: event.shippingAddress.country,
},
},
},
{handleActions: false}
);
if (error) {
// Report to the browser that the payment failed.
event.complete('fail');
handlePayment({error});
} else {
// Report to the browser that the confirmation was successful, prompting
// it to close the browser payment method collection interface.
event.complete('success');
// Let Stripe.js handle the rest of the payment flow, including 3D Secure if needed.
const response = await stripe.confirmCardPayment(
paymentIntent.client_secret
);
handlePayment(response);
}
});
// Callback when the shipping address is updated.
paymentRequest.on('shippingaddresschange', (event) => {
event.updateWith({status: 'success'});
});
// Callback when the shipping option is changed.
paymentRequest.on('shippingoptionchange', async (event) => {
// Update the PaymentIntent to reflect the shipping cost.
const response = await store.updatePaymentIntentWithShippingCost(
paymentIntent.id,
store.getLineItems(),
event.shippingOption
);
event.updateWith({
total: {
label: 'Total',
amount: response.paymentIntent.amount,
},
status: 'success',
});
const amount = store.formatPrice(
response.paymentIntent.amount,
activeCurrency
);
updateSubmitButtonPayText(`Pay ${amount}`);
});
// Create the Payment Request Button.
const paymentRequestButton = elements.create('paymentRequestButton', {
paymentRequest,
});
// Check if the Payment Request is available (or Apple Pay on the Web).
const paymentRequestSupport = await paymentRequest.canMakePayment();
if (paymentRequestSupport) {
// Display the Pay button by mounting the Element in the DOM.
paymentRequestButton.mount('#payment-request-button');
// Replace the instruction.
document.querySelector('.instruction span').innerText = 'Or enter';
// Show the payment request section.
document.getElementById('payment-request').classList.add('visible');
}
/**
* Handle the form submission.
*
* This uses Stripe.js to confirm the PaymentIntent using payment details collected
* with Elements.
*
* Please note this form is not submitted when the user chooses the "Pay" button
* or Apple Pay, Google Pay, and Microsoft Pay since they provide name and
* shipping information directly.
*/
// Listen to changes to the user-selected country.
form
.querySelector('select[name=country]')
.addEventListener('change', (event) => {
event.preventDefault();
selectCountry(event.target.value);
});
// Submit handler for our payment form.
form.addEventListener('submit', async (event) => {
event.preventDefault();
// Retrieve the user information from the form.
const payment = form.querySelector('input[name=payment]:checked').value;
const name = form.querySelector('input[name=name]').value;
const country = form.querySelector('select[name=country] option:checked')
.value;
const email = form.querySelector('input[name=email]').value;
const billingAddress = {
line1: form.querySelector('input[name=address]').value,
postal_code: form.querySelector('input[name=postal_code]').value,
};
const shipping = {
name,
address: {
line1: form.querySelector('input[name=address]').value,
city: form.querySelector('input[name=city]').value,
postal_code: form.querySelector('input[name=postal_code]').value,
state: form.querySelector('input[name=state]').value,
country,
},
};
// Disable the Pay button to prevent multiple click events.
submitButton.disabled = true;
submitButton.textContent = 'Processingβ¦';
// Update Payment Intent if currency is different to default
if (config.currency !== activeCurrency) {
const response = await store.updatePaymentIntentCurrency(
paymentIntent.id,
activeCurrency,
[payment]
);
if (response.error) {
handleError(response);
return;
}
}
if (payment === 'card') {
// Let Stripe.js handle the confirmation of the PaymentIntent with the card Element.
const response = await stripe.confirmCardPayment(
paymentIntent.client_secret,
{
payment_method: {
card,
billing_details: {
name,
address: billingAddress,
},
},
shipping,
}
);
handlePayment(response);
} else if (payment === 'sepa_debit') {
// Confirm the PaymentIntent with the IBAN Element.
const response = await stripe.confirmSepaDebitPayment(
paymentIntent.client_secret,
{
payment_method: {
sepa_debit: iban,
billing_details: {
name,
email,
},
},
}
);
handlePayment(response);
} else if (payment === 'p24') {
const response = await stripe.confirmP24Payment(
paymentIntent.client_secret,
{
payment_method: {
billing_details: {
name,
email,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment === 'ideal') {
// Confirm the PaymentIntent with the iDEAL Element.
const response = await stripe.confirmIdealPayment(
paymentIntent.client_secret,
{
payment_method: {
ideal: idealBank,
billing_details: {
name,
email,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment === 'bancontact') {
const response = await stripe.confirmBancontactPayment(
paymentIntent.client_secret,
{
payment_method: {
billing_details: {
name,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment === 'eps') {
const response = await stripe.confirmEpsPayment(
paymentIntent.client_secret,
{
payment_method: {
billing_details: {
name,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment === 'giropay') {
const response = await stripe.confirmGiropayPayment(
paymentIntent.client_secret,
{
payment_method: {
billing_details: {
name,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment === 'alipay') {
const response = await stripe.confirmAlipayPayment(
paymentIntent.client_secret,
{
payment_method: {
billing_details: {
name,
},
},
return_url: window.location.href,
}
);
handlePayment(response);
} else if (payment == 'au_becs_debit') {
const response = await stripe.confirmAuBecsDebitPayment(
paymentIntent.client_secret,
{
payment_method: {
au_becs_debit: becsBank,
billing_details: {
name,
email,
},
},
}
);
handlePayment(response);
} else {
// Prepare all the Stripe source common data.
const sourceData = {
type: payment,
amount: paymentIntent.amount,
currency: paymentIntent.currency,
owner: {
name,
email,
},
redirect: {
return_url: `${window.location.href}?payment_intent=${paymentIntent.id}`,
},
statement_descriptor: 'Stripe Payments Demo',
metadata: {
paymentIntent: paymentIntent.id,
},
};
// Add extra source information which are specific to a payment method.
switch (payment) {
case 'sofort':
// SOFORT: The country is required before redirecting to the bank.
sourceData.sofort = {
country,
};
break;
case 'ach_credit_transfer':
// ACH Bank Transfer: Only supports USD payments, edit the default config to try it.
// In test mode, we can set the funds to be received via the owner email.
sourceData.owner.email = `amount_${paymentIntent.amount}@example.com`;
break;
}
// Create a Stripe source with the common data and extra information.
const {source} = await stripe.createSource(sourceData);
handleSourceActivation(source);
}
});
// Handle new PaymentIntent result
const handlePayment = (paymentResponse) => {
const {paymentIntent, error} = paymentResponse;
const mainElement = document.getElementById('main');
const confirmationElement = document.getElementById('confirmation');
if (error && error.type === 'validation_error') {
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
submitButton.disabled = false;
submitButton.textContent = submitButtonPayText;
} else if (error) {
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
confirmationElement.querySelector('.error-message').innerText =
error.message;
mainElement.classList.add('error');
} else if (paymentIntent.status === 'succeeded') {
// Success! Payment is confirmed. Update the interface to display the confirmation screen.
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
// Update the note about receipt and shipping (the payment has been fully confirmed by the bank).
confirmationElement.querySelector('.note').innerText =
'We just sent your receipt to your email address, and your items will be on their way shortly.';
mainElement.classList.add('success');
} else if (paymentIntent.status === 'processing') {
// Success! Now waiting for payment confirmation. Update the interface to display the confirmation screen.
mainElement.classList.remove('processing');
// Update the note about receipt and shipping (the payment is not yet confirmed by the bank).
confirmationElement.querySelector('.note').innerText =
'Weβll send your receipt and ship your items as soon as your payment is confirmed.';
mainElement.classList.add('success');
} else if (paymentIntent.status === 'requires_payment_method') {
// Failure. Requires new PaymentMethod, show last payment error message.
mainElement.classList.remove('processing');
confirmationElement.querySelector('.error-message').innerText =
paymentIntent.last_payment_error || 'Payment failed';
mainElement.classList.add('error');
} else {
// Payment has failed.
mainElement.classList.remove('success');
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
mainElement.classList.add('error');
}
};
const handleError = (updateResponse) => {
// handle any error
const {paymentIntent, error} = updateResponse;
const mainElement = document.getElementById('main');
const confirmationElement = document.getElementById('confirmation');
if (error && error.type === 'validation_error') {
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
submitButton.disabled = false;
submitButton.textContent = submitButtonPayText;
} else if (error) {
mainElement.classList.remove('processing');
mainElement.classList.remove('receiver');
confirmationElement.querySelector('.error-message').innerText =
error.message;
mainElement.classList.add('error');
}
};
// Handle activation of payment sources not yet supported by PaymentIntents
const handleSourceActivation = (source) => {
const mainElement = document.getElementById('main');
const confirmationElement = document.getElementById('confirmation');
switch (source.flow) {
case 'none':
// Normally, sources with a `flow` value of `none` are chargeable right away,
// but there are exceptions, for instance for WeChat QR codes just below.
if (source.type === 'wechat') {
// Display the QR code.
const qrCode = new QRCode('wechat-qrcode', {
text: source.wechat.qr_code_url,
width: 128,
height: 128,
colorDark: '#424770',
colorLight: '#f8fbfd',
correctLevel: QRCode.CorrectLevel.H,
});
// Hide the previous text and update the call to action.
form.querySelector('.payment-info.wechat p').style.display = 'none';
let amount = store.formatPrice(
store.getPaymentTotal(),
activeCurrency
);
updateSubmitButtonPayText(
`Scan this QR code on WeChat to pay ${amount}`
);
// Start polling the PaymentIntent status.
pollPaymentIntentStatus(paymentIntent.id, 300000);
} else {
console.log('Unhandled none flow.', source);
}
break;
case 'redirect':
// Immediately redirect the customer.
submitButton.textContent = 'Redirectingβ¦';
window.location.replace(source.redirect.url);
break;
case 'code_verification':
// Display a code verification input to verify the source.
break;
case 'receiver':
// Display the receiver address to send the funds to.
mainElement.classList.add('success', 'receiver');
const receiverInfo = confirmationElement.querySelector(
'.receiver .info'
);
let amount = store.formatPrice(source.amount, activeCurrency);
switch (source.type) {
case 'ach_credit_transfer':
// Display the ACH Bank Transfer information to the user.
const ach = source.ach_credit_transfer;
receiverInfo.innerHTML = `
<ul>
<li>
Amount:
<strong>${amount}</strong>
</li>
<li>
Bank Name:
<strong>${ach.bank_name}</strong>
</li>
<li>
Account Number:
<strong>${ach.account_number}</strong>
</li>
<li>
Routing Number:
<strong>${ach.routing_number}</strong>
</li>
</ul>`;
break;
case 'multibanco':
// Display the Multibanco payment information to the user.
const multibanco = source.multibanco;
receiverInfo.innerHTML = `
<ul>
<li>
Amount (Montante):
<strong>${amount}</strong>
</li>
<li>
Entity (Entidade):
<strong>${multibanco.entity}</strong>
</li>
<li>
Reference (Referencia):
<strong>${multibanco.reference}</strong>
</li>
</ul>`;
break;
default:
console.log('Unhandled receiver flow.', source);
}
// Poll the PaymentIntent status.
pollPaymentIntentStatus(paymentIntent.id);
break;
default:
// Customer's PaymentIntent is received, pending payment confirmation.
break;
}
};
/**
* Check if the PaymentIntent is in a "terminal" status
* and therefore if we should show an error in the UI
*/
const paymentIntentTerminalState = ({status, last_payment_error}) => {
const endStates = ['succeeded', 'processing', 'canceled'];
const hasError = typeof last_payment_error !== 'undefined';
return (
endStates.includes(status) ||
(status === 'requires_payment_method' && hasError)
);
};
/**
* Monitor the status of a source after a redirect flow.
*
* This means there is a `source` parameter in the URL, and an active PaymentIntent.
* When this happens, we'll monitor the status of the PaymentIntent and present real-time
* information to the user.
*/
const pollPaymentIntentStatus = async (
paymentIntent,
timeout = 30000,
interval = 500,
start = null
) => {
start = start ? start : Date.now();
const endStates = [
'succeeded',
'processing',
'canceled',
'requires_payment_method',
];
// Retrieve the PaymentIntent status from our server.
const rawResponse = await fetch(`payment_intents/${paymentIntent}/status`);
const response = await rawResponse.json();
const isTerminalState = paymentIntentTerminalState(response.paymentIntent);
if (!isTerminalState && Date.now() < start + timeout) {
// Not done yet. Let's wait and check again.
setTimeout(
pollPaymentIntentStatus,
interval,
paymentIntent,
timeout,
interval,
start
);
} else {
handlePayment(response);
if (!isTerminalState) {
// Status has not changed yet. Let's time out.
console.warn(new Error('Polling timed out.'));
}
}
};
const url = new URL(window.location.href);
const mainElement = document.getElementById('main');
if (url.searchParams.get('payment_intent')) {
if (
url.searchParams.get('source') &&
url.searchParams.get('client_secret')
) {
mainElement.classList.add('checkout', 'success', 'processing');
}
// Poll the PaymentIntent status.
pollPaymentIntentStatus(url.searchParams.get('payment_intent'));
} else {
// Update the interface to display the checkout form.
mainElement.classList.add('checkout');
// Create the PaymentIntent with the cart details.
const response = await store.createPaymentIntent(
config.currency,
store.getLineItems()
);
paymentIntent = response.paymentIntent;
}
document.getElementById('main').classList.remove('loading');
/**
* Display the relevant payment methods for a selected country.
*/
// List of relevant countries for the payment methods supported in this demo.
// Read the Stripe guide: https://stripe.com/payments/payment-methods-guide
const paymentMethods = {
ach_credit_transfer: {
name: 'Bank Transfer',
flow: 'receiver',
countries: ['US'],
currencies: ['usd'],
},
alipay: {
name: 'Alipay',
flow: 'redirect',
countries: ['CN', 'HK', 'SG', 'JP'],
currencies: [
'aud',
'cad',
'eur',
'gbp',
'hkd',
'jpy',
'nzd',
'sgd',
'usd',
],
},
bancontact: {
name: 'Bancontact',
flow: 'redirect',
countries: ['BE'],
currencies: ['eur'],
},
card: {
name: 'Card',
flow: 'none',
},
eps: {
name: 'EPS',
flow: 'redirect',
countries: ['AT'],
currencies: ['eur'],
},
ideal: {
name: 'iDEAL',
flow: 'redirect',
countries: ['NL'],
currencies: ['eur'],
},
giropay: {
name: 'Giropay',
flow: 'redirect',
countries: ['DE'],
currencies: ['eur'],
},
multibanco: {
name: 'Multibanco',
flow: 'receiver',
countries: ['PT'],
currencies: ['eur'],
},
p24: {
name: 'Przelewy24',
flow: 'redirect',
countries: ['PL'],
currencies: ['eur', 'pln'],
},
sepa_debit: {
name: 'SEPA Direct Debit',
flow: 'none',
countries: [
'FR',
'DE',
'ES',
'BE',
'NL',
'LU',
'IT',
'PT',
'AT',
'IE',
'FI',
],
currencies: ['eur'],
},
sofort: {
name: 'SOFORT',
flow: 'redirect',
countries: ['DE', 'AT'],
currencies: ['eur'],
},
wechat: {
name: 'WeChat',
flow: 'none',
countries: ['CN', 'HK', 'SG', 'JP'],
currencies: [
'aud',
'cad',
'eur',
'gbp',
'hkd',
'jpy',
'nzd',
'sgd',
'usd',
],
},
au_becs_debit: {
name: 'BECS Direct Debit',
flow: 'none',
countries: ['AU'],
currencies: ['aud'],
},
};
// Update the main button to reflect the payment method being selected.
const updateButtonLabel = (paymentMethod, bankName) => {
let amount = store.formatPrice(store.getPaymentTotal(), activeCurrency);
let name = paymentMethods[paymentMethod].name;
let label = `Pay ${amount}`;
if (paymentMethod !== 'card') {
label = `Pay ${amount} with ${name}`;
}
if (paymentMethod === 'wechat') {
label = `Generate QR code to pay ${amount} with ${name}`;
}
if (['sepa_debit', 'au_becs_debit'].includes(paymentMethod) && bankName) {
label = `Debit ${amount} from ${bankName}`;
}
updateSubmitButtonPayText(label);
};
const selectCountry = (country) => {
const selector = document.getElementById('country');
selector.querySelector(`option[value=${country}]`).selected = 'selected';
selector.className = `field ${country.toLowerCase()}`;
//update currency if there's a currency for that country
if (country === 'AU') {
activeCurrency = 'aud';
} else {
activeCurrency = config.currency;
}
// Trigger the methods to show relevant fields and payment methods on page load.
showRelevantFormFields();
showRelevantPaymentMethods();
};
// Show only form fields that are relevant to the selected country.
const showRelevantFormFields = (country) => {
if (!country) {
country = form.querySelector('select[name=country] option:checked').value;
}
const zipLabel = form.querySelector('label.zip');
// Only show the state input for the United States.
zipLabel.parentElement.classList.toggle(
'with-state',
['AU', 'US'].includes(country)
);
// Update the ZIP label to make it more relevant for each country.
const zipInput = form.querySelector('label.zip input');
const zipSpan = form.querySelector('label.zip span');
switch (country) {
case 'US':
zipSpan.innerText = 'ZIP';
zipInput.placeholder = '94103';
break;
case 'GB':
zipSpan.innerText = 'Postcode';
zipInput.placeholder = 'EC1V 9NR';
break;
case 'AU':
zipSpan.innerText = 'Postcode';
zipInput.placeholder = '3000';
break;
default:
zipSpan.innerText = 'Postal Code';
zipInput.placeholder = '94103';
break;
}
// Update the 'City' to appropriate name
const cityInput = form.querySelector('label.city input');
const citySpan = form.querySelector('label.city span');
switch (country) {
case 'AU':
citySpan.innerText = 'City / Suburb';
cityInput.placeholder = 'Melbourne';
break;
default:
citySpan.innerText = 'City';
cityInput.placeholder = 'San Francisco';
break;
}
};
// Show only the payment methods that are relevant to the selected country.
const showRelevantPaymentMethods = (country) => {
if (!country) {
country = form.querySelector('select[name=country] option:checked').value;
}
const paymentInputs = form.querySelectorAll('input[name=payment]');
for (let i = 0; i < paymentInputs.length; i++) {
let input = paymentInputs[i];
input.parentElement.classList.toggle(
'visible',
input.value === 'card' ||
(config.paymentMethods.includes(input.value) &&
paymentMethods[input.value].countries.includes(country) &&
paymentMethods[input.value].currencies.includes(activeCurrency))
);
}
// Hide the tabs if card is the only available option.
const paymentMethodsTabs = document.getElementById('payment-methods');
paymentMethodsTabs.classList.toggle(
'visible',
paymentMethodsTabs.querySelectorAll('li.visible').length > 1
);
// Check the first payment option again.
paymentInputs[0].checked = 'checked';
form.querySelector('.payment-info.card').classList.add('visible');
form.querySelector('.payment-info.ideal').classList.remove('visible');
form.querySelector('.payment-info.sepa_debit').classList.remove('visible');
form.querySelector('.payment-info.wechat').classList.remove('visible');
form.querySelector('.payment-info.redirect').classList.remove('visible');
form
.querySelector('.payment-info.au_becs_debit')
.classList.remove('visible');
updateButtonLabel(paymentInputs[0].value);
};
// Listen to changes to the payment method selector.
for (let input of document.querySelectorAll('input[name=payment]')) {
input.addEventListener('change', (event) => {
event.preventDefault();
const payment = form.querySelector('input[name=payment]:checked').value;
const flow = paymentMethods[payment].flow;
// Update button label.
updateButtonLabel(event.target.value);
// Show the relevant details, whether it's an extra element or extra information for the user.