@funnelfox/billing 0.12.0 → 0.13.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/dist/chunk-index.cjs.js +679 -8
- package/dist/chunk-index.es.js +679 -8
- package/dist/funnelfox-billing.js +679 -8
- package/dist/funnelfox-billing.min.js +1 -1
- package/package.json +2 -2
- package/src/types.d.ts +82 -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.13.0';
|
|
569
569
|
const DEFAULTS = {
|
|
570
570
|
BASE_URL: 'https://billing.funnelfox.com',
|
|
571
571
|
REGION: 'default',
|
|
@@ -619,6 +619,9 @@
|
|
|
619
619
|
CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
|
|
620
620
|
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
621
621
|
};
|
|
622
|
+
const PRIMER_DEFAULTS = {
|
|
623
|
+
PAYMENT_HANDLING: 'MANUAL',
|
|
624
|
+
API_VERSION: '2.4'};
|
|
622
625
|
const ALLOWED_BUTTON_PAYMENT_METHODS = [
|
|
623
626
|
exports.PaymentMethod.GOOGLE_PAY,
|
|
624
627
|
exports.PaymentMethod.APPLE_PAY,
|
|
@@ -682,6 +685,13 @@
|
|
|
682
685
|
requiredShippingContactFields: ['emailAddress'],
|
|
683
686
|
},
|
|
684
687
|
};
|
|
688
|
+
/**
|
|
689
|
+
* Backend decline codes that mean "the 3DS check failed" (as opposed to "3DS passed, then the
|
|
690
|
+
* issuer declined"). `null`: the codes cannot tell the two apart, so any decline after a
|
|
691
|
+
* challenge is `3ds_failed` (FFB-704 spike: in production both cases share REFER_TO_CARD_ISSUER,
|
|
692
|
+
* DO_NOT_HONOR and AUTHENTICATION_REQUIRED; human decision 10).
|
|
693
|
+
*/
|
|
694
|
+
const THREE_DS_AUTH_DECLINE_CODES = null;
|
|
685
695
|
|
|
686
696
|
/**
|
|
687
697
|
* @fileoverview Input validation utilities for Funnefox SDK
|
|
@@ -1370,6 +1380,37 @@
|
|
|
1370
1380
|
throw new NetworkError('Network request failed', error);
|
|
1371
1381
|
}
|
|
1372
1382
|
}
|
|
1383
|
+
/**
|
|
1384
|
+
* Sends the request exactly once: no retry and no client timeout (FFB-704, decision 9).
|
|
1385
|
+
* 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.
|
|
1387
|
+
*/
|
|
1388
|
+
async requestOnce(endpoint, options = {}) {
|
|
1389
|
+
const url = `${this.baseUrl}/${this.orgId}${endpoint}`;
|
|
1390
|
+
const requestOptions = {
|
|
1391
|
+
method: 'GET',
|
|
1392
|
+
headers: {
|
|
1393
|
+
'Content-Type': 'application/json',
|
|
1394
|
+
'X-SDK-Version': SDK_VERSION,
|
|
1395
|
+
...(options.headers || {}),
|
|
1396
|
+
},
|
|
1397
|
+
...options,
|
|
1398
|
+
};
|
|
1399
|
+
try {
|
|
1400
|
+
return await this._makeRequest(url, requestOptions);
|
|
1401
|
+
}
|
|
1402
|
+
catch (error) {
|
|
1403
|
+
if (error instanceof APIError) {
|
|
1404
|
+
// Same instance, enriched: _makeRequest leaves errorCode empty; fill it from the body.
|
|
1405
|
+
error.errorCode ?? (error.errorCode = getResponseErrorCodes(error.response)[0] ?? null);
|
|
1406
|
+
throw error;
|
|
1407
|
+
}
|
|
1408
|
+
if (error instanceof Error && error.name === 'NetworkError') {
|
|
1409
|
+
throw error;
|
|
1410
|
+
}
|
|
1411
|
+
throw new NetworkError('Network request failed', error);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1373
1414
|
async _makeRequest(url, options) {
|
|
1374
1415
|
let response;
|
|
1375
1416
|
try {
|
|
@@ -1561,6 +1602,23 @@
|
|
|
1561
1602
|
body: JSON.stringify(payload),
|
|
1562
1603
|
}));
|
|
1563
1604
|
}
|
|
1605
|
+
/** `one_click`, POSTed exactly once (see `requestOnce`). */
|
|
1606
|
+
async oneClickOnce(payload) {
|
|
1607
|
+
return (await this.requestOnce(API_ENDPOINTS.ONE_CLICK, {
|
|
1608
|
+
method: 'POST',
|
|
1609
|
+
body: JSON.stringify(payload),
|
|
1610
|
+
}));
|
|
1611
|
+
}
|
|
1612
|
+
/** `resume_payment`, POSTed exactly once (see `requestOnce`). */
|
|
1613
|
+
async resumePaymentOnce(params) {
|
|
1614
|
+
return (await this.requestOnce(API_ENDPOINTS.RESUME_PAYMENT, {
|
|
1615
|
+
method: 'POST',
|
|
1616
|
+
body: JSON.stringify({
|
|
1617
|
+
order_id: params.orderId,
|
|
1618
|
+
resume_token: params.resumeToken,
|
|
1619
|
+
}),
|
|
1620
|
+
}));
|
|
1621
|
+
}
|
|
1564
1622
|
async createSimpleClientSession(payload) {
|
|
1565
1623
|
return (await this.request(API_ENDPOINTS.CREATE_SIMPLE_CLIENT_SESSION, {
|
|
1566
1624
|
method: 'POST',
|
|
@@ -1572,6 +1630,19 @@
|
|
|
1572
1630
|
}));
|
|
1573
1631
|
}
|
|
1574
1632
|
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Returns the `error[].code` values of a backend error envelope
|
|
1635
|
+
* (`{status: 'error', error: [{msg, code, type}]}`), e.g. from `APIError.response`.
|
|
1636
|
+
*/
|
|
1637
|
+
function getResponseErrorCodes(response) {
|
|
1638
|
+
const errors = response?.error;
|
|
1639
|
+
if (!Array.isArray(errors)) {
|
|
1640
|
+
return [];
|
|
1641
|
+
}
|
|
1642
|
+
return errors
|
|
1643
|
+
.map(item => item?.code)
|
|
1644
|
+
.filter((code) => typeof code === 'string');
|
|
1645
|
+
}
|
|
1575
1646
|
|
|
1576
1647
|
class SessionService {
|
|
1577
1648
|
constructor() {
|
|
@@ -2906,6 +2977,278 @@
|
|
|
2906
2977
|
}
|
|
2907
2978
|
}
|
|
2908
2979
|
|
|
2980
|
+
/**
|
|
2981
|
+
* @fileoverview 3DS challenge for one-click purchases (FFB-704). Internal module.
|
|
2982
|
+
*
|
|
2983
|
+
* Primer can run a 3DS challenge only from a tokenization handler
|
|
2984
|
+
* (`handler.continueWithNewClientToken`), and a one-click purchase has no tokenization in the
|
|
2985
|
+
* browser: the backend charges the vaulted instrument. Mechanism M1 (proven by the FFB-704 spike
|
|
2986
|
+
* on the Primer sandbox): open a customer-bound CHECKOUT client session, start a headless
|
|
2987
|
+
* checkout, tokenize a vaulted instrument through the vault manager as a stand-in, and in
|
|
2988
|
+
* `onTokenizeSuccess` continue with the one-click `action_required_token` instead. Primer then
|
|
2989
|
+
* shows its 3DS overlay; `onResumeSuccess` yields the resume token for `resume_payment`.
|
|
2990
|
+
*/
|
|
2991
|
+
/** Primer client-token intents that carry a 3DS step. */
|
|
2992
|
+
const PRIMER_3DS_INTENTS = ['3DS_AUTHENTICATION', 'PROCESSOR_3DS'];
|
|
2993
|
+
/** Default `threeDSTimeoutMs`. */
|
|
2994
|
+
const DEFAULT_THREE_DS_TIMEOUT_MS = 600000;
|
|
2995
|
+
/** Close button of Primer's 3DS overlay (Primer Web SDK 2.57.3, verified in the spike). */
|
|
2996
|
+
const PRIMER_OVERLAY_CLOSE_SELECTOR = '[data-testid="OverlayContent.closeButton"]';
|
|
2997
|
+
/** Decodes the `intent` of a Primer client token (a JWT); `null` if it is not one. */
|
|
2998
|
+
function getPrimerTokenIntent(token) {
|
|
2999
|
+
try {
|
|
3000
|
+
const parts = token.split('.');
|
|
3001
|
+
if (parts.length !== 3) {
|
|
3002
|
+
return null;
|
|
3003
|
+
}
|
|
3004
|
+
const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
|
|
3005
|
+
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
3006
|
+
const payload = JSON.parse(atob(padded));
|
|
3007
|
+
return typeof payload?.intent === 'string' ? payload.intent : null;
|
|
3008
|
+
}
|
|
3009
|
+
catch {
|
|
3010
|
+
return null;
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
3013
|
+
/** The error Primer reports when the user closes the 3DS overlay (same check as checkout.ts). */
|
|
3014
|
+
function isThreeDSCloseError(error) {
|
|
3015
|
+
return Boolean(error?.stack?.includes('PROCESSOR_3DS') &&
|
|
3016
|
+
error.code === 'RESUME_ERROR' &&
|
|
3017
|
+
error.message?.includes('fetch resume key'));
|
|
3018
|
+
}
|
|
3019
|
+
/**
|
|
3020
|
+
* Picks the vaulted instrument used as the stand-in tokenization. Primer returns the vault
|
|
3021
|
+
* oldest-first and has no "default" flag; the 3DS check runs on the action token, not on this
|
|
3022
|
+
* instrument. Cards are preferred because their vault tokenization is silent (no CVV prompt).
|
|
3023
|
+
*/
|
|
3024
|
+
function pickVaultedMethod(methods) {
|
|
3025
|
+
if (!methods || methods.length === 0) {
|
|
3026
|
+
return null;
|
|
3027
|
+
}
|
|
3028
|
+
return (methods.find(method => method.paymentInstrumentType === 'PAYMENT_CARD') ||
|
|
3029
|
+
methods[0]);
|
|
3030
|
+
}
|
|
3031
|
+
function dismissPrimerOverlay() {
|
|
3032
|
+
if (typeof document === 'undefined') {
|
|
3033
|
+
return;
|
|
3034
|
+
}
|
|
3035
|
+
const button = document.querySelector(PRIMER_OVERLAY_CLOSE_SELECTOR);
|
|
3036
|
+
button?.click();
|
|
3037
|
+
}
|
|
3038
|
+
function toPrimerError(message, error) {
|
|
3039
|
+
return error instanceof PrimerError ? error : new PrimerError(message, error);
|
|
3040
|
+
}
|
|
3041
|
+
function callSafely(fn) {
|
|
3042
|
+
try {
|
|
3043
|
+
fn();
|
|
3044
|
+
}
|
|
3045
|
+
catch {
|
|
3046
|
+
// Primer handler calls are best effort once the result is decided.
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* Runs the 3DS challenge for a one-click order and, when Primer hands back a resume token,
|
|
3051
|
+
* calls `resume_payment` once. Never rejects; the caller maps the result to an outcome.
|
|
3052
|
+
*/
|
|
3053
|
+
async function runOneClickThreeDS(params) {
|
|
3054
|
+
const { apiClient, region, priceId, externalId, orderId, actionToken } = params;
|
|
3055
|
+
const intent = getPrimerTokenIntent(actionToken);
|
|
3056
|
+
if (!intent || !PRIMER_3DS_INTENTS.includes(intent)) {
|
|
3057
|
+
return {
|
|
3058
|
+
kind: 'error',
|
|
3059
|
+
error: new PrimerError('Unsupported one-click action: the action token is not a Primer 3DS client token', { intent }),
|
|
3060
|
+
};
|
|
3061
|
+
}
|
|
3062
|
+
const timeoutMs = Number.isFinite(params.timeoutMs) && params.timeoutMs > 0
|
|
3063
|
+
? params.timeoutMs
|
|
3064
|
+
: DEFAULT_THREE_DS_TIMEOUT_MS;
|
|
3065
|
+
return new Promise(resolve => {
|
|
3066
|
+
let settled = false;
|
|
3067
|
+
let tokenized = false;
|
|
3068
|
+
// True only while create_client_session is in flight.
|
|
3069
|
+
let sessionInFlight = false;
|
|
3070
|
+
let timer = null;
|
|
3071
|
+
let headless = null;
|
|
3072
|
+
const settle = (result) => {
|
|
3073
|
+
if (settled) {
|
|
3074
|
+
return;
|
|
3075
|
+
}
|
|
3076
|
+
settled = true;
|
|
3077
|
+
if (timer) {
|
|
3078
|
+
clearTimeout(timer);
|
|
3079
|
+
timer = null;
|
|
3080
|
+
}
|
|
3081
|
+
headless = null;
|
|
3082
|
+
resolve(result);
|
|
3083
|
+
};
|
|
3084
|
+
timer = setTimeout(() => {
|
|
3085
|
+
timer = null;
|
|
3086
|
+
if (tokenized) {
|
|
3087
|
+
// The challenge was shown and the customer did not finish it.
|
|
3088
|
+
settle({ kind: 'abandoned' });
|
|
3089
|
+
// Close Primer's overlay; the close error it raises is ignored (already settled).
|
|
3090
|
+
dismissPrimerOverlay();
|
|
3091
|
+
return;
|
|
3092
|
+
}
|
|
3093
|
+
// No challenge was shown yet: this is a setup failure, not an abandoned challenge.
|
|
3094
|
+
if (sessionInFlight) {
|
|
3095
|
+
settle({
|
|
3096
|
+
kind: 'error',
|
|
3097
|
+
error: new NetworkError('create_client_session did not respond within threeDSTimeoutMs'),
|
|
3098
|
+
failedRequest: 'create_client_session',
|
|
3099
|
+
});
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
3102
|
+
settle({
|
|
3103
|
+
kind: 'error',
|
|
3104
|
+
error: new PrimerError('Timed out before the 3DS challenge was shown (threeDSTimeoutMs)'),
|
|
3105
|
+
});
|
|
3106
|
+
}, timeoutMs);
|
|
3107
|
+
const onTokenizeSuccess = (_data, handler) => {
|
|
3108
|
+
if (settled) {
|
|
3109
|
+
callSafely(() => handler.handleFailure());
|
|
3110
|
+
return;
|
|
3111
|
+
}
|
|
3112
|
+
tokenized = true;
|
|
3113
|
+
// Ignore the stand-in vaulted token: run the one-click 3DS action instead.
|
|
3114
|
+
handler.continueWithNewClientToken(actionToken);
|
|
3115
|
+
};
|
|
3116
|
+
const onResumeSuccess = async (data, handler) => {
|
|
3117
|
+
if (settled) {
|
|
3118
|
+
callSafely(() => handler.handleFailure());
|
|
3119
|
+
return;
|
|
3120
|
+
}
|
|
3121
|
+
// The challenge is over: the timeout bounds only the challenge, never the POST.
|
|
3122
|
+
if (timer) {
|
|
3123
|
+
clearTimeout(timer);
|
|
3124
|
+
timer = null;
|
|
3125
|
+
}
|
|
3126
|
+
let response;
|
|
3127
|
+
try {
|
|
3128
|
+
response = await apiClient.resumePaymentOnce({
|
|
3129
|
+
orderId,
|
|
3130
|
+
resumeToken: data.resumeToken,
|
|
3131
|
+
});
|
|
3132
|
+
}
|
|
3133
|
+
catch (error) {
|
|
3134
|
+
callSafely(() => handler.handleFailure());
|
|
3135
|
+
settle({
|
|
3136
|
+
kind: 'error',
|
|
3137
|
+
error: error,
|
|
3138
|
+
failedRequest: 'resume_payment',
|
|
3139
|
+
});
|
|
3140
|
+
return;
|
|
3141
|
+
}
|
|
3142
|
+
const resumeData = response?.data;
|
|
3143
|
+
if (response?.status === 'success' &&
|
|
3144
|
+
resumeData?.checkout_status === 'succeeded' &&
|
|
3145
|
+
!resumeData.action_required_token) {
|
|
3146
|
+
callSafely(() => handler.handleSuccess());
|
|
3147
|
+
}
|
|
3148
|
+
else {
|
|
3149
|
+
// A chained action token is never continued (no second challenge).
|
|
3150
|
+
callSafely(() => handler.handleFailure(resumeData?.failed_message_for_user || undefined));
|
|
3151
|
+
}
|
|
3152
|
+
settle({ kind: 'resumed', response });
|
|
3153
|
+
};
|
|
3154
|
+
const onResumeError = (error) => {
|
|
3155
|
+
if (isThreeDSCloseError(error)) {
|
|
3156
|
+
settle({ kind: 'abandoned' });
|
|
3157
|
+
return;
|
|
3158
|
+
}
|
|
3159
|
+
settle({
|
|
3160
|
+
kind: 'failed',
|
|
3161
|
+
error: toPrimerError('3DS authentication failed', error),
|
|
3162
|
+
});
|
|
3163
|
+
};
|
|
3164
|
+
const onCheckoutFail = (error) => {
|
|
3165
|
+
if (tokenized) {
|
|
3166
|
+
settle({
|
|
3167
|
+
kind: 'failed',
|
|
3168
|
+
error: toPrimerError('3DS authentication failed', error),
|
|
3169
|
+
});
|
|
3170
|
+
}
|
|
3171
|
+
else {
|
|
3172
|
+
settle({
|
|
3173
|
+
kind: 'error',
|
|
3174
|
+
error: toPrimerError('Primer checkout failed before the 3DS challenge', error),
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
};
|
|
3178
|
+
const onTokenizeError = (error) => {
|
|
3179
|
+
settle({
|
|
3180
|
+
kind: 'error',
|
|
3181
|
+
error: toPrimerError('Failed to tokenize the saved payment method', error),
|
|
3182
|
+
});
|
|
3183
|
+
};
|
|
3184
|
+
(async () => {
|
|
3185
|
+
await new PrimerWrapper().ensurePrimerLoaded();
|
|
3186
|
+
if (settled) {
|
|
3187
|
+
return;
|
|
3188
|
+
}
|
|
3189
|
+
let clientToken;
|
|
3190
|
+
sessionInFlight = true;
|
|
3191
|
+
try {
|
|
3192
|
+
const session = await apiClient.createClientSession({
|
|
3193
|
+
priceId,
|
|
3194
|
+
externalId,
|
|
3195
|
+
region,
|
|
3196
|
+
clientMetadata: {},
|
|
3197
|
+
});
|
|
3198
|
+
clientToken = apiClient.processSessionResponse(session).clientToken;
|
|
3199
|
+
sessionInFlight = false;
|
|
3200
|
+
}
|
|
3201
|
+
catch (error) {
|
|
3202
|
+
sessionInFlight = false;
|
|
3203
|
+
settle({
|
|
3204
|
+
kind: 'error',
|
|
3205
|
+
error: error,
|
|
3206
|
+
failedRequest: 'create_client_session',
|
|
3207
|
+
});
|
|
3208
|
+
return;
|
|
3209
|
+
}
|
|
3210
|
+
if (settled) {
|
|
3211
|
+
return;
|
|
3212
|
+
}
|
|
3213
|
+
// A dedicated instance, not the HeadlessManager cache used by checkouts.
|
|
3214
|
+
const instance = await window.Primer.createHeadless(clientToken, {
|
|
3215
|
+
paymentHandling: PRIMER_DEFAULTS.PAYMENT_HANDLING,
|
|
3216
|
+
apiVersion: PRIMER_DEFAULTS.API_VERSION,
|
|
3217
|
+
onAvailablePaymentMethodsLoad: () => { },
|
|
3218
|
+
onTokenizeSuccess,
|
|
3219
|
+
onTokenizeError,
|
|
3220
|
+
onResumeSuccess,
|
|
3221
|
+
onResumeError,
|
|
3222
|
+
onCheckoutFail,
|
|
3223
|
+
});
|
|
3224
|
+
headless = await instance;
|
|
3225
|
+
await headless.start();
|
|
3226
|
+
if (settled) {
|
|
3227
|
+
return;
|
|
3228
|
+
}
|
|
3229
|
+
const vaultManager = headless.createVaultManager();
|
|
3230
|
+
const methods = await vaultManager.fetchVaultedPaymentMethods();
|
|
3231
|
+
const method = pickVaultedMethod(methods);
|
|
3232
|
+
if (!method) {
|
|
3233
|
+
settle({
|
|
3234
|
+
kind: 'error',
|
|
3235
|
+
error: new PrimerError('No saved payment method found for the one-click 3DS challenge'),
|
|
3236
|
+
});
|
|
3237
|
+
return;
|
|
3238
|
+
}
|
|
3239
|
+
if (settled) {
|
|
3240
|
+
return;
|
|
3241
|
+
}
|
|
3242
|
+
await vaultManager.startPaymentFlow(method.id);
|
|
3243
|
+
})().catch(error => {
|
|
3244
|
+
settle({
|
|
3245
|
+
kind: 'error',
|
|
3246
|
+
error: toPrimerError('Failed to start the one-click 3DS challenge', error),
|
|
3247
|
+
});
|
|
3248
|
+
});
|
|
3249
|
+
});
|
|
3250
|
+
}
|
|
3251
|
+
|
|
2909
3252
|
/**
|
|
2910
3253
|
* @fileoverview Public API with configuration and orchestration logic
|
|
2911
3254
|
*/
|
|
@@ -2972,20 +3315,46 @@
|
|
|
2972
3315
|
});
|
|
2973
3316
|
return apiClient.processSessionResponse(sessionResponse);
|
|
2974
3317
|
}
|
|
2975
|
-
|
|
2976
|
-
|
|
3318
|
+
/**
|
|
3319
|
+
* Builds the `one_click` body. The CIT fields are added only when the caller set them.
|
|
3320
|
+
*/
|
|
3321
|
+
function buildOneClickRequest(options) {
|
|
3322
|
+
const payload = {
|
|
3323
|
+
pp_ident: options.priceId,
|
|
3324
|
+
external_id: options.externalId,
|
|
3325
|
+
client_metadata: options.clientMetadata,
|
|
3326
|
+
};
|
|
3327
|
+
if (options.initiationType !== undefined) {
|
|
3328
|
+
payload.initiation_type = options.initiationType;
|
|
3329
|
+
}
|
|
3330
|
+
if (options.citCardNetworks !== undefined) {
|
|
3331
|
+
payload.cit_card_networks = options.citCardNetworks;
|
|
3332
|
+
}
|
|
3333
|
+
if (options.citGooglePayPanOnly !== undefined) {
|
|
3334
|
+
payload.cit_google_pay_pan_only = options.citGooglePayPanOnly;
|
|
3335
|
+
}
|
|
3336
|
+
return payload;
|
|
3337
|
+
}
|
|
3338
|
+
function silentPurchase(options) {
|
|
3339
|
+
if (options?.handle3DS === true) {
|
|
3340
|
+
return silentPurchaseDetailed(options);
|
|
3341
|
+
}
|
|
3342
|
+
return silentPurchaseLegacy(options);
|
|
3343
|
+
}
|
|
3344
|
+
async function silentPurchaseLegacy(options) {
|
|
3345
|
+
const { orgId, baseUrl } = options;
|
|
2977
3346
|
const apiClient = new APIClient({
|
|
2978
3347
|
baseUrl: baseUrl,
|
|
2979
3348
|
orgId: orgId,
|
|
2980
3349
|
timeout: DEFAULTS.REQUEST_TIMEOUT,
|
|
2981
3350
|
retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
|
|
2982
3351
|
});
|
|
3352
|
+
if (options.initiationType === 'cit') {
|
|
3353
|
+
// eslint-disable-next-line no-console
|
|
3354
|
+
console.warn('[FunnelfoxSDK] silentPurchase: initiationType "cit" can trigger a 3DS challenge, which only silentPurchase({ handle3DS: true }) handles.');
|
|
3355
|
+
}
|
|
2983
3356
|
try {
|
|
2984
|
-
const response = await apiClient.oneClick(
|
|
2985
|
-
pp_ident: priceId,
|
|
2986
|
-
external_id: externalId,
|
|
2987
|
-
client_metadata: clientMetadata,
|
|
2988
|
-
});
|
|
3357
|
+
const response = await apiClient.oneClick(buildOneClickRequest(options));
|
|
2989
3358
|
if (response.status !== 'success' &&
|
|
2990
3359
|
response.error.some(({ code }) => code === 'double_purchase')) {
|
|
2991
3360
|
throw new APIError('This product was already purchased');
|
|
@@ -3005,6 +3374,308 @@
|
|
|
3005
3374
|
throw error;
|
|
3006
3375
|
}
|
|
3007
3376
|
}
|
|
3377
|
+
const ONE_CLICK_IN_PROGRESS = 'one_click_in_progress';
|
|
3378
|
+
const DOUBLE_PURCHASE = 'double_purchase';
|
|
3379
|
+
const oneClickInFlight = new Set();
|
|
3380
|
+
function declineFields(data) {
|
|
3381
|
+
return {
|
|
3382
|
+
failedMessageForUser: data.failed_message_for_user ?? null,
|
|
3383
|
+
declineCode: data.decline_code ?? null,
|
|
3384
|
+
declineCodeSource: data.decline_code_source ?? null,
|
|
3385
|
+
};
|
|
3386
|
+
}
|
|
3387
|
+
function envelopeError(response, fallbackMessage) {
|
|
3388
|
+
const firstError = response?.error?.[0];
|
|
3389
|
+
return new APIError(firstError?.msg || fallbackMessage, null, {
|
|
3390
|
+
errorCode: firstError?.code,
|
|
3391
|
+
errorType: firstError?.type,
|
|
3392
|
+
response,
|
|
3393
|
+
});
|
|
3394
|
+
}
|
|
3395
|
+
/** Maps a 2xx `one_click` response (rows 1-4, plus "needs action"). */
|
|
3396
|
+
function mapOneClickResponse(response) {
|
|
3397
|
+
const data = response?.data;
|
|
3398
|
+
if (response?.status !== 'success' || !data) {
|
|
3399
|
+
if (getResponseErrorCodes(response).includes(DOUBLE_PURCHASE)) {
|
|
3400
|
+
return {
|
|
3401
|
+
kind: 'result',
|
|
3402
|
+
result: {
|
|
3403
|
+
outcome: 'already_purchased',
|
|
3404
|
+
orderId: null,
|
|
3405
|
+
threeDSChallenged: false,
|
|
3406
|
+
},
|
|
3407
|
+
};
|
|
3408
|
+
}
|
|
3409
|
+
return {
|
|
3410
|
+
kind: 'result',
|
|
3411
|
+
result: {
|
|
3412
|
+
outcome: 'error',
|
|
3413
|
+
orderId: data?.order_id ?? null,
|
|
3414
|
+
threeDSChallenged: false,
|
|
3415
|
+
error: envelopeError(response, 'One-click purchase failed'),
|
|
3416
|
+
failedRequest: 'one_click',
|
|
3417
|
+
},
|
|
3418
|
+
};
|
|
3419
|
+
}
|
|
3420
|
+
const orderId = data.order_id ?? null;
|
|
3421
|
+
if (data.action_required_token) {
|
|
3422
|
+
return { kind: 'action', orderId, actionToken: data.action_required_token };
|
|
3423
|
+
}
|
|
3424
|
+
switch (data.checkout_status) {
|
|
3425
|
+
case 'succeeded':
|
|
3426
|
+
return {
|
|
3427
|
+
kind: 'result',
|
|
3428
|
+
result: { outcome: 'succeeded', orderId, threeDSChallenged: false },
|
|
3429
|
+
};
|
|
3430
|
+
case 'failed':
|
|
3431
|
+
return {
|
|
3432
|
+
kind: 'result',
|
|
3433
|
+
result: {
|
|
3434
|
+
outcome: 'declined',
|
|
3435
|
+
orderId,
|
|
3436
|
+
threeDSChallenged: false,
|
|
3437
|
+
...declineFields(data),
|
|
3438
|
+
},
|
|
3439
|
+
};
|
|
3440
|
+
case 'processing':
|
|
3441
|
+
return {
|
|
3442
|
+
kind: 'result',
|
|
3443
|
+
result: { outcome: 'processing', orderId, threeDSChallenged: false },
|
|
3444
|
+
};
|
|
3445
|
+
case 'cancelled':
|
|
3446
|
+
return {
|
|
3447
|
+
kind: 'result',
|
|
3448
|
+
result: {
|
|
3449
|
+
outcome: 'error',
|
|
3450
|
+
orderId,
|
|
3451
|
+
threeDSChallenged: false,
|
|
3452
|
+
error: new APIError('Payment was cancelled', null, { response }),
|
|
3453
|
+
},
|
|
3454
|
+
};
|
|
3455
|
+
default:
|
|
3456
|
+
return {
|
|
3457
|
+
kind: 'result',
|
|
3458
|
+
result: {
|
|
3459
|
+
outcome: 'error',
|
|
3460
|
+
orderId,
|
|
3461
|
+
threeDSChallenged: false,
|
|
3462
|
+
error: new APIError(`Unhandled checkout status: ${data.checkout_status}`, null, { response }),
|
|
3463
|
+
},
|
|
3464
|
+
};
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
/** 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) {
|
|
3469
|
+
const data = response?.data;
|
|
3470
|
+
if (response?.status !== 'success' || !data) {
|
|
3471
|
+
return {
|
|
3472
|
+
threeDS: 'error',
|
|
3473
|
+
result: {
|
|
3474
|
+
outcome: 'error',
|
|
3475
|
+
orderId,
|
|
3476
|
+
threeDSChallenged: true,
|
|
3477
|
+
error: envelopeError(response, 'Resume payment failed'),
|
|
3478
|
+
failedRequest: 'resume_payment',
|
|
3479
|
+
},
|
|
3480
|
+
};
|
|
3481
|
+
}
|
|
3482
|
+
if (data.action_required_token) {
|
|
3483
|
+
// Chained action: not supported, no second challenge (human decision F3).
|
|
3484
|
+
return {
|
|
3485
|
+
threeDS: 'error',
|
|
3486
|
+
result: {
|
|
3487
|
+
outcome: 'error',
|
|
3488
|
+
orderId,
|
|
3489
|
+
threeDSChallenged: true,
|
|
3490
|
+
error: new FunnefoxSDKError('resume_payment returned another action token; chained 3DS is not supported', 'one_click_chained_action'),
|
|
3491
|
+
},
|
|
3492
|
+
};
|
|
3493
|
+
}
|
|
3494
|
+
switch (data.checkout_status) {
|
|
3495
|
+
case 'succeeded':
|
|
3496
|
+
return {
|
|
3497
|
+
threeDS: 'passed',
|
|
3498
|
+
result: { outcome: 'succeeded', orderId, threeDSChallenged: true },
|
|
3499
|
+
};
|
|
3500
|
+
case 'processing':
|
|
3501
|
+
return {
|
|
3502
|
+
threeDS: 'passed',
|
|
3503
|
+
result: { outcome: 'processing', orderId, threeDSChallenged: true },
|
|
3504
|
+
};
|
|
3505
|
+
case 'failed': {
|
|
3506
|
+
const authFailed = threeDSAuthDeclineCodes === null ||
|
|
3507
|
+
threeDSAuthDeclineCodes.includes(data.decline_code ?? '');
|
|
3508
|
+
return {
|
|
3509
|
+
threeDS: authFailed ? 'failed' : 'passed',
|
|
3510
|
+
result: {
|
|
3511
|
+
outcome: authFailed ? '3ds_failed' : 'declined',
|
|
3512
|
+
orderId,
|
|
3513
|
+
threeDSChallenged: true,
|
|
3514
|
+
...declineFields(data),
|
|
3515
|
+
},
|
|
3516
|
+
};
|
|
3517
|
+
}
|
|
3518
|
+
default:
|
|
3519
|
+
return {
|
|
3520
|
+
threeDS: 'error',
|
|
3521
|
+
result: {
|
|
3522
|
+
outcome: 'error',
|
|
3523
|
+
orderId,
|
|
3524
|
+
threeDSChallenged: true,
|
|
3525
|
+
error: new APIError(data.checkout_status === 'cancelled'
|
|
3526
|
+
? 'Payment was cancelled'
|
|
3527
|
+
: `Unhandled checkout status: ${data.checkout_status}`, null, { response }),
|
|
3528
|
+
},
|
|
3529
|
+
};
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
function mapThreeDSRun(run, orderId) {
|
|
3533
|
+
switch (run.kind) {
|
|
3534
|
+
case 'resumed':
|
|
3535
|
+
return mapResumeResponse(run.response, orderId);
|
|
3536
|
+
case 'failed':
|
|
3537
|
+
return {
|
|
3538
|
+
threeDS: 'failed',
|
|
3539
|
+
result: {
|
|
3540
|
+
outcome: '3ds_failed',
|
|
3541
|
+
orderId,
|
|
3542
|
+
threeDSChallenged: true,
|
|
3543
|
+
error: run.error,
|
|
3544
|
+
},
|
|
3545
|
+
};
|
|
3546
|
+
case 'abandoned':
|
|
3547
|
+
return {
|
|
3548
|
+
threeDS: 'abandoned',
|
|
3549
|
+
result: { outcome: '3ds_abandoned', orderId, threeDSChallenged: true },
|
|
3550
|
+
};
|
|
3551
|
+
default: {
|
|
3552
|
+
const result = {
|
|
3553
|
+
outcome: 'error',
|
|
3554
|
+
orderId,
|
|
3555
|
+
threeDSChallenged: true,
|
|
3556
|
+
error: run.error,
|
|
3557
|
+
};
|
|
3558
|
+
if (run.failedRequest) {
|
|
3559
|
+
result.failedRequest = run.failedRequest;
|
|
3560
|
+
}
|
|
3561
|
+
return { threeDS: 'error', result };
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
}
|
|
3565
|
+
function callHook(name, hook, info) {
|
|
3566
|
+
if (typeof hook !== 'function') {
|
|
3567
|
+
return;
|
|
3568
|
+
}
|
|
3569
|
+
try {
|
|
3570
|
+
hook(info);
|
|
3571
|
+
}
|
|
3572
|
+
catch (error) {
|
|
3573
|
+
// eslint-disable-next-line no-console
|
|
3574
|
+
console.error(`[FunnelfoxSDK] silentPurchase ${name} hook threw`, error);
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
async function silentPurchaseDetailed(options) {
|
|
3578
|
+
for (const field of ['orgId', 'baseUrl', 'priceId', 'externalId']) {
|
|
3579
|
+
if (!options?.[field]) {
|
|
3580
|
+
throw new ValidationError(field, 'is required', options?.[field]);
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
const { orgId, baseUrl, externalId } = options;
|
|
3584
|
+
const inFlightKey = `${orgId}\u0000${externalId}`;
|
|
3585
|
+
if (oneClickInFlight.has(inFlightKey)) {
|
|
3586
|
+
return {
|
|
3587
|
+
outcome: 'error',
|
|
3588
|
+
orderId: null,
|
|
3589
|
+
threeDSChallenged: false,
|
|
3590
|
+
error: new FunnefoxSDKError('A one-click purchase is already in progress for this customer', ONE_CLICK_IN_PROGRESS),
|
|
3591
|
+
};
|
|
3592
|
+
}
|
|
3593
|
+
oneClickInFlight.add(inFlightKey);
|
|
3594
|
+
try {
|
|
3595
|
+
const result = await runSilentPurchaseDetailed(options);
|
|
3596
|
+
if (result.outcome === 'error' && result.error) {
|
|
3597
|
+
const error = result.error;
|
|
3598
|
+
getErrorImage(orgId, {
|
|
3599
|
+
baseUrl,
|
|
3600
|
+
message: error.message,
|
|
3601
|
+
code: error.code,
|
|
3602
|
+
req_id: error.response?.req_id,
|
|
3603
|
+
});
|
|
3604
|
+
}
|
|
3605
|
+
return result;
|
|
3606
|
+
}
|
|
3607
|
+
finally {
|
|
3608
|
+
oneClickInFlight.delete(inFlightKey);
|
|
3609
|
+
}
|
|
3610
|
+
}
|
|
3611
|
+
async function runSilentPurchaseDetailed(options) {
|
|
3612
|
+
const apiClient = new APIClient({
|
|
3613
|
+
baseUrl: options.baseUrl,
|
|
3614
|
+
orgId: options.orgId,
|
|
3615
|
+
timeout: DEFAULTS.REQUEST_TIMEOUT,
|
|
3616
|
+
retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
|
|
3617
|
+
});
|
|
3618
|
+
let response;
|
|
3619
|
+
try {
|
|
3620
|
+
response = await apiClient.oneClickOnce(buildOneClickRequest(options));
|
|
3621
|
+
}
|
|
3622
|
+
catch (error) {
|
|
3623
|
+
if (error instanceof APIError &&
|
|
3624
|
+
getResponseErrorCodes(error.response).includes(DOUBLE_PURCHASE)) {
|
|
3625
|
+
return {
|
|
3626
|
+
outcome: 'already_purchased',
|
|
3627
|
+
orderId: null,
|
|
3628
|
+
threeDSChallenged: false,
|
|
3629
|
+
};
|
|
3630
|
+
}
|
|
3631
|
+
return {
|
|
3632
|
+
outcome: 'error',
|
|
3633
|
+
orderId: null,
|
|
3634
|
+
threeDSChallenged: false,
|
|
3635
|
+
error: error,
|
|
3636
|
+
failedRequest: 'one_click',
|
|
3637
|
+
};
|
|
3638
|
+
}
|
|
3639
|
+
const step = mapOneClickResponse(response);
|
|
3640
|
+
if (step.kind === 'result') {
|
|
3641
|
+
return step.result;
|
|
3642
|
+
}
|
|
3643
|
+
const { orderId, actionToken } = step;
|
|
3644
|
+
let ended = false;
|
|
3645
|
+
const end = (result) => {
|
|
3646
|
+
if (ended) {
|
|
3647
|
+
return;
|
|
3648
|
+
}
|
|
3649
|
+
ended = true;
|
|
3650
|
+
callHook('onThreeDSEnd', options.onThreeDSEnd, { result });
|
|
3651
|
+
};
|
|
3652
|
+
callHook('onThreeDSStart', options.onThreeDSStart, { orderId });
|
|
3653
|
+
try {
|
|
3654
|
+
const run = await runOneClickThreeDS({
|
|
3655
|
+
apiClient,
|
|
3656
|
+
region: defaultConfig?.region || DEFAULTS.REGION,
|
|
3657
|
+
priceId: options.priceId,
|
|
3658
|
+
externalId: options.externalId,
|
|
3659
|
+
orderId,
|
|
3660
|
+
actionToken,
|
|
3661
|
+
timeoutMs: options.threeDSTimeoutMs ?? DEFAULT_THREE_DS_TIMEOUT_MS,
|
|
3662
|
+
});
|
|
3663
|
+
const mapped = mapThreeDSRun(run, orderId);
|
|
3664
|
+
end(mapped.threeDS);
|
|
3665
|
+
return mapped.result;
|
|
3666
|
+
}
|
|
3667
|
+
catch (error) {
|
|
3668
|
+
end('error');
|
|
3669
|
+
return {
|
|
3670
|
+
outcome: 'error',
|
|
3671
|
+
orderId,
|
|
3672
|
+
threeDSChallenged: true,
|
|
3673
|
+
error: error instanceof Error
|
|
3674
|
+
? error
|
|
3675
|
+
: new FunnefoxSDKError('One-click 3DS step failed', undefined, error),
|
|
3676
|
+
};
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3008
3679
|
async function initMethod(method, element, options) {
|
|
3009
3680
|
try {
|
|
3010
3681
|
const checkoutInstance = new CheckoutInstance({
|