stripe-pwa-elements 4.0.2 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/{factory-tenFFIul.js → factory-Cm6zUD7R.js} +7 -1
- package/dist/cjs/{i18n-FhEOc8vz.js → i18n-lBbOeQt4.js} +6 -0
- package/dist/cjs/ion-icon_2.cjs.entry.js +5 -3
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/stripe-address-element.cjs.entry.js +2 -2
- package/dist/cjs/stripe-card-element.cjs.entry.js +2 -2
- package/dist/cjs/stripe-currency-selector.cjs.entry.js +2 -2
- package/dist/cjs/stripe-elements.cjs.js +1 -1
- package/dist/cjs/stripe-express-checkout-element.cjs.entry.js +2 -2
- package/dist/cjs/stripe-link-authentication-element.cjs.entry.js +2 -2
- package/dist/cjs/stripe-payment-element.cjs.entry.js +3 -3
- package/dist/cjs/{stripe-payment-element.helpers-DukP33RI.js → stripe-payment-element.helpers-CvAhIqLt.js} +4 -7
- package/dist/cjs/stripe-payment-sheet.cjs.entry.js +516 -35
- package/dist/collection/components/stripe-address-element/stripe-address-element.js +2 -2
- package/dist/collection/components/stripe-modal/stripe-modal.css +4 -0
- package/dist/collection/components/stripe-modal/stripe-modal.js +3 -1
- package/dist/collection/components/stripe-payment-element/stripe-payment-element.helpers.js +4 -7
- package/dist/collection/components/stripe-payment-element/stripe-payment-element.js +4 -4
- package/dist/collection/components/stripe-payment-sheet/payment-sheet-state.js +21 -7
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.checkout.js +94 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.helpers.js +193 -9
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.js +240 -21
- package/dist/collection/services/link-authentication-element-manager.js +5 -0
- package/dist/collection/utils/i18n.js +6 -0
- package/dist/components/factory.js +1 -1
- package/dist/components/i18n.js +1 -1
- package/dist/components/stripe-modal2.js +1 -1
- package/dist/components/stripe-payment-element.helpers.js +1 -1
- package/dist/components/stripe-payment-sheet.js +1 -1
- package/dist/esm/{factory-Cd6OGTWN.js → factory-BaGv8K_i.js} +7 -2
- package/dist/esm/{i18n-B6JoQetF.js → i18n-CL5n3bOb.js} +6 -0
- package/dist/esm/ion-icon_2.entry.js +5 -3
- package/dist/esm/loader.js +1 -1
- package/dist/esm/stripe-address-element.entry.js +2 -2
- package/dist/esm/stripe-card-element.entry.js +2 -2
- package/dist/esm/stripe-currency-selector.entry.js +2 -2
- package/dist/esm/stripe-elements.js +1 -1
- package/dist/esm/stripe-express-checkout-element.entry.js +2 -2
- package/dist/esm/stripe-link-authentication-element.entry.js +2 -2
- package/dist/esm/stripe-payment-element.entry.js +3 -3
- package/dist/esm/{stripe-payment-element.helpers-CipV87b8.js → stripe-payment-element.helpers-Bu9RfxS4.js} +4 -7
- package/dist/esm/stripe-payment-sheet.entry.js +516 -35
- package/dist/stripe-elements/{p-f892e375.entry.js → p-2e41699e.entry.js} +1 -1
- package/dist/stripe-elements/{p-6a9c8b7a.entry.js → p-3fc4583a.entry.js} +1 -1
- package/dist/stripe-elements/{p-915b86ed.entry.js → p-7e826964.entry.js} +1 -1
- package/dist/stripe-elements/p-BhKXGROE.js +1 -0
- package/dist/stripe-elements/p-CL5n3bOb.js +1 -0
- package/dist/stripe-elements/{p-C7MQk9FK.js → p-DlMEZwSe.js} +1 -1
- package/dist/stripe-elements/{p-b06a254f.entry.js → p-a395a5d1.entry.js} +1 -1
- package/dist/stripe-elements/{p-5338bc5e.entry.js → p-b62c9dae.entry.js} +1 -1
- package/dist/stripe-elements/p-b7c20e8e.entry.js +1 -0
- package/dist/stripe-elements/{p-b91c4fa7.entry.js → p-bec62526.entry.js} +1 -1
- package/dist/stripe-elements/{p-7fe4a287.entry.js → p-f5552c81.entry.js} +1 -1
- package/dist/stripe-elements/stripe-elements.esm.js +1 -1
- package/dist/types/components/stripe-payment-element/stripe-payment-element.helpers.d.ts +7 -2
- package/dist/types/components/stripe-payment-sheet/payment-sheet-state.d.ts +21 -4
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.checkout.d.ts +39 -0
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.d.ts +50 -5
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.helpers.d.ts +66 -8
- package/dist/types/components.d.ts +22 -9
- package/dist/types/services/interfaces.d.ts +2 -0
- package/package.json +4 -2
- package/readme.md +17 -0
- package/dist/stripe-elements/p-B6JoQetF.js +0 -1
- package/dist/stripe-elements/p-DFAhQS0n.js +0 -1
- package/dist/stripe-elements/p-e7372ca7.entry.js +0 -1
|
@@ -483,7 +483,7 @@ export class StripeAddressElement {
|
|
|
483
483
|
"references": {
|
|
484
484
|
"AddressSubmitHandler": {
|
|
485
485
|
"location": "local",
|
|
486
|
-
"path": "/
|
|
486
|
+
"path": "/home/circleci/project/src/components/stripe-address-element/stripe-address-element.tsx",
|
|
487
487
|
"id": "src/components/stripe-address-element/stripe-address-element.tsx::AddressSubmitHandler"
|
|
488
488
|
}
|
|
489
489
|
}
|
|
@@ -573,7 +573,7 @@ export class StripeAddressElement {
|
|
|
573
573
|
"references": {
|
|
574
574
|
"AddressSubmitEvent": {
|
|
575
575
|
"location": "local",
|
|
576
|
-
"path": "/
|
|
576
|
+
"path": "/home/circleci/project/src/components/stripe-address-element/stripe-address-element.tsx",
|
|
577
577
|
"id": "src/components/stripe-address-element/stripe-address-element.tsx::AddressSubmitEvent"
|
|
578
578
|
}
|
|
579
579
|
}
|
|
@@ -186,6 +186,8 @@ export class StripeModal {
|
|
|
186
186
|
return;
|
|
187
187
|
}
|
|
188
188
|
const modalChild = this.el.shadowRoot.querySelector('.modal-child');
|
|
189
|
+
// The panel stays in the DOM while closed and keeps its scroll position; start each opening at the top (title, close button)
|
|
190
|
+
modalChild.scrollTop = 0;
|
|
189
191
|
modalChild.classList.add('loading');
|
|
190
192
|
modalChild.style.willChange = 'transform';
|
|
191
193
|
modalChild.addEventListener('transitionend', () => {
|
|
@@ -200,7 +202,7 @@ export class StripeModal {
|
|
|
200
202
|
render() {
|
|
201
203
|
const { renderedOpen, showCloseButton } = this;
|
|
202
204
|
const guardTabIndex = renderedOpen ? 0 : -1;
|
|
203
|
-
return (h(Host, { key: '
|
|
205
|
+
return (h(Host, { key: '55e0d63b18df165313ea3dd8b8f17aa0e0d0a253', role: renderedOpen ? 'dialog' : undefined, "aria-modal": renderedOpen ? 'true' : undefined, onKeyDown: this.handleKeyDown, onFocusin: this.handleFocusIn }, h("div", { key: 'adb0b493c0ae525f275025c03e1b8f02b04de1af', class: `modal-row${renderedOpen ? ' open' : ''}`, onClick: () => this.closeModal() }, h("div", { key: '478bdad2ad481bfc2d8ffcf552788292fa93daed', class: "modal-child", tabindex: "-1", ref: el => (this.dialogElement = el), onClick: e => e.stopPropagation() }, h("div", { key: '8cc68ecefa37ec1db65f6c910d1da40f9144e722', class: "focus-guard", tabindex: guardTabIndex, onFocus: this.handleStartGuardFocus }), showCloseButton ? (h("div", { class: "modal-close-button-wrap" }, h("button", { type: "button", class: "modal-close-button", "aria-label": i18n.t('Close'), ref: el => (this.closeButtonElement = el), onClick: () => this.closeModal() }, h("ion-icon", { name: "close", size: "large", "aria-hidden": "true" })))) : null, h("div", { key: '26271a75906a72e9944599f98da007bfebc2e348', class: "modal-loading" }, h("svg", { key: 'bc6b1267e2f3b6b1dd7209db55be717fddf9acd4', version: "1.1", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 2400 2400" }, h("g", { key: '00ed54798ac376ed71f87a088fad2519cb1262dc', "stroke-width": "200", "stroke-linecap": "round", stroke: "currentColor", fill: "none", id: "spinner" }, h("line", { key: '98242d65eb9470430bd5cc656cbb479232782746', x1: "1200", y1: "600", x2: "1200", y2: "100" }), h("line", { key: '2e2f0538a6d048465ef5f8a3d5ed1932f6c59270', opacity: "0.5", x1: "1200", y1: "2300", x2: "1200", y2: "1800" }), h("line", { key: '11f850b0e7dc0d259f35f56aab17068d4d25ca95', opacity: "0.917", x1: "900", y1: "680.4", x2: "650", y2: "247.4" }), h("line", { key: '14c3226b5d2741902f428a208966adf016c1bb7e', opacity: "0.417", x1: "1750", y1: "2152.6", x2: "1500", y2: "1719.6" }), h("line", { key: 'd41da322484b62c1db1df72b9bd1bcc0dbb85a2c', opacity: "0.833", x1: "680.4", y1: "900", x2: "247.4", y2: "650" }), h("line", { key: '246dcdd4a011775dc8fea8a05cfcfc429d5bd03c', opacity: "0.333", x1: "2152.6", y1: "1750", x2: "1719.6", y2: "1500" }), h("line", { key: 'f0a2d217a33a8caa6995a30d840002cb8c076e8d', opacity: "0.75", x1: "600", y1: "1200", x2: "100", y2: "1200" }), h("line", { key: '7552fb2c04f85c54b8bcc2235a32690e4489177f', opacity: "0.25", x1: "2300", y1: "1200", x2: "1800", y2: "1200" }), h("line", { key: '7b5a62164a0ef759b057f9100c7c5af834c9f6b7', opacity: "0.667", x1: "680.4", y1: "1500", x2: "247.4", y2: "1750" }), h("line", { key: 'd36af9d23059da4ca91147084e47ba94e19f5ada', opacity: "0.167", x1: "2152.6", y1: "650", x2: "1719.6", y2: "900" }), h("line", { key: 'f8eed2a97d817b68e0b29f5b5037d34d46a79a46', opacity: "0.583", x1: "900", y1: "1719.6", x2: "650", y2: "2152.6" }), h("line", { key: '1641d577b703e213157bc553966e434a40ba7158', opacity: "0.083", x1: "1750", y1: "247.4", x2: "1500", y2: "680.4" }), h("animateTransform", { key: '60fb2d30c248db1b4195d6015dc96225a15e4010', attributeName: "transform", attributeType: "XML", type: "rotate", keyTimes: "0;0.08333;0.16667;0.25;0.33333;0.41667;0.5;0.58333;0.66667;0.75;0.83333;0.91667", values: "0 1199 1199;30 1199 1199;60 1199 1199;90 1199 1199;120 1199 1199;150 1199 1199;180 1199 1199;210 1199 1199;240 1199 1199;270 1199 1199;300 1199 1199;330 1199 1199", dur: "0.83333s", begin: "0s", repeatCount: "indefinite", calcMode: "discrete" })))), h("div", { key: '27d3b71da43e5935dff3eff2e1bc507a24261b70', class: "modal-content", ref: el => (this.contentElement = el) }, h("slot", { key: '641408238d3969b830d1f3e9398be6f05bc1f65b' })), h("div", { key: 'c6178542b98a99d6a50b74270adc273671d5cc19', class: "focus-guard", tabindex: guardTabIndex, onFocus: this.handleEndGuardFocus })))));
|
|
204
206
|
}
|
|
205
207
|
static get is() { return "stripe-modal"; }
|
|
206
208
|
static get encapsulation() { return "shadow"; }
|
|
@@ -18,25 +18,22 @@ export const isSubmitButtonDisabled = (params) => {
|
|
|
18
18
|
* @param elements the Stripe Elements instance bound to the payment element
|
|
19
19
|
* @param intentType the configured intent type
|
|
20
20
|
* @param returnUrl the URL Stripe should redirect back to when required
|
|
21
|
+
* @param extraConfirmParams optional params merged into `confirmParams` (for example `payment_method_data.billing_details`)
|
|
21
22
|
* @returns the confirmation result
|
|
22
23
|
* @throws StripeAPIError when Stripe returns an error
|
|
23
24
|
*/
|
|
24
|
-
export const confirmPaymentOrSetup = async (stripe, elements, intentType, returnUrl) => {
|
|
25
|
+
export const confirmPaymentOrSetup = async (stripe, elements, intentType, returnUrl, extraConfirmParams) => {
|
|
25
26
|
const result = await (() => {
|
|
26
27
|
if (intentType === 'payment') {
|
|
27
28
|
return stripe.confirmPayment({
|
|
28
29
|
elements,
|
|
29
|
-
confirmParams: {
|
|
30
|
-
return_url: returnUrl,
|
|
31
|
-
},
|
|
30
|
+
confirmParams: Object.assign({ return_url: returnUrl }, extraConfirmParams),
|
|
32
31
|
redirect: 'if_required',
|
|
33
32
|
});
|
|
34
33
|
}
|
|
35
34
|
return stripe.confirmSetup({
|
|
36
35
|
elements,
|
|
37
|
-
confirmParams: {
|
|
38
|
-
return_url: returnUrl,
|
|
39
|
-
},
|
|
36
|
+
confirmParams: Object.assign({ return_url: returnUrl }, extraConfirmParams),
|
|
40
37
|
redirect: 'if_required',
|
|
41
38
|
});
|
|
42
39
|
})();
|
|
@@ -658,7 +658,7 @@ export class StripePaymentElement {
|
|
|
658
658
|
"references": {
|
|
659
659
|
"PaymentElementSubmitHandler": {
|
|
660
660
|
"location": "local",
|
|
661
|
-
"path": "/
|
|
661
|
+
"path": "/home/circleci/project/src/components/stripe-payment-element/stripe-payment-element.tsx",
|
|
662
662
|
"id": "src/components/stripe-payment-element/stripe-payment-element.tsx::PaymentElementSubmitHandler"
|
|
663
663
|
}
|
|
664
664
|
}
|
|
@@ -748,7 +748,7 @@ export class StripePaymentElement {
|
|
|
748
748
|
"references": {
|
|
749
749
|
"PaymentElementSubmitEvent": {
|
|
750
750
|
"location": "local",
|
|
751
|
-
"path": "/
|
|
751
|
+
"path": "/home/circleci/project/src/components/stripe-payment-element/stripe-payment-element.tsx",
|
|
752
752
|
"id": "src/components/stripe-payment-element/stripe-payment-element.tsx::PaymentElementSubmitEvent"
|
|
753
753
|
}
|
|
754
754
|
}
|
|
@@ -797,7 +797,7 @@ export class StripePaymentElement {
|
|
|
797
797
|
"references": {
|
|
798
798
|
"CheckoutSessionFormSubmitResult": {
|
|
799
799
|
"location": "local",
|
|
800
|
-
"path": "/
|
|
800
|
+
"path": "/home/circleci/project/src/components/stripe-payment-element/stripe-payment-element.tsx",
|
|
801
801
|
"id": "src/components/stripe-payment-element/stripe-payment-element.tsx::CheckoutSessionFormSubmitResult"
|
|
802
802
|
}
|
|
803
803
|
}
|
|
@@ -869,7 +869,7 @@ export class StripePaymentElement {
|
|
|
869
869
|
},
|
|
870
870
|
"InitCheckoutSessionOptions": {
|
|
871
871
|
"location": "local",
|
|
872
|
-
"path": "/
|
|
872
|
+
"path": "/home/circleci/project/src/components/stripe-payment-element/stripe-payment-element.tsx",
|
|
873
873
|
"id": "src/components/stripe-payment-element/stripe-payment-element.tsx::InitCheckoutSessionOptions"
|
|
874
874
|
}
|
|
875
875
|
},
|
|
@@ -24,6 +24,20 @@ export const intentKindFromClientSecret = (clientSecret) => {
|
|
|
24
24
|
const match = CLIENT_SECRET_PATTERNS.find(([pattern]) => pattern.test(clientSecret));
|
|
25
25
|
return match === null || match === void 0 ? void 0 : match[1];
|
|
26
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Tell what a client secret belongs to.
|
|
29
|
+
* A secret from `checkout-session-client-secret` is always a Checkout Session: Stripe does not document the format
|
|
30
|
+
* of a Checkout Session client secret, so it is never inspected. Intent secrets are told apart by their prefix.
|
|
31
|
+
* @param clientSecret - The client secret
|
|
32
|
+
* @param source - The attribute the secret was set through
|
|
33
|
+
* @returns The kind, or undefined when an intent secret has an unknown format
|
|
34
|
+
*/
|
|
35
|
+
export const intentKindOf = (clientSecret, source) => {
|
|
36
|
+
if (source === 'checkoutSession') {
|
|
37
|
+
return 'checkoutSession';
|
|
38
|
+
}
|
|
39
|
+
return intentKindFromClientSecret(clientSecret);
|
|
40
|
+
};
|
|
27
41
|
const unchanged = (state) => ({ state, effects: [] });
|
|
28
42
|
/**
|
|
29
43
|
* Resolve the pending `present()` promise, if any.
|
|
@@ -45,17 +59,17 @@ const cancel = (state) => {
|
|
|
45
59
|
effects: [...(visible ? [{ type: 'hide' }] : []), { type: 'emit', event: 'paymentSheetCanceled' }, ...resolved.effects],
|
|
46
60
|
};
|
|
47
61
|
};
|
|
48
|
-
const changeSecret = (state, clientSecret) => {
|
|
62
|
+
const changeSecret = (state, clientSecret, source) => {
|
|
49
63
|
if (clientSecret === '') {
|
|
50
64
|
return unchanged(state);
|
|
51
65
|
}
|
|
52
66
|
// Never drop the result of a running confirmation; apply the new secret once it finishes
|
|
53
67
|
if (state.phase === 'confirming') {
|
|
54
|
-
return unchanged(Object.assign(Object.assign({}, state), {
|
|
68
|
+
return unchanged(Object.assign(Object.assign({}, state), { deferredSecret: { clientSecret, source } }));
|
|
55
69
|
}
|
|
56
70
|
const canceled = state.phase === 'presented' ? cancel(state) : unchanged(state);
|
|
57
71
|
const generation = state.generation + 1;
|
|
58
|
-
const intentKind =
|
|
72
|
+
const intentKind = intentKindOf(clientSecret, source);
|
|
59
73
|
if (intentKind == null) {
|
|
60
74
|
const resolved = resolvePending(Object.assign(Object.assign({}, canceled.state), { phase: 'loadFailed', generation, intentKind: undefined, openRequested: false }), 'paymentSheetFailed');
|
|
61
75
|
return {
|
|
@@ -72,11 +86,11 @@ const changeSecret = (state, clientSecret) => {
|
|
|
72
86
|
* Apply a secret that arrived while confirming.
|
|
73
87
|
*/
|
|
74
88
|
const applyDeferredSecret = (result) => {
|
|
75
|
-
const {
|
|
76
|
-
if (
|
|
89
|
+
const { deferredSecret } = result.state;
|
|
90
|
+
if (deferredSecret == null) {
|
|
77
91
|
return result;
|
|
78
92
|
}
|
|
79
|
-
const next = changeSecret(Object.assign(Object.assign({}, result.state), {
|
|
93
|
+
const next = changeSecret(Object.assign(Object.assign({}, result.state), { deferredSecret: undefined }), deferredSecret.clientSecret, deferredSecret.source);
|
|
80
94
|
return { state: next.state, effects: [...result.effects, ...next.effects] };
|
|
81
95
|
};
|
|
82
96
|
const isCurrentConfirmation = (state, generation) => state.phase === 'confirming' && state.generation === generation;
|
|
@@ -88,7 +102,7 @@ const isCurrentConfirmation = (state, generation) => state.phase === 'confirming
|
|
|
88
102
|
export const transition = (state, event) => {
|
|
89
103
|
switch (event.type) {
|
|
90
104
|
case 'secretChanged':
|
|
91
|
-
return changeSecret(state, event.clientSecret);
|
|
105
|
+
return changeSecret(state, event.clientSecret, event.source);
|
|
92
106
|
case 'loadSucceeded': {
|
|
93
107
|
if (event.generation !== state.generation || state.phase !== 'loading') {
|
|
94
108
|
return unchanged(state);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { StripeAPIError } from "../../utils/error";
|
|
2
|
+
import { toAppearance, toPaymentElementOptions } from "./stripe-payment-sheet.helpers";
|
|
3
|
+
/**
|
|
4
|
+
* Build the Checkout Payment Element options from the payment sheet options.
|
|
5
|
+
* The Checkout Payment Element takes fewer options than the regular one: `business` and `defaultValues` are not
|
|
6
|
+
* available, so only the layout is passed.
|
|
7
|
+
* @param options - Payment sheet options
|
|
8
|
+
*/
|
|
9
|
+
export const toCheckoutPaymentElementOptions = (options) => {
|
|
10
|
+
const { layout } = toPaymentElementOptions(options);
|
|
11
|
+
return layout == null ? {} : { layout };
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* List the given options that a Checkout Session does not use, sorted by name.
|
|
15
|
+
* Name, phone and address come from the session or from other Checkout elements; the email is passed to `confirm`.
|
|
16
|
+
* @param options - Payment sheet options
|
|
17
|
+
*/
|
|
18
|
+
export const checkoutIgnoredOptionKeys = (options) => {
|
|
19
|
+
if (options == null) {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const { merchantDisplayName, defaultBillingDetails } = options;
|
|
23
|
+
const keys = ['name', 'phone', 'address'].filter(key => (defaultBillingDetails === null || defaultBillingDetails === void 0 ? void 0 : defaultBillingDetails[key]) != null).map(key => `defaultBillingDetails.${key}`);
|
|
24
|
+
if (merchantDisplayName != null) {
|
|
25
|
+
keys.push('merchantDisplayName');
|
|
26
|
+
}
|
|
27
|
+
// The extra billing elements and the wallet mapping are implemented for PaymentIntent / SetupIntent mode only
|
|
28
|
+
['billingDetailsCollectionConfiguration', 'shippingDetails', 'enableApplePay', 'enableGooglePay']
|
|
29
|
+
.filter(key => options[key] !== undefined)
|
|
30
|
+
.forEach(key => keys.push(key));
|
|
31
|
+
return keys.sort();
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Build the `elementsOptions` passed to `initCheckoutElementsSdk` from the payment sheet options.
|
|
35
|
+
* @param options - Payment sheet options
|
|
36
|
+
*/
|
|
37
|
+
export const toCheckoutElementsOptions = (options) => {
|
|
38
|
+
const appearance = toAppearance(options === null || options === void 0 ? void 0 : options.style);
|
|
39
|
+
return appearance == null ? undefined : { appearance };
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Throw when the session can no longer be paid. Stripe documents an `expired` session as "No further processing will occur",
|
|
43
|
+
* and a `complete` one is already paid or being paid, so confirming again cannot succeed.
|
|
44
|
+
* @param status - `session.status` from `getSession()`; undefined when it cannot be read
|
|
45
|
+
* @param message - Message of the confirm error, if any
|
|
46
|
+
* @throws StripeAPIError of type `invalid_request_error` (unrecoverable), with the code `checkout_session_expired` or `checkout_session_complete`
|
|
47
|
+
*/
|
|
48
|
+
const assertSessionOpen = (status, message) => {
|
|
49
|
+
if (status == null || status.type === 'open') {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
throw new StripeAPIError({
|
|
53
|
+
type: 'invalid_request_error',
|
|
54
|
+
code: `checkout_session_${status.type}`,
|
|
55
|
+
message: message !== null && message !== void 0 ? message : `The Checkout Session is ${status.type} and can no longer be paid.`,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Confirm a Checkout Session from the payment sheet.
|
|
60
|
+
*
|
|
61
|
+
* Unlike the default of `confirm`, which always redirects to `returnUrl`, this passes `redirect: 'if_required'`
|
|
62
|
+
* so that payment methods without a redirect finish in the page and can be reported as `paymentSheetCompleted`.
|
|
63
|
+
*
|
|
64
|
+
* Errors are thrown as `StripeAPIError` with a `type` that `isRecoverableConfirmError` understands.
|
|
65
|
+
* A confirm error only carries `code: 'paymentFailed'` or `code: null` (`StripeCheckoutConfirmResult` in @stripe/stripe-js),
|
|
66
|
+
* so whether the session can still be paid is read from `session.status`:
|
|
67
|
+
* - session `expired` or `complete` (before confirming or after a failed confirm): `invalid_request_error`, which ends the sheet
|
|
68
|
+
* - `paymentFailed` on an open session (a decline): `card_error`, which keeps the sheet open
|
|
69
|
+
* - any other confirm error on an open session, or a failure to load the actions: `api_error`, which keeps the sheet open
|
|
70
|
+
* @param checkout - The Checkout instance from `initCheckoutElementsSdk`
|
|
71
|
+
* @param returnUrl - Where Stripe redirects after a redirect-based payment method
|
|
72
|
+
* @param email - Customer email; needed when the session was created without `customer_email` or `customer`
|
|
73
|
+
* @returns The confirmation result
|
|
74
|
+
* @throws StripeAPIError when the actions cannot be loaded, the session can no longer be paid, or the confirmation fails
|
|
75
|
+
*/
|
|
76
|
+
export const confirmCheckoutSessionSheet = async (checkout, returnUrl, email) => {
|
|
77
|
+
const loadActionsResult = await checkout.loadActions();
|
|
78
|
+
if (loadActionsResult.type === 'error') {
|
|
79
|
+
throw new StripeAPIError({ type: 'api_error', code: 'checkout_session_error', message: loadActionsResult.error.message });
|
|
80
|
+
}
|
|
81
|
+
const { actions } = loadActionsResult;
|
|
82
|
+
const sessionStatus = () => { var _a, _b; return (_b = (_a = actions.getSession) === null || _a === void 0 ? void 0 : _a.call(actions)) === null || _b === void 0 ? void 0 : _b.status; };
|
|
83
|
+
assertSessionOpen(sessionStatus());
|
|
84
|
+
const result = await actions.confirm(Object.assign({ returnUrl, redirect: 'if_required' }, (email != null && email !== '' ? { email } : {})));
|
|
85
|
+
if (result.type === 'error') {
|
|
86
|
+
const { code, message } = result.error;
|
|
87
|
+
assertSessionOpen(sessionStatus(), message);
|
|
88
|
+
if (code === 'paymentFailed') {
|
|
89
|
+
throw new StripeAPIError({ type: 'card_error', code, message });
|
|
90
|
+
}
|
|
91
|
+
throw new StripeAPIError({ type: 'api_error', code: 'checkout_session_error', message });
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
};
|
|
@@ -10,7 +10,17 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
}
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
|
-
const SUPPORTED_OPTION_KEYS = [
|
|
13
|
+
const SUPPORTED_OPTION_KEYS = [
|
|
14
|
+
'merchantDisplayName',
|
|
15
|
+
'paymentMethodLayout',
|
|
16
|
+
'defaultBillingDetails',
|
|
17
|
+
'returnURL',
|
|
18
|
+
'style',
|
|
19
|
+
'billingDetailsCollectionConfiguration',
|
|
20
|
+
'shippingDetails',
|
|
21
|
+
'enableApplePay',
|
|
22
|
+
'enableGooglePay',
|
|
23
|
+
];
|
|
14
24
|
const LAYOUTS = {
|
|
15
25
|
horizontal: 'tabs',
|
|
16
26
|
vertical: 'accordion',
|
|
@@ -24,6 +34,34 @@ const THEMES = {
|
|
|
24
34
|
* Drop keys whose value is undefined.
|
|
25
35
|
*/
|
|
26
36
|
const compact = (value) => Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
|
|
37
|
+
/**
|
|
38
|
+
* Convert a Capacitor address to the Stripe shape (`postalCode` → `postal_code`), dropping keys that are not given.
|
|
39
|
+
*/
|
|
40
|
+
const toStripeAddress = (address) => {
|
|
41
|
+
const { postalCode } = address, rest = __rest(address, ["postalCode"]);
|
|
42
|
+
return compact(Object.assign(Object.assign({}, rest), { postal_code: postalCode }));
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Payment Element billing fields to turn off: the `never` ones (passed from `defaultBillingDetails` at confirmation)
|
|
46
|
+
* and the ones collected by a separate element (email element, billing Address Element).
|
|
47
|
+
*/
|
|
48
|
+
const toBillingDetailsFields = (config) => {
|
|
49
|
+
if (config == null) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const { email, name, phone, address } = config;
|
|
53
|
+
const fields = compact({
|
|
54
|
+
email: email === 'never' || email === 'always' ? 'never' : undefined,
|
|
55
|
+
name: name === 'never' ? 'never' : undefined,
|
|
56
|
+
phone: phone === 'never' || (phone === 'always' && address === 'full') ? 'never' : undefined,
|
|
57
|
+
address: address === 'never' || address === 'full' ? 'never' : undefined,
|
|
58
|
+
});
|
|
59
|
+
return Object.keys(fields).length === 0 ? undefined : fields;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* `true` → `auto` (shown when available), `false` → `never`, undefined → left to Stripe
|
|
63
|
+
*/
|
|
64
|
+
const toWalletOption = (enabled) => (enabled == null ? undefined : enabled ? 'auto' : 'never');
|
|
27
65
|
/**
|
|
28
66
|
* Build the Payment Element options from the payment sheet options.
|
|
29
67
|
* @param options - Payment sheet options
|
|
@@ -32,8 +70,16 @@ export const toPaymentElementOptions = (options) => {
|
|
|
32
70
|
if (options == null) {
|
|
33
71
|
return {};
|
|
34
72
|
}
|
|
35
|
-
const { merchantDisplayName, paymentMethodLayout, defaultBillingDetails } = options;
|
|
73
|
+
const { merchantDisplayName, paymentMethodLayout, defaultBillingDetails, billingDetailsCollectionConfiguration, enableApplePay, enableGooglePay } = options;
|
|
36
74
|
const elementOptions = {};
|
|
75
|
+
const billingDetailsFields = toBillingDetailsFields(billingDetailsCollectionConfiguration);
|
|
76
|
+
const wallets = compact({ applePay: toWalletOption(enableApplePay), googlePay: toWalletOption(enableGooglePay) });
|
|
77
|
+
if (billingDetailsFields != null) {
|
|
78
|
+
elementOptions.fields = { billingDetails: billingDetailsFields };
|
|
79
|
+
}
|
|
80
|
+
if (Object.keys(wallets).length > 0) {
|
|
81
|
+
elementOptions.wallets = wallets;
|
|
82
|
+
}
|
|
37
83
|
if (merchantDisplayName != null) {
|
|
38
84
|
elementOptions.business = { name: merchantDisplayName };
|
|
39
85
|
}
|
|
@@ -44,8 +90,7 @@ export const toPaymentElementOptions = (options) => {
|
|
|
44
90
|
const { address } = defaultBillingDetails, contact = __rest(defaultBillingDetails, ["address"]);
|
|
45
91
|
const billingDetails = compact(contact);
|
|
46
92
|
if (address != null) {
|
|
47
|
-
|
|
48
|
-
billingDetails.address = compact(Object.assign(Object.assign({}, rest), { postal_code: postalCode }));
|
|
93
|
+
billingDetails.address = toStripeAddress(address);
|
|
49
94
|
}
|
|
50
95
|
elementOptions.defaultValues = { billingDetails };
|
|
51
96
|
}
|
|
@@ -62,17 +107,156 @@ export const toAppearance = (style) => {
|
|
|
62
107
|
return { theme: THEMES[style] };
|
|
63
108
|
};
|
|
64
109
|
/**
|
|
65
|
-
* List the given options that have no effect on the web (yet), sorted by name.
|
|
110
|
+
* List the given options (or option values, as `option.key`) that have no effect on the web (yet), sorted by name.
|
|
66
111
|
* @param options - Payment sheet options
|
|
112
|
+
* @param intentKind - Kind of the intent, when known
|
|
67
113
|
*/
|
|
68
|
-
export const unsupportedOptionKeys = (options) => {
|
|
114
|
+
export const unsupportedOptionKeys = (options, intentKind) => {
|
|
69
115
|
if (options == null) {
|
|
70
116
|
return [];
|
|
71
117
|
}
|
|
72
|
-
|
|
118
|
+
const keys = Object.entries(options)
|
|
73
119
|
.filter(([key, value]) => value !== undefined && !SUPPORTED_OPTION_KEYS.includes(key))
|
|
74
|
-
.map(([key]) => key)
|
|
75
|
-
|
|
120
|
+
.map(([key]) => key);
|
|
121
|
+
const config = options.billingDetailsCollectionConfiguration;
|
|
122
|
+
// `fields.billingDetails.name: 'always'` of the Payment Element requires Stripe beta access, so it is not sent
|
|
123
|
+
if ((config === null || config === void 0 ? void 0 : config.name) === 'always') {
|
|
124
|
+
keys.push('billingDetailsCollectionConfiguration.name');
|
|
125
|
+
}
|
|
126
|
+
// The phone can only be required through the billing Address Element
|
|
127
|
+
if ((config === null || config === void 0 ? void 0 : config.phone) === 'always' && config.address !== 'full') {
|
|
128
|
+
keys.push('billingDetailsCollectionConfiguration.phone');
|
|
129
|
+
}
|
|
130
|
+
// A SetupIntent has no shipping details
|
|
131
|
+
if (options.shippingDetails != null && intentKind === 'setup') {
|
|
132
|
+
keys.push('shippingDetails');
|
|
133
|
+
}
|
|
134
|
+
return keys.sort();
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Prefill values of an Address Element. The address is left out without a country, which the Address Element requires.
|
|
138
|
+
*/
|
|
139
|
+
const toAddressDefaultValues = (details) => {
|
|
140
|
+
const { name, phone, address } = details;
|
|
141
|
+
const country = address === null || address === void 0 ? void 0 : address.country;
|
|
142
|
+
return compact({ name, phone, address: country == null || country === '' ? undefined : Object.assign(Object.assign({}, toStripeAddress(address)), { country }) });
|
|
143
|
+
};
|
|
144
|
+
const isBlank = (value) => value == null || value === '';
|
|
145
|
+
/**
|
|
146
|
+
* Loose email check, used only when the email element has not told whether its value is complete (a prefilled value nobody edited)
|
|
147
|
+
*/
|
|
148
|
+
const looksLikeEmail = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
|
149
|
+
/**
|
|
150
|
+
* Error for a `never` field without a default value. Thrown while loading so that Stripe never rejects the confirmation.
|
|
151
|
+
*/
|
|
152
|
+
const missingDefaultError = (field, defaultKey) => new Error(`<stripe-payment-sheet>: billingDetailsCollectionConfiguration.${field} is 'never', so defaultBillingDetails.${defaultKey} is required`);
|
|
153
|
+
/**
|
|
154
|
+
* Error the customer can fix in the open sheet (handled like a Stripe `validation_error`).
|
|
155
|
+
* Its message is an English i18n key, translated when it is shown (see `isSheetValidationError`).
|
|
156
|
+
*/
|
|
157
|
+
const validationError = (message) => Object.assign(new Error(message), { type: 'validation_error', sheetValidation: true });
|
|
158
|
+
/**
|
|
159
|
+
* Tell whether an error was raised by this sheet's own validation (its message is an i18n key), rather than by Stripe (already localized).
|
|
160
|
+
* @param error - Error thrown while confirming
|
|
161
|
+
*/
|
|
162
|
+
export const isSheetValidationError = (error) => (error === null || error === void 0 ? void 0 : error.sheetValidation) === true;
|
|
163
|
+
/**
|
|
164
|
+
* Decide which extra elements to show and which billing details to pass at confirmation (PaymentIntent / SetupIntent mode).
|
|
165
|
+
* @param options - Payment sheet options
|
|
166
|
+
* @param intentKind - Kind of the intent being confirmed
|
|
167
|
+
* @throws Error when a `never` field has no value in `defaultBillingDetails`
|
|
168
|
+
*/
|
|
169
|
+
export const planBillingDetailsCollection = (options, intentKind) => {
|
|
170
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
171
|
+
const config = (_a = options === null || options === void 0 ? void 0 : options.billingDetailsCollectionConfiguration) !== null && _a !== void 0 ? _a : {};
|
|
172
|
+
const defaults = (_b = options === null || options === void 0 ? void 0 : options.defaultBillingDetails) !== null && _b !== void 0 ? _b : {};
|
|
173
|
+
const fixedBillingDetails = {};
|
|
174
|
+
['email', 'name', 'phone'].forEach(field => {
|
|
175
|
+
if (config[field] !== 'never') {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (isBlank(defaults[field])) {
|
|
179
|
+
throw missingDefaultError(field, field);
|
|
180
|
+
}
|
|
181
|
+
fixedBillingDetails[field] = defaults[field];
|
|
182
|
+
});
|
|
183
|
+
if (config.address === 'never') {
|
|
184
|
+
const address = defaults.address;
|
|
185
|
+
if (address == null || isBlank(address.country)) {
|
|
186
|
+
throw missingDefaultError('address', 'address.country');
|
|
187
|
+
}
|
|
188
|
+
// Every key is passed: the Payment Element does not collect any part of the address
|
|
189
|
+
fixedBillingDetails.address = {
|
|
190
|
+
line1: (_c = address.line1) !== null && _c !== void 0 ? _c : '',
|
|
191
|
+
line2: (_d = address.line2) !== null && _d !== void 0 ? _d : '',
|
|
192
|
+
city: (_e = address.city) !== null && _e !== void 0 ? _e : '',
|
|
193
|
+
state: (_f = address.state) !== null && _f !== void 0 ? _f : '',
|
|
194
|
+
postal_code: (_g = address.postalCode) !== null && _g !== void 0 ? _g : '',
|
|
195
|
+
country: address.country,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const collectPhoneFromAddress = config.address === 'full' && config.phone === 'always';
|
|
199
|
+
const plan = { fixedBillingDetails, collectPhoneFromAddress };
|
|
200
|
+
if (config.address === 'full') {
|
|
201
|
+
const billingAddressElement = { mode: 'billing', defaultValues: toAddressDefaultValues(defaults) };
|
|
202
|
+
if (collectPhoneFromAddress) {
|
|
203
|
+
billingAddressElement.fields = { phone: 'always' };
|
|
204
|
+
billingAddressElement.validation = { phone: { required: 'always' } };
|
|
205
|
+
}
|
|
206
|
+
else if (config.phone === 'never') {
|
|
207
|
+
billingAddressElement.fields = { phone: 'never' };
|
|
208
|
+
}
|
|
209
|
+
plan.billingAddressElement = billingAddressElement;
|
|
210
|
+
}
|
|
211
|
+
if (config.email === 'always') {
|
|
212
|
+
plan.emailElement = isBlank(defaults.email) ? {} : { defaultValues: { email: defaults.email } };
|
|
213
|
+
}
|
|
214
|
+
const shippingDetails = options === null || options === void 0 ? void 0 : options.shippingDetails;
|
|
215
|
+
if (shippingDetails != null && intentKind === 'payment') {
|
|
216
|
+
const shippingAddressElement = { mode: 'shipping', defaultValues: toAddressDefaultValues(shippingDetails) };
|
|
217
|
+
// The Address Element hides the phone field by default; show it so that the given phone is kept and editable
|
|
218
|
+
if (!isBlank(shippingDetails.phone)) {
|
|
219
|
+
shippingAddressElement.fields = { phone: 'always' };
|
|
220
|
+
}
|
|
221
|
+
plan.shippingAddressElement = shippingAddressElement;
|
|
222
|
+
}
|
|
223
|
+
return plan;
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* Build `payment_method_data.billing_details` for the confirmation.
|
|
227
|
+
* Values fixed by a `never` field win over the values typed into the extra elements.
|
|
228
|
+
* @param plan - Result of `planBillingDetailsCollection`
|
|
229
|
+
* @param collected - Values read from the extra elements
|
|
230
|
+
* @returns The billing details, or undefined when nothing has to be passed
|
|
231
|
+
* @throws Error with `type: 'validation_error'` when an extra element is not complete
|
|
232
|
+
*/
|
|
233
|
+
export const collectedBillingDetails = (plan, collected) => {
|
|
234
|
+
var _a, _b;
|
|
235
|
+
const details = {};
|
|
236
|
+
if (plan.billingAddressElement != null) {
|
|
237
|
+
const billingAddress = collected.billingAddress;
|
|
238
|
+
if (billingAddress == null || !billingAddress.complete) {
|
|
239
|
+
throw validationError('Please complete your billing address.');
|
|
240
|
+
}
|
|
241
|
+
details.name = billingAddress.value.name;
|
|
242
|
+
details.address = billingAddress.value.address;
|
|
243
|
+
if (plan.collectPhoneFromAddress) {
|
|
244
|
+
details.phone = billingAddress.value.phone;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (plan.emailElement != null) {
|
|
248
|
+
// Before any change event the element shows its prefilled value
|
|
249
|
+
const email = (_a = collected.email) !== null && _a !== void 0 ? _a : (_b = plan.emailElement.defaultValues) === null || _b === void 0 ? void 0 : _b.email;
|
|
250
|
+
if (isBlank(email)) {
|
|
251
|
+
throw validationError('Please enter your email address.');
|
|
252
|
+
}
|
|
253
|
+
if (collected.emailComplete === false || (collected.emailComplete == null && !looksLikeEmail(email))) {
|
|
254
|
+
throw validationError('Please enter a valid email address.');
|
|
255
|
+
}
|
|
256
|
+
details.email = email;
|
|
257
|
+
}
|
|
258
|
+
Object.assign(details, plan.fixedBillingDetails);
|
|
259
|
+
return Object.keys(details).length === 0 ? undefined : details;
|
|
76
260
|
};
|
|
77
261
|
/**
|
|
78
262
|
* Stripe error types after which the customer can fix the input or simply try again.
|