@fantastic.dev/better-stripe 0.45.0 → 0.45.2
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/CHANGELOG.md +14 -0
- package/MIGRATION.md +6 -4
- package/README.md +8 -6
- package/docs/affiliate-clawbacks.md +3 -3
- package/docs/architecture/component-extraction.md +415 -0
- package/docs/architecture/durable-execution.md +200 -0
- package/docs/architecture/recovery-workflow.md +69 -0
- package/docs/balance-funding-state.md +114 -0
- package/docs/balance-funding-workflow.md +53 -0
- package/docs/example-balance-funding-cutover.md +94 -0
- package/docs/example-recovery-cutover.md +21 -0
- package/docs/processing-cost-reconciliation.md +23 -0
- package/docs/release-handoff.md +196 -0
- package/docs/releasing.md +41 -0
- package/package.json +12 -2
- package/src/component/durableExecution/crossDomain.test.ts +241 -0
- package/src/component/durableExecution/crossDomainBoundaries.test.ts +233 -0
- package/src/component/durableExecution/crossDomainDisputes.test.ts +108 -0
- package/src/component/durableExecution/crossDomainMigration.test.ts +192 -0
- package/src/component/durableExecution/crossDomainWebhooks.test.ts +122 -0
- package/src/testing/crossDomain/fixture.ts +96 -0
- package/src/testing/crossDomain/host.ts +288 -0
- package/src/testing/crossDomain/transport.ts +293 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// One real BetterStripe component and its nested runtime; only provider HTTP responses are synthetic.
|
|
2
|
+
import workflowTest from "@convex-dev/workflow/test";
|
|
3
|
+
import { convexTest } from "convex-test";
|
|
4
|
+
import {
|
|
5
|
+
defineSchema,
|
|
6
|
+
defineTable,
|
|
7
|
+
internalMutationGeneric,
|
|
8
|
+
internalQueryGeneric,
|
|
9
|
+
} from "convex/server";
|
|
10
|
+
import type { WorkflowId } from "@convex-dev/workflow";
|
|
11
|
+
import { v } from "convex/values";
|
|
12
|
+
import { afterEach, beforeEach, vi } from "vitest";
|
|
13
|
+
import { schema, modules } from "../../test.js";
|
|
14
|
+
import { getStripeClient } from "../../client/helpers.js";
|
|
15
|
+
import { executionWorkflow } from "../../component/durableExecution/runtime.js";
|
|
16
|
+
import { host } from "./host.js";
|
|
17
|
+
import { reset, httpClient, secret, preview } from "./transport.js";
|
|
18
|
+
export { scope, fundingScope } from "./host.js";
|
|
19
|
+
export { wire } from "./transport.js";
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.useFakeTimers();
|
|
22
|
+
reset();
|
|
23
|
+
// Funding has no transport option; configure only the test-key SDK cache entry.
|
|
24
|
+
const stripe = getStripeClient(secret, preview);
|
|
25
|
+
const set = stripe as unknown as {
|
|
26
|
+
_setApiField: (key: string, value: unknown) => void;
|
|
27
|
+
};
|
|
28
|
+
set._setApiField("httpClient", httpClient);
|
|
29
|
+
set._setApiField("maxNetworkRetries", 0);
|
|
30
|
+
});
|
|
31
|
+
afterEach(() => vi.useRealTimers());
|
|
32
|
+
export function fixture() {
|
|
33
|
+
const t = convexTest(
|
|
34
|
+
defineSchema({
|
|
35
|
+
reports: defineTable({
|
|
36
|
+
domain: v.string(),
|
|
37
|
+
operationId: v.string(),
|
|
38
|
+
version: v.number(),
|
|
39
|
+
amount: v.number(),
|
|
40
|
+
}).index("by_operation", ["operationId"]),
|
|
41
|
+
}),
|
|
42
|
+
{
|
|
43
|
+
"./_generated/api.ts": async () => ({}),
|
|
44
|
+
"./host.ts": async () => host,
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
t.registerComponent("betterStripe", schema, {
|
|
48
|
+
...modules,
|
|
49
|
+
"./component/crossDomainFixture.ts": async () => ({
|
|
50
|
+
cleanFundingHistory: internalMutationGeneric({
|
|
51
|
+
args: { operationId: v.id("balanceFundingOperations") },
|
|
52
|
+
returns: v.any(),
|
|
53
|
+
handler: async (ctx, args) => {
|
|
54
|
+
const op = await ctx.db.get(
|
|
55
|
+
"balanceFundingOperations",
|
|
56
|
+
args.operationId,
|
|
57
|
+
);
|
|
58
|
+
if (!op?.execution?.workflowId)
|
|
59
|
+
throw new Error("Missing test workflow");
|
|
60
|
+
return executionWorkflow.cleanup(
|
|
61
|
+
ctx,
|
|
62
|
+
op.execution.workflowId as WorkflowId,
|
|
63
|
+
);
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
journals: internalQueryGeneric({
|
|
67
|
+
args: {},
|
|
68
|
+
returns: v.any(),
|
|
69
|
+
handler: async (ctx) => {
|
|
70
|
+
const ids: string[] = [];
|
|
71
|
+
for (const op of await ctx.db
|
|
72
|
+
.query("balanceFundingOperations")
|
|
73
|
+
.take(20))
|
|
74
|
+
if (op.execution?.workflowId) ids.push(op.execution.workflowId);
|
|
75
|
+
for (const op of await ctx.db
|
|
76
|
+
.query("recoveryWorkflowOperations")
|
|
77
|
+
.take(20))
|
|
78
|
+
if (op.workflowId) ids.push(op.workflowId);
|
|
79
|
+
for (const op of await ctx.db
|
|
80
|
+
.query("processingCostReconciliationOperations")
|
|
81
|
+
.take(20))
|
|
82
|
+
if (op.workflowId) ids.push(op.workflowId);
|
|
83
|
+
return Promise.all(
|
|
84
|
+
ids.map((id) =>
|
|
85
|
+
executionWorkflow.listSteps(ctx, id as WorkflowId, {
|
|
86
|
+
paginationOpts: { cursor: null, numItems: 100 },
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
workflowTest.register(t, "betterStripe/workflow");
|
|
95
|
+
return t;
|
|
96
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
anyApi,
|
|
3
|
+
componentsGeneric,
|
|
4
|
+
internalActionGeneric as action,
|
|
5
|
+
internalMutationGeneric as mutation,
|
|
6
|
+
} from "convex/server";
|
|
7
|
+
import { v } from "convex/values";
|
|
8
|
+
import { BetterStripe } from "../../client/index.js";
|
|
9
|
+
import type { Component } from "../../client/helpers.js";
|
|
10
|
+
import { scopeFields } from "../../component/costRecovery/schema.js";
|
|
11
|
+
import { httpClient, secret, ga, preview, wire } from "./transport.js";
|
|
12
|
+
const nested = componentsGeneric().betterStripe;
|
|
13
|
+
const sdk = () =>
|
|
14
|
+
new BetterStripe(nested as unknown as Component, {
|
|
15
|
+
STRIPE_SECRET_KEY: secret,
|
|
16
|
+
stripeApiVersion: preview,
|
|
17
|
+
});
|
|
18
|
+
const recovery = () => sdk().costRecovery;
|
|
19
|
+
export const scope = (name = "a") => ({
|
|
20
|
+
accountId: `acct_${name}`,
|
|
21
|
+
scopeKey: `cost:${name}`,
|
|
22
|
+
currency: "usd",
|
|
23
|
+
});
|
|
24
|
+
export const fundingScope = (name = "a") => ({
|
|
25
|
+
accountId: `acct_${name}`,
|
|
26
|
+
scopeKey: `funding:${name}`,
|
|
27
|
+
stripeSubscriptionId: `sub_${name}`,
|
|
28
|
+
});
|
|
29
|
+
const ref = { ...scopeFields, operationId: v.string(), version: v.number() };
|
|
30
|
+
const groupRef = { ...ref, groupKey: v.string() };
|
|
31
|
+
const phase = {
|
|
32
|
+
accountId: v.string(),
|
|
33
|
+
scopeKey: v.string(),
|
|
34
|
+
operationId: v.string(),
|
|
35
|
+
version: v.number(),
|
|
36
|
+
stripeSubscriptionId: v.string(),
|
|
37
|
+
providerApiVersion: v.string(),
|
|
38
|
+
definitionVersion: v.string(),
|
|
39
|
+
phase: v.union(v.literal("create"), v.literal("attach"), v.literal("apply")),
|
|
40
|
+
providerKey: v.string(),
|
|
41
|
+
};
|
|
42
|
+
const groupArgs = {
|
|
43
|
+
name: v.string(),
|
|
44
|
+
kind: v.union(v.literal("invoice"), v.literal("settlement")),
|
|
45
|
+
};
|
|
46
|
+
export const host = {
|
|
47
|
+
fundingControl: action({
|
|
48
|
+
args: {
|
|
49
|
+
operationId: v.string(),
|
|
50
|
+
version: v.number(),
|
|
51
|
+
command: v.union(v.literal("resume"), v.literal("cancel")),
|
|
52
|
+
},
|
|
53
|
+
returns: v.any(),
|
|
54
|
+
handler: (ctx, { command, ...op }) =>
|
|
55
|
+
sdk().balanceFundingWorkflow[command](ctx, { ...fundingScope(), ...op }),
|
|
56
|
+
}),
|
|
57
|
+
funding: action({
|
|
58
|
+
args: { requestKey: v.string() },
|
|
59
|
+
returns: v.any(),
|
|
60
|
+
handler: (ctx, args) =>
|
|
61
|
+
sdk().balanceFundingWorkflow.prepare(ctx, {
|
|
62
|
+
...fundingScope(),
|
|
63
|
+
...args,
|
|
64
|
+
consent: true,
|
|
65
|
+
consentAt: 123,
|
|
66
|
+
consentReference: "original-consent",
|
|
67
|
+
providerAction: anyApi.host.fundingProvider,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
apply: action({
|
|
71
|
+
args: {
|
|
72
|
+
preparationId: v.string(),
|
|
73
|
+
enabled: v.boolean(),
|
|
74
|
+
requestKey: v.string(),
|
|
75
|
+
},
|
|
76
|
+
returns: v.any(),
|
|
77
|
+
handler: (ctx, args) =>
|
|
78
|
+
sdk().balanceFundingWorkflow.apply(ctx, {
|
|
79
|
+
...fundingScope(),
|
|
80
|
+
...args,
|
|
81
|
+
...(args.enabled ? { consent: true as const } : {}),
|
|
82
|
+
providerAction: anyApi.host.fundingProvider,
|
|
83
|
+
}),
|
|
84
|
+
}),
|
|
85
|
+
fundingProvider: action({
|
|
86
|
+
args: phase,
|
|
87
|
+
returns: v.any(),
|
|
88
|
+
handler: (ctx, args) =>
|
|
89
|
+
sdk().balanceFundingWorkflow.executeProviderPhase(ctx, args),
|
|
90
|
+
}),
|
|
91
|
+
prepareRecovery: mutation({
|
|
92
|
+
args: { ...groupArgs, amount: v.number() },
|
|
93
|
+
returns: v.any(),
|
|
94
|
+
handler: async (ctx, args) => {
|
|
95
|
+
const s = scope(args.name);
|
|
96
|
+
await recovery().recordClaim(ctx, {
|
|
97
|
+
...s,
|
|
98
|
+
claimKey: `debt:${args.name}`,
|
|
99
|
+
sourceChargeId: `ch_debt_${args.name}`,
|
|
100
|
+
sourceGross: 1000,
|
|
101
|
+
sourceProcessingFee: 30,
|
|
102
|
+
amountDue: args.amount,
|
|
103
|
+
revision: 0,
|
|
104
|
+
});
|
|
105
|
+
return recovery().prepareGroup(ctx, {
|
|
106
|
+
...s,
|
|
107
|
+
groupKey: `${args.kind}:${args.name}`,
|
|
108
|
+
...(args.kind === "settlement"
|
|
109
|
+
? {
|
|
110
|
+
sourceKind: "settlement" as const,
|
|
111
|
+
sourceId: "ch_source",
|
|
112
|
+
sourceGross: 1000,
|
|
113
|
+
sourceProcessingFee: 30,
|
|
114
|
+
}
|
|
115
|
+
: { sourceKind: "invoice" as const, sourceId: `in_${args.name}` }),
|
|
116
|
+
maxDebit: 1000,
|
|
117
|
+
maxCredit: 1000,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
startRecovery: mutation({
|
|
122
|
+
args: {
|
|
123
|
+
...groupArgs,
|
|
124
|
+
requestKey: v.string(),
|
|
125
|
+
affiliate: v.optional(v.boolean()),
|
|
126
|
+
},
|
|
127
|
+
returns: v.any(),
|
|
128
|
+
handler: async (ctx, args) => {
|
|
129
|
+
const s = scope(args.name),
|
|
130
|
+
groupKey = `${args.kind}:${args.name}`;
|
|
131
|
+
const group = await recovery().getGroup(ctx, { groupKey });
|
|
132
|
+
return recovery().startGroup(ctx, {
|
|
133
|
+
...s,
|
|
134
|
+
groupKey,
|
|
135
|
+
requestKey: args.requestKey,
|
|
136
|
+
provider: anyApi.host.recoveryProvider,
|
|
137
|
+
completion: anyApi.host.recoveryProjection,
|
|
138
|
+
providerApiVersion: ga,
|
|
139
|
+
definitionVersion: 1,
|
|
140
|
+
releaseUncollectible: false,
|
|
141
|
+
maxAttempts: 2,
|
|
142
|
+
retryDelayMs: 1,
|
|
143
|
+
...(args.kind === "settlement"
|
|
144
|
+
? {
|
|
145
|
+
allocation: {
|
|
146
|
+
sourceChargeId: "ch_source",
|
|
147
|
+
paymentIntentId: "pi_source",
|
|
148
|
+
sourceInvoiceId: "in_original",
|
|
149
|
+
gross: 1000,
|
|
150
|
+
currency: "usd",
|
|
151
|
+
processingFee: 30,
|
|
152
|
+
platformNet: 70,
|
|
153
|
+
adjustmentAmount: group!.amount,
|
|
154
|
+
transfers: [
|
|
155
|
+
{
|
|
156
|
+
destinationAccountId: s.accountId,
|
|
157
|
+
role: "store" as const,
|
|
158
|
+
amount: 900 - group!.amount - (args.affiliate ? 100 : 0),
|
|
159
|
+
},
|
|
160
|
+
...(args.affiliate
|
|
161
|
+
? [
|
|
162
|
+
{
|
|
163
|
+
destinationAccountId: "acct_affiliate",
|
|
164
|
+
role: "affiliate" as const,
|
|
165
|
+
amount: 100,
|
|
166
|
+
},
|
|
167
|
+
]
|
|
168
|
+
: []),
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
: {}),
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
}),
|
|
176
|
+
recoveryProvider: action({
|
|
177
|
+
args: {
|
|
178
|
+
...groupRef,
|
|
179
|
+
phase: v.union(v.literal("execute"), v.literal("reconcile")),
|
|
180
|
+
providerApiVersion: v.string(),
|
|
181
|
+
definitionVersion: v.number(),
|
|
182
|
+
},
|
|
183
|
+
returns: v.any(),
|
|
184
|
+
handler: (ctx, args) =>
|
|
185
|
+
recovery().executeGroupProvider(ctx, args, {
|
|
186
|
+
stripeSecretKey: secret,
|
|
187
|
+
httpClient,
|
|
188
|
+
}),
|
|
189
|
+
}),
|
|
190
|
+
recoveryProjection: mutation({
|
|
191
|
+
args: {
|
|
192
|
+
...groupRef,
|
|
193
|
+
sourceKind: v.string(),
|
|
194
|
+
sourceId: v.string(),
|
|
195
|
+
status: v.string(),
|
|
196
|
+
cancelRequested: v.boolean(),
|
|
197
|
+
outcome: v.optional(v.string()),
|
|
198
|
+
},
|
|
199
|
+
returns: v.null(),
|
|
200
|
+
handler: async (ctx, args) => {
|
|
201
|
+
const prior = await ctx.db
|
|
202
|
+
.query("reports")
|
|
203
|
+
.withIndex("by_operation", (q) => q.eq("operationId", args.operationId))
|
|
204
|
+
.unique();
|
|
205
|
+
if (!prior)
|
|
206
|
+
await ctx.db.insert("reports", {
|
|
207
|
+
domain: "recovery",
|
|
208
|
+
operationId: args.operationId,
|
|
209
|
+
version: args.version,
|
|
210
|
+
amount: 0,
|
|
211
|
+
});
|
|
212
|
+
if (wire.projectionFailures > 0) {
|
|
213
|
+
wire.projectionFailures--;
|
|
214
|
+
throw new Error("private database projection");
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
},
|
|
218
|
+
}),
|
|
219
|
+
costs: mutation({
|
|
220
|
+
args: {
|
|
221
|
+
requestKey: v.string(),
|
|
222
|
+
expectedRevision: v.number(),
|
|
223
|
+
rollback: v.optional(
|
|
224
|
+
v.object({ groupKey: v.string(), expectedRevision: v.number() }),
|
|
225
|
+
),
|
|
226
|
+
},
|
|
227
|
+
returns: v.any(),
|
|
228
|
+
handler: (ctx, args) =>
|
|
229
|
+
recovery().startProcessingCostReconciliation(ctx, {
|
|
230
|
+
...scope(),
|
|
231
|
+
...args,
|
|
232
|
+
claimKey: "cost:pi_source",
|
|
233
|
+
source: {
|
|
234
|
+
kind: "sale",
|
|
235
|
+
paymentIntentId: "pi_source",
|
|
236
|
+
gross: 1000,
|
|
237
|
+
customerAccountId: "acct_buyer",
|
|
238
|
+
invoiceId: "in_original",
|
|
239
|
+
},
|
|
240
|
+
policy: {
|
|
241
|
+
sellingFee: 100,
|
|
242
|
+
liability: { processingCosts: "uncovered", disputeCosts: "all" },
|
|
243
|
+
rounding: "nearest",
|
|
244
|
+
},
|
|
245
|
+
provider: anyApi.host.costProvider,
|
|
246
|
+
projection: anyApi.host.costProjection,
|
|
247
|
+
providerApiVersion: ga,
|
|
248
|
+
definitionVersion: 1,
|
|
249
|
+
maxAttempts: 2,
|
|
250
|
+
retryDelayMs: 1,
|
|
251
|
+
}),
|
|
252
|
+
}),
|
|
253
|
+
costProvider: action({
|
|
254
|
+
args: {
|
|
255
|
+
...ref,
|
|
256
|
+
providerApiVersion: v.string(),
|
|
257
|
+
definitionVersion: v.number(),
|
|
258
|
+
},
|
|
259
|
+
returns: v.any(),
|
|
260
|
+
handler: (ctx, args) =>
|
|
261
|
+
recovery().observeProcessingCosts(ctx, args, {
|
|
262
|
+
stripeSecretKey: secret,
|
|
263
|
+
httpClient,
|
|
264
|
+
}),
|
|
265
|
+
}),
|
|
266
|
+
costProjection: mutation({
|
|
267
|
+
args: { ...ref, status: v.literal("completed"), result: v.any() },
|
|
268
|
+
returns: v.null(),
|
|
269
|
+
handler: async (ctx, args) => {
|
|
270
|
+
const prior = await ctx.db
|
|
271
|
+
.query("reports")
|
|
272
|
+
.withIndex("by_operation", (q) => q.eq("operationId", args.operationId))
|
|
273
|
+
.unique();
|
|
274
|
+
if (!prior)
|
|
275
|
+
await ctx.db.insert("reports", {
|
|
276
|
+
domain: "cost",
|
|
277
|
+
operationId: args.operationId,
|
|
278
|
+
version: args.version,
|
|
279
|
+
amount: args.result.claim.amountDue,
|
|
280
|
+
});
|
|
281
|
+
if (wire.projectionFailures > 0) {
|
|
282
|
+
wire.projectionFailures--;
|
|
283
|
+
throw new Error("private cost projection");
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
},
|
|
287
|
+
}),
|
|
288
|
+
};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// Test-only Stripe HTTP boundary. Unexpected endpoints fail instead of reaching Stripe.
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
export const secret = "sk_test_cross_domain_runtime_only";
|
|
4
|
+
export const ga = "2026-09-30.endive";
|
|
5
|
+
export const preview = "2026-09-30.preview";
|
|
6
|
+
export const wire = {
|
|
7
|
+
calls: [] as {
|
|
8
|
+
path: string;
|
|
9
|
+
method: string;
|
|
10
|
+
version: string | null;
|
|
11
|
+
key: string | null;
|
|
12
|
+
authenticated: boolean;
|
|
13
|
+
}[],
|
|
14
|
+
enabled: new Set<string>(),
|
|
15
|
+
attached: new Set<string>(),
|
|
16
|
+
transfers: [] as Record<string, unknown>[],
|
|
17
|
+
items: [] as Record<string, unknown>[],
|
|
18
|
+
refunded: false,
|
|
19
|
+
dispute: "none" as "none" | "lost" | "won",
|
|
20
|
+
invoicePaid: false,
|
|
21
|
+
loseApply: false,
|
|
22
|
+
loseTransfer: false,
|
|
23
|
+
loseCreate: false,
|
|
24
|
+
gate: null as Promise<void> | null,
|
|
25
|
+
transferGate: null as Promise<void> | null,
|
|
26
|
+
projectionFailures: 0,
|
|
27
|
+
};
|
|
28
|
+
const list = (data: unknown[]) => ({ object: "list", data, has_more: false });
|
|
29
|
+
const tx = (id: string, source: string, amount: number, fee: number) => ({
|
|
30
|
+
id,
|
|
31
|
+
object: "balance_transaction",
|
|
32
|
+
source,
|
|
33
|
+
amount,
|
|
34
|
+
fee,
|
|
35
|
+
net: amount - fee,
|
|
36
|
+
currency: "usd",
|
|
37
|
+
created: 1,
|
|
38
|
+
fee_details: [],
|
|
39
|
+
});
|
|
40
|
+
export function reset() {
|
|
41
|
+
Object.assign(wire, {
|
|
42
|
+
calls: [],
|
|
43
|
+
enabled: new Set(),
|
|
44
|
+
attached: new Set(),
|
|
45
|
+
transfers: [],
|
|
46
|
+
items: [],
|
|
47
|
+
refunded: false,
|
|
48
|
+
dispute: "none",
|
|
49
|
+
invoicePaid: false,
|
|
50
|
+
loseApply: false,
|
|
51
|
+
loseTransfer: false,
|
|
52
|
+
loseCreate: false,
|
|
53
|
+
gate: null,
|
|
54
|
+
transferGate: null,
|
|
55
|
+
projectionFailures: 0,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
export const fixtureFetch: typeof fetch = async (url, init) => {
|
|
59
|
+
const u = new URL(String(url)),
|
|
60
|
+
path = u.pathname;
|
|
61
|
+
const method = init?.method ?? "GET",
|
|
62
|
+
h = new Headers(init?.headers);
|
|
63
|
+
wire.calls.push({
|
|
64
|
+
path,
|
|
65
|
+
method,
|
|
66
|
+
version: h.get("Stripe-Version"),
|
|
67
|
+
key: h.get("Idempotency-Key"),
|
|
68
|
+
authenticated: h.get("Authorization") === `Bearer ${secret}`,
|
|
69
|
+
});
|
|
70
|
+
const p = new URLSearchParams(String(init?.body ?? ""));
|
|
71
|
+
let body: unknown;
|
|
72
|
+
if (path.includes("/subscriptions/")) {
|
|
73
|
+
const suffix = path.split("_").pop()!;
|
|
74
|
+
if (method === "POST") {
|
|
75
|
+
if (p.get("default_payment_method") === `pm_balance_${suffix}`)
|
|
76
|
+
wire.enabled.add(suffix);
|
|
77
|
+
else wire.enabled.delete(suffix);
|
|
78
|
+
if (wire.loseApply) {
|
|
79
|
+
wire.loseApply = false;
|
|
80
|
+
throw new TypeError("private transport body");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
body = {
|
|
84
|
+
id: `sub_${suffix}`,
|
|
85
|
+
object: "subscription",
|
|
86
|
+
status: "active",
|
|
87
|
+
customer_account: `acct_${suffix}`,
|
|
88
|
+
default_payment_method: wire.enabled.has(suffix)
|
|
89
|
+
? `pm_balance_${suffix}`
|
|
90
|
+
: `pm_card_${suffix}`,
|
|
91
|
+
payment_settings: {
|
|
92
|
+
payment_method_types: [
|
|
93
|
+
wire.enabled.has(suffix) ? "stripe_balance" : "card",
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
} else if (path.includes("payment_methods")) {
|
|
98
|
+
let suffix = path.split("_").pop()!.split("/")[0];
|
|
99
|
+
if (path.endsWith("/payment_methods")) {
|
|
100
|
+
suffix = "a";
|
|
101
|
+
if (wire.gate) await wire.gate;
|
|
102
|
+
if (wire.loseCreate) {
|
|
103
|
+
wire.loseCreate = false;
|
|
104
|
+
throw new TypeError("private create body");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (path.endsWith("/attach")) wire.attached.add(suffix);
|
|
108
|
+
const balance = !path.includes("pm_card_");
|
|
109
|
+
body = {
|
|
110
|
+
id: `pm_${balance ? "balance" : "card"}_${suffix}`,
|
|
111
|
+
object: "payment_method",
|
|
112
|
+
type: balance ? "stripe_balance" : "card",
|
|
113
|
+
customer_account:
|
|
114
|
+
balance && !wire.attached.has(suffix) ? null : `acct_${suffix}`,
|
|
115
|
+
};
|
|
116
|
+
} else if (path.endsWith("/transfers")) {
|
|
117
|
+
if (method === "POST") {
|
|
118
|
+
const transfer = {
|
|
119
|
+
id: `tr_${wire.transfers.length + 1}`,
|
|
120
|
+
object: "transfer",
|
|
121
|
+
amount: Number(p.get("amount")),
|
|
122
|
+
currency: p.get("currency"),
|
|
123
|
+
destination: p.get("destination"),
|
|
124
|
+
source_transaction: p.get("source_transaction"),
|
|
125
|
+
transfer_group: p.get("transfer_group"),
|
|
126
|
+
amount_reversed: 0,
|
|
127
|
+
metadata: {
|
|
128
|
+
bsRole: p.get("metadata[bsRole]"),
|
|
129
|
+
bsSourceCharge: p.get("metadata[bsSourceCharge]"),
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
wire.transfers.push(transfer);
|
|
133
|
+
body = transfer;
|
|
134
|
+
if (wire.transferGate) await wire.transferGate;
|
|
135
|
+
if (wire.loseTransfer) {
|
|
136
|
+
wire.loseTransfer = false;
|
|
137
|
+
throw new TypeError("private transfer body");
|
|
138
|
+
}
|
|
139
|
+
} else
|
|
140
|
+
body = list(
|
|
141
|
+
wire.transfers.filter(
|
|
142
|
+
(t) =>
|
|
143
|
+
!u.searchParams.has("transfer_group") ||
|
|
144
|
+
t.transfer_group === u.searchParams.get("transfer_group"),
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
} else if (path.endsWith("/invoiceitems")) {
|
|
148
|
+
if (method === "POST") {
|
|
149
|
+
const item = {
|
|
150
|
+
id: `ii_${wire.items.length + 1}`,
|
|
151
|
+
object: "invoiceitem",
|
|
152
|
+
invoice: p.get("invoice"),
|
|
153
|
+
customer_account: p.get("customer_account"),
|
|
154
|
+
currency: p.get("currency"),
|
|
155
|
+
amount: Number(p.get("amount")),
|
|
156
|
+
discountable: false,
|
|
157
|
+
metadata: { bsRecoveryGroup: p.get("metadata[bsRecoveryGroup]") },
|
|
158
|
+
};
|
|
159
|
+
wire.items.push(item);
|
|
160
|
+
body = item;
|
|
161
|
+
} else
|
|
162
|
+
body = list(
|
|
163
|
+
wire.items.filter((i) => i.invoice === u.searchParams.get("invoice")),
|
|
164
|
+
);
|
|
165
|
+
} else if (path.includes("/payment_intents/")) {
|
|
166
|
+
body = {
|
|
167
|
+
id: "pi_source",
|
|
168
|
+
object: "payment_intent",
|
|
169
|
+
status: "succeeded",
|
|
170
|
+
amount: 1000,
|
|
171
|
+
amount_received: 1000,
|
|
172
|
+
currency: "usd",
|
|
173
|
+
customer_account: "acct_buyer",
|
|
174
|
+
livemode: false,
|
|
175
|
+
metadata: {},
|
|
176
|
+
latest_charge: charge(),
|
|
177
|
+
};
|
|
178
|
+
} else if (path.includes("/charges/")) body = charge();
|
|
179
|
+
else if (path.endsWith("/refunds"))
|
|
180
|
+
body = list(
|
|
181
|
+
wire.refunded
|
|
182
|
+
? [
|
|
183
|
+
{
|
|
184
|
+
id: "re_source",
|
|
185
|
+
object: "refund",
|
|
186
|
+
amount: 1000,
|
|
187
|
+
status: "succeeded",
|
|
188
|
+
currency: "usd",
|
|
189
|
+
charge: "ch_source",
|
|
190
|
+
payment_intent: "pi_source",
|
|
191
|
+
balance_transaction: tx("txn_refund", "re_source", -1000, 0),
|
|
192
|
+
failure_balance_transaction: null,
|
|
193
|
+
},
|
|
194
|
+
]
|
|
195
|
+
: [],
|
|
196
|
+
);
|
|
197
|
+
else if (path.endsWith("/disputes"))
|
|
198
|
+
body = list(
|
|
199
|
+
wire.dispute === "none"
|
|
200
|
+
? []
|
|
201
|
+
: [
|
|
202
|
+
{
|
|
203
|
+
id: "dp_source",
|
|
204
|
+
object: "dispute",
|
|
205
|
+
amount: 600,
|
|
206
|
+
status: wire.dispute,
|
|
207
|
+
currency: "usd",
|
|
208
|
+
charge: "ch_source",
|
|
209
|
+
payment_intent: "pi_source",
|
|
210
|
+
balance_transactions: [
|
|
211
|
+
tx("txn_dispute", "dp_source", -600, 15),
|
|
212
|
+
...(wire.dispute === "won"
|
|
213
|
+
? [tx("txn_dispute_credit", "dp_source", 600, -15)]
|
|
214
|
+
: []),
|
|
215
|
+
],
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
);
|
|
219
|
+
else if (path.endsWith("/invoice_payments"))
|
|
220
|
+
body = list([
|
|
221
|
+
{
|
|
222
|
+
id: "inpay_source",
|
|
223
|
+
object: "invoice_payment",
|
|
224
|
+
invoice: "in_original",
|
|
225
|
+
status: "paid",
|
|
226
|
+
currency: "usd",
|
|
227
|
+
amount_paid: 1000,
|
|
228
|
+
amount_requested: 1000,
|
|
229
|
+
livemode: false,
|
|
230
|
+
payment: { type: "payment_intent", payment_intent: "pi_source" },
|
|
231
|
+
},
|
|
232
|
+
]);
|
|
233
|
+
else if (path.endsWith("/lines"))
|
|
234
|
+
body = list([
|
|
235
|
+
{
|
|
236
|
+
id: "il_original",
|
|
237
|
+
object: "line_item",
|
|
238
|
+
invoice: "in_original",
|
|
239
|
+
currency: "usd",
|
|
240
|
+
amount: 1000,
|
|
241
|
+
quantity: 1,
|
|
242
|
+
livemode: false,
|
|
243
|
+
parent: {
|
|
244
|
+
type: "invoice_item_details",
|
|
245
|
+
invoice_item_details: { proration: false },
|
|
246
|
+
},
|
|
247
|
+
pricing: {
|
|
248
|
+
type: "price_details",
|
|
249
|
+
price_details: { price: "price_original", product: "prod_original" },
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
]);
|
|
253
|
+
else if (path.includes("/invoices/")) {
|
|
254
|
+
const id = path.split("/").pop()!;
|
|
255
|
+
const original = id === "in_original";
|
|
256
|
+
body = {
|
|
257
|
+
id,
|
|
258
|
+
object: "invoice",
|
|
259
|
+
status: original || wire.invoicePaid ? "paid" : "draft",
|
|
260
|
+
currency: "usd",
|
|
261
|
+
amount_paid: original ? 1000 : 0,
|
|
262
|
+
amount_due: original ? 1000 : 0,
|
|
263
|
+
amount_remaining: 0,
|
|
264
|
+
total: original ? 1000 : 0,
|
|
265
|
+
amount_overpaid: 0,
|
|
266
|
+
customer_account: original ? "acct_buyer" : `acct_${id.split("_").pop()}`,
|
|
267
|
+
livemode: false,
|
|
268
|
+
};
|
|
269
|
+
} else throw new Error(`Unexpected fixture endpoint ${method} ${path}`);
|
|
270
|
+
return new Response(JSON.stringify(body), {
|
|
271
|
+
status: 200,
|
|
272
|
+
headers: { "content-type": "application/json" },
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
export const httpClient = Stripe.createFetchHttpClient(fixtureFetch);
|
|
276
|
+
function charge() {
|
|
277
|
+
return {
|
|
278
|
+
id: "ch_source",
|
|
279
|
+
object: "charge",
|
|
280
|
+
payment_intent: "pi_source",
|
|
281
|
+
amount: 1000,
|
|
282
|
+
amount_captured: 1000,
|
|
283
|
+
currency: "usd",
|
|
284
|
+
paid: true,
|
|
285
|
+
captured: true,
|
|
286
|
+
status: "succeeded",
|
|
287
|
+
amount_refunded: wire.refunded ? 1000 : 0,
|
|
288
|
+
disputed: wire.dispute !== "none",
|
|
289
|
+
livemode: false,
|
|
290
|
+
created: 1,
|
|
291
|
+
balance_transaction: tx("txn_source", "ch_source", 1000, 30),
|
|
292
|
+
};
|
|
293
|
+
}
|