@funnelfox/billing 0.13.2 → 0.14.0
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/README.md +58 -0
- package/dist/chunk-index.cjs.js +785 -14
- package/dist/chunk-index.es.js +785 -15
- package/dist/funnelfox-billing.cjs.js +1 -0
- package/dist/funnelfox-billing.esm.js +1 -1
- package/dist/funnelfox-billing.js +785 -14
- package/dist/funnelfox-billing.min.js +1 -1
- package/package.json +2 -2
- package/src/types.d.ts +86 -2
|
@@ -565,7 +565,7 @@
|
|
|
565
565
|
/**
|
|
566
566
|
* @fileoverview Constants for Funnefox SDK
|
|
567
567
|
*/
|
|
568
|
-
const SDK_VERSION = '0.
|
|
568
|
+
const SDK_VERSION = '0.14.0';
|
|
569
569
|
const DEFAULTS = {
|
|
570
570
|
BASE_URL: 'https://billing.funnelfox.com',
|
|
571
571
|
REGION: 'default',
|
|
@@ -607,6 +607,7 @@
|
|
|
607
607
|
CREATE_PAYMENT: '/v1/checkout/create_payment',
|
|
608
608
|
RESUME_PAYMENT: '/v1/checkout/resume_payment',
|
|
609
609
|
ONE_CLICK: '/v1/checkout/one_click',
|
|
610
|
+
ONE_CLICK_WALLET_SESSION: '/v1/checkout/one_click_wallet_session',
|
|
610
611
|
CREATE_SIMPLE_CLIENT_SESSION: '/v1/checkout/create_simple_client_session',
|
|
611
612
|
RECALCULATE_TAX: '/v1/checkout/recalculate_tax',
|
|
612
613
|
};
|
|
@@ -810,7 +811,7 @@
|
|
|
810
811
|
'height:100%',
|
|
811
812
|
'--apple-pay-button-width:100%',
|
|
812
813
|
'--apple-pay-button-height:100%',
|
|
813
|
-
|
|
814
|
+
`--apple-pay-button-border-radius:${options.borderRadius ?? 28}px`,
|
|
814
815
|
'display:block',
|
|
815
816
|
'cursor:pointer',
|
|
816
817
|
'z-index:1',
|
|
@@ -1614,6 +1615,13 @@
|
|
|
1614
1615
|
body: JSON.stringify(payload),
|
|
1615
1616
|
}, timeoutMs));
|
|
1616
1617
|
}
|
|
1618
|
+
/** `one_click_wallet_session`: prepares the wallet re-auth button of a one-click upsell. */
|
|
1619
|
+
async oneClickWalletSession(payload, timeoutMs) {
|
|
1620
|
+
return (await this.requestOnce(API_ENDPOINTS.ONE_CLICK_WALLET_SESSION, {
|
|
1621
|
+
method: 'POST',
|
|
1622
|
+
body: JSON.stringify(payload),
|
|
1623
|
+
}, timeoutMs));
|
|
1624
|
+
}
|
|
1617
1625
|
/** `resume_payment`, POSTed exactly once (see `requestOnce`). */
|
|
1618
1626
|
async resumePaymentOnce(params) {
|
|
1619
1627
|
return (await this.requestOnce(API_ENDPOINTS.RESUME_PAYMENT, {
|
|
@@ -3090,7 +3098,7 @@
|
|
|
3090
3098
|
function toPrimerError(message, error) {
|
|
3091
3099
|
return error instanceof PrimerError ? error : new PrimerError(message, error);
|
|
3092
3100
|
}
|
|
3093
|
-
function callSafely(fn) {
|
|
3101
|
+
function callSafely$1(fn) {
|
|
3094
3102
|
try {
|
|
3095
3103
|
fn();
|
|
3096
3104
|
}
|
|
@@ -3173,7 +3181,7 @@
|
|
|
3173
3181
|
// A repeated tokenization would start a second reveal timer, so the challenge
|
|
3174
3182
|
// (and the caller's onThreeDSStart) would be reported twice: refuse it.
|
|
3175
3183
|
if (settled || tokenized) {
|
|
3176
|
-
callSafely(() => handler.handleFailure());
|
|
3184
|
+
callSafely$1(() => handler.handleFailure());
|
|
3177
3185
|
return;
|
|
3178
3186
|
}
|
|
3179
3187
|
tokenized = true;
|
|
@@ -3183,7 +3191,7 @@
|
|
|
3183
3191
|
revealed = true;
|
|
3184
3192
|
showOverlay();
|
|
3185
3193
|
warnIfOverlayNotFound();
|
|
3186
|
-
callSafely(params.onChallengeShown);
|
|
3194
|
+
callSafely$1(params.onChallengeShown);
|
|
3187
3195
|
// The whole timeout belongs to the challenge the customer can see.
|
|
3188
3196
|
if (timer) {
|
|
3189
3197
|
clearTimeout(timer);
|
|
@@ -3195,7 +3203,7 @@
|
|
|
3195
3203
|
};
|
|
3196
3204
|
const onResumeSuccess = async (data, handler) => {
|
|
3197
3205
|
if (settled) {
|
|
3198
|
-
callSafely(() => handler.handleFailure());
|
|
3206
|
+
callSafely$1(() => handler.handleFailure());
|
|
3199
3207
|
return;
|
|
3200
3208
|
}
|
|
3201
3209
|
// The challenge is over: the timeout bounds only the challenge, never the POST.
|
|
@@ -3215,7 +3223,7 @@
|
|
|
3215
3223
|
});
|
|
3216
3224
|
}
|
|
3217
3225
|
catch (error) {
|
|
3218
|
-
callSafely(() => handler.handleFailure());
|
|
3226
|
+
callSafely$1(() => handler.handleFailure());
|
|
3219
3227
|
settle({
|
|
3220
3228
|
kind: 'error',
|
|
3221
3229
|
error: error,
|
|
@@ -3227,11 +3235,11 @@
|
|
|
3227
3235
|
if (response?.status === 'success' &&
|
|
3228
3236
|
resumeData?.checkout_status === 'succeeded' &&
|
|
3229
3237
|
!resumeData.action_required_token) {
|
|
3230
|
-
callSafely(() => handler.handleSuccess());
|
|
3238
|
+
callSafely$1(() => handler.handleSuccess());
|
|
3231
3239
|
}
|
|
3232
3240
|
else {
|
|
3233
3241
|
// A chained action token is never continued (no second challenge).
|
|
3234
|
-
callSafely(() => handler.handleFailure(resumeData?.failed_message_for_user || undefined));
|
|
3242
|
+
callSafely$1(() => handler.handleFailure(resumeData?.failed_message_for_user || undefined));
|
|
3235
3243
|
}
|
|
3236
3244
|
settle({ kind: 'resumed', response });
|
|
3237
3245
|
};
|
|
@@ -3340,6 +3348,10 @@
|
|
|
3340
3348
|
function configure(config) {
|
|
3341
3349
|
defaultConfig = config;
|
|
3342
3350
|
}
|
|
3351
|
+
/** The base URL from `configure()`, else the production one. */
|
|
3352
|
+
function defaultBaseUrl() {
|
|
3353
|
+
return defaultConfig?.baseUrl || DEFAULTS.BASE_URL;
|
|
3354
|
+
}
|
|
3343
3355
|
function resolveConfig(options, functionName) {
|
|
3344
3356
|
const { orgId, apiConfig } = options || {};
|
|
3345
3357
|
const finalOrgId = orgId || defaultConfig?.orgId;
|
|
@@ -3414,9 +3426,6 @@
|
|
|
3414
3426
|
if (options.citCardNetworks !== undefined) {
|
|
3415
3427
|
payload.cit_card_networks = options.citCardNetworks;
|
|
3416
3428
|
}
|
|
3417
|
-
if (options.citGooglePayPanOnly !== undefined) {
|
|
3418
|
-
payload.cit_google_pay_pan_only = options.citGooglePayPanOnly;
|
|
3419
|
-
}
|
|
3420
3429
|
return payload;
|
|
3421
3430
|
}
|
|
3422
3431
|
function silentPurchase(options) {
|
|
@@ -3464,7 +3473,11 @@
|
|
|
3464
3473
|
const ONE_CLICK_RETRY_DELAYS_MS = [1000, 2000, 4000];
|
|
3465
3474
|
/** Longer than the backend's wait for the user's lock (~12.6 s). */
|
|
3466
3475
|
const ONE_CLICK_TIMEOUT_MS = 25000;
|
|
3476
|
+
/** org + external id of the customers with a one-click purchase running (silent or wallet). */
|
|
3467
3477
|
const oneClickInFlight = new Set();
|
|
3478
|
+
function oneClickInFlightKey(orgId, externalId) {
|
|
3479
|
+
return `${orgId}\u0000${externalId}`;
|
|
3480
|
+
}
|
|
3468
3481
|
/** A random UUID v4, or `null` when the browser has no crypto (then one_click is not retried). */
|
|
3469
3482
|
function createIdempotencyKey() {
|
|
3470
3483
|
const cryptoApi = typeof crypto === 'undefined' ? undefined : crypto;
|
|
@@ -3730,7 +3743,7 @@
|
|
|
3730
3743
|
}
|
|
3731
3744
|
catch (error) {
|
|
3732
3745
|
// eslint-disable-next-line no-console
|
|
3733
|
-
console.error(`[FunnelfoxSDK]
|
|
3746
|
+
console.error(`[FunnelfoxSDK] ${name} hook threw`, error);
|
|
3734
3747
|
}
|
|
3735
3748
|
}
|
|
3736
3749
|
async function silentPurchaseDetailed(options) {
|
|
@@ -3740,7 +3753,7 @@
|
|
|
3740
3753
|
}
|
|
3741
3754
|
}
|
|
3742
3755
|
const { orgId, baseUrl, externalId } = options;
|
|
3743
|
-
const inFlightKey =
|
|
3756
|
+
const inFlightKey = oneClickInFlightKey(orgId, externalId);
|
|
3744
3757
|
if (oneClickInFlight.has(inFlightKey)) {
|
|
3745
3758
|
return {
|
|
3746
3759
|
outcome: 'error',
|
|
@@ -4039,6 +4052,762 @@
|
|
|
4039
4052
|
: [exports.PaymentMethod.PAYMENT_CARD];
|
|
4040
4053
|
}
|
|
4041
4054
|
|
|
4055
|
+
/**
|
|
4056
|
+
* @fileoverview One-click upsell confirmed in the Apple Pay / Google Pay sheet (FFB-829).
|
|
4057
|
+
*
|
|
4058
|
+
* Primer (PRMSUP-10609): a saved wallet token must not be charged as a customer-initiated
|
|
4059
|
+
* payment, so the customer confirms the upsell in the wallet sheet again and the backend charges
|
|
4060
|
+
* that fresh, single-use token. The button is rendered from a client session the backend prepares
|
|
4061
|
+
* for the upsell (`one_click_wallet_session`). Apple Pay opens its sheet only from the tap itself,
|
|
4062
|
+
* with no request in between, so the session is prepared when the screen loads and kept alive
|
|
4063
|
+
* until the tap.
|
|
4064
|
+
*/
|
|
4065
|
+
const WALLET_SESSION_INVALID = 'wallet_session_invalid';
|
|
4066
|
+
const WALLETS = ['APPLE_PAY', 'GOOGLE_PAY'];
|
|
4067
|
+
const SESSION_REQUEST_TIMEOUT_MS = 15000;
|
|
4068
|
+
/** How long Primer may take to report the payment methods the device can show. */
|
|
4069
|
+
const METHODS_TIMEOUT_MS = 10000;
|
|
4070
|
+
/**
|
|
4071
|
+
* The session is prepared again this long before it expires. The backend hands back an unused
|
|
4072
|
+
* session only while it has at least 10 minutes to live, so 15 always yields a fresh one.
|
|
4073
|
+
*/
|
|
4074
|
+
const REFRESH_BEFORE_EXPIRY_MS = 15 * 60 * 1000;
|
|
4075
|
+
/** A tap on a session with less left than this is refused and the session is replaced. */
|
|
4076
|
+
const TAP_MIN_REMAINING_MS = 60 * 1000;
|
|
4077
|
+
/** Bounds the refresh timer for a session shorter than the refresh margin; a tap still checks it. */
|
|
4078
|
+
const MIN_REFRESH_DELAY_MS = 60 * 1000;
|
|
4079
|
+
const DEFAULT_BUTTON_HEIGHT = 54;
|
|
4080
|
+
const DEFAULT_BUTTON_RADIUS = 28;
|
|
4081
|
+
/** The backend sends naive UTC timestamps; without a zone `Date.parse` would read local time. */
|
|
4082
|
+
function parseUtc(value) {
|
|
4083
|
+
const hasZone = /([zZ]|[+-]\d{2}:?\d{2})$/.test(value);
|
|
4084
|
+
return Date.parse(hasZone ? value : `${value}Z`);
|
|
4085
|
+
}
|
|
4086
|
+
function parseWalletSession(data) {
|
|
4087
|
+
const expiresAt = data.expires_at ? parseUtc(data.expires_at) : NaN;
|
|
4088
|
+
if (!WALLETS.includes(data.wallet) ||
|
|
4089
|
+
!data.client_token ||
|
|
4090
|
+
!data.session_id ||
|
|
4091
|
+
!data.order_id ||
|
|
4092
|
+
!Number.isFinite(expiresAt)) {
|
|
4093
|
+
throw new APIError('Invalid one_click_wallet_session response', null, {
|
|
4094
|
+
response: data,
|
|
4095
|
+
});
|
|
4096
|
+
}
|
|
4097
|
+
return {
|
|
4098
|
+
wallet: data.wallet,
|
|
4099
|
+
clientToken: data.client_token,
|
|
4100
|
+
sessionId: data.session_id,
|
|
4101
|
+
orderId: data.order_id,
|
|
4102
|
+
amount: data.amount ?? null,
|
|
4103
|
+
currency: data.currency ?? null,
|
|
4104
|
+
expiresAt,
|
|
4105
|
+
};
|
|
4106
|
+
}
|
|
4107
|
+
/** `null` = the customer's saved method is not a Primer wallet. */
|
|
4108
|
+
async function prepareSession(apiClient, options) {
|
|
4109
|
+
const response = await apiClient.oneClickWalletSession({
|
|
4110
|
+
pp_ident: options.priceId,
|
|
4111
|
+
external_id: options.externalId,
|
|
4112
|
+
client_metadata: options.clientMetadata || {},
|
|
4113
|
+
}, SESSION_REQUEST_TIMEOUT_MS);
|
|
4114
|
+
if (response?.status !== 'success' || !response.data) {
|
|
4115
|
+
throw new APIError('one_click_wallet_session failed', null, { response });
|
|
4116
|
+
}
|
|
4117
|
+
if (response.data.wallet === null) {
|
|
4118
|
+
return null;
|
|
4119
|
+
}
|
|
4120
|
+
return parseWalletSession(response.data);
|
|
4121
|
+
}
|
|
4122
|
+
/** Primer's wallet options for the requested style; a type the wallet lacks becomes `plain`. */
|
|
4123
|
+
function primerWalletOptions(wallet, style) {
|
|
4124
|
+
const buttonType = style.buttonType || 'plain';
|
|
4125
|
+
const buttonColor = style.buttonColor || 'black';
|
|
4126
|
+
if (wallet === 'APPLE_PAY') {
|
|
4127
|
+
return {
|
|
4128
|
+
applePay: {
|
|
4129
|
+
buttonType: buttonType === 'buy' ? 'buy' : 'plain',
|
|
4130
|
+
buttonStyle: buttonColor,
|
|
4131
|
+
},
|
|
4132
|
+
};
|
|
4133
|
+
}
|
|
4134
|
+
return {
|
|
4135
|
+
googlePay: {
|
|
4136
|
+
buttonType: buttonType === 'continue' ? 'plain' : buttonType,
|
|
4137
|
+
buttonColor: buttonColor === 'black' ? 'black' : 'white',
|
|
4138
|
+
buttonSizeMode: 'fill',
|
|
4139
|
+
},
|
|
4140
|
+
};
|
|
4141
|
+
}
|
|
4142
|
+
function toError(message, error) {
|
|
4143
|
+
if (error instanceof FunnefoxSDKError) {
|
|
4144
|
+
return error;
|
|
4145
|
+
}
|
|
4146
|
+
return new PrimerError(message, error);
|
|
4147
|
+
}
|
|
4148
|
+
function callSafely(fn) {
|
|
4149
|
+
try {
|
|
4150
|
+
fn();
|
|
4151
|
+
}
|
|
4152
|
+
catch {
|
|
4153
|
+
// Primer handler calls are best effort once the outcome is decided.
|
|
4154
|
+
}
|
|
4155
|
+
}
|
|
4156
|
+
function isApiErrorWithCode(error, code) {
|
|
4157
|
+
return (error instanceof APIError &&
|
|
4158
|
+
(error.errorCode === code ||
|
|
4159
|
+
getResponseErrorCodes(error.response).includes(code)));
|
|
4160
|
+
}
|
|
4161
|
+
/** One mounted wallet button and the attempts made with it. */
|
|
4162
|
+
class WalletUpsell {
|
|
4163
|
+
constructor(container, options, session) {
|
|
4164
|
+
this.container = container;
|
|
4165
|
+
this.options = options;
|
|
4166
|
+
this.rendered = null;
|
|
4167
|
+
this.generation = 0;
|
|
4168
|
+
this.destroyed = false;
|
|
4169
|
+
/** An outcome other than `cancelled` was reported: the button stays disabled. */
|
|
4170
|
+
this.finished = false;
|
|
4171
|
+
this.disabledByCaller = false;
|
|
4172
|
+
this.refreshing = null;
|
|
4173
|
+
this.refreshTimer = null;
|
|
4174
|
+
this.attemptRunning = false;
|
|
4175
|
+
this.tokenizeStarted = false;
|
|
4176
|
+
/** The wallet token reached us: from here the attempt ends only with the charge's answer. */
|
|
4177
|
+
this.charging = false;
|
|
4178
|
+
this.threeDS = null;
|
|
4179
|
+
this.onVisibilityChange = () => {
|
|
4180
|
+
if (document.visibilityState === 'visible' && this.expiresSoon()) {
|
|
4181
|
+
this.refresh();
|
|
4182
|
+
}
|
|
4183
|
+
};
|
|
4184
|
+
this.session = session;
|
|
4185
|
+
this.apiClient = WalletUpsell.apiClientFor(options);
|
|
4186
|
+
this.inFlightKey = oneClickInFlightKey(options.orgId, options.externalId);
|
|
4187
|
+
}
|
|
4188
|
+
static apiClientFor(options) {
|
|
4189
|
+
return new APIClient({
|
|
4190
|
+
baseUrl: options.baseUrl || defaultBaseUrl(),
|
|
4191
|
+
orgId: options.orgId,
|
|
4192
|
+
timeout: DEFAULTS.REQUEST_TIMEOUT,
|
|
4193
|
+
retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
|
|
4194
|
+
});
|
|
4195
|
+
}
|
|
4196
|
+
/** Renders the button for the current session: `false` when the device cannot show the wallet. */
|
|
4197
|
+
async render() {
|
|
4198
|
+
const generation = ++this.generation;
|
|
4199
|
+
const session = this.session;
|
|
4200
|
+
const style = this.options.style || {};
|
|
4201
|
+
await new PrimerWrapper().ensurePrimerLoaded();
|
|
4202
|
+
let reportMethods = () => { };
|
|
4203
|
+
let methodsTimer = null;
|
|
4204
|
+
const methods = new Promise((resolve, reject) => {
|
|
4205
|
+
reportMethods = resolve;
|
|
4206
|
+
methodsTimer = setTimeout(() => reject(new PrimerError('Primer did not report the available payment methods')), METHODS_TIMEOUT_MS);
|
|
4207
|
+
}).finally(() => clearTimeout(methodsTimer));
|
|
4208
|
+
// A dedicated instance: the checkouts' HeadlessManager caches by client token, and a
|
|
4209
|
+
// remounted upsell gets the same token back, so the cache would hand out a headless whose
|
|
4210
|
+
// callbacks belong to a destroyed mount.
|
|
4211
|
+
const headless = await window.Primer.createHeadless(session.clientToken, {
|
|
4212
|
+
paymentHandling: PRIMER_DEFAULTS.PAYMENT_HANDLING,
|
|
4213
|
+
apiVersion: PRIMER_DEFAULTS.API_VERSION,
|
|
4214
|
+
...primerWalletOptions(session.wallet, style),
|
|
4215
|
+
onAvailablePaymentMethodsLoad: (items) => reportMethods(items.map(item => item.type)),
|
|
4216
|
+
onTokenizeShouldStart: () => this.onTap(generation),
|
|
4217
|
+
onTokenizeStart: () => {
|
|
4218
|
+
if (generation === this.generation) {
|
|
4219
|
+
this.tokenizeStarted = true;
|
|
4220
|
+
}
|
|
4221
|
+
},
|
|
4222
|
+
onPaymentMethodAction: action => {
|
|
4223
|
+
if (action === 'PAYMENT_METHOD_UNSELECTED') {
|
|
4224
|
+
this.onSheetClosed(generation);
|
|
4225
|
+
}
|
|
4226
|
+
},
|
|
4227
|
+
onTokenizeSuccess: (data, handler) => this.onTokenized(generation, data, handler),
|
|
4228
|
+
onTokenizeError: (error) => this.onPrimerFailure(generation, 'Wallet tokenization failed', error),
|
|
4229
|
+
onResumeSuccess: (data, handler) => this.onResumed(generation, data, handler),
|
|
4230
|
+
onResumeError: (error) => this.onResumeError(generation, error),
|
|
4231
|
+
onCheckoutFail: (error) => this.onPrimerFailure(generation, 'Primer checkout failed', error),
|
|
4232
|
+
});
|
|
4233
|
+
await headless.start();
|
|
4234
|
+
const available = await methods;
|
|
4235
|
+
if (!available.includes(session.wallet)) {
|
|
4236
|
+
return false;
|
|
4237
|
+
}
|
|
4238
|
+
const manager = await headless.createPaymentMethodManager(session.wallet);
|
|
4239
|
+
if (!manager || typeof manager.createButton !== 'function') {
|
|
4240
|
+
throw new PrimerError(`No ${session.wallet} button manager`);
|
|
4241
|
+
}
|
|
4242
|
+
const rendered = this.mountButton(generation, manager.createButton(), session.wallet, style);
|
|
4243
|
+
try {
|
|
4244
|
+
await rendered.button.render(rendered.wrapper, {});
|
|
4245
|
+
if (session.wallet === 'APPLE_PAY' && rendered.nativeOverlay === null) {
|
|
4246
|
+
rendered.nativeOverlay = await this.overlayAppleType(rendered, style);
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
catch (error) {
|
|
4250
|
+
this.removeButton(rendered);
|
|
4251
|
+
throw error;
|
|
4252
|
+
}
|
|
4253
|
+
if (this.destroyed || generation !== this.generation) {
|
|
4254
|
+
this.removeButton(rendered);
|
|
4255
|
+
return true;
|
|
4256
|
+
}
|
|
4257
|
+
const previous = this.rendered;
|
|
4258
|
+
this.rendered = rendered;
|
|
4259
|
+
if (previous) {
|
|
4260
|
+
this.removeButton(previous);
|
|
4261
|
+
}
|
|
4262
|
+
this.applyDisabled();
|
|
4263
|
+
this.scheduleRefresh();
|
|
4264
|
+
return true;
|
|
4265
|
+
}
|
|
4266
|
+
mountButton(generation, button, wallet, style) {
|
|
4267
|
+
// Primer does not size its wallet buttons (FFB-169), so the wrapper's rules do.
|
|
4268
|
+
const wrapper = document.createElement('div');
|
|
4269
|
+
wrapper.className = generateId('funnelfox-wallet-upsell');
|
|
4270
|
+
const height = style.height ?? DEFAULT_BUTTON_HEIGHT;
|
|
4271
|
+
const radius = style.borderRadius ?? DEFAULT_BUTTON_RADIUS;
|
|
4272
|
+
const styleEl = document.createElement('style');
|
|
4273
|
+
styleEl.textContent = [
|
|
4274
|
+
`.${wrapper.className}{width:100%!important}`,
|
|
4275
|
+
`.${wrapper.className} button{height:${height}px!important;border-radius:${radius}px!important}`,
|
|
4276
|
+
wallet === 'APPLE_PAY' && style.buttonType === 'continue'
|
|
4277
|
+
? `.${wrapper.className} > button{opacity:0!important}`
|
|
4278
|
+
: '',
|
|
4279
|
+
].join('\n');
|
|
4280
|
+
document.head.appendChild(styleEl);
|
|
4281
|
+
this.container.appendChild(wrapper);
|
|
4282
|
+
return { generation, button, wrapper, styleEl, nativeOverlay: null };
|
|
4283
|
+
}
|
|
4284
|
+
/** PRD-1426: WebKit draws only the legacy Apple Pay types, so `continue` uses Apple's element. */
|
|
4285
|
+
async overlayAppleType(rendered, style) {
|
|
4286
|
+
if (style.buttonType !== 'continue') {
|
|
4287
|
+
return null;
|
|
4288
|
+
}
|
|
4289
|
+
const overlay = await overlayNativeApplePayButton(rendered.wrapper, {
|
|
4290
|
+
buttonType: style.buttonType,
|
|
4291
|
+
buttonStyle: style.buttonColor || 'black',
|
|
4292
|
+
borderRadius: style.borderRadius ?? DEFAULT_BUTTON_RADIUS,
|
|
4293
|
+
});
|
|
4294
|
+
if (!overlay) {
|
|
4295
|
+
// Apple's element could not load: show Primer's own (plain) button.
|
|
4296
|
+
rendered.styleEl.textContent = rendered.styleEl.textContent.replace(`.${rendered.wrapper.className} > button{opacity:0!important}`, '');
|
|
4297
|
+
}
|
|
4298
|
+
return overlay;
|
|
4299
|
+
}
|
|
4300
|
+
removeButton(rendered) {
|
|
4301
|
+
callSafely(() => rendered.button.clean());
|
|
4302
|
+
rendered.nativeOverlay?.remove();
|
|
4303
|
+
rendered.wrapper.remove();
|
|
4304
|
+
rendered.styleEl.remove();
|
|
4305
|
+
}
|
|
4306
|
+
applyDisabled() {
|
|
4307
|
+
const rendered = this.rendered;
|
|
4308
|
+
if (!rendered) {
|
|
4309
|
+
return;
|
|
4310
|
+
}
|
|
4311
|
+
const disabled = this.finished || this.disabledByCaller;
|
|
4312
|
+
void Promise.resolve(rendered.button.setDisabled(disabled)).catch(() => { });
|
|
4313
|
+
if (rendered.nativeOverlay) {
|
|
4314
|
+
rendered.nativeOverlay.style.pointerEvents = disabled ? 'none' : '';
|
|
4315
|
+
rendered.nativeOverlay.style.opacity = disabled ? '0.5' : '';
|
|
4316
|
+
}
|
|
4317
|
+
}
|
|
4318
|
+
setDisabled(disabled) {
|
|
4319
|
+
this.disabledByCaller = disabled;
|
|
4320
|
+
this.applyDisabled();
|
|
4321
|
+
}
|
|
4322
|
+
destroy() {
|
|
4323
|
+
if (this.destroyed) {
|
|
4324
|
+
return;
|
|
4325
|
+
}
|
|
4326
|
+
this.destroyed = true;
|
|
4327
|
+
this.generation += 1;
|
|
4328
|
+
if (this.attemptRunning && !this.charging) {
|
|
4329
|
+
// Primer's callbacks for this mount are ignored from now on, so nothing else ends it.
|
|
4330
|
+
this.attemptRunning = false;
|
|
4331
|
+
oneClickInFlight.delete(this.inFlightKey);
|
|
4332
|
+
}
|
|
4333
|
+
this.clearRefresh();
|
|
4334
|
+
if (typeof document !== 'undefined') {
|
|
4335
|
+
document.removeEventListener('visibilitychange', this.onVisibilityChange);
|
|
4336
|
+
}
|
|
4337
|
+
if (this.rendered) {
|
|
4338
|
+
this.removeButton(this.rendered);
|
|
4339
|
+
this.rendered = null;
|
|
4340
|
+
}
|
|
4341
|
+
}
|
|
4342
|
+
// ---------- session life ----------
|
|
4343
|
+
watchVisibility() {
|
|
4344
|
+
if (typeof document !== 'undefined') {
|
|
4345
|
+
document.addEventListener('visibilitychange', this.onVisibilityChange);
|
|
4346
|
+
}
|
|
4347
|
+
}
|
|
4348
|
+
expiresSoon(minRemainingMs = REFRESH_BEFORE_EXPIRY_MS) {
|
|
4349
|
+
return this.session.expiresAt - Date.now() < minRemainingMs;
|
|
4350
|
+
}
|
|
4351
|
+
clearRefresh() {
|
|
4352
|
+
if (this.refreshTimer) {
|
|
4353
|
+
clearTimeout(this.refreshTimer);
|
|
4354
|
+
this.refreshTimer = null;
|
|
4355
|
+
}
|
|
4356
|
+
}
|
|
4357
|
+
scheduleRefresh() {
|
|
4358
|
+
this.clearRefresh();
|
|
4359
|
+
if (this.destroyed || this.finished) {
|
|
4360
|
+
return;
|
|
4361
|
+
}
|
|
4362
|
+
const delay = Math.max(this.session.expiresAt - REFRESH_BEFORE_EXPIRY_MS - Date.now(), MIN_REFRESH_DELAY_MS);
|
|
4363
|
+
this.refreshTimer = setTimeout(() => {
|
|
4364
|
+
this.refreshTimer = null;
|
|
4365
|
+
this.refresh();
|
|
4366
|
+
}, delay);
|
|
4367
|
+
}
|
|
4368
|
+
/** Replaces the session and the button before the session expires; never during an attempt. */
|
|
4369
|
+
refresh() {
|
|
4370
|
+
// An attempt ends either for good or as `cancelled`, which checks the session again.
|
|
4371
|
+
if (this.refreshing ||
|
|
4372
|
+
this.destroyed ||
|
|
4373
|
+
this.finished ||
|
|
4374
|
+
this.attemptRunning) {
|
|
4375
|
+
return;
|
|
4376
|
+
}
|
|
4377
|
+
this.refreshing = this.replaceSession().finally(() => {
|
|
4378
|
+
this.refreshing = null;
|
|
4379
|
+
});
|
|
4380
|
+
}
|
|
4381
|
+
async replaceSession() {
|
|
4382
|
+
let failure = null;
|
|
4383
|
+
try {
|
|
4384
|
+
const next = await prepareSession(this.apiClient, this.options);
|
|
4385
|
+
if (this.destroyed || this.finished) {
|
|
4386
|
+
return;
|
|
4387
|
+
}
|
|
4388
|
+
if (!next || next.wallet !== this.session.wallet) {
|
|
4389
|
+
failure = new FunnefoxSDKError('The wallet upsell is no longer available for this customer', 'wallet_upsell_unavailable');
|
|
4390
|
+
}
|
|
4391
|
+
else {
|
|
4392
|
+
this.session = next;
|
|
4393
|
+
if (!(await this.render())) {
|
|
4394
|
+
failure = new FunnefoxSDKError('This device can no longer show the wallet', 'wallet_upsell_unavailable');
|
|
4395
|
+
}
|
|
4396
|
+
}
|
|
4397
|
+
}
|
|
4398
|
+
catch (error) {
|
|
4399
|
+
failure = toError('Failed to prepare the wallet upsell again', error);
|
|
4400
|
+
}
|
|
4401
|
+
if (failure && !this.destroyed && !this.finished && !this.attemptRunning) {
|
|
4402
|
+
// The button can no longer charge: the caller takes its own path.
|
|
4403
|
+
this.report({
|
|
4404
|
+
outcome: 'error',
|
|
4405
|
+
orderId: null,
|
|
4406
|
+
threeDSChallenged: false,
|
|
4407
|
+
error: failure,
|
|
4408
|
+
});
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4411
|
+
// ---------- attempt ----------
|
|
4412
|
+
onTap(generation) {
|
|
4413
|
+
if (generation !== this.generation ||
|
|
4414
|
+
this.destroyed ||
|
|
4415
|
+
this.finished ||
|
|
4416
|
+
this.disabledByCaller ||
|
|
4417
|
+
this.refreshing ||
|
|
4418
|
+
// Double tap, either wallet.
|
|
4419
|
+
this.attemptRunning ||
|
|
4420
|
+
oneClickInFlight.has(this.inFlightKey)) {
|
|
4421
|
+
return false;
|
|
4422
|
+
}
|
|
4423
|
+
if (this.expiresSoon(TAP_MIN_REMAINING_MS)) {
|
|
4424
|
+
// Apple Pay needs the tap itself to open the sheet: the customer taps the new button.
|
|
4425
|
+
this.refresh();
|
|
4426
|
+
return false;
|
|
4427
|
+
}
|
|
4428
|
+
this.attemptRunning = true;
|
|
4429
|
+
this.tokenizeStarted = false;
|
|
4430
|
+
oneClickInFlight.add(this.inFlightKey);
|
|
4431
|
+
callHook('onStart', this.options.onStart, undefined);
|
|
4432
|
+
return true;
|
|
4433
|
+
}
|
|
4434
|
+
onSheetClosed(generation) {
|
|
4435
|
+
if (generation !== this.generation ||
|
|
4436
|
+
!this.attemptRunning ||
|
|
4437
|
+
this.tokenizeStarted) {
|
|
4438
|
+
return;
|
|
4439
|
+
}
|
|
4440
|
+
this.finish({
|
|
4441
|
+
outcome: 'cancelled',
|
|
4442
|
+
orderId: null,
|
|
4443
|
+
threeDSChallenged: false,
|
|
4444
|
+
});
|
|
4445
|
+
}
|
|
4446
|
+
onPrimerFailure(generation, message, error) {
|
|
4447
|
+
if (generation !== this.generation || !this.attemptRunning) {
|
|
4448
|
+
return;
|
|
4449
|
+
}
|
|
4450
|
+
if (this.threeDS) {
|
|
4451
|
+
this.endThreeDS('failed');
|
|
4452
|
+
this.finish({
|
|
4453
|
+
outcome: '3ds_failed',
|
|
4454
|
+
orderId: this.session.orderId,
|
|
4455
|
+
threeDSChallenged: this.threeDSChallenged(),
|
|
4456
|
+
error: toError('3DS authentication failed', error),
|
|
4457
|
+
});
|
|
4458
|
+
return;
|
|
4459
|
+
}
|
|
4460
|
+
this.finish({
|
|
4461
|
+
outcome: 'error',
|
|
4462
|
+
orderId: null,
|
|
4463
|
+
threeDSChallenged: false,
|
|
4464
|
+
error: toError(message, error),
|
|
4465
|
+
});
|
|
4466
|
+
}
|
|
4467
|
+
onTokenized(generation, data, handler) {
|
|
4468
|
+
if (generation !== this.generation ||
|
|
4469
|
+
!this.attemptRunning ||
|
|
4470
|
+
this.threeDS) {
|
|
4471
|
+
callSafely(() => handler.handleFailure());
|
|
4472
|
+
return;
|
|
4473
|
+
}
|
|
4474
|
+
this.tokenizeStarted = true;
|
|
4475
|
+
this.charging = true;
|
|
4476
|
+
void this.charge(data.token, handler, false).catch(error => {
|
|
4477
|
+
callSafely(() => handler.handleFailure());
|
|
4478
|
+
this.finish({
|
|
4479
|
+
outcome: 'error',
|
|
4480
|
+
orderId: this.session.orderId,
|
|
4481
|
+
threeDSChallenged: false,
|
|
4482
|
+
error: toError('Wallet upsell charge failed', error),
|
|
4483
|
+
});
|
|
4484
|
+
});
|
|
4485
|
+
}
|
|
4486
|
+
async charge(paymentMethodToken, handler, retried) {
|
|
4487
|
+
const session = this.session;
|
|
4488
|
+
const payload = {
|
|
4489
|
+
pp_ident: this.options.priceId,
|
|
4490
|
+
external_id: this.options.externalId,
|
|
4491
|
+
client_metadata: (this.options.clientMetadata ||
|
|
4492
|
+
{}),
|
|
4493
|
+
payment_method_token: paymentMethodToken,
|
|
4494
|
+
session_id: session.sessionId,
|
|
4495
|
+
};
|
|
4496
|
+
const idempotencyKey = createIdempotencyKey();
|
|
4497
|
+
if (idempotencyKey) {
|
|
4498
|
+
payload.idempotency_key = idempotencyKey;
|
|
4499
|
+
}
|
|
4500
|
+
let response;
|
|
4501
|
+
try {
|
|
4502
|
+
response = await postOneClick(this.apiClient, payload);
|
|
4503
|
+
}
|
|
4504
|
+
catch (error) {
|
|
4505
|
+
if (!retried &&
|
|
4506
|
+
isApiErrorWithCode(error, WALLET_SESSION_INVALID) &&
|
|
4507
|
+
(await this.sameAmountSession())) {
|
|
4508
|
+
// Nothing was charged and the customer approved this very amount: one more try.
|
|
4509
|
+
return this.charge(paymentMethodToken, handler, true);
|
|
4510
|
+
}
|
|
4511
|
+
callSafely(() => handler.handleFailure());
|
|
4512
|
+
this.finish(this.chargeFailure(error, Boolean(idempotencyKey)));
|
|
4513
|
+
return;
|
|
4514
|
+
}
|
|
4515
|
+
const step = mapOneClickResponse(response);
|
|
4516
|
+
if (step.kind === 'result') {
|
|
4517
|
+
const result = step.result;
|
|
4518
|
+
if (result.outcome === 'succeeded' || result.outcome === 'processing') {
|
|
4519
|
+
callSafely(() => handler.handleSuccess());
|
|
4520
|
+
}
|
|
4521
|
+
else {
|
|
4522
|
+
callSafely(() => handler.handleFailure(result.failedMessageForUser || undefined));
|
|
4523
|
+
}
|
|
4524
|
+
this.finish(result);
|
|
4525
|
+
return;
|
|
4526
|
+
}
|
|
4527
|
+
this.startThreeDS(step.orderId || session.orderId, step.actionToken, handler);
|
|
4528
|
+
}
|
|
4529
|
+
/** A new session for the same amount the customer approved in the sheet. */
|
|
4530
|
+
async sameAmountSession() {
|
|
4531
|
+
try {
|
|
4532
|
+
const next = await prepareSession(this.apiClient, this.options);
|
|
4533
|
+
if (!next ||
|
|
4534
|
+
next.wallet !== this.session.wallet ||
|
|
4535
|
+
next.amount !== this.session.amount ||
|
|
4536
|
+
next.currency !== this.session.currency) {
|
|
4537
|
+
return false;
|
|
4538
|
+
}
|
|
4539
|
+
this.session = next;
|
|
4540
|
+
return true;
|
|
4541
|
+
}
|
|
4542
|
+
catch {
|
|
4543
|
+
return false;
|
|
4544
|
+
}
|
|
4545
|
+
}
|
|
4546
|
+
chargeFailure(error, keyed) {
|
|
4547
|
+
if (isApiErrorWithCode(error, DOUBLE_PURCHASE)) {
|
|
4548
|
+
return {
|
|
4549
|
+
outcome: 'already_purchased',
|
|
4550
|
+
orderId: null,
|
|
4551
|
+
threeDSChallenged: false,
|
|
4552
|
+
};
|
|
4553
|
+
}
|
|
4554
|
+
if (keyed && isRetryableOneClickError(error)) {
|
|
4555
|
+
// The charge may have gone through: the caller must not start a new payment.
|
|
4556
|
+
return {
|
|
4557
|
+
outcome: 'processing',
|
|
4558
|
+
orderId: this.session.orderId,
|
|
4559
|
+
threeDSChallenged: false,
|
|
4560
|
+
error: error,
|
|
4561
|
+
failedRequest: 'one_click',
|
|
4562
|
+
};
|
|
4563
|
+
}
|
|
4564
|
+
return {
|
|
4565
|
+
outcome: 'error',
|
|
4566
|
+
orderId: null,
|
|
4567
|
+
threeDSChallenged: false,
|
|
4568
|
+
error: error instanceof Error ? error : toError('one_click failed', error),
|
|
4569
|
+
failedRequest: 'one_click',
|
|
4570
|
+
};
|
|
4571
|
+
}
|
|
4572
|
+
// ---------- 3DS ----------
|
|
4573
|
+
threeDSChallenged() {
|
|
4574
|
+
return Boolean(this.threeDS?.challenged);
|
|
4575
|
+
}
|
|
4576
|
+
startThreeDS(orderId, actionToken, handler) {
|
|
4577
|
+
const intent = getPrimerTokenIntent(actionToken);
|
|
4578
|
+
if (!intent || !PRIMER_3DS_INTENTS.includes(intent)) {
|
|
4579
|
+
callSafely(() => handler.handleFailure());
|
|
4580
|
+
this.finish({
|
|
4581
|
+
outcome: 'error',
|
|
4582
|
+
orderId,
|
|
4583
|
+
threeDSChallenged: false,
|
|
4584
|
+
error: new PrimerError('Unsupported one-click action: the action token is not a Primer 3DS client token', { intent }),
|
|
4585
|
+
});
|
|
4586
|
+
return;
|
|
4587
|
+
}
|
|
4588
|
+
const timeoutMs = DEFAULT_THREE_DS_TIMEOUT_MS;
|
|
4589
|
+
const threeDS = {
|
|
4590
|
+
orderId,
|
|
4591
|
+
challenged: false,
|
|
4592
|
+
revealTimer: null,
|
|
4593
|
+
timer: null,
|
|
4594
|
+
showOverlay: hidePrimerOverlay(),
|
|
4595
|
+
};
|
|
4596
|
+
this.threeDS = threeDS;
|
|
4597
|
+
const onTimeout = () => {
|
|
4598
|
+
threeDS.timer = null;
|
|
4599
|
+
if (!threeDS.challenged) {
|
|
4600
|
+
// The overlay is about to be shown: the reveal restarts the timer.
|
|
4601
|
+
return;
|
|
4602
|
+
}
|
|
4603
|
+
this.endThreeDS('abandoned');
|
|
4604
|
+
this.finish({
|
|
4605
|
+
outcome: '3ds_abandoned',
|
|
4606
|
+
orderId,
|
|
4607
|
+
threeDSChallenged: true,
|
|
4608
|
+
});
|
|
4609
|
+
dismissPrimerOverlay();
|
|
4610
|
+
};
|
|
4611
|
+
threeDS.timer = setTimeout(onTimeout, timeoutMs);
|
|
4612
|
+
threeDS.revealTimer = setTimeout(() => {
|
|
4613
|
+
threeDS.revealTimer = null;
|
|
4614
|
+
threeDS.challenged = true;
|
|
4615
|
+
threeDS.showOverlay();
|
|
4616
|
+
warnIfOverlayNotFound();
|
|
4617
|
+
callHook('onThreeDSStart', this.options.onThreeDSStart, { orderId });
|
|
4618
|
+
if (threeDS.timer) {
|
|
4619
|
+
clearTimeout(threeDS.timer);
|
|
4620
|
+
}
|
|
4621
|
+
threeDS.timer = setTimeout(onTimeout, timeoutMs);
|
|
4622
|
+
}, THREE_DS_OVERLAY_REVEAL_DELAY_MS);
|
|
4623
|
+
handler.continueWithNewClientToken(actionToken);
|
|
4624
|
+
}
|
|
4625
|
+
/** Stops the 3DS timers; reports `onThreeDSEnd` once, and only after `onThreeDSStart`. */
|
|
4626
|
+
endThreeDS(result) {
|
|
4627
|
+
const threeDS = this.threeDS;
|
|
4628
|
+
if (!threeDS) {
|
|
4629
|
+
return;
|
|
4630
|
+
}
|
|
4631
|
+
if (threeDS.timer) {
|
|
4632
|
+
clearTimeout(threeDS.timer);
|
|
4633
|
+
threeDS.timer = null;
|
|
4634
|
+
}
|
|
4635
|
+
if (threeDS.revealTimer) {
|
|
4636
|
+
clearTimeout(threeDS.revealTimer);
|
|
4637
|
+
threeDS.revealTimer = null;
|
|
4638
|
+
}
|
|
4639
|
+
threeDS.showOverlay();
|
|
4640
|
+
if (threeDS.challenged) {
|
|
4641
|
+
callHook('onThreeDSEnd', this.options.onThreeDSEnd, { result });
|
|
4642
|
+
}
|
|
4643
|
+
}
|
|
4644
|
+
async onResumed(generation, data, handler) {
|
|
4645
|
+
const threeDS = this.threeDS;
|
|
4646
|
+
if (generation !== this.generation || !this.attemptRunning || !threeDS) {
|
|
4647
|
+
callSafely(() => handler.handleFailure());
|
|
4648
|
+
return;
|
|
4649
|
+
}
|
|
4650
|
+
// The challenge is over: the timeout bounds only the challenge, never the POST.
|
|
4651
|
+
if (threeDS.timer) {
|
|
4652
|
+
clearTimeout(threeDS.timer);
|
|
4653
|
+
threeDS.timer = null;
|
|
4654
|
+
}
|
|
4655
|
+
if (threeDS.revealTimer) {
|
|
4656
|
+
clearTimeout(threeDS.revealTimer);
|
|
4657
|
+
threeDS.revealTimer = null;
|
|
4658
|
+
}
|
|
4659
|
+
let response;
|
|
4660
|
+
try {
|
|
4661
|
+
response = await this.apiClient.resumePaymentOnce({
|
|
4662
|
+
orderId: threeDS.orderId,
|
|
4663
|
+
resumeToken: data.resumeToken,
|
|
4664
|
+
});
|
|
4665
|
+
}
|
|
4666
|
+
catch (error) {
|
|
4667
|
+
callSafely(() => handler.handleFailure());
|
|
4668
|
+
this.endThreeDS('error');
|
|
4669
|
+
this.finish({
|
|
4670
|
+
outcome: 'error',
|
|
4671
|
+
orderId: threeDS.orderId,
|
|
4672
|
+
threeDSChallenged: threeDS.challenged,
|
|
4673
|
+
error: error,
|
|
4674
|
+
failedRequest: 'resume_payment',
|
|
4675
|
+
});
|
|
4676
|
+
return;
|
|
4677
|
+
}
|
|
4678
|
+
const mapped = mapResumeResponse(response, threeDS.orderId, threeDS.challenged);
|
|
4679
|
+
if (mapped.result.outcome === 'succeeded' ||
|
|
4680
|
+
mapped.result.outcome === 'processing') {
|
|
4681
|
+
callSafely(() => handler.handleSuccess());
|
|
4682
|
+
}
|
|
4683
|
+
else {
|
|
4684
|
+
callSafely(() => handler.handleFailure(mapped.result.failedMessageForUser || undefined));
|
|
4685
|
+
}
|
|
4686
|
+
this.endThreeDS(mapped.threeDS);
|
|
4687
|
+
this.finish(mapped.result);
|
|
4688
|
+
}
|
|
4689
|
+
onResumeError(generation, error) {
|
|
4690
|
+
const threeDS = this.threeDS;
|
|
4691
|
+
if (generation !== this.generation || !this.attemptRunning || !threeDS) {
|
|
4692
|
+
return;
|
|
4693
|
+
}
|
|
4694
|
+
if (isThreeDSCloseError(error)) {
|
|
4695
|
+
this.endThreeDS('abandoned');
|
|
4696
|
+
this.finish({
|
|
4697
|
+
outcome: '3ds_abandoned',
|
|
4698
|
+
orderId: threeDS.orderId,
|
|
4699
|
+
threeDSChallenged: threeDS.challenged,
|
|
4700
|
+
});
|
|
4701
|
+
return;
|
|
4702
|
+
}
|
|
4703
|
+
this.endThreeDS('failed');
|
|
4704
|
+
this.finish({
|
|
4705
|
+
outcome: '3ds_failed',
|
|
4706
|
+
orderId: threeDS.orderId,
|
|
4707
|
+
threeDSChallenged: threeDS.challenged,
|
|
4708
|
+
error: toError('3DS authentication failed', error),
|
|
4709
|
+
});
|
|
4710
|
+
}
|
|
4711
|
+
// ---------- result ----------
|
|
4712
|
+
/** Ends the running attempt with its one result; later calls for it are ignored. */
|
|
4713
|
+
finish(result) {
|
|
4714
|
+
if (!this.attemptRunning) {
|
|
4715
|
+
return;
|
|
4716
|
+
}
|
|
4717
|
+
this.attemptRunning = false;
|
|
4718
|
+
this.tokenizeStarted = false;
|
|
4719
|
+
this.charging = false;
|
|
4720
|
+
this.threeDS = null;
|
|
4721
|
+
oneClickInFlight.delete(this.inFlightKey);
|
|
4722
|
+
this.report(result);
|
|
4723
|
+
}
|
|
4724
|
+
report(result) {
|
|
4725
|
+
if (result.outcome !== 'cancelled') {
|
|
4726
|
+
this.finished = true;
|
|
4727
|
+
this.clearRefresh();
|
|
4728
|
+
this.applyDisabled();
|
|
4729
|
+
}
|
|
4730
|
+
else if (this.expiresSoon()) {
|
|
4731
|
+
this.refresh();
|
|
4732
|
+
}
|
|
4733
|
+
if (result.outcome === 'error' && result.error) {
|
|
4734
|
+
const error = result.error;
|
|
4735
|
+
getErrorImage(this.options.orgId, {
|
|
4736
|
+
baseUrl: this.options.baseUrl || defaultBaseUrl(),
|
|
4737
|
+
message: error.message,
|
|
4738
|
+
code: error.code,
|
|
4739
|
+
req_id: error.response?.req_id,
|
|
4740
|
+
});
|
|
4741
|
+
}
|
|
4742
|
+
if (!this.destroyed) {
|
|
4743
|
+
callHook('onResult', this.options.onResult, result);
|
|
4744
|
+
}
|
|
4745
|
+
}
|
|
4746
|
+
}
|
|
4747
|
+
function validate(container, options) {
|
|
4748
|
+
if (typeof HTMLElement === 'undefined' ||
|
|
4749
|
+
!(container instanceof HTMLElement)) {
|
|
4750
|
+
throw new ValidationError('container', 'must be an HTMLElement', container);
|
|
4751
|
+
}
|
|
4752
|
+
for (const field of ['orgId', 'priceId', 'externalId']) {
|
|
4753
|
+
if (!options?.[field]) {
|
|
4754
|
+
throw new ValidationError(field, 'is required', options?.[field]);
|
|
4755
|
+
}
|
|
4756
|
+
}
|
|
4757
|
+
if (typeof options.onResult !== 'function') {
|
|
4758
|
+
throw new ValidationError('onResult', 'must be a function', options.onResult);
|
|
4759
|
+
}
|
|
4760
|
+
}
|
|
4761
|
+
/**
|
|
4762
|
+
* Mounts Primer's native Apple Pay / Google Pay button for a one-click upsell. Rejects only with
|
|
4763
|
+
* `ValidationError`; any `mounted: false` leaves nothing in `container`.
|
|
4764
|
+
*/
|
|
4765
|
+
async function mountWalletUpsell(container, options) {
|
|
4766
|
+
validate(container, options);
|
|
4767
|
+
let session;
|
|
4768
|
+
try {
|
|
4769
|
+
session = await prepareSession(WalletUpsell.apiClientFor(options), options);
|
|
4770
|
+
}
|
|
4771
|
+
catch (error) {
|
|
4772
|
+
return {
|
|
4773
|
+
mounted: false,
|
|
4774
|
+
reason: 'error',
|
|
4775
|
+
error: error instanceof Error
|
|
4776
|
+
? error
|
|
4777
|
+
: toError('one_click_wallet_session failed', error),
|
|
4778
|
+
};
|
|
4779
|
+
}
|
|
4780
|
+
if (!session) {
|
|
4781
|
+
return { mounted: false, reason: 'not_wallet' };
|
|
4782
|
+
}
|
|
4783
|
+
const upsell = new WalletUpsell(container, options, session);
|
|
4784
|
+
try {
|
|
4785
|
+
if (!(await upsell.render())) {
|
|
4786
|
+
upsell.destroy();
|
|
4787
|
+
return {
|
|
4788
|
+
mounted: false,
|
|
4789
|
+
reason: 'wallet_unavailable',
|
|
4790
|
+
wallet: session.wallet,
|
|
4791
|
+
};
|
|
4792
|
+
}
|
|
4793
|
+
}
|
|
4794
|
+
catch (error) {
|
|
4795
|
+
upsell.destroy();
|
|
4796
|
+
return {
|
|
4797
|
+
mounted: false,
|
|
4798
|
+
reason: 'error',
|
|
4799
|
+
error: toError('Failed to mount the wallet button', error),
|
|
4800
|
+
};
|
|
4801
|
+
}
|
|
4802
|
+
upsell.watchVisibility();
|
|
4803
|
+
return {
|
|
4804
|
+
mounted: true,
|
|
4805
|
+
wallet: session.wallet,
|
|
4806
|
+
setDisabled: (disabled) => upsell.setDisabled(disabled),
|
|
4807
|
+
destroy: () => upsell.destroy(),
|
|
4808
|
+
};
|
|
4809
|
+
}
|
|
4810
|
+
|
|
4042
4811
|
/**
|
|
4043
4812
|
* @fileoverview Main entry point for @funnelfox/billing
|
|
4044
4813
|
*/
|
|
@@ -4048,6 +4817,7 @@
|
|
|
4048
4817
|
createClientSession: createClientSession,
|
|
4049
4818
|
initMethod: initMethod,
|
|
4050
4819
|
silentPurchase: silentPurchase,
|
|
4820
|
+
mountWalletUpsell,
|
|
4051
4821
|
getAvailablePaymentMethods: getAvailablePaymentMethods,
|
|
4052
4822
|
stripe: {
|
|
4053
4823
|
createCardForm: createStripeCardForm,
|
|
@@ -4880,6 +5650,7 @@
|
|
|
4880
5650
|
exports.createClientSession = createClientSession;
|
|
4881
5651
|
exports.default = Billing;
|
|
4882
5652
|
exports.getAvailablePaymentMethods = getAvailablePaymentMethods;
|
|
5653
|
+
exports.mountWalletUpsell = mountWalletUpsell;
|
|
4883
5654
|
|
|
4884
5655
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4885
5656
|
|