@funnelsgrove/runtime 0.9.0 → 0.11.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.
@@ -0,0 +1,44 @@
1
+ export type PublishedStripeSelectionInput = {
2
+ planId?: string | null;
3
+ planKey?: string | null;
4
+ offerSetId?: string | null;
5
+ paymentProfileId?: string | null;
6
+ provider?: 'stripe' | null;
7
+ providerPlanId?: string | null;
8
+ runtimeConfigRevisionId?: string | null;
9
+ };
10
+ export declare const toV2CheckoutSelection: (input: PublishedStripeSelectionInput) => {
11
+ runtimeConfigRevisionId: string | undefined;
12
+ offerSetId: string | undefined;
13
+ paymentProfileId: string | undefined;
14
+ provider: "stripe" | null | undefined;
15
+ providerPlanId: string | undefined;
16
+ planKey: string | undefined;
17
+ };
18
+ export declare const toV2CheckoutSessionRequest: (input: PublishedStripeSelectionInput & {
19
+ couponId?: string | null;
20
+ analyticsMetadata?: Record<string, unknown> | null;
21
+ customerEmail?: string | null;
22
+ returnUrl: string;
23
+ userId?: string | null;
24
+ anonymousUserId?: string | null;
25
+ idempotencyKey?: string | null;
26
+ environment?: "test" | "live";
27
+ }, checkoutType: "subscription" | "one_time") => {
28
+ checkoutType: "one_time" | "subscription";
29
+ uiMode: string;
30
+ couponId: string | null | undefined;
31
+ analyticsMetadata: Record<string, unknown> | null | undefined;
32
+ customerEmail: string | null | undefined;
33
+ returnUrl: string;
34
+ userId: string | null | undefined;
35
+ anonymousUserId: string | null | undefined;
36
+ idempotencyKey: string | null | undefined;
37
+ environment: "test" | "live" | undefined;
38
+ runtimeConfigRevisionId: string | undefined;
39
+ offerSetId: string | undefined;
40
+ paymentProfileId: string | undefined;
41
+ provider: "stripe" | null | undefined;
42
+ providerPlanId: string | undefined;
43
+ planKey: string | undefined;
44
+ };
@@ -0,0 +1,16 @@
1
+ export const toV2CheckoutSelection = (input) => {
2
+ var _a, _b, _c, _d, _e, _f;
3
+ const selection = {
4
+ runtimeConfigRevisionId: (_a = input.runtimeConfigRevisionId) === null || _a === void 0 ? void 0 : _a.trim(),
5
+ offerSetId: (_b = input.offerSetId) === null || _b === void 0 ? void 0 : _b.trim(),
6
+ paymentProfileId: (_c = input.paymentProfileId) === null || _c === void 0 ? void 0 : _c.trim(),
7
+ provider: input.provider,
8
+ providerPlanId: (_d = input.providerPlanId) === null || _d === void 0 ? void 0 : _d.trim(),
9
+ planKey: ((_e = input.planKey) === null || _e === void 0 ? void 0 : _e.trim()) || ((_f = input.planId) === null || _f === void 0 ? void 0 : _f.trim()),
10
+ };
11
+ if (selection.provider !== 'stripe' || Object.values(selection).some((value) => !value)) {
12
+ throw new Error('Published Stripe checkout selection is incomplete');
13
+ }
14
+ return selection;
15
+ };
16
+ export const toV2CheckoutSessionRequest = (input, checkoutType) => (Object.assign(Object.assign({}, toV2CheckoutSelection(input)), { checkoutType, uiMode: 'custom', couponId: input.couponId, analyticsMetadata: input.analyticsMetadata, customerEmail: input.customerEmail, returnUrl: input.returnUrl, userId: input.userId, anonymousUserId: input.anonymousUserId, idempotencyKey: input.idempotencyKey, environment: input.environment }));
@@ -152,6 +152,12 @@ export type FunnelSdkCreateSubscriptionCheckoutResponse = {
152
152
  };
153
153
  export type FunnelSdkCreateOneTimeCheckoutInput = {
154
154
  planId: string;
155
+ planKey?: string | null;
156
+ paymentProfileId?: string | null;
157
+ provider?: 'stripe' | null;
158
+ runtimeConfigRevisionId?: string | null;
159
+ anonymousUserId?: string | null;
160
+ idempotencyKey?: string | null;
155
161
  offerSetId?: string | null;
156
162
  offerSetKey?: string | null;
157
163
  amountCents: number;
@@ -175,6 +181,7 @@ export type FunnelSdkCreateOneTimeCheckoutResponse = {
175
181
  export type FunnelSdkUpdateSubscriptionCheckoutPlanInput = {
176
182
  checkoutSessionId: string;
177
183
  planId: string;
184
+ planKey?: string | null;
178
185
  offerSetId?: string | null;
179
186
  offerSetKey?: string | null;
180
187
  amountCents: number;
@@ -191,6 +198,9 @@ export type FunnelSdkUpdateSubscriptionCheckoutPlanInput = {
191
198
  description?: string | null;
192
199
  environment?: FunnelSdkRuntimeMode;
193
200
  providerPlanId?: string | null;
201
+ provider?: 'stripe' | null;
202
+ paymentProfileId?: string | null;
203
+ runtimeConfigRevisionId?: string | null;
194
204
  runtimeConfig?: FunnelSdkRuntimeConfigOverrides;
195
205
  title?: string | null;
196
206
  userId?: string | null;
@@ -236,6 +246,11 @@ export type FunnelSdkCreateOneTimePaymentIntentResponse = {
236
246
  };
237
247
  export type FunnelSdkChargeOneClickPaymentInput = {
238
248
  planId: string;
249
+ planKey?: string | null;
250
+ offerSetId?: string | null;
251
+ paymentProfileId?: string | null;
252
+ provider?: 'stripe' | null;
253
+ runtimeConfigRevisionId?: string | null;
239
254
  providerPlanId?: string | null;
240
255
  amountCents: number;
241
256
  analyticsMetadata?: Record<string, unknown> | null;
@@ -274,7 +289,9 @@ export type FunnelSdkListSubscriptionsInput = {
274
289
  };
275
290
  declare class FunnelSdkService {
276
291
  private getJson;
292
+ private writeJson;
277
293
  private postJson;
294
+ private patchJson;
278
295
  bootstrapUser<ResponsePayload = FunnelSdkJsonObject>(input?: FunnelSdkBootstrapUserInput): Promise<ResponsePayload>;
279
296
  captureEmail(input: FunnelSdkCaptureEmailInput): Promise<FunnelSdkCaptureEmailResponse>;
280
297
  getCurrentUser<ResponsePayload = FunnelSdkJsonObject>(input: FunnelSdkGetCurrentUserInput): Promise<ResponsePayload>;
@@ -1,6 +1,7 @@
1
1
  import { buildMainApiUrl, FUNNEL_ID, FUNNEL_SDK_PUBLISHABLE_KEY, FUNNEL_VERSION_ID, } from './runtime-api.config.js';
2
2
  import { resolvePreviewPaymentPublishableKey } from './public-env.js';
3
3
  import { CURRENT_STEP_CONTRACT_VERSION } from '../steps/step-contract.js';
4
+ import { toV2CheckoutSelection, toV2CheckoutSessionRequest } from './funnel-sdk-payments.functions.js';
4
5
  const trimTrailingSlash = (value) => value.replace(/\/+$/, '');
5
6
  const toNormalizedPath = (path) => (path.startsWith('/') ? path : `/${path}`);
6
7
  const asTrimmedStringOrNull = (value) => {
@@ -84,10 +85,10 @@ class FunnelSdkService {
84
85
  }
85
86
  return (await response.json());
86
87
  }
87
- async postJson(path, input) {
88
+ async writeJson(method, path, input) {
88
89
  var _a;
89
90
  const response = await fetch(buildApiUrl(path, input.runtimeConfig), {
90
- method: 'POST',
91
+ method,
91
92
  headers: buildHeaders(input.runtimeConfig, {
92
93
  'Content-Type': 'application/json',
93
94
  }),
@@ -102,6 +103,12 @@ class FunnelSdkService {
102
103
  }
103
104
  return (await response.json());
104
105
  }
106
+ postJson(path, input) {
107
+ return this.writeJson('POST', path, input);
108
+ }
109
+ async patchJson(path, input) {
110
+ return this.writeJson('PATCH', path, input);
111
+ }
105
112
  async bootstrapUser(input = {}) {
106
113
  return this.postJson('/sdk/public/users/bootstrap', {
107
114
  runtimeConfig: input.runtimeConfig,
@@ -257,39 +264,9 @@ class FunnelSdkService {
257
264
  async createSubscriptionCheckout(input) {
258
265
  const { paymentsApiVersion } = resolveRuntimeConfig(input.runtimeConfig);
259
266
  if (paymentsApiVersion === 'v2') {
260
- const runtimeConfigRevisionId = asTrimmedStringOrNull(input.runtimeConfigRevisionId);
261
- const offerSetId = asTrimmedStringOrNull(input.offerSetId);
262
- const paymentProfileId = asTrimmedStringOrNull(input.paymentProfileId);
263
- const providerPlanId = asTrimmedStringOrNull(input.providerPlanId);
264
- const planKey = asTrimmedStringOrNull(input.planKey) || asTrimmedStringOrNull(input.planId);
265
- if (!runtimeConfigRevisionId
266
- || !offerSetId
267
- || !paymentProfileId
268
- || !providerPlanId
269
- || !planKey
270
- || input.provider !== 'stripe') {
271
- throw new Error('Published Stripe checkout selection is incomplete');
272
- }
273
267
  return this.postJson('/sdk/public/v2/payments/checkout-sessions', {
274
268
  runtimeConfig: input.runtimeConfig,
275
- body: {
276
- runtimeConfigRevisionId,
277
- offerSetId,
278
- paymentProfileId,
279
- provider: 'stripe',
280
- providerPlanId,
281
- planKey,
282
- checkoutType: 'subscription',
283
- uiMode: 'custom',
284
- couponId: input.couponId,
285
- analyticsMetadata: input.analyticsMetadata,
286
- customerEmail: input.customerEmail,
287
- returnUrl: input.returnUrl,
288
- userId: input.userId,
289
- anonymousUserId: input.anonymousUserId,
290
- idempotencyKey: input.idempotencyKey,
291
- environment: input.environment,
292
- },
269
+ body: toV2CheckoutSessionRequest(input, 'subscription'),
293
270
  errorMessage: 'Unable to create subscription checkout',
294
271
  });
295
272
  }
@@ -323,6 +300,13 @@ class FunnelSdkService {
323
300
  });
324
301
  }
325
302
  async createOneTimeCheckout(input) {
303
+ if (resolveRuntimeConfig(input.runtimeConfig).paymentsApiVersion === 'v2') {
304
+ return this.postJson('/sdk/public/v2/payments/checkout-sessions', {
305
+ runtimeConfig: input.runtimeConfig,
306
+ body: toV2CheckoutSessionRequest(input, 'one_time'),
307
+ errorMessage: 'Unable to create one-time checkout',
308
+ });
309
+ }
326
310
  return this.postJson('/sdk/public/payments/one-time-checkout', {
327
311
  runtimeConfig: input.runtimeConfig,
328
312
  body: {
@@ -344,6 +328,14 @@ class FunnelSdkService {
344
328
  });
345
329
  }
346
330
  async updateSubscriptionCheckoutPlan(input) {
331
+ const { paymentsApiVersion } = resolveRuntimeConfig(input.runtimeConfig);
332
+ if (paymentsApiVersion === 'v2') {
333
+ return this.patchJson(`/sdk/public/v2/payments/checkout-sessions/${encodeURIComponent(input.checkoutSessionId)}`, {
334
+ runtimeConfig: input.runtimeConfig,
335
+ body: Object.assign(Object.assign({}, toV2CheckoutSelection(input)), { couponId: input.couponId, analyticsMetadata: input.analyticsMetadata, environment: input.environment }),
336
+ errorMessage: 'Unable to update subscription checkout',
337
+ });
338
+ }
347
339
  return this.postJson('/sdk/public/payments/subscriptions/update-plan', {
348
340
  runtimeConfig: input.runtimeConfig,
349
341
  body: {
@@ -367,6 +359,14 @@ class FunnelSdkService {
367
359
  });
368
360
  }
369
361
  async verifySubscriptionCheckout(input) {
362
+ const { funnelId, paymentsApiVersion } = resolveRuntimeConfig(input.runtimeConfig);
363
+ if (paymentsApiVersion === 'v2') {
364
+ return this.getJson(`/sdk/public/v2/payments/checkout-sessions/${encodeURIComponent(input.checkoutSessionId)}`, {
365
+ runtimeConfig: input.runtimeConfig,
366
+ searchParams: { funnelId },
367
+ errorMessage: 'Unable to verify subscription checkout',
368
+ });
369
+ }
370
370
  return this.postJson('/sdk/public/payments/subscriptions/checkout-session-status', {
371
371
  runtimeConfig: input.runtimeConfig,
372
372
  body: {
@@ -396,6 +396,18 @@ class FunnelSdkService {
396
396
  });
397
397
  }
398
398
  async chargeOneClickPayment(input) {
399
+ var _a, _b, _c;
400
+ if (resolveRuntimeConfig(input.runtimeConfig).paymentsApiVersion === 'v2') {
401
+ const selection = toV2CheckoutSelection(input);
402
+ if (!((_a = input.checkoutSessionId) === null || _a === void 0 ? void 0 : _a.trim()) || !((_b = input.userId) === null || _b === void 0 ? void 0 : _b.trim()) || !((_c = input.idempotencyKey) === null || _c === void 0 ? void 0 : _c.trim())) {
403
+ throw new Error('V2 one-click requires checkoutSessionId, userId and idempotencyKey');
404
+ }
405
+ return this.postJson('/sdk/public/v2/payments/one-click', {
406
+ runtimeConfig: input.runtimeConfig,
407
+ body: Object.assign(Object.assign({}, selection), { checkoutSessionId: input.checkoutSessionId, userId: input.userId, idempotencyKey: input.idempotencyKey, analyticsMetadata: input.analyticsMetadata, environment: input.environment }),
408
+ errorMessage: 'Unable to charge one-click payment',
409
+ });
410
+ }
399
411
  return this.postJson('/sdk/public/payments/one-click', {
400
412
  runtimeConfig: input.runtimeConfig,
401
413
  body: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/runtime",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",