@funnelfox/billing 0.13.1 → 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 +992 -42
- package/dist/chunk-index.es.js +992 -43
- package/dist/funnelfox-billing.cjs.js +1 -0
- package/dist/funnelfox-billing.esm.js +1 -1
- package/dist/funnelfox-billing.js +992 -42
- package/dist/funnelfox-billing.min.js +1 -1
- package/package.json +2 -2
- package/src/types.d.ts +128 -18
|
@@ -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',
|
|
@@ -1381,11 +1382,13 @@
|
|
|
1381
1382
|
}
|
|
1382
1383
|
}
|
|
1383
1384
|
/**
|
|
1384
|
-
* Sends the request exactly once: no retry and no client timeout
|
|
1385
|
+
* Sends the request exactly once: no retry and, without `timeoutMs`, no client timeout
|
|
1386
|
+
* (FFB-704, decision 9).
|
|
1385
1387
|
* Same URL, headers and error classes as `request()`. Used only by `silentPurchase`'s
|
|
1386
|
-
* `handle3DS` mode for `one_click` and `resume_payment`, which must not be POSTed twice
|
|
1388
|
+
* `handle3DS` mode for `one_click` and `resume_payment`, which must not be POSTed twice;
|
|
1389
|
+
* `one_click` is re-sent by the caller only with the same idempotency key.
|
|
1387
1390
|
*/
|
|
1388
|
-
async requestOnce(endpoint, options = {}) {
|
|
1391
|
+
async requestOnce(endpoint, options = {}, timeoutMs) {
|
|
1389
1392
|
const url = `${this.baseUrl}/${this.orgId}${endpoint}`;
|
|
1390
1393
|
const requestOptions = {
|
|
1391
1394
|
method: 'GET',
|
|
@@ -1397,7 +1400,10 @@
|
|
|
1397
1400
|
...options,
|
|
1398
1401
|
};
|
|
1399
1402
|
try {
|
|
1400
|
-
|
|
1403
|
+
const request = this._makeRequest(url, requestOptions);
|
|
1404
|
+
return await (timeoutMs
|
|
1405
|
+
? withTimeout(request, timeoutMs, 'Request timed out')
|
|
1406
|
+
: request);
|
|
1401
1407
|
}
|
|
1402
1408
|
catch (error) {
|
|
1403
1409
|
if (error instanceof APIError) {
|
|
@@ -1603,11 +1609,18 @@
|
|
|
1603
1609
|
}));
|
|
1604
1610
|
}
|
|
1605
1611
|
/** `one_click`, POSTed exactly once (see `requestOnce`). */
|
|
1606
|
-
async oneClickOnce(payload) {
|
|
1612
|
+
async oneClickOnce(payload, timeoutMs) {
|
|
1607
1613
|
return (await this.requestOnce(API_ENDPOINTS.ONE_CLICK, {
|
|
1608
1614
|
method: 'POST',
|
|
1609
1615
|
body: JSON.stringify(payload),
|
|
1610
|
-
}));
|
|
1616
|
+
}, timeoutMs));
|
|
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));
|
|
1611
1624
|
}
|
|
1612
1625
|
/** `resume_payment`, POSTed exactly once (see `requestOnce`). */
|
|
1613
1626
|
async resumePaymentOnce(params) {
|
|
@@ -2160,6 +2173,9 @@
|
|
|
2160
2173
|
this.counter = 0;
|
|
2161
2174
|
this.cachedSessionResponse = null;
|
|
2162
2175
|
this.taxRecalcSeq = 0;
|
|
2176
|
+
// WebKit allows one ApplePaySession per page: a second tap while the sheet is
|
|
2177
|
+
// still opening throws InvalidAccessError, but Primer asks to start first.
|
|
2178
|
+
this.isApplePaySheetOpen = false;
|
|
2163
2179
|
this.cardSessionFieldConfig = {};
|
|
2164
2180
|
this.isTelemetryEnabled = false;
|
|
2165
2181
|
this.telemetryCleanup = null;
|
|
@@ -2326,6 +2342,7 @@
|
|
|
2326
2342
|
primerHandler.handleFailure(error?.message || 'Payment processing failed');
|
|
2327
2343
|
}
|
|
2328
2344
|
finally {
|
|
2345
|
+
this.isApplePaySheetOpen = false;
|
|
2329
2346
|
this.onLoaderChangeWithRace(false);
|
|
2330
2347
|
this._setState('ready');
|
|
2331
2348
|
}
|
|
@@ -2351,6 +2368,7 @@
|
|
|
2351
2368
|
primerHandler.handleFailure(error?.message || 'Payment processing failed');
|
|
2352
2369
|
}
|
|
2353
2370
|
finally {
|
|
2371
|
+
this.isApplePaySheetOpen = false;
|
|
2354
2372
|
this.emit(EVENTS.PURCHASE_COMPLETED);
|
|
2355
2373
|
this.onLoaderChangeWithRace(false);
|
|
2356
2374
|
this._setState('ready');
|
|
@@ -2679,6 +2697,7 @@
|
|
|
2679
2697
|
onTokenizeSuccess: this.handleTokenizeSuccess,
|
|
2680
2698
|
onResumeSuccess: this.handleResumeSuccess,
|
|
2681
2699
|
onResumeError: error => {
|
|
2700
|
+
this.isApplePaySheetOpen = false;
|
|
2682
2701
|
if (error.stack?.includes('PROCESSOR_3DS') &&
|
|
2683
2702
|
error.code === 'RESUME_ERROR' &&
|
|
2684
2703
|
error.message?.includes('fetch resume key')) {
|
|
@@ -2688,15 +2707,23 @@
|
|
|
2688
2707
|
this.emit(EVENTS.PURCHASE_FAILURE, error);
|
|
2689
2708
|
},
|
|
2690
2709
|
onCheckoutFail: error => {
|
|
2710
|
+
this.isApplePaySheetOpen = false;
|
|
2691
2711
|
this.emit(EVENTS.PURCHASE_FAILURE, error);
|
|
2692
2712
|
},
|
|
2693
2713
|
onTokenizeError: error => {
|
|
2714
|
+
this.isApplePaySheetOpen = false;
|
|
2694
2715
|
this.emit(EVENTS.PURCHASE_FAILURE, error);
|
|
2695
2716
|
},
|
|
2696
2717
|
onTokenizeStart: () => {
|
|
2697
2718
|
wasPaymentProcessedStarted = true;
|
|
2698
2719
|
},
|
|
2699
2720
|
onTokenizeShouldStart: data => {
|
|
2721
|
+
if (data.paymentMethodType === exports.PaymentMethod.APPLE_PAY) {
|
|
2722
|
+
if (this.isApplePaySheetOpen) {
|
|
2723
|
+
return false;
|
|
2724
|
+
}
|
|
2725
|
+
this.isApplePaySheetOpen = true;
|
|
2726
|
+
}
|
|
2700
2727
|
this.activePaymentMethodType = data.paymentMethodType;
|
|
2701
2728
|
this.emit(EVENTS.ERROR, undefined);
|
|
2702
2729
|
this.emit(EVENTS.START_PURCHASE, data.paymentMethodType);
|
|
@@ -2708,6 +2735,7 @@
|
|
|
2708
2735
|
this.emit(EVENTS.ERROR, undefined);
|
|
2709
2736
|
break;
|
|
2710
2737
|
case 'PAYMENT_METHOD_UNSELECTED':
|
|
2738
|
+
this.isApplePaySheetOpen = false;
|
|
2711
2739
|
if (!wasPaymentProcessedStarted) {
|
|
2712
2740
|
this.emit(EVENTS.PURCHASE_CANCELLED);
|
|
2713
2741
|
}
|
|
@@ -2987,11 +3015,23 @@
|
|
|
2987
3015
|
* checkout, tokenize a vaulted instrument through the vault manager as a stand-in, and in
|
|
2988
3016
|
* `onTokenizeSuccess` continue with the one-click `action_required_token` instead. Primer then
|
|
2989
3017
|
* shows its 3DS overlay; `onResumeSuccess` yields the resume token for `resume_payment`.
|
|
3018
|
+
*
|
|
3019
|
+
* With processor 3DS (`PROCESSOR_3DS`) Primer frames the processor's hosted 3DS page and cannot
|
|
3020
|
+
* tell a frictionless check from a challenge, and nothing reaches the page from the cross-origin
|
|
3021
|
+
* frame (FFB-758 spike). A frictionless check finishes within ~5 s, so the overlay stays
|
|
3022
|
+
* invisible for `THREE_DS_OVERLAY_REVEAL_DELAY_MS` and only then counts as a shown challenge.
|
|
2990
3023
|
*/
|
|
2991
3024
|
/** Primer client-token intents that carry a 3DS step. */
|
|
2992
3025
|
const PRIMER_3DS_INTENTS = ['3DS_AUTHENTICATION', 'PROCESSOR_3DS'];
|
|
2993
3026
|
/** Default `threeDSTimeoutMs`. */
|
|
2994
3027
|
const DEFAULT_THREE_DS_TIMEOUT_MS = 600000;
|
|
3028
|
+
/**
|
|
3029
|
+
* How long Primer's 3DS overlay stays invisible. Frictionless checks on billing-dev took
|
|
3030
|
+
* 4.2-5.0 s from overlay to resume; a real challenge is shown this much later.
|
|
3031
|
+
*/
|
|
3032
|
+
const THREE_DS_OVERLAY_REVEAL_DELAY_MS = 6000;
|
|
3033
|
+
/** Primer's 3DS overlay (Primer Web SDK 2.57.3, verified in the FFB-758 spike). */
|
|
3034
|
+
const PRIMER_OVERLAY_SELECTOR = '#primer-overlay';
|
|
2995
3035
|
/** Close button of Primer's 3DS overlay (Primer Web SDK 2.57.3, verified in the spike). */
|
|
2996
3036
|
const PRIMER_OVERLAY_CLOSE_SELECTOR = '[data-testid="OverlayContent.closeButton"]';
|
|
2997
3037
|
/** Decodes the `intent` of a Primer client token (a JWT); `null` if it is not one. */
|
|
@@ -3035,10 +3075,30 @@
|
|
|
3035
3075
|
const button = document.querySelector(PRIMER_OVERLAY_CLOSE_SELECTOR);
|
|
3036
3076
|
button?.click();
|
|
3037
3077
|
}
|
|
3078
|
+
/** Hides Primer's overlay until the returned function is called. */
|
|
3079
|
+
function hidePrimerOverlay() {
|
|
3080
|
+
if (typeof document === 'undefined') {
|
|
3081
|
+
return () => { };
|
|
3082
|
+
}
|
|
3083
|
+
const style = document.createElement('style');
|
|
3084
|
+
style.textContent = `${PRIMER_OVERLAY_SELECTOR}{opacity:0!important;pointer-events:none!important}`;
|
|
3085
|
+
document.head.appendChild(style);
|
|
3086
|
+
return () => style.remove();
|
|
3087
|
+
}
|
|
3088
|
+
/**
|
|
3089
|
+
* A Primer update that renames the overlay would leave it visible during the hidden phase, while
|
|
3090
|
+
* `threeDSChallenged` stays false for a check that finishes in that time.
|
|
3091
|
+
*/
|
|
3092
|
+
function warnIfOverlayNotFound() {
|
|
3093
|
+
if (typeof document !== 'undefined' &&
|
|
3094
|
+
!document.querySelector(PRIMER_OVERLAY_SELECTOR)) {
|
|
3095
|
+
console.warn(`[FunnelfoxSDK] Primer 3DS overlay ${PRIMER_OVERLAY_SELECTOR} not found`);
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3038
3098
|
function toPrimerError(message, error) {
|
|
3039
3099
|
return error instanceof PrimerError ? error : new PrimerError(message, error);
|
|
3040
3100
|
}
|
|
3041
|
-
function callSafely(fn) {
|
|
3101
|
+
function callSafely$1(fn) {
|
|
3042
3102
|
try {
|
|
3043
3103
|
fn();
|
|
3044
3104
|
}
|
|
@@ -3065,6 +3125,9 @@
|
|
|
3065
3125
|
return new Promise(resolve => {
|
|
3066
3126
|
let settled = false;
|
|
3067
3127
|
let tokenized = false;
|
|
3128
|
+
let revealed = false;
|
|
3129
|
+
let revealTimer = null;
|
|
3130
|
+
let showOverlay = () => { };
|
|
3068
3131
|
// True only while create_client_session is in flight.
|
|
3069
3132
|
let sessionInFlight = false;
|
|
3070
3133
|
let timer = null;
|
|
@@ -3078,12 +3141,17 @@
|
|
|
3078
3141
|
clearTimeout(timer);
|
|
3079
3142
|
timer = null;
|
|
3080
3143
|
}
|
|
3144
|
+
if (revealTimer) {
|
|
3145
|
+
clearTimeout(revealTimer);
|
|
3146
|
+
revealTimer = null;
|
|
3147
|
+
}
|
|
3148
|
+
showOverlay();
|
|
3081
3149
|
headless = null;
|
|
3082
3150
|
resolve(result);
|
|
3083
3151
|
};
|
|
3084
|
-
|
|
3152
|
+
const onTimeout = () => {
|
|
3085
3153
|
timer = null;
|
|
3086
|
-
if (
|
|
3154
|
+
if (revealed) {
|
|
3087
3155
|
// The challenge was shown and the customer did not finish it.
|
|
3088
3156
|
settle({ kind: 'abandoned' });
|
|
3089
3157
|
// Close Primer's overlay; the close error it raises is ignored (already settled).
|
|
@@ -3099,23 +3167,43 @@
|
|
|
3099
3167
|
});
|
|
3100
3168
|
return;
|
|
3101
3169
|
}
|
|
3170
|
+
if (tokenized) {
|
|
3171
|
+
// The overlay is about to be shown: the reveal restarts the timer.
|
|
3172
|
+
return;
|
|
3173
|
+
}
|
|
3102
3174
|
settle({
|
|
3103
3175
|
kind: 'error',
|
|
3104
3176
|
error: new PrimerError('Timed out before the 3DS challenge was shown (threeDSTimeoutMs)'),
|
|
3105
3177
|
});
|
|
3106
|
-
}
|
|
3178
|
+
};
|
|
3179
|
+
timer = setTimeout(onTimeout, timeoutMs);
|
|
3107
3180
|
const onTokenizeSuccess = (_data, handler) => {
|
|
3108
|
-
|
|
3109
|
-
|
|
3181
|
+
// A repeated tokenization would start a second reveal timer, so the challenge
|
|
3182
|
+
// (and the caller's onThreeDSStart) would be reported twice: refuse it.
|
|
3183
|
+
if (settled || tokenized) {
|
|
3184
|
+
callSafely$1(() => handler.handleFailure());
|
|
3110
3185
|
return;
|
|
3111
3186
|
}
|
|
3112
3187
|
tokenized = true;
|
|
3188
|
+
showOverlay = hidePrimerOverlay();
|
|
3189
|
+
revealTimer = setTimeout(() => {
|
|
3190
|
+
revealTimer = null;
|
|
3191
|
+
revealed = true;
|
|
3192
|
+
showOverlay();
|
|
3193
|
+
warnIfOverlayNotFound();
|
|
3194
|
+
callSafely$1(params.onChallengeShown);
|
|
3195
|
+
// The whole timeout belongs to the challenge the customer can see.
|
|
3196
|
+
if (timer) {
|
|
3197
|
+
clearTimeout(timer);
|
|
3198
|
+
}
|
|
3199
|
+
timer = setTimeout(onTimeout, timeoutMs);
|
|
3200
|
+
}, params.revealDelayMs);
|
|
3113
3201
|
// Ignore the stand-in vaulted token: run the one-click 3DS action instead.
|
|
3114
3202
|
handler.continueWithNewClientToken(actionToken);
|
|
3115
3203
|
};
|
|
3116
3204
|
const onResumeSuccess = async (data, handler) => {
|
|
3117
3205
|
if (settled) {
|
|
3118
|
-
callSafely(() => handler.handleFailure());
|
|
3206
|
+
callSafely$1(() => handler.handleFailure());
|
|
3119
3207
|
return;
|
|
3120
3208
|
}
|
|
3121
3209
|
// The challenge is over: the timeout bounds only the challenge, never the POST.
|
|
@@ -3123,6 +3211,10 @@
|
|
|
3123
3211
|
clearTimeout(timer);
|
|
3124
3212
|
timer = null;
|
|
3125
3213
|
}
|
|
3214
|
+
if (revealTimer) {
|
|
3215
|
+
clearTimeout(revealTimer);
|
|
3216
|
+
revealTimer = null;
|
|
3217
|
+
}
|
|
3126
3218
|
let response;
|
|
3127
3219
|
try {
|
|
3128
3220
|
response = await apiClient.resumePaymentOnce({
|
|
@@ -3131,7 +3223,7 @@
|
|
|
3131
3223
|
});
|
|
3132
3224
|
}
|
|
3133
3225
|
catch (error) {
|
|
3134
|
-
callSafely(() => handler.handleFailure());
|
|
3226
|
+
callSafely$1(() => handler.handleFailure());
|
|
3135
3227
|
settle({
|
|
3136
3228
|
kind: 'error',
|
|
3137
3229
|
error: error,
|
|
@@ -3143,11 +3235,11 @@
|
|
|
3143
3235
|
if (response?.status === 'success' &&
|
|
3144
3236
|
resumeData?.checkout_status === 'succeeded' &&
|
|
3145
3237
|
!resumeData.action_required_token) {
|
|
3146
|
-
callSafely(() => handler.handleSuccess());
|
|
3238
|
+
callSafely$1(() => handler.handleSuccess());
|
|
3147
3239
|
}
|
|
3148
3240
|
else {
|
|
3149
3241
|
// A chained action token is never continued (no second challenge).
|
|
3150
|
-
callSafely(() => handler.handleFailure(resumeData?.failed_message_for_user || undefined));
|
|
3242
|
+
callSafely$1(() => handler.handleFailure(resumeData?.failed_message_for_user || undefined));
|
|
3151
3243
|
}
|
|
3152
3244
|
settle({ kind: 'resumed', response });
|
|
3153
3245
|
};
|
|
@@ -3256,6 +3348,10 @@
|
|
|
3256
3348
|
function configure(config) {
|
|
3257
3349
|
defaultConfig = config;
|
|
3258
3350
|
}
|
|
3351
|
+
/** The base URL from `configure()`, else the production one. */
|
|
3352
|
+
function defaultBaseUrl() {
|
|
3353
|
+
return defaultConfig?.baseUrl || DEFAULTS.BASE_URL;
|
|
3354
|
+
}
|
|
3259
3355
|
function resolveConfig(options, functionName) {
|
|
3260
3356
|
const { orgId, apiConfig } = options || {};
|
|
3261
3357
|
const finalOrgId = orgId || defaultConfig?.orgId;
|
|
@@ -3330,9 +3426,6 @@
|
|
|
3330
3426
|
if (options.citCardNetworks !== undefined) {
|
|
3331
3427
|
payload.cit_card_networks = options.citCardNetworks;
|
|
3332
3428
|
}
|
|
3333
|
-
if (options.citGooglePayPanOnly !== undefined) {
|
|
3334
|
-
payload.cit_google_pay_pan_only = options.citGooglePayPanOnly;
|
|
3335
|
-
}
|
|
3336
3429
|
return payload;
|
|
3337
3430
|
}
|
|
3338
3431
|
function silentPurchase(options) {
|
|
@@ -3376,7 +3469,84 @@
|
|
|
3376
3469
|
}
|
|
3377
3470
|
const ONE_CLICK_IN_PROGRESS = 'one_click_in_progress';
|
|
3378
3471
|
const DOUBLE_PURCHASE = 'double_purchase';
|
|
3472
|
+
const IDEMPOTENCY_KEY_IN_PROGRESS = 'idempotency_key_in_progress';
|
|
3473
|
+
const ONE_CLICK_RETRY_DELAYS_MS = [1000, 2000, 4000];
|
|
3474
|
+
/** Longer than the backend's wait for the user's lock (~12.6 s). */
|
|
3475
|
+
const ONE_CLICK_TIMEOUT_MS = 25000;
|
|
3476
|
+
/** org + external id of the customers with a one-click purchase running (silent or wallet). */
|
|
3379
3477
|
const oneClickInFlight = new Set();
|
|
3478
|
+
function oneClickInFlightKey(orgId, externalId) {
|
|
3479
|
+
return `${orgId}\u0000${externalId}`;
|
|
3480
|
+
}
|
|
3481
|
+
/** A random UUID v4, or `null` when the browser has no crypto (then one_click is not retried). */
|
|
3482
|
+
function createIdempotencyKey() {
|
|
3483
|
+
const cryptoApi = typeof crypto === 'undefined' ? undefined : crypto;
|
|
3484
|
+
if (typeof cryptoApi?.randomUUID === 'function') {
|
|
3485
|
+
return cryptoApi.randomUUID();
|
|
3486
|
+
}
|
|
3487
|
+
if (typeof cryptoApi?.getRandomValues !== 'function') {
|
|
3488
|
+
return null;
|
|
3489
|
+
}
|
|
3490
|
+
const bytes = cryptoApi.getRandomValues(new Uint8Array(16));
|
|
3491
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
3492
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
3493
|
+
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0'));
|
|
3494
|
+
return [
|
|
3495
|
+
hex.slice(0, 4).join(''),
|
|
3496
|
+
hex.slice(4, 6).join(''),
|
|
3497
|
+
hex.slice(6, 8).join(''),
|
|
3498
|
+
hex.slice(8, 10).join(''),
|
|
3499
|
+
hex.slice(10, 16).join(''),
|
|
3500
|
+
].join('-');
|
|
3501
|
+
}
|
|
3502
|
+
/**
|
|
3503
|
+
* Failures after which the backend may or may not have processed the request. Retrying them
|
|
3504
|
+
* with the same idempotency key is safe: the backend never charges a key twice (FFB-757).
|
|
3505
|
+
* A 2xx APIError means the body could not be parsed.
|
|
3506
|
+
*/
|
|
3507
|
+
function isRetryableOneClickError(error) {
|
|
3508
|
+
if (error instanceof NetworkError) {
|
|
3509
|
+
return true;
|
|
3510
|
+
}
|
|
3511
|
+
if (!(error instanceof APIError) || error.statusCode === null) {
|
|
3512
|
+
return false;
|
|
3513
|
+
}
|
|
3514
|
+
if (error.statusCode >= 500) {
|
|
3515
|
+
return true;
|
|
3516
|
+
}
|
|
3517
|
+
if (error.statusCode >= 200 && error.statusCode < 300) {
|
|
3518
|
+
return true;
|
|
3519
|
+
}
|
|
3520
|
+
return (error.statusCode === 400 && error.errorCode === IDEMPOTENCY_KEY_IN_PROGRESS);
|
|
3521
|
+
}
|
|
3522
|
+
function isEmptyResponse(response) {
|
|
3523
|
+
return !response || typeof response !== 'object' || !('status' in response);
|
|
3524
|
+
}
|
|
3525
|
+
/** `one_click`, retried with the same idempotency key; sent once when there is no key. */
|
|
3526
|
+
async function postOneClick(apiClient, payload) {
|
|
3527
|
+
if (!payload.idempotency_key) {
|
|
3528
|
+
return apiClient.oneClickOnce(payload);
|
|
3529
|
+
}
|
|
3530
|
+
for (const delay of ONE_CLICK_RETRY_DELAYS_MS) {
|
|
3531
|
+
try {
|
|
3532
|
+
const response = await apiClient.oneClickOnce(payload, ONE_CLICK_TIMEOUT_MS);
|
|
3533
|
+
if (!isEmptyResponse(response)) {
|
|
3534
|
+
return response;
|
|
3535
|
+
}
|
|
3536
|
+
}
|
|
3537
|
+
catch (error) {
|
|
3538
|
+
if (!isRetryableOneClickError(error)) {
|
|
3539
|
+
throw error;
|
|
3540
|
+
}
|
|
3541
|
+
}
|
|
3542
|
+
await sleep(delay);
|
|
3543
|
+
}
|
|
3544
|
+
const response = await apiClient.oneClickOnce(payload, ONE_CLICK_TIMEOUT_MS);
|
|
3545
|
+
if (isEmptyResponse(response)) {
|
|
3546
|
+
throw new NetworkError('one_click returned an empty response');
|
|
3547
|
+
}
|
|
3548
|
+
return response;
|
|
3549
|
+
}
|
|
3380
3550
|
function declineFields(data) {
|
|
3381
3551
|
return {
|
|
3382
3552
|
failedMessageForUser: data.failed_message_for_user ?? null,
|
|
@@ -3465,7 +3635,7 @@
|
|
|
3465
3635
|
}
|
|
3466
3636
|
}
|
|
3467
3637
|
/** Maps a `resume_payment` response after a challenge (rows 5, 6, 6b, 6c, 7, 8). */
|
|
3468
|
-
function mapResumeResponse(response, orderId, threeDSAuthDeclineCodes = THREE_DS_AUTH_DECLINE_CODES) {
|
|
3638
|
+
function mapResumeResponse(response, orderId, threeDSChallenged, threeDSAuthDeclineCodes = THREE_DS_AUTH_DECLINE_CODES) {
|
|
3469
3639
|
const data = response?.data;
|
|
3470
3640
|
if (response?.status !== 'success' || !data) {
|
|
3471
3641
|
return {
|
|
@@ -3473,7 +3643,7 @@
|
|
|
3473
3643
|
result: {
|
|
3474
3644
|
outcome: 'error',
|
|
3475
3645
|
orderId,
|
|
3476
|
-
threeDSChallenged
|
|
3646
|
+
threeDSChallenged,
|
|
3477
3647
|
error: envelopeError(response, 'Resume payment failed'),
|
|
3478
3648
|
failedRequest: 'resume_payment',
|
|
3479
3649
|
},
|
|
@@ -3486,7 +3656,7 @@
|
|
|
3486
3656
|
result: {
|
|
3487
3657
|
outcome: 'error',
|
|
3488
3658
|
orderId,
|
|
3489
|
-
threeDSChallenged
|
|
3659
|
+
threeDSChallenged,
|
|
3490
3660
|
error: new FunnefoxSDKError('resume_payment returned another action token; chained 3DS is not supported', 'one_click_chained_action'),
|
|
3491
3661
|
},
|
|
3492
3662
|
};
|
|
@@ -3495,22 +3665,24 @@
|
|
|
3495
3665
|
case 'succeeded':
|
|
3496
3666
|
return {
|
|
3497
3667
|
threeDS: 'passed',
|
|
3498
|
-
result: { outcome: 'succeeded', orderId, threeDSChallenged
|
|
3668
|
+
result: { outcome: 'succeeded', orderId, threeDSChallenged },
|
|
3499
3669
|
};
|
|
3500
3670
|
case 'processing':
|
|
3501
3671
|
return {
|
|
3502
3672
|
threeDS: 'passed',
|
|
3503
|
-
result: { outcome: 'processing', orderId, threeDSChallenged
|
|
3673
|
+
result: { outcome: 'processing', orderId, threeDSChallenged },
|
|
3504
3674
|
};
|
|
3505
3675
|
case 'failed': {
|
|
3506
|
-
|
|
3507
|
-
|
|
3676
|
+
// Without a shown challenge the customer saw no 3DS step: a decline is a plain decline.
|
|
3677
|
+
const authFailed = threeDSChallenged &&
|
|
3678
|
+
(threeDSAuthDeclineCodes === null ||
|
|
3679
|
+
threeDSAuthDeclineCodes.includes(data.decline_code ?? ''));
|
|
3508
3680
|
return {
|
|
3509
3681
|
threeDS: authFailed ? 'failed' : 'passed',
|
|
3510
3682
|
result: {
|
|
3511
3683
|
outcome: authFailed ? '3ds_failed' : 'declined',
|
|
3512
3684
|
orderId,
|
|
3513
|
-
threeDSChallenged
|
|
3685
|
+
threeDSChallenged,
|
|
3514
3686
|
...declineFields(data),
|
|
3515
3687
|
},
|
|
3516
3688
|
};
|
|
@@ -3521,7 +3693,7 @@
|
|
|
3521
3693
|
result: {
|
|
3522
3694
|
outcome: 'error',
|
|
3523
3695
|
orderId,
|
|
3524
|
-
threeDSChallenged
|
|
3696
|
+
threeDSChallenged,
|
|
3525
3697
|
error: new APIError(data.checkout_status === 'cancelled'
|
|
3526
3698
|
? 'Payment was cancelled'
|
|
3527
3699
|
: `Unhandled checkout status: ${data.checkout_status}`, null, { response }),
|
|
@@ -3529,30 +3701,30 @@
|
|
|
3529
3701
|
};
|
|
3530
3702
|
}
|
|
3531
3703
|
}
|
|
3532
|
-
function mapThreeDSRun(run, orderId) {
|
|
3704
|
+
function mapThreeDSRun(run, orderId, threeDSChallenged) {
|
|
3533
3705
|
switch (run.kind) {
|
|
3534
3706
|
case 'resumed':
|
|
3535
|
-
return mapResumeResponse(run.response, orderId);
|
|
3707
|
+
return mapResumeResponse(run.response, orderId, threeDSChallenged);
|
|
3536
3708
|
case 'failed':
|
|
3537
3709
|
return {
|
|
3538
3710
|
threeDS: 'failed',
|
|
3539
3711
|
result: {
|
|
3540
3712
|
outcome: '3ds_failed',
|
|
3541
3713
|
orderId,
|
|
3542
|
-
threeDSChallenged
|
|
3714
|
+
threeDSChallenged,
|
|
3543
3715
|
error: run.error,
|
|
3544
3716
|
},
|
|
3545
3717
|
};
|
|
3546
3718
|
case 'abandoned':
|
|
3547
3719
|
return {
|
|
3548
3720
|
threeDS: 'abandoned',
|
|
3549
|
-
result: { outcome: '3ds_abandoned', orderId, threeDSChallenged
|
|
3721
|
+
result: { outcome: '3ds_abandoned', orderId, threeDSChallenged },
|
|
3550
3722
|
};
|
|
3551
3723
|
default: {
|
|
3552
3724
|
const result = {
|
|
3553
3725
|
outcome: 'error',
|
|
3554
3726
|
orderId,
|
|
3555
|
-
threeDSChallenged
|
|
3727
|
+
threeDSChallenged,
|
|
3556
3728
|
error: run.error,
|
|
3557
3729
|
};
|
|
3558
3730
|
if (run.failedRequest) {
|
|
@@ -3571,7 +3743,7 @@
|
|
|
3571
3743
|
}
|
|
3572
3744
|
catch (error) {
|
|
3573
3745
|
// eslint-disable-next-line no-console
|
|
3574
|
-
console.error(`[FunnelfoxSDK]
|
|
3746
|
+
console.error(`[FunnelfoxSDK] ${name} hook threw`, error);
|
|
3575
3747
|
}
|
|
3576
3748
|
}
|
|
3577
3749
|
async function silentPurchaseDetailed(options) {
|
|
@@ -3581,7 +3753,7 @@
|
|
|
3581
3753
|
}
|
|
3582
3754
|
}
|
|
3583
3755
|
const { orgId, baseUrl, externalId } = options;
|
|
3584
|
-
const inFlightKey =
|
|
3756
|
+
const inFlightKey = oneClickInFlightKey(orgId, externalId);
|
|
3585
3757
|
if (oneClickInFlight.has(inFlightKey)) {
|
|
3586
3758
|
return {
|
|
3587
3759
|
outcome: 'error',
|
|
@@ -3615,9 +3787,14 @@
|
|
|
3615
3787
|
timeout: DEFAULTS.REQUEST_TIMEOUT,
|
|
3616
3788
|
retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
|
|
3617
3789
|
});
|
|
3790
|
+
const payload = buildOneClickRequest(options);
|
|
3791
|
+
const idempotencyKey = createIdempotencyKey();
|
|
3792
|
+
if (idempotencyKey) {
|
|
3793
|
+
payload.idempotency_key = idempotencyKey;
|
|
3794
|
+
}
|
|
3618
3795
|
let response;
|
|
3619
3796
|
try {
|
|
3620
|
-
response = await apiClient
|
|
3797
|
+
response = await postOneClick(apiClient, payload);
|
|
3621
3798
|
}
|
|
3622
3799
|
catch (error) {
|
|
3623
3800
|
if (error instanceof APIError &&
|
|
@@ -3628,6 +3805,16 @@
|
|
|
3628
3805
|
threeDSChallenged: false,
|
|
3629
3806
|
};
|
|
3630
3807
|
}
|
|
3808
|
+
if (idempotencyKey && isRetryableOneClickError(error)) {
|
|
3809
|
+
// The charge may have gone through: the caller must not start a new payment.
|
|
3810
|
+
return {
|
|
3811
|
+
outcome: 'processing',
|
|
3812
|
+
orderId: null,
|
|
3813
|
+
threeDSChallenged: false,
|
|
3814
|
+
error: error,
|
|
3815
|
+
failedRequest: 'one_click',
|
|
3816
|
+
};
|
|
3817
|
+
}
|
|
3631
3818
|
return {
|
|
3632
3819
|
outcome: 'error',
|
|
3633
3820
|
orderId: null,
|
|
@@ -3641,15 +3828,15 @@
|
|
|
3641
3828
|
return step.result;
|
|
3642
3829
|
}
|
|
3643
3830
|
const { orderId, actionToken } = step;
|
|
3831
|
+
let challenged = false;
|
|
3644
3832
|
let ended = false;
|
|
3645
3833
|
const end = (result) => {
|
|
3646
|
-
if (ended) {
|
|
3834
|
+
if (!challenged || ended) {
|
|
3647
3835
|
return;
|
|
3648
3836
|
}
|
|
3649
3837
|
ended = true;
|
|
3650
3838
|
callHook('onThreeDSEnd', options.onThreeDSEnd, { result });
|
|
3651
3839
|
};
|
|
3652
|
-
callHook('onThreeDSStart', options.onThreeDSStart, { orderId });
|
|
3653
3840
|
try {
|
|
3654
3841
|
const run = await runOneClickThreeDS({
|
|
3655
3842
|
apiClient,
|
|
@@ -3659,8 +3846,13 @@
|
|
|
3659
3846
|
orderId,
|
|
3660
3847
|
actionToken,
|
|
3661
3848
|
timeoutMs: options.threeDSTimeoutMs ?? DEFAULT_THREE_DS_TIMEOUT_MS,
|
|
3849
|
+
revealDelayMs: THREE_DS_OVERLAY_REVEAL_DELAY_MS,
|
|
3850
|
+
onChallengeShown: () => {
|
|
3851
|
+
challenged = true;
|
|
3852
|
+
callHook('onThreeDSStart', options.onThreeDSStart, { orderId });
|
|
3853
|
+
},
|
|
3662
3854
|
});
|
|
3663
|
-
const mapped = mapThreeDSRun(run, orderId);
|
|
3855
|
+
const mapped = mapThreeDSRun(run, orderId, challenged);
|
|
3664
3856
|
end(mapped.threeDS);
|
|
3665
3857
|
return mapped.result;
|
|
3666
3858
|
}
|
|
@@ -3669,7 +3861,7 @@
|
|
|
3669
3861
|
return {
|
|
3670
3862
|
outcome: 'error',
|
|
3671
3863
|
orderId,
|
|
3672
|
-
threeDSChallenged:
|
|
3864
|
+
threeDSChallenged: challenged,
|
|
3673
3865
|
error: error instanceof Error
|
|
3674
3866
|
? error
|
|
3675
3867
|
: new FunnefoxSDKError('One-click 3DS step failed', undefined, error),
|
|
@@ -3860,6 +4052,762 @@
|
|
|
3860
4052
|
: [exports.PaymentMethod.PAYMENT_CARD];
|
|
3861
4053
|
}
|
|
3862
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
|
+
|
|
3863
4811
|
/**
|
|
3864
4812
|
* @fileoverview Main entry point for @funnelfox/billing
|
|
3865
4813
|
*/
|
|
@@ -3869,6 +4817,7 @@
|
|
|
3869
4817
|
createClientSession: createClientSession,
|
|
3870
4818
|
initMethod: initMethod,
|
|
3871
4819
|
silentPurchase: silentPurchase,
|
|
4820
|
+
mountWalletUpsell,
|
|
3872
4821
|
getAvailablePaymentMethods: getAvailablePaymentMethods,
|
|
3873
4822
|
stripe: {
|
|
3874
4823
|
createCardForm: createStripeCardForm,
|
|
@@ -4701,6 +5650,7 @@
|
|
|
4701
5650
|
exports.createClientSession = createClientSession;
|
|
4702
5651
|
exports.default = Billing;
|
|
4703
5652
|
exports.getAvailablePaymentMethods = getAvailablePaymentMethods;
|
|
5653
|
+
exports.mountWalletUpsell = mountWalletUpsell;
|
|
4704
5654
|
|
|
4705
5655
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4706
5656
|
|