@fantastic.dev/better-stripe 0.41.0 → 0.42.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +1 -1
  3. package/dist/client/balanceFundingWorkflow/client.d.ts +172 -0
  4. package/dist/client/balanceFundingWorkflow/client.d.ts.map +1 -0
  5. package/dist/client/balanceFundingWorkflow/client.js +289 -0
  6. package/dist/client/balanceFundingWorkflow/client.js.map +1 -0
  7. package/dist/client/billing/balanceFunding.d.ts +5 -0
  8. package/dist/client/billing/balanceFunding.d.ts.map +1 -1
  9. package/dist/client/billing/balanceFunding.js +30 -0
  10. package/dist/client/billing/balanceFunding.js.map +1 -1
  11. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +366 -2
  12. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  13. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +199 -17
  14. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  15. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +366 -2
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  17. package/dist/client/connect/payoutSyncTestHelpers.d.ts +199 -17
  18. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  19. package/dist/client/index.d.ts +3 -0
  20. package/dist/client/index.d.ts.map +1 -1
  21. package/dist/client/index.js +4 -0
  22. package/dist/client/index.js.map +1 -1
  23. package/dist/component/_generated/api.d.ts +18 -0
  24. package/dist/component/_generated/api.d.ts.map +1 -1
  25. package/dist/component/_generated/api.js.map +1 -1
  26. package/dist/component/_generated/component.d.ts +473 -0
  27. package/dist/component/_generated/component.d.ts.map +1 -1
  28. package/dist/component/balanceFunding/helpers.d.ts +42 -0
  29. package/dist/component/balanceFunding/helpers.d.ts.map +1 -1
  30. package/dist/component/balanceFunding/internal/actions.d.ts +39 -0
  31. package/dist/component/balanceFunding/internal/actions.d.ts.map +1 -0
  32. package/dist/component/balanceFunding/internal/actions.js +80 -0
  33. package/dist/component/balanceFunding/internal/actions.js.map +1 -0
  34. package/dist/component/balanceFunding/internal/mutations.d.ts +55 -0
  35. package/dist/component/balanceFunding/internal/mutations.d.ts.map +1 -0
  36. package/dist/component/balanceFunding/internal/mutations.js +54 -0
  37. package/dist/component/balanceFunding/internal/mutations.js.map +1 -0
  38. package/dist/component/balanceFunding/internal/queries.d.ts +116 -0
  39. package/dist/component/balanceFunding/internal/queries.d.ts.map +1 -0
  40. package/dist/component/balanceFunding/internal/queries.js +35 -0
  41. package/dist/component/balanceFunding/internal/queries.js.map +1 -0
  42. package/dist/component/balanceFunding/internal/workflow.d.ts +10 -0
  43. package/dist/component/balanceFunding/internal/workflow.d.ts.map +1 -0
  44. package/dist/component/balanceFunding/internal/workflow.js +31 -0
  45. package/dist/component/balanceFunding/internal/workflow.js.map +1 -0
  46. package/dist/component/balanceFunding/lifecycleHandlers.d.ts +126 -0
  47. package/dist/component/balanceFunding/lifecycleHandlers.d.ts.map +1 -1
  48. package/dist/component/balanceFunding/mutations.d.ts +403 -0
  49. package/dist/component/balanceFunding/mutations.d.ts.map +1 -1
  50. package/dist/component/balanceFunding/mutations.js +24 -0
  51. package/dist/component/balanceFunding/mutations.js.map +1 -1
  52. package/dist/component/balanceFunding/preparationHandlers.d.ts +42 -0
  53. package/dist/component/balanceFunding/preparationHandlers.d.ts.map +1 -1
  54. package/dist/component/balanceFunding/queries.d.ts +78 -0
  55. package/dist/component/balanceFunding/queries.d.ts.map +1 -1
  56. package/dist/component/balanceFunding/queries.js +35 -0
  57. package/dist/component/balanceFunding/queries.js.map +1 -1
  58. package/dist/component/balanceFunding/receipt.d.ts +5 -0
  59. package/dist/component/balanceFunding/receipt.d.ts.map +1 -0
  60. package/dist/component/balanceFunding/receipt.js +37 -0
  61. package/dist/component/balanceFunding/receipt.js.map +1 -0
  62. package/dist/component/balanceFunding/schema.d.ts +506 -2
  63. package/dist/component/balanceFunding/schema.d.ts.map +1 -1
  64. package/dist/component/balanceFunding/schema.js +2 -0
  65. package/dist/component/balanceFunding/schema.js.map +1 -1
  66. package/dist/component/balanceFunding/stateHandlers.d.ts +50 -8
  67. package/dist/component/balanceFunding/stateHandlers.d.ts.map +1 -1
  68. package/dist/component/balanceFunding/workflowControl.d.ts +19 -0
  69. package/dist/component/balanceFunding/workflowControl.d.ts.map +1 -0
  70. package/dist/component/balanceFunding/workflowControl.js +149 -0
  71. package/dist/component/balanceFunding/workflowControl.js.map +1 -0
  72. package/dist/component/balanceFunding/workflowSchema.d.ts +356 -0
  73. package/dist/component/balanceFunding/workflowSchema.d.ts.map +1 -0
  74. package/dist/component/balanceFunding/workflowSchema.js +48 -0
  75. package/dist/component/balanceFunding/workflowSchema.js.map +1 -0
  76. package/dist/component/balanceFunding/workflowState.d.ts +157 -0
  77. package/dist/component/balanceFunding/workflowState.d.ts.map +1 -0
  78. package/dist/component/balanceFunding/workflowState.js +194 -0
  79. package/dist/component/balanceFunding/workflowState.js.map +1 -0
  80. package/dist/component/balanceFunding/workflowValidators.d.ts +306 -0
  81. package/dist/component/balanceFunding/workflowValidators.d.ts.map +1 -0
  82. package/dist/component/balanceFunding/workflowValidators.js +27 -0
  83. package/dist/component/balanceFunding/workflowValidators.js.map +1 -0
  84. package/dist/component/earnings/triggers.d.ts +102 -18
  85. package/dist/component/earnings/triggers.d.ts.map +1 -1
  86. package/dist/component/schema.d.ts +183 -1
  87. package/package.json +1 -1
  88. package/src/client/balanceFundingWorkflow/capture.test.ts +144 -0
  89. package/src/client/balanceFundingWorkflow/client.ts +419 -0
  90. package/src/client/billing/balanceFunding.ts +50 -0
  91. package/src/client/index.ts +15 -0
  92. package/src/component/_generated/api.ts +18 -0
  93. package/src/component/_generated/component.ts +524 -0
  94. package/src/component/balanceFunding/internal/actions.ts +107 -0
  95. package/src/component/balanceFunding/internal/mutations.ts +66 -0
  96. package/src/component/balanceFunding/internal/queries.ts +34 -0
  97. package/src/component/balanceFunding/internal/workflow.ts +49 -0
  98. package/src/component/balanceFunding/mutations.ts +34 -0
  99. package/src/component/balanceFunding/queries.ts +38 -0
  100. package/src/component/balanceFunding/receipt.ts +48 -0
  101. package/src/component/balanceFunding/schema.ts +2 -0
  102. package/src/component/balanceFunding/scopeLookup.test.ts +94 -0
  103. package/src/component/balanceFunding/workflow.fixtures.ts +366 -0
  104. package/src/component/balanceFunding/workflow.test.ts +552 -0
  105. package/src/component/balanceFunding/workflowControl.ts +198 -0
  106. package/src/component/balanceFunding/workflowSchema.ts +78 -0
  107. package/src/component/balanceFunding/workflowState.ts +285 -0
  108. package/src/component/balanceFunding/workflowValidators.ts +40 -0
@@ -0,0 +1,366 @@
1
+ // @vitest-environment edge-runtime
2
+ import workflowTest from "@convex-dev/workflow/test";
3
+ import type { WorkflowId } from "@convex-dev/workflow";
4
+ import { executionWorkflow } from "../durableExecution/runtime.js";
5
+ import { convexTest } from "convex-test";
6
+ import {
7
+ anyApi,
8
+ componentsGeneric,
9
+ defineSchema,
10
+ internalActionGeneric,
11
+ internalQueryGeneric,
12
+ internalMutationGeneric,
13
+ } from "convex/server";
14
+ import { v } from "convex/values";
15
+ import Stripe from "stripe";
16
+ import { afterEach, beforeEach, vi } from "vitest";
17
+ import { BetterStripe } from "../../client/index.js";
18
+ import { getStripeClient, type Component } from "../../client/helpers.js";
19
+ import { STRIPE_CUSTOMER_PREVIEW_API_VERSION as version } from "../../client/apiVersion.js";
20
+ import { internal } from "../_generated/api.js";
21
+ import { workflowRefFields } from "./workflowValidators.js";
22
+ import { provisioningReceipt } from "./workflowSchema.js";
23
+ import { schema, modules } from "../../test.js";
24
+
25
+ export const nested = componentsGeneric();
26
+ export const scope = { accountId: "acct_owner", scopeKey: "opaque-scope" };
27
+ export const stripeSubscriptionId = "sub_original";
28
+ export const original = {
29
+ customerAccount: scope.accountId,
30
+ stripeSubscriptionId,
31
+ previousPaymentMethodId: "pm_card",
32
+ previousPaymentMethodTypes: ["card"],
33
+ };
34
+ const phaseArgs = {
35
+ ...{
36
+ accountId: v.string(),
37
+ scopeKey: v.string(),
38
+ operationId: v.string(),
39
+ version: v.number(),
40
+ },
41
+ stripeSubscriptionId: v.string(),
42
+ providerApiVersion: v.string(),
43
+ definitionVersion: v.string(),
44
+ phase: v.union(v.literal("create"), v.literal("attach"), v.literal("apply")),
45
+ providerKey: v.string(),
46
+ };
47
+ let requests: {
48
+ method: string;
49
+ path: string;
50
+ version: string | null;
51
+ key: string | null;
52
+ }[];
53
+ let methodOwner: string | null;
54
+ let unknownPhase: "create" | "attach" | null;
55
+ let extraFields: boolean;
56
+ let enabled: boolean;
57
+ let lostApply: boolean;
58
+ let checkpointFault: boolean;
59
+ let cancelPhase: "create" | "attach" | "apply" | null;
60
+ function client() {
61
+ const sdk = new BetterStripe(nested.betterStripe as unknown as Component, {
62
+ stripeApiVersion: version,
63
+ STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
64
+ });
65
+ return sdk.balanceFundingWorkflow;
66
+ }
67
+
68
+ const host = {
69
+ prepare: internalActionGeneric({
70
+ args: { requestKey: v.string() },
71
+ returns: v.any(),
72
+ handler: async (ctx, { requestKey }) =>
73
+ client().prepare(ctx, {
74
+ ...scope,
75
+ stripeSubscriptionId,
76
+ requestKey,
77
+ consent: true,
78
+ consentAt: 100,
79
+ consentReference: "original-consent",
80
+ providerAction: anyApi.host.provider,
81
+ }),
82
+ }),
83
+ provider: internalActionGeneric({
84
+ args: phaseArgs,
85
+ returns: v.any(),
86
+ handler: async (ctx, args) => {
87
+ const result = await client().executeProviderPhase(ctx, args);
88
+ if (cancelPhase === args.phase) {
89
+ cancelPhase = null;
90
+ await client().cancel(ctx, {
91
+ accountId: args.accountId,
92
+ scopeKey: args.scopeKey,
93
+ stripeSubscriptionId: args.stripeSubscriptionId,
94
+ operationId: args.operationId,
95
+ version: args.version,
96
+ });
97
+ }
98
+ if (!extraFields) return result;
99
+ const response = result as { status: string; value?: object };
100
+ return {
101
+ ...response,
102
+ value: {
103
+ ...response.value,
104
+ rawProviderObject: "raw_provider_private_field",
105
+ },
106
+ };
107
+ },
108
+ }),
109
+ apply: internalActionGeneric({
110
+ args: {
111
+ preparationId: v.string(),
112
+ enabled: v.boolean(),
113
+ requestKey: v.string(),
114
+ },
115
+ returns: v.any(),
116
+ handler: (ctx, args) =>
117
+ new BetterStripe(nested.betterStripe as unknown as Component, {
118
+ stripeApiVersion: version,
119
+ STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
120
+ }).balanceFundingWorkflow.apply(ctx, {
121
+ ...args,
122
+ ...scope,
123
+ stripeSubscriptionId,
124
+ consent: true,
125
+ providerAction: anyApi.host.provider,
126
+ }),
127
+ }),
128
+ resume: internalActionGeneric({
129
+ args: { operationId: v.string(), version: v.number() },
130
+ returns: v.any(),
131
+ handler: async (ctx, args) =>
132
+ client().resume(ctx, { ...args, ...scope, stripeSubscriptionId }),
133
+ }),
134
+ inspect: internalActionGeneric({
135
+ args: { operationId: v.string(), version: v.number() },
136
+ returns: v.any(),
137
+ handler: (ctx, args) =>
138
+ client().inspect(ctx, { ...args, ...scope, stripeSubscriptionId }),
139
+ }),
140
+ cancel: internalActionGeneric({
141
+ args: { operationId: v.string(), version: v.number() },
142
+ returns: v.any(),
143
+ handler: async (ctx, args) =>
144
+ client().cancel(ctx, { ...args, ...scope, stripeSubscriptionId }),
145
+ }),
146
+ };
147
+ export function setup(
148
+ failFinish = false,
149
+ failReceipt = false,
150
+ stopBeforeApplyRecord = false,
151
+ ) {
152
+ let attempts = 0;
153
+ let receiptAttempts = 0;
154
+ const t = convexTest(defineSchema({}), {
155
+ "./_generated/api.ts": async () => ({}),
156
+ "./host.ts": async () => host,
157
+ });
158
+ t.registerComponent("betterStripe", schema, {
159
+ ...modules,
160
+ ...(stopBeforeApplyRecord
161
+ ? {
162
+ "./component/balanceFunding/internal/workflow.ts": async () => {
163
+ const modulePath = "./internal/workflow.ts";
164
+ const actual = await import(/* @vite-ignore */ modulePath);
165
+ if (!checkpointFault) return actual;
166
+ return {
167
+ ...actual,
168
+ run: executionWorkflow.define({
169
+ args: workflowRefFields,
170
+ returns: v.null(),
171
+ handler: async (step, args): Promise<null> => {
172
+ const plan = await step.runQuery(
173
+ internal.balanceFunding.internal.queries.plan,
174
+ args,
175
+ { name: "plan-v1" },
176
+ );
177
+ await step.runAction(
178
+ internal.balanceFunding.internal.actions.bridge,
179
+ { ...args, phase: "apply", providerKey: plan.applyKey },
180
+ { name: "apply-provider-v1", retry: false },
181
+ );
182
+ throw new Error(
183
+ "controlled stop at durable provider checkpoint",
184
+ );
185
+ },
186
+ }),
187
+ };
188
+ },
189
+ }
190
+ : {}),
191
+ ...(failFinish || failReceipt
192
+ ? {
193
+ "./component/balanceFunding/internal/mutations.ts": async () => {
194
+ const modulePath = "./internal/mutations.ts";
195
+ const statePath = "./workflowState.ts";
196
+ const actual = await import(/* @vite-ignore */ modulePath);
197
+ const state = await import(/* @vite-ignore */ statePath);
198
+ return {
199
+ ...actual,
200
+ recordProviderReceipt: internalMutationGeneric({
201
+ args: { ...phaseArgs, receipt: provisioningReceipt },
202
+ returns: v.null(),
203
+ handler: async (ctx, args) => {
204
+ if (
205
+ failReceipt &&
206
+ args.phase ===
207
+ (stopBeforeApplyRecord ? "apply" : "attach") &&
208
+ receiptAttempts++ < 2
209
+ )
210
+ throw new Error("controlled receipt-store outage");
211
+ return state.recordProviderReceiptHandler(ctx, args);
212
+ },
213
+ }),
214
+ finishFromReceipts: internalMutationGeneric({
215
+ args: {
216
+ accountId: v.string(),
217
+ scopeKey: v.string(),
218
+ operationId: v.string(),
219
+ version: v.number(),
220
+ stripeSubscriptionId: v.string(),
221
+ providerApiVersion: v.string(),
222
+ definitionVersion: v.string(),
223
+ },
224
+ returns: v.null(),
225
+ handler: async (ctx, args) => {
226
+ if (failFinish && ++attempts === 1)
227
+ throw new Error("controlled receipt-finish outage");
228
+ return state.finishFromReceiptsHandler(ctx, args);
229
+ },
230
+ }),
231
+ };
232
+ },
233
+ }
234
+ : {}),
235
+ "./component/fundingWorkflowFixture.ts": async () => ({
236
+ status: internalQueryGeneric({
237
+ args: { id: v.string() },
238
+ returns: v.any(),
239
+ handler: (ctx, { id }) =>
240
+ executionWorkflow.status(ctx, id as WorkflowId),
241
+ }),
242
+ cleanup: internalMutationGeneric({
243
+ args: { id: v.string() },
244
+ returns: v.any(),
245
+ handler: (ctx, args) =>
246
+ executionWorkflow.cleanup(ctx, args.id as WorkflowId),
247
+ }),
248
+ history: internalQueryGeneric({
249
+ args: { id: v.string() },
250
+ returns: v.any(),
251
+ handler: (ctx, { id }) =>
252
+ executionWorkflow.listSteps(ctx, id as WorkflowId, {
253
+ paginationOpts: { cursor: null, numItems: 30 },
254
+ }),
255
+ }),
256
+ }),
257
+ });
258
+ workflowTest.register(t, "betterStripe/workflow");
259
+ return t;
260
+ }
261
+ beforeEach(() => {
262
+ vi.useFakeTimers();
263
+ const secret = "sk_test_funding_" + crypto.randomUUID();
264
+ vi.stubEnv("STRIPE_SECRET_KEY", secret);
265
+ requests = [];
266
+ methodOwner = null;
267
+ unknownPhase = null;
268
+ extraFields = false;
269
+ enabled = false;
270
+ lostApply = false;
271
+ checkpointFault = false;
272
+ cancelPhase = null;
273
+ const stripe = getStripeClient(secret, version);
274
+ (
275
+ stripe as unknown as {
276
+ _setApiField: (name: string, value: unknown) => void;
277
+ }
278
+ )._setApiField(
279
+ "httpClient",
280
+ Stripe.createFetchHttpClient(async (url, init) => {
281
+ const path = new URL(String(url)).pathname;
282
+ const method = init?.method ?? "GET";
283
+ const headers = new Headers(init?.headers);
284
+ requests.push({
285
+ method,
286
+ path,
287
+ version: headers.get("Stripe-Version"),
288
+ key: headers.get("Idempotency-Key"),
289
+ });
290
+ if (path.includes("subscriptions") && method === "POST") {
291
+ const body = new URLSearchParams(String(init?.body));
292
+ enabled = body.get("default_payment_method") === "pm_balance";
293
+ if (lostApply) {
294
+ lostApply = false;
295
+ throw new TypeError("private apply response");
296
+ }
297
+ }
298
+ if (path.endsWith("/attach") && method === "POST")
299
+ methodOwner = scope.accountId;
300
+ if (
301
+ (unknownPhase === "create" &&
302
+ path === "/v1/payment_methods" &&
303
+ method === "POST") ||
304
+ (unknownPhase === "attach" &&
305
+ path.endsWith("/attach") &&
306
+ method === "POST")
307
+ ) {
308
+ unknownPhase = null;
309
+ throw new TypeError("raw_provider_private_failure");
310
+ }
311
+ const body = path.includes("subscriptions")
312
+ ? {
313
+ id: stripeSubscriptionId,
314
+ object: "subscription",
315
+ customer_account: scope.accountId,
316
+ status: "active",
317
+ default_payment_method: enabled ? "pm_balance" : "pm_card",
318
+ payment_settings: {
319
+ payment_method_types: [enabled ? "stripe_balance" : "card"],
320
+ },
321
+ }
322
+ : path.endsWith("/pm_card")
323
+ ? {
324
+ id: "pm_card",
325
+ object: "payment_method",
326
+ type: "card",
327
+ customer_account: scope.accountId,
328
+ }
329
+ : {
330
+ id: "pm_balance",
331
+ object: "payment_method",
332
+ type: "stripe_balance",
333
+ customer_account: methodOwner,
334
+ };
335
+ return new Response(JSON.stringify(body), { status: 200 });
336
+ }),
337
+ );
338
+ });
339
+ afterEach(() => {
340
+ vi.useRealTimers();
341
+ vi.unstubAllEnvs();
342
+ });
343
+
344
+ export const fixture = {
345
+ set checkpointFault(value: boolean) {
346
+ checkpointFault = value;
347
+ },
348
+ set cancelPhase(value: "create" | "attach" | "apply" | null) {
349
+ cancelPhase = value;
350
+ },
351
+ get requests() {
352
+ return requests;
353
+ },
354
+ set unknownPhase(value: "create" | "attach" | null) {
355
+ unknownPhase = value;
356
+ },
357
+ set extraFields(value: boolean) {
358
+ extraFields = value;
359
+ },
360
+ set lostApply(value: boolean) {
361
+ lostApply = value;
362
+ },
363
+ get enabled() {
364
+ return enabled;
365
+ },
366
+ };