@funnelsgrove/payments 0.55.0 → 0.56.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const PAYMENTS_PACKAGE_VERSION = "0.
|
|
1
|
+
export declare const PAYMENTS_PACKAGE_VERSION = "0.56.0";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by scripts/generate-package-version.mjs.
|
|
2
|
-
export const PAYMENTS_PACKAGE_VERSION = "0.
|
|
2
|
+
export const PAYMENTS_PACKAGE_VERSION = "0.56.0";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { paywallCheckoutReturnUrl, retainPaywallCheckout, FUNNEL_ID, FUNNEL_SDK_PUBLISHABLE_KEY, buildMainApiUrl, resolvePreviewPaymentPublishableKey, } from '@funnelsgrove/runtime';
|
|
1
|
+
import { paywallCheckoutReturnUrl, retainPaywallCheckout, FUNNEL_ID, FUNNEL_SDK_PUBLISHABLE_KEY, buildMainApiUrl, funnelSdkService, getCheckoutIdentityToken, resolvePreviewPaymentPublishableKey, retainCheckoutIdentityToken, } from '@funnelsgrove/runtime';
|
|
2
2
|
import { trackSolidgateCheckoutEvent } from './solidgateCheckoutAnalytics.service.js';
|
|
3
3
|
export class SolidgateExistingSubscriptionError extends Error {
|
|
4
4
|
constructor() {
|
|
@@ -15,6 +15,24 @@ export class SolidgateSavedPaymentMethodUnavailableError extends Error {
|
|
|
15
15
|
this.name = 'SolidgateSavedPaymentMethodUnavailableError';
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
+
/** The server rejected the bootstrap identity proof as expired; refresh it and resend once. */
|
|
19
|
+
class SolidgateCheckoutIdentityExpiredError extends Error {
|
|
20
|
+
constructor() {
|
|
21
|
+
super('Checkout identity has expired');
|
|
22
|
+
this.name = 'SolidgateCheckoutIdentityExpiredError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const MAX_CHECKOUT_PROOF_LENGTH = 4096;
|
|
26
|
+
/** Checkout access tokens issued by the create response, keyed by attempt id (memory only, like Stripe). */
|
|
27
|
+
const checkoutAccessTokens = new Map();
|
|
28
|
+
const retainCheckoutAccessToken = (attemptId, token) => {
|
|
29
|
+
const normalized = typeof token === 'string' ? token.trim() : '';
|
|
30
|
+
if (!normalized || normalized.length > MAX_CHECKOUT_PROOF_LENGTH) {
|
|
31
|
+
checkoutAccessTokens.delete(attemptId);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
checkoutAccessTokens.set(attemptId, normalized);
|
|
35
|
+
};
|
|
18
36
|
const trimTrailingSlash = (value) => value.replace(/\/+$/, '');
|
|
19
37
|
const requiredString = (value, name) => {
|
|
20
38
|
const normalized = typeof value === 'string' ? value.trim() : '';
|
|
@@ -44,26 +62,28 @@ const postJson = async (path, body, runtimeConfig, signal) => {
|
|
|
44
62
|
signal,
|
|
45
63
|
});
|
|
46
64
|
if (!response.ok) {
|
|
47
|
-
if (response.status === 409) {
|
|
65
|
+
if (response.status === 409 || response.status === 401) {
|
|
48
66
|
const payload = await response.json().catch(() => null);
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
const code = payload && typeof payload === 'object' && 'code' in payload ? payload.code : null;
|
|
68
|
+
if (response.status === 409 && code === 'already_subscribed_to_product') {
|
|
51
69
|
throw new SolidgateExistingSubscriptionError();
|
|
52
70
|
}
|
|
53
|
-
if (
|
|
54
|
-
&& payload.code === 'saved_payment_method_unavailable') {
|
|
71
|
+
if (response.status === 409 && code === 'saved_payment_method_unavailable') {
|
|
55
72
|
throw new SolidgateSavedPaymentMethodUnavailableError();
|
|
56
73
|
}
|
|
74
|
+
if (response.status === 401 && code === 'checkout_identity_expired') {
|
|
75
|
+
throw new SolidgateCheckoutIdentityExpiredError();
|
|
76
|
+
}
|
|
57
77
|
}
|
|
58
78
|
throw new Error('Solidgate checkout is temporarily unavailable');
|
|
59
79
|
}
|
|
60
80
|
return response.json();
|
|
61
81
|
};
|
|
62
|
-
const getJson = async (path, runtimeConfig, signal) => {
|
|
82
|
+
const getJson = async (path, headers, runtimeConfig, signal) => {
|
|
63
83
|
const publishableKey = resolvePublishableKey(runtimeConfig);
|
|
64
84
|
const response = await fetch(resolveApiUrl(path, runtimeConfig), {
|
|
65
85
|
method: 'GET',
|
|
66
|
-
headers: Object.assign({}, (publishableKey ? { 'x-sdk-publishable-key': publishableKey } : {})),
|
|
86
|
+
headers: Object.assign(Object.assign({}, (publishableKey ? { 'x-sdk-publishable-key': publishableKey } : {})), headers),
|
|
67
87
|
signal,
|
|
68
88
|
});
|
|
69
89
|
if (!response.ok) {
|
|
@@ -72,6 +92,41 @@ const getJson = async (path, runtimeConfig, signal) => {
|
|
|
72
92
|
return response.json();
|
|
73
93
|
};
|
|
74
94
|
const isRecord = (value) => (Boolean(value) && typeof value === 'object' && !Array.isArray(value));
|
|
95
|
+
const refreshCheckoutIdentity = async (userId, idempotencyKey, runtimeConfig, signal) => {
|
|
96
|
+
const payload = await funnelSdkService.bootstrapUser({
|
|
97
|
+
userId, idempotencyKey, runtimeConfig, signal,
|
|
98
|
+
});
|
|
99
|
+
const token = typeof payload.checkoutIdentityToken === 'string' ? payload.checkoutIdentityToken.trim() : '';
|
|
100
|
+
if (!token) {
|
|
101
|
+
throw new Error('Solidgate checkout is temporarily unavailable');
|
|
102
|
+
}
|
|
103
|
+
retainCheckoutIdentityToken(userId, token);
|
|
104
|
+
return token;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Sends the request with the runtime's bootstrap identity proof when it holds one.
|
|
108
|
+
* Mirrors the Stripe path: an expired proof is refreshed through bootstrap and the
|
|
109
|
+
* same attempt is sent once more. The server still verifies every proof.
|
|
110
|
+
*/
|
|
111
|
+
const postWithCheckoutIdentity = async (identity, send) => {
|
|
112
|
+
const checkoutIdentityToken = getCheckoutIdentityToken(identity.userId);
|
|
113
|
+
try {
|
|
114
|
+
return await send(checkoutIdentityToken ? { checkoutIdentityToken } : {});
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (!(error instanceof SolidgateCheckoutIdentityExpiredError)) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const refreshed = await refreshCheckoutIdentity(identity.userId, identity.idempotencyKey, identity.runtimeConfig, identity.signal);
|
|
122
|
+
try {
|
|
123
|
+
return await send({ checkoutIdentityToken: refreshed });
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
throw error instanceof SolidgateCheckoutIdentityExpiredError
|
|
127
|
+
? new Error('Solidgate checkout is temporarily unavailable') : error;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
75
130
|
const parsePreparation = (value) => {
|
|
76
131
|
if (!isRecord(value) || !isRecord(value.display) || !isRecord(value.initialization)) {
|
|
77
132
|
throw new Error('Solidgate checkout returned an invalid response');
|
|
@@ -134,7 +189,7 @@ const parseStatus = (value) => {
|
|
|
134
189
|
&& /^\d\.\d{2}$/.test(value.failureCode) ? { failureCode: value.failureCode } : {}));
|
|
135
190
|
};
|
|
136
191
|
export const prepareSolidgateCheckout = async (input) => {
|
|
137
|
-
var _a, _b
|
|
192
|
+
var _a, _b;
|
|
138
193
|
const paymentProfileId = ((_a = input.paymentProfileId) === null || _a === void 0 ? void 0 : _a.trim()) || null;
|
|
139
194
|
const providerPlanId = ((_b = input.providerPlanId) === null || _b === void 0 ? void 0 : _b.trim()) || null;
|
|
140
195
|
const checkoutType = input.checkoutType || null;
|
|
@@ -142,8 +197,15 @@ export const prepareSolidgateCheckout = async (input) => {
|
|
|
142
197
|
if (!paymentProfileId || !providerPlanId || !checkoutType || !environment) {
|
|
143
198
|
throw new Error('Solidgate V2 checkout selection is incomplete');
|
|
144
199
|
}
|
|
145
|
-
const
|
|
146
|
-
|
|
200
|
+
const funnelEndUserId = requiredString(input.funnelEndUserId, 'funnelEndUserId');
|
|
201
|
+
const idempotencyKey = requiredString(input.idempotencyKey, 'idempotencyKey');
|
|
202
|
+
const response = await postWithCheckoutIdentity({ userId: funnelEndUserId, idempotencyKey, runtimeConfig: input.runtimeConfig, signal: input.signal }, (proof) => {
|
|
203
|
+
var _a;
|
|
204
|
+
return postJson('/sdk/public/v2/payments/checkout-sessions', Object.assign(Object.assign(Object.assign(Object.assign({ checkoutType, uiMode: 'custom', environment,
|
|
205
|
+
paymentProfileId, provider: 'solidgate', providerPlanId, planKey: requiredString(input.funnelPlanKey, 'funnelPlanKey'), funnelId: resolveFunnelId(input.funnelId, input.runtimeConfig), funnelEndUserId, offerSetId: requiredString(input.offerSetId, 'offerSetId'), idempotencyKey }, proof), (input.language != null ? { language: input.language } : {})), (input.retryAttemptId ? { retryAttemptId: input.retryAttemptId } : {})), { couponId: ((_a = input.discountKeyOrProviderId) === null || _a === void 0 ? void 0 : _a.trim()) || null, returnUrl: paywallCheckoutReturnUrl(requiredString(input.returnUrl, 'returnUrl'), 'solidgate'), successUrl: paywallCheckoutReturnUrl(requiredString(input.successUrl, 'successUrl'), 'solidgate'), failUrl: paywallCheckoutReturnUrl(requiredString(input.failUrl, 'failUrl'), 'solidgate', true) }), input.runtimeConfig, input.signal);
|
|
206
|
+
});
|
|
207
|
+
const result = parsePreparation(response);
|
|
208
|
+
retainCheckoutAccessToken(result.attemptId, isRecord(response) ? response.checkoutAccessToken : null);
|
|
147
209
|
retainPaywallCheckout('solidgate', result.attemptId);
|
|
148
210
|
return result;
|
|
149
211
|
};
|
|
@@ -151,7 +213,9 @@ export const retrieveSolidgateCheckoutStatus = async (input) => {
|
|
|
151
213
|
const attemptId = requiredString(input.attemptId, 'attemptId');
|
|
152
214
|
const funnelId = resolveFunnelId(input.funnelId, input.runtimeConfig);
|
|
153
215
|
const funnelEndUserId = requiredString(input.funnelEndUserId, 'funnelEndUserId');
|
|
154
|
-
const
|
|
216
|
+
const checkoutIdentityToken = getCheckoutIdentityToken(funnelEndUserId);
|
|
217
|
+
const checkoutAccessToken = checkoutAccessTokens.get(attemptId);
|
|
218
|
+
const result = parseStatus(await getJson(`/sdk/public/v2/payments/checkout-sessions/${encodeURIComponent(attemptId)}?provider=solidgate&funnelId=${encodeURIComponent(funnelId)}&funnelEndUserId=${encodeURIComponent(funnelEndUserId)}`, Object.assign(Object.assign({}, (checkoutIdentityToken ? { 'x-funnelsgrove-checkout-identity-token': checkoutIdentityToken } : {})), (checkoutAccessToken ? { 'x-funnelsgrove-checkout-access-token': checkoutAccessToken } : {})), input.runtimeConfig, input.signal));
|
|
155
219
|
if (result.attemptId !== attemptId) {
|
|
156
220
|
throw new Error('Solidgate checkout status returned the wrong attempt');
|
|
157
221
|
}
|
|
@@ -231,18 +295,9 @@ export const pollSolidgateCheckoutStatus = async (input, options = {}) => {
|
|
|
231
295
|
const oneClickStatuses = new Set(['verifying', 'succeeded', 'failed']);
|
|
232
296
|
/** Charges a one-time offer to the instrument saved by the user's paid Solidgate checkout. */
|
|
233
297
|
export const chargeSolidgateOneClickPayment = async (input) => {
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
funnelId: resolveFunnelId(input.funnelId, input.runtimeConfig),
|
|
238
|
-
offerSetId: requiredString(input.offerSetId, 'offerSetId'),
|
|
239
|
-
paymentProfileId: requiredString(input.paymentProfileId, 'paymentProfileId'),
|
|
240
|
-
providerPlanId: requiredString(input.providerPlanId, 'providerPlanId'),
|
|
241
|
-
planKey: requiredString(input.funnelPlanKey, 'funnelPlanKey'),
|
|
242
|
-
userId: requiredString(input.userId, 'userId'),
|
|
243
|
-
checkoutSessionId: requiredString(input.checkoutSessionId, 'checkoutSessionId'),
|
|
244
|
-
idempotencyKey: requiredString(input.idempotencyKey, 'idempotencyKey'),
|
|
245
|
-
}, input.runtimeConfig, input.signal);
|
|
298
|
+
const userId = requiredString(input.userId, 'userId');
|
|
299
|
+
const idempotencyKey = requiredString(input.idempotencyKey, 'idempotencyKey');
|
|
300
|
+
const value = await postWithCheckoutIdentity({ userId, idempotencyKey, runtimeConfig: input.runtimeConfig, signal: input.signal }, (proof) => postJson('/sdk/public/v2/payments/one-click', Object.assign({ provider: 'solidgate', environment: input.environment, funnelId: resolveFunnelId(input.funnelId, input.runtimeConfig), offerSetId: requiredString(input.offerSetId, 'offerSetId'), paymentProfileId: requiredString(input.paymentProfileId, 'paymentProfileId'), providerPlanId: requiredString(input.providerPlanId, 'providerPlanId'), planKey: requiredString(input.funnelPlanKey, 'funnelPlanKey'), userId, checkoutSessionId: requiredString(input.checkoutSessionId, 'checkoutSessionId'), idempotencyKey }, proof), input.runtimeConfig, input.signal));
|
|
246
301
|
if (!isRecord(value) || value.provider !== 'solidgate' || typeof value.status !== 'string'
|
|
247
302
|
|| !oneClickStatuses.has(value.status)) {
|
|
248
303
|
throw new Error('Solidgate one-click returned an invalid response');
|