stripe-pwa-elements 4.0.1 → 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-Cm6zUD7R.js +1206 -0
- package/dist/cjs/{factory-Cd2VI_N2.js → i18n-lBbOeQt4.js} +12 -1149
- package/dist/cjs/ion-icon_2.cjs.entry.js +725 -0
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/stripe-address-element.cjs.entry.js +5 -4
- package/dist/cjs/stripe-card-element-modal.cjs.entry.js +3 -2
- package/dist/cjs/{stripe-card-element_2.cjs.entry.js → stripe-card-element.cjs.entry.js} +8 -93
- package/dist/cjs/stripe-currency-selector.cjs.entry.js +4 -3
- package/dist/cjs/stripe-elements.cjs.js +1 -1
- package/dist/cjs/stripe-express-checkout-element.cjs.entry.js +24 -5
- package/dist/cjs/stripe-link-authentication-element.cjs.entry.js +4 -3
- package/dist/cjs/stripe-payment-element.cjs.entry.js +12 -124
- package/dist/cjs/stripe-payment-element.helpers-CvAhIqLt.js +119 -0
- package/dist/cjs/stripe-payment-request-button.cjs.entry.js +2 -2
- package/dist/cjs/stripe-payment-sheet.cjs.entry.js +987 -0
- package/dist/collection/collection-manifest.json +2 -1
- package/dist/collection/components/stripe-address-element/stripe-address-element.js +2 -2
- package/dist/collection/components/stripe-card-element/stripe-card-element.js +3 -2
- package/dist/collection/components/stripe-card-element-modal/stripe-card-element-modal.js +4 -3
- package/dist/collection/components/stripe-express-checkout-element/stripe-express-checkout-element.js +18 -0
- package/dist/collection/components/stripe-modal/stripe-modal.css +27 -1
- package/dist/collection/components/stripe-modal/stripe-modal.js +132 -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-request-button/stripe-payment-request-button.js +3 -3
- package/dist/collection/components/stripe-payment-sheet/payment-sheet-state.js +193 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.checkout.js +94 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.css +68 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.helpers.js +274 -0
- package/dist/collection/components/stripe-payment-sheet/stripe-payment-sheet.js +763 -0
- package/dist/collection/services/express-checkout-element-manager.js +36 -2
- package/dist/collection/services/link-authentication-element-manager.js +5 -0
- package/dist/collection/services/stripe-service.js +20 -6
- package/dist/collection/utils/focus.js +76 -0
- package/dist/collection/utils/i18n.js +12 -0
- package/dist/components/factory.js +1 -1
- package/dist/components/i18n.js +1 -0
- package/dist/components/stripe-address-element.js +1 -1
- package/dist/components/stripe-card-element-modal.js +1 -1
- package/dist/components/stripe-card-element2.js +1 -1
- package/dist/components/stripe-currency-selector.js +1 -1
- package/dist/components/stripe-express-checkout-element.js +1 -1
- package/dist/components/stripe-link-authentication-element.js +1 -1
- package/dist/components/stripe-modal2.js +1 -1
- package/dist/components/stripe-payment-element.helpers.js +1 -0
- package/dist/components/stripe-payment-element.js +1 -1
- package/dist/components/stripe-payment-request-button2.js +1 -1
- package/dist/components/stripe-payment-sheet.d.ts +11 -0
- package/dist/components/stripe-payment-sheet.js +1 -0
- package/dist/esm/factory-BaGv8K_i.js +1203 -0
- package/dist/esm/{factory-VTa1Gc94.js → i18n-CL5n3bOb.js} +13 -1149
- package/dist/esm/ion-icon_2.entry.js +722 -0
- package/dist/esm/loader.js +1 -1
- package/dist/esm/stripe-address-element.entry.js +2 -1
- package/dist/esm/stripe-card-element-modal.entry.js +3 -2
- package/dist/esm/{stripe-card-element_2.entry.js → stripe-card-element.entry.js} +6 -90
- package/dist/esm/stripe-currency-selector.entry.js +2 -1
- package/dist/esm/stripe-elements.js +1 -1
- package/dist/esm/stripe-express-checkout-element.entry.js +20 -1
- package/dist/esm/stripe-link-authentication-element.entry.js +2 -1
- package/dist/esm/stripe-payment-element.entry.js +4 -116
- package/dist/esm/stripe-payment-element.helpers-Bu9RfxS4.js +114 -0
- package/dist/esm/stripe-payment-request-button.entry.js +2 -2
- package/dist/esm/stripe-payment-sheet.entry.js +985 -0
- package/dist/stripe-elements/p-2e41699e.entry.js +1 -0
- package/dist/stripe-elements/p-3fc4583a.entry.js +1 -0
- package/dist/stripe-elements/{p-ae0c336e.entry.js → p-4891edf0.entry.js} +1 -1
- package/dist/stripe-elements/p-7e826964.entry.js +1 -0
- package/dist/stripe-elements/p-BhKXGROE.js +1 -0
- package/dist/stripe-elements/p-CL5n3bOb.js +1 -0
- package/dist/stripe-elements/p-DlMEZwSe.js +1 -0
- package/dist/stripe-elements/p-a395a5d1.entry.js +1 -0
- package/dist/stripe-elements/p-b62c9dae.entry.js +1 -0
- package/dist/stripe-elements/p-b7c20e8e.entry.js +1 -0
- package/dist/stripe-elements/p-ba791c8f.entry.js +1 -0
- package/dist/stripe-elements/p-bec62526.entry.js +1 -0
- package/dist/stripe-elements/p-f5552c81.entry.js +1 -0
- package/dist/stripe-elements/stripe-elements.esm.js +1 -1
- package/dist/types/components/stripe-card-element/stripe-card-element.d.ts +2 -1
- package/dist/types/components/stripe-card-element-modal/stripe-card-element-modal.d.ts +2 -1
- package/dist/types/components/stripe-express-checkout-element/stripe-express-checkout-element.d.ts +5 -0
- package/dist/types/components/stripe-modal/stripe-modal.d.ts +43 -0
- package/dist/types/components/stripe-payment-element/stripe-payment-element.helpers.d.ts +7 -2
- package/dist/types/components/stripe-payment-request-button/stripe-payment-request-button.d.ts +1 -1
- package/dist/types/components/stripe-payment-sheet/payment-sheet-state.d.ts +121 -0
- 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 +157 -0
- package/dist/types/components/stripe-payment-sheet/stripe-payment-sheet.helpers.d.ts +153 -0
- package/dist/types/components.d.ts +182 -10
- package/dist/types/index.d.ts +2 -0
- package/dist/types/services/interfaces.d.ts +3 -0
- package/dist/types/services/stripe-service.d.ts +7 -2
- package/dist/types/utils/focus.d.ts +15 -0
- package/package.json +7 -3
- package/readme.md +59 -6
- package/dist/cjs/ion-icon.cjs.entry.js +0 -431
- package/dist/components/utils.js +0 -1
- package/dist/esm/ion-icon.entry.js +0 -429
- package/dist/stripe-elements/p-1bd46766.entry.js +0 -1
- package/dist/stripe-elements/p-1c6e0a45.entry.js +0 -1
- package/dist/stripe-elements/p-3f42ca0c.entry.js +0 -1
- package/dist/stripe-elements/p-5cf62945.entry.js +0 -1
- package/dist/stripe-elements/p-74de1556.entry.js +0 -1
- package/dist/stripe-elements/p-76e57d78.entry.js +0 -1
- package/dist/stripe-elements/p-840d499f.entry.js +0 -1
- package/dist/stripe-elements/p-CrjrbGJ3.js +0 -1
- package/dist/stripe-elements/p-cfd8e081.entry.js +0 -1
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--color-label: #565656;
|
|
3
|
+
--color-placeholder: #acacac;
|
|
4
|
+
--color-heading-text: #333;
|
|
5
|
+
--color-text-error: #ef4444;
|
|
6
|
+
--color-input-border: #e4e4e6;
|
|
7
|
+
--color-input-border-shadow: rgba(228, 228, 230, 0.6);
|
|
8
|
+
--color-button-text: rgba(255, 255, 255, 0.95);
|
|
9
|
+
--color-button-text-disabled: rgba(255, 255, 255, 0.4);
|
|
10
|
+
--color-primary: #0074d4;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/* Remove all the styles of the "User-Agent-Stylesheet", except for the 'display' property */
|
|
14
|
+
stripe-payment-sheet {
|
|
15
|
+
display: contents;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.stripe-payment-sheet-wrap {
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
}
|
|
21
|
+
.stripe-payment-sheet-wrap *:where(:not(iframe, canvas, img, svg, video):not(svg *)) {
|
|
22
|
+
all: unset;
|
|
23
|
+
display: revert;
|
|
24
|
+
background: none;
|
|
25
|
+
border: none;
|
|
26
|
+
font-family: "Roboto", sans-serif;
|
|
27
|
+
box-sizing: border-box;
|
|
28
|
+
}
|
|
29
|
+
.stripe-payment-sheet-wrap button {
|
|
30
|
+
cursor: pointer;
|
|
31
|
+
}
|
|
32
|
+
.stripe-payment-sheet-wrap {
|
|
33
|
+
display: block;
|
|
34
|
+
width: 100%;
|
|
35
|
+
background: #fff;
|
|
36
|
+
padding: 32px 16px;
|
|
37
|
+
}
|
|
38
|
+
.stripe-payment-sheet-wrap .stripe-heading {
|
|
39
|
+
font-size: 24px;
|
|
40
|
+
font-weight: bold;
|
|
41
|
+
color: var(--color-heading-text);
|
|
42
|
+
margin-bottom: 24px;
|
|
43
|
+
}
|
|
44
|
+
.stripe-payment-sheet-wrap .payment-info {
|
|
45
|
+
margin-top: 8px;
|
|
46
|
+
}
|
|
47
|
+
.stripe-payment-sheet-wrap .stripe-element-errors {
|
|
48
|
+
margin-top: 8px;
|
|
49
|
+
font-size: 14px;
|
|
50
|
+
color: var(--color-text-error);
|
|
51
|
+
}
|
|
52
|
+
.stripe-payment-sheet-wrap .stripe-submit {
|
|
53
|
+
margin-top: 32px;
|
|
54
|
+
}
|
|
55
|
+
.stripe-payment-sheet-wrap button {
|
|
56
|
+
display: block;
|
|
57
|
+
color: var(--color-button-text);
|
|
58
|
+
background-color: var(--color-primary);
|
|
59
|
+
width: 100%;
|
|
60
|
+
text-align: center;
|
|
61
|
+
padding: 16px 16px;
|
|
62
|
+
font-size: 18px;
|
|
63
|
+
border-radius: 6px;
|
|
64
|
+
min-height: 42px;
|
|
65
|
+
}
|
|
66
|
+
.stripe-payment-sheet-wrap button:disabled {
|
|
67
|
+
color: var(--color-button-text-disabled);
|
|
68
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s)
|
|
4
|
+
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
const SUPPORTED_OPTION_KEYS = [
|
|
14
|
+
'merchantDisplayName',
|
|
15
|
+
'paymentMethodLayout',
|
|
16
|
+
'defaultBillingDetails',
|
|
17
|
+
'returnURL',
|
|
18
|
+
'style',
|
|
19
|
+
'billingDetailsCollectionConfiguration',
|
|
20
|
+
'shippingDetails',
|
|
21
|
+
'enableApplePay',
|
|
22
|
+
'enableGooglePay',
|
|
23
|
+
];
|
|
24
|
+
const LAYOUTS = {
|
|
25
|
+
horizontal: 'tabs',
|
|
26
|
+
vertical: 'accordion',
|
|
27
|
+
automatic: 'auto',
|
|
28
|
+
};
|
|
29
|
+
const THEMES = {
|
|
30
|
+
alwaysLight: 'stripe',
|
|
31
|
+
alwaysDark: 'night',
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Drop keys whose value is undefined.
|
|
35
|
+
*/
|
|
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');
|
|
65
|
+
/**
|
|
66
|
+
* Build the Payment Element options from the payment sheet options.
|
|
67
|
+
* @param options - Payment sheet options
|
|
68
|
+
*/
|
|
69
|
+
export const toPaymentElementOptions = (options) => {
|
|
70
|
+
if (options == null) {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
const { merchantDisplayName, paymentMethodLayout, defaultBillingDetails, billingDetailsCollectionConfiguration, enableApplePay, enableGooglePay } = options;
|
|
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
|
+
}
|
|
83
|
+
if (merchantDisplayName != null) {
|
|
84
|
+
elementOptions.business = { name: merchantDisplayName };
|
|
85
|
+
}
|
|
86
|
+
if (paymentMethodLayout != null) {
|
|
87
|
+
elementOptions.layout = LAYOUTS[paymentMethodLayout];
|
|
88
|
+
}
|
|
89
|
+
if (defaultBillingDetails != null) {
|
|
90
|
+
const { address } = defaultBillingDetails, contact = __rest(defaultBillingDetails, ["address"]);
|
|
91
|
+
const billingDetails = compact(contact);
|
|
92
|
+
if (address != null) {
|
|
93
|
+
billingDetails.address = toStripeAddress(address);
|
|
94
|
+
}
|
|
95
|
+
elementOptions.defaultValues = { billingDetails };
|
|
96
|
+
}
|
|
97
|
+
return elementOptions;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Build the Elements appearance from the payment sheet style.
|
|
101
|
+
* @param style - `alwaysLight` or `alwaysDark`
|
|
102
|
+
*/
|
|
103
|
+
export const toAppearance = (style) => {
|
|
104
|
+
if (style == null) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
return { theme: THEMES[style] };
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* List the given options (or option values, as `option.key`) that have no effect on the web (yet), sorted by name.
|
|
111
|
+
* @param options - Payment sheet options
|
|
112
|
+
* @param intentKind - Kind of the intent, when known
|
|
113
|
+
*/
|
|
114
|
+
export const unsupportedOptionKeys = (options, intentKind) => {
|
|
115
|
+
if (options == null) {
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
const keys = Object.entries(options)
|
|
119
|
+
.filter(([key, value]) => value !== undefined && !SUPPORTED_OPTION_KEYS.includes(key))
|
|
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;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Stripe error types after which the customer can fix the input or simply try again.
|
|
263
|
+
* Types are listed at https://docs.stripe.com/api/errors and in `StripeErrorType` of @stripe/stripe-js.
|
|
264
|
+
*/
|
|
265
|
+
const RECOVERABLE_ERROR_TYPES = ['card_error', 'validation_error', 'api_connection_error', 'rate_limit_error', 'api_error'];
|
|
266
|
+
/**
|
|
267
|
+
* Tell whether a confirmation error lets the customer try again in the open sheet.
|
|
268
|
+
* Other errors (for example `invalid_request_error` for an intent that already succeeded) end the sheet with `paymentSheetFailed`.
|
|
269
|
+
* @param error - Error thrown while confirming
|
|
270
|
+
*/
|
|
271
|
+
export const isRecoverableConfirmError = (error) => {
|
|
272
|
+
const type = error === null || error === void 0 ? void 0 : error.type;
|
|
273
|
+
return typeof type === 'string' && RECOVERABLE_ERROR_TYPES.includes(type);
|
|
274
|
+
};
|