@fantastic.dev/better-stripe 0.45.2 → 0.46.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/CHANGELOG.md +20 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +46 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +23 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +46 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +23 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/refundSync.d.ts +32 -0
- package/dist/client/connect/refundSync.d.ts.map +1 -0
- package/dist/client/connect/refundSync.js +61 -0
- package/dist/client/connect/refundSync.js.map +1 -0
- package/dist/client/connect/refundSyncValidation.d.ts +16 -0
- package/dist/client/connect/refundSyncValidation.d.ts.map +1 -0
- package/dist/client/connect/refundSyncValidation.js +87 -0
- package/dist/client/connect/refundSyncValidation.js.map +1 -0
- package/dist/client/index.d.ts +30 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +12 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +67 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/connect/mutations.d.ts.map +1 -1
- package/dist/component/connect/mutations.js +2 -53
- package/dist/component/connect/mutations.js.map +1 -1
- package/dist/component/connect/refundWrites.d.ts +6 -0
- package/dist/component/connect/refundWrites.d.ts.map +1 -0
- package/dist/component/connect/refundWrites.js +56 -0
- package/dist/component/connect/refundWrites.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +46 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/refundHistory/originalSeller.d.ts +1 -1
- package/dist/component/refundHistory/originalSeller.d.ts.map +1 -1
- package/dist/component/refundHistory/originalSeller.js.map +1 -1
- package/dist/component/refundSync/helpers.d.ts +43 -0
- package/dist/component/refundSync/helpers.d.ts.map +1 -0
- package/dist/component/refundSync/helpers.js +71 -0
- package/dist/component/refundSync/helpers.js.map +1 -0
- package/dist/component/refundSync/mutations.d.ts +56 -0
- package/dist/component/refundSync/mutations.d.ts.map +1 -0
- package/dist/component/refundSync/mutations.js +113 -0
- package/dist/component/refundSync/mutations.js.map +1 -0
- package/dist/component/refundSync/queries.d.ts +12 -0
- package/dist/component/refundSync/queries.d.ts.map +1 -0
- package/dist/component/refundSync/queries.js +10 -0
- package/dist/component/refundSync/queries.js.map +1 -0
- package/dist/component/refundSync/routing.d.ts +8 -0
- package/dist/component/refundSync/routing.d.ts.map +1 -0
- package/dist/component/refundSync/routing.js +42 -0
- package/dist/component/refundSync/routing.js.map +1 -0
- package/dist/component/refundSync/schema.d.ts +25 -0
- package/dist/component/refundSync/schema.d.ts.map +1 -0
- package/dist/component/refundSync/schema.js +15 -0
- package/dist/component/refundSync/schema.js.map +1 -0
- package/dist/component/refundSync/validators.d.ts +164 -0
- package/dist/component/refundSync/validators.d.ts.map +1 -0
- package/dist/component/refundSync/validators.js +32 -0
- package/dist/component/refundSync/validators.js.map +1 -0
- package/dist/component/schema.d.ts +23 -0
- package/dist/component/schema.js +2 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/refund-context.md +54 -0
- package/package.json +1 -1
- package/src/client/connect/refundSync.test.ts +239 -0
- package/src/client/connect/refundSync.transport.test.ts +396 -0
- package/src/client/connect/refundSync.ts +125 -0
- package/src/client/connect/refundSyncValidation.ts +120 -0
- package/src/client/index.ts +19 -0
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +100 -0
- package/src/component/connect/mutations.ts +2 -67
- package/src/component/connect/refundWrites.ts +76 -0
- package/src/component/refundHistory/originalSeller.ts +4 -1
- package/src/component/refundSync/helpers.ts +99 -0
- package/src/component/refundSync/mutations.ts +131 -0
- package/src/component/refundSync/queries.ts +10 -0
- package/src/component/refundSync/refund-sync.test.ts +255 -0
- package/src/component/refundSync/routing.ts +55 -0
- package/src/component/refundSync/schema.ts +19 -0
- package/src/component/refundSync/validators.ts +35 -0
- package/src/component/schema.ts +2 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { convexTest } from "convex-test";
|
|
3
|
+
import { componentsGeneric, defineSchema } from "convex/server";
|
|
4
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
5
|
+
import type Stripe from "stripe";
|
|
6
|
+
|
|
7
|
+
import schema from "../../component/schema.js";
|
|
8
|
+
import { BetterStripe } from "../index.js";
|
|
9
|
+
import { getStripeClient, type Component, type RunCtx } from "../helpers.js";
|
|
10
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
11
|
+
|
|
12
|
+
const modules = import.meta.glob("../../component/**/*.*s");
|
|
13
|
+
const component = (
|
|
14
|
+
componentsGeneric() as unknown as { betterStripe: Component }
|
|
15
|
+
).betterStripe;
|
|
16
|
+
const key = "sk_test_refund_sync_scope";
|
|
17
|
+
type Options = {
|
|
18
|
+
stripeRefundId: string;
|
|
19
|
+
scope: { type: "platform" } | { type: "connected"; accountId: string };
|
|
20
|
+
};
|
|
21
|
+
type Client = BetterStripe & {
|
|
22
|
+
syncRefundFromStripe?: (ctx: RunCtx, input: Options) => Promise<unknown>;
|
|
23
|
+
};
|
|
24
|
+
afterEach(() => vi.restoreAllMocks());
|
|
25
|
+
|
|
26
|
+
function fixture() {
|
|
27
|
+
const t = convexTest(defineSchema({}), {
|
|
28
|
+
"./_generated/server.ts": () =>
|
|
29
|
+
import("../../component/_generated/server.js"),
|
|
30
|
+
});
|
|
31
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
32
|
+
const sdk: Client = new BetterStripe(component, { STRIPE_SECRET_KEY: key });
|
|
33
|
+
const ctx = { runQuery: t.query, runMutation: t.mutation } as RunCtx;
|
|
34
|
+
const current: Stripe.Refund = {
|
|
35
|
+
id: "re_original",
|
|
36
|
+
object: "refund",
|
|
37
|
+
amount: 250,
|
|
38
|
+
currency: "usd",
|
|
39
|
+
payment_intent: "pi_original",
|
|
40
|
+
charge: "ch_original",
|
|
41
|
+
status: "succeeded",
|
|
42
|
+
reason: "requested_by_customer",
|
|
43
|
+
balance_transaction: null,
|
|
44
|
+
created: 100,
|
|
45
|
+
customer: null,
|
|
46
|
+
customer_account: null,
|
|
47
|
+
payment_method: null,
|
|
48
|
+
receipt_number: null,
|
|
49
|
+
source_transfer_reversal: null,
|
|
50
|
+
transfer_reversal: null,
|
|
51
|
+
metadata: { betterStripeRefundCommand: "original-command" },
|
|
52
|
+
};
|
|
53
|
+
const retrieve = vi
|
|
54
|
+
.spyOn(getStripeClient(key, sdk.stripeApiVersion).refunds, "retrieve")
|
|
55
|
+
.mockResolvedValue(current as Stripe.Response<Stripe.Refund>);
|
|
56
|
+
return { t, sdk, ctx, current, retrieve };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
it("control: preserves the current component refund writer and public identity read", async () => {
|
|
60
|
+
const f = fixture();
|
|
61
|
+
await f.t.mutation(component.connect.mutations.upsertRefund, {
|
|
62
|
+
stripeRefundId: "re_original",
|
|
63
|
+
stripePaymentIntentId: "pi_original",
|
|
64
|
+
stripeChargeId: "ch_original",
|
|
65
|
+
accountId: "acct_original_store",
|
|
66
|
+
amount: 250,
|
|
67
|
+
currency: "usd",
|
|
68
|
+
status: "pending",
|
|
69
|
+
});
|
|
70
|
+
expect(
|
|
71
|
+
await f.sdk.getRefundByStripeId(f.ctx, { stripeRefundId: "re_original" }),
|
|
72
|
+
).toMatchObject({ status: "pending", amount: 250, currency: "usd" });
|
|
73
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("exposes a current-provider refund reconciliation primitive independently of webhook notifications", async () => {
|
|
77
|
+
const f = fixture();
|
|
78
|
+
expect(
|
|
79
|
+
f.sdk.syncRefundFromStripe,
|
|
80
|
+
"current refund reconciliation is missing",
|
|
81
|
+
).toBeTypeOf("function");
|
|
82
|
+
const result = await f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
83
|
+
stripeRefundId: "re_original",
|
|
84
|
+
scope: { type: "platform" },
|
|
85
|
+
});
|
|
86
|
+
expect(result).toMatchObject({
|
|
87
|
+
status: "accepted",
|
|
88
|
+
generation: 1,
|
|
89
|
+
changed: true,
|
|
90
|
+
});
|
|
91
|
+
expect(f.retrieve).toHaveBeenCalledWith("re_original", {}, {});
|
|
92
|
+
expect(
|
|
93
|
+
await f.sdk.getRefundByStripeId(f.ctx, { stripeRefundId: "re_original" }),
|
|
94
|
+
).toMatchObject({ status: "succeeded", amount: 250, currency: "usd" });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("allows current succeeded -> requires_action -> pending observations without a status rank", async () => {
|
|
98
|
+
const f = fixture();
|
|
99
|
+
await f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
100
|
+
stripeRefundId: "re_original",
|
|
101
|
+
scope: { type: "platform" },
|
|
102
|
+
});
|
|
103
|
+
for (const status of [
|
|
104
|
+
"requires_action",
|
|
105
|
+
"pending",
|
|
106
|
+
"failed",
|
|
107
|
+
"canceled",
|
|
108
|
+
] as const) {
|
|
109
|
+
f.retrieve.mockResolvedValue({
|
|
110
|
+
...f.current,
|
|
111
|
+
status,
|
|
112
|
+
} as Stripe.Response<Stripe.Refund>);
|
|
113
|
+
expect(
|
|
114
|
+
await f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
115
|
+
stripeRefundId: "re_original",
|
|
116
|
+
scope: { type: "platform" },
|
|
117
|
+
}),
|
|
118
|
+
).toMatchObject({ status: "accepted", changed: true });
|
|
119
|
+
expect(
|
|
120
|
+
await f.sdk.getRefundByStripeId(f.ctx, { stripeRefundId: "re_original" }),
|
|
121
|
+
).toMatchObject({ status });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("rejects malformed API scopes and identifiers before component or provider reads", async () => {
|
|
126
|
+
const f = fixture();
|
|
127
|
+
const runQuery = vi.fn();
|
|
128
|
+
const runMutation = vi.fn();
|
|
129
|
+
const context = { runQuery, runMutation } as RunCtx;
|
|
130
|
+
for (const input of [
|
|
131
|
+
{ stripeRefundId: "re_original" },
|
|
132
|
+
{
|
|
133
|
+
stripeRefundId: { toString: () => "re_original" },
|
|
134
|
+
scope: { type: "platform" },
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
stripeRefundId: "re_original",
|
|
138
|
+
scope: { type: "platform", accountId: "acct_guessed" },
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
stripeRefundId: "re_original",
|
|
142
|
+
scope: { type: "connected", accountId: "foreign" },
|
|
143
|
+
},
|
|
144
|
+
{ stripeRefundId: " re_original", scope: { type: "platform" } },
|
|
145
|
+
]) {
|
|
146
|
+
await expect(
|
|
147
|
+
f.sdk.syncRefundFromStripe!(context, input as Options),
|
|
148
|
+
).rejects.toThrow();
|
|
149
|
+
}
|
|
150
|
+
expect(runQuery.mock.calls.length).toBe(0);
|
|
151
|
+
expect(runMutation.mock.calls.length).toBe(0);
|
|
152
|
+
expect(f.retrieve).not.toHaveBeenCalled();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("does not let a failed first API context poison a later verified context", async () => {
|
|
156
|
+
const f = fixture();
|
|
157
|
+
f.retrieve.mockRejectedValueOnce(new Error("Provider read unavailable"));
|
|
158
|
+
await expect(
|
|
159
|
+
f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
160
|
+
stripeRefundId: "re_original",
|
|
161
|
+
scope: { type: "connected", accountId: "acct_wrong" },
|
|
162
|
+
}),
|
|
163
|
+
).rejects.toThrow("Provider read unavailable");
|
|
164
|
+
expect(
|
|
165
|
+
await f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
166
|
+
stripeRefundId: "re_original",
|
|
167
|
+
scope: { type: "platform" },
|
|
168
|
+
}),
|
|
169
|
+
).toMatchObject({ status: "accepted", generation: 2 });
|
|
170
|
+
await expect(
|
|
171
|
+
f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
172
|
+
stripeRefundId: "re_original",
|
|
173
|
+
scope: { type: "connected", accountId: "acct_wrong" },
|
|
174
|
+
}),
|
|
175
|
+
).rejects.toThrow("API context");
|
|
176
|
+
expect(f.retrieve).toHaveBeenCalledTimes(2);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("supersedes an older delayed pending read after a newer current observation commits", async () => {
|
|
180
|
+
const f = fixture();
|
|
181
|
+
let resolve!: (value: Stripe.Response<Stripe.Refund>) => void;
|
|
182
|
+
const response = new Promise<Stripe.Response<Stripe.Refund>>((done) => {
|
|
183
|
+
resolve = done;
|
|
184
|
+
});
|
|
185
|
+
f.retrieve.mockImplementationOnce(() => response);
|
|
186
|
+
const old = f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
187
|
+
stripeRefundId: "re_original",
|
|
188
|
+
scope: { type: "platform" },
|
|
189
|
+
});
|
|
190
|
+
await vi.waitFor(() => expect(f.retrieve).toHaveBeenCalledTimes(1));
|
|
191
|
+
expect(
|
|
192
|
+
await f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
193
|
+
stripeRefundId: "re_original",
|
|
194
|
+
scope: { type: "platform" },
|
|
195
|
+
}),
|
|
196
|
+
).toMatchObject({ status: "accepted", generation: 2 });
|
|
197
|
+
resolve({
|
|
198
|
+
...f.current,
|
|
199
|
+
status: "pending",
|
|
200
|
+
} as Stripe.Response<Stripe.Refund>);
|
|
201
|
+
expect(await old).toMatchObject({ status: "superseded", generation: 1 });
|
|
202
|
+
expect(
|
|
203
|
+
await f.sdk.getRefundByStripeId(f.ctx, { stripeRefundId: "re_original" }),
|
|
204
|
+
).toMatchObject({ status: "succeeded" });
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("does not accept an old snapshot when the newer generation's current read failed", async () => {
|
|
208
|
+
const f = fixture();
|
|
209
|
+
let resolve!: (value: Stripe.Response<Stripe.Refund>) => void;
|
|
210
|
+
const response = new Promise<Stripe.Response<Stripe.Refund>>((done) => {
|
|
211
|
+
resolve = done;
|
|
212
|
+
});
|
|
213
|
+
f.retrieve.mockImplementationOnce(() => response);
|
|
214
|
+
const old = f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
215
|
+
stripeRefundId: "re_original",
|
|
216
|
+
scope: { type: "platform" },
|
|
217
|
+
});
|
|
218
|
+
await vi.waitFor(() => expect(f.retrieve).toHaveBeenCalledTimes(1));
|
|
219
|
+
f.retrieve.mockRejectedValueOnce(new Error("Current read failed"));
|
|
220
|
+
await expect(
|
|
221
|
+
f.sdk.syncRefundFromStripe!(f.ctx, {
|
|
222
|
+
stripeRefundId: "re_original",
|
|
223
|
+
scope: { type: "platform" },
|
|
224
|
+
}),
|
|
225
|
+
).rejects.toThrow("Current read failed");
|
|
226
|
+
resolve(f.current as Stripe.Response<Stripe.Refund>);
|
|
227
|
+
expect(await old).toMatchObject({ status: "superseded" });
|
|
228
|
+
expect(
|
|
229
|
+
await f.sdk.getRefundByStripeId(f.ctx, { stripeRefundId: "re_original" }),
|
|
230
|
+
).toBeNull();
|
|
231
|
+
expect(
|
|
232
|
+
await f.t.query(componentRef(component, "refundSync/queries/routing"), {
|
|
233
|
+
stripeRefundId: "re_original",
|
|
234
|
+
stripePaymentIntentId: "pi_original",
|
|
235
|
+
stripeChargeId: "ch_original",
|
|
236
|
+
currency: "usd",
|
|
237
|
+
}),
|
|
238
|
+
).toMatchObject({ status: "unknown" });
|
|
239
|
+
});
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { convexTest } from "convex-test";
|
|
4
|
+
import { componentsGeneric, defineSchema } from "convex/server";
|
|
5
|
+
import { expect, it } from "vitest";
|
|
6
|
+
import schema from "../../component/schema.js";
|
|
7
|
+
import { BetterStripe } from "../index.js";
|
|
8
|
+
import { prepareRefundSync } from "./refundSync.js";
|
|
9
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
10
|
+
import { getStripeClient, type Component, type RunCtx } from "../helpers.js";
|
|
11
|
+
|
|
12
|
+
const modules = import.meta.glob("../../component/**/*.*s");
|
|
13
|
+
const component = componentsGeneric().betterStripe as unknown as Component;
|
|
14
|
+
const options = {
|
|
15
|
+
stripeRefundId: "re_original",
|
|
16
|
+
scope: { type: "platform" as const },
|
|
17
|
+
};
|
|
18
|
+
function refund() {
|
|
19
|
+
return {
|
|
20
|
+
id: "re_original",
|
|
21
|
+
object: "refund",
|
|
22
|
+
amount: 250,
|
|
23
|
+
currency: "usd",
|
|
24
|
+
payment_intent: "pi_original",
|
|
25
|
+
charge: "ch_original",
|
|
26
|
+
status: "succeeded",
|
|
27
|
+
reason: null,
|
|
28
|
+
metadata: { betterStripeRefundCommand: "original-command" },
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function fixture() {
|
|
32
|
+
const t = convexTest(defineSchema({}), {
|
|
33
|
+
"./_generated/server.ts": () =>
|
|
34
|
+
import("../../component/_generated/server.js"),
|
|
35
|
+
});
|
|
36
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
37
|
+
const key = `sk_test_refund_sync_transport_${crypto.randomUUID()}`;
|
|
38
|
+
const sdk = new BetterStripe(component, { STRIPE_SECRET_KEY: key });
|
|
39
|
+
const stripe = getStripeClient(key, sdk.stripeApiVersion);
|
|
40
|
+
const calls: {
|
|
41
|
+
path: string;
|
|
42
|
+
method: string;
|
|
43
|
+
account: string | undefined;
|
|
44
|
+
version: string;
|
|
45
|
+
body: string;
|
|
46
|
+
}[] = [];
|
|
47
|
+
let response: object = refund();
|
|
48
|
+
let code = 200;
|
|
49
|
+
let charge = {
|
|
50
|
+
id: "ch_original",
|
|
51
|
+
object: "charge",
|
|
52
|
+
payment_intent: "pi_original",
|
|
53
|
+
currency: "usd",
|
|
54
|
+
amount: 1000,
|
|
55
|
+
amount_captured: 1000,
|
|
56
|
+
transfer_data: { destination: "acct_original" },
|
|
57
|
+
};
|
|
58
|
+
// Replace only the SDK HTTP slot: actual Stripe request paths, headers and parsing execute.
|
|
59
|
+
const slot = stripe as unknown as {
|
|
60
|
+
_setApiField: (key: string, value: unknown) => void;
|
|
61
|
+
};
|
|
62
|
+
slot._setApiField("maxNetworkRetries", 0);
|
|
63
|
+
slot._setApiField("httpClient", {
|
|
64
|
+
getClientName: () => "refund-sync-test",
|
|
65
|
+
makeRequest: async (
|
|
66
|
+
...[, , path, method, headers, body]: Parameters<
|
|
67
|
+
ReturnType<typeof Stripe.createNodeHttpClient>["makeRequest"]
|
|
68
|
+
>
|
|
69
|
+
) => {
|
|
70
|
+
calls.push({
|
|
71
|
+
path,
|
|
72
|
+
method,
|
|
73
|
+
account: headers["Stripe-Account"] as string | undefined,
|
|
74
|
+
version: String(headers["Stripe-Version"]),
|
|
75
|
+
body: String(body),
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
getStatusCode: () => code,
|
|
79
|
+
getHeaders: () => ({}),
|
|
80
|
+
getRawResponse: () => ({}),
|
|
81
|
+
toJSON: async () =>
|
|
82
|
+
structuredClone(path.startsWith("/v1/charges/") ? charge : response),
|
|
83
|
+
toStream: () => {
|
|
84
|
+
throw new Error("Unexpected stream");
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const ctx = { runQuery: t.query, runMutation: t.mutation } as RunCtx;
|
|
90
|
+
return {
|
|
91
|
+
t,
|
|
92
|
+
sdk,
|
|
93
|
+
stripe,
|
|
94
|
+
ctx,
|
|
95
|
+
calls,
|
|
96
|
+
set: (value: object, status = 200) => {
|
|
97
|
+
response = value;
|
|
98
|
+
code = status;
|
|
99
|
+
},
|
|
100
|
+
setCharge: (value: typeof charge) => {
|
|
101
|
+
charge = value;
|
|
102
|
+
},
|
|
103
|
+
run: () => sdk.syncRefundFromStripe(ctx, options),
|
|
104
|
+
row: () => sdk.getRefundByStripeId(ctx, { stripeRefundId: "re_original" }),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
it.each(["platform", "connected"] as const)(
|
|
109
|
+
"serializes explicit %s API scope without treating it as merchant authority",
|
|
110
|
+
async (type) => {
|
|
111
|
+
const f = fixture();
|
|
112
|
+
f.set({ ...refund(), metadata: { accountId: "acct_metadata_guessed" } });
|
|
113
|
+
const scope =
|
|
114
|
+
type === "platform" ? { type } : { type, accountId: "acct_api_realm" };
|
|
115
|
+
expect(
|
|
116
|
+
await f.sdk.syncRefundFromStripe(f.ctx, { ...options, scope }),
|
|
117
|
+
).toMatchObject({ status: "accepted", changed: true });
|
|
118
|
+
expect(f.calls).toEqual([
|
|
119
|
+
{
|
|
120
|
+
path: "/v1/refunds/re_original",
|
|
121
|
+
method: "GET",
|
|
122
|
+
account: type === "platform" ? undefined : "acct_api_realm",
|
|
123
|
+
version: f.sdk.stripeApiVersion,
|
|
124
|
+
body: "",
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
expect((await f.row())?.accountId).toBeUndefined();
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
it("validates expanded original references returned through the actual HTTP parser", async () => {
|
|
132
|
+
const f = fixture();
|
|
133
|
+
f.set({
|
|
134
|
+
...refund(),
|
|
135
|
+
payment_intent: { id: "pi_original", object: "payment_intent" },
|
|
136
|
+
charge: { id: "ch_original", object: "charge" },
|
|
137
|
+
});
|
|
138
|
+
expect(await f.run()).toMatchObject({
|
|
139
|
+
status: "accepted",
|
|
140
|
+
refund: {
|
|
141
|
+
stripePaymentIntentId: "pi_original",
|
|
142
|
+
stripeChargeId: "ch_original",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it.each([
|
|
148
|
+
{ object: "charge" },
|
|
149
|
+
{ id: "re_foreign" },
|
|
150
|
+
{ amount: Number.MAX_SAFE_INTEGER + 1 },
|
|
151
|
+
{ currency: "USD" },
|
|
152
|
+
{ status: "invented" },
|
|
153
|
+
{ reason: "invented" },
|
|
154
|
+
{ metadata: { private: 123 } },
|
|
155
|
+
{ payment_intent: { id: "pi_original" } },
|
|
156
|
+
{ charge: { id: "ch_original", object: "payment_intent" } },
|
|
157
|
+
])(
|
|
158
|
+
"holds malformed current HTTP observation %j without accepting a financial row",
|
|
159
|
+
async (patch) => {
|
|
160
|
+
const f = fixture();
|
|
161
|
+
f.set({ ...refund(), ...patch });
|
|
162
|
+
await expect(f.run()).rejects.toThrow();
|
|
163
|
+
expect(await f.row()).toBeNull();
|
|
164
|
+
expect(f.calls).toHaveLength(1);
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
it("propagates provider HTTP failure with no snapshot fallback and permits a later fresh read", async () => {
|
|
169
|
+
const f = fixture();
|
|
170
|
+
f.set(
|
|
171
|
+
{ error: { type: "api_error", message: "Current refund unavailable" } },
|
|
172
|
+
503,
|
|
173
|
+
);
|
|
174
|
+
await expect(f.run()).rejects.toThrow("Current refund unavailable");
|
|
175
|
+
expect(await f.row()).toBeNull();
|
|
176
|
+
f.set(refund());
|
|
177
|
+
expect(await f.run()).toMatchObject({ status: "accepted", generation: 2 });
|
|
178
|
+
expect(f.calls.every((call) => call.method === "GET")).toBe(true);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("uses original charge destination proof instead of a mutable payment destination", async () => {
|
|
182
|
+
const f = fixture();
|
|
183
|
+
await f.t.mutation(component.connect.mutations.upsertPayment, {
|
|
184
|
+
stripePaymentIntentId: "pi_original",
|
|
185
|
+
userId: "original-buyer",
|
|
186
|
+
amount: 1000,
|
|
187
|
+
currency: "usd",
|
|
188
|
+
status: "succeeded",
|
|
189
|
+
destinationAccountId: "acct_replacement",
|
|
190
|
+
});
|
|
191
|
+
expect(await f.run()).toMatchObject({
|
|
192
|
+
status: "accepted",
|
|
193
|
+
refund: { accountId: "acct_original" },
|
|
194
|
+
});
|
|
195
|
+
expect(f.calls.map((call) => call.path)).toEqual([
|
|
196
|
+
"/v1/refunds/re_original",
|
|
197
|
+
"/v1/charges/ch_original",
|
|
198
|
+
]);
|
|
199
|
+
expect(
|
|
200
|
+
f.calls.every(
|
|
201
|
+
(call) => call.account === undefined && call.method === "GET",
|
|
202
|
+
),
|
|
203
|
+
).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("holds explicit original merchant disagreement instead of remapping an old refund", async () => {
|
|
207
|
+
const f = fixture();
|
|
208
|
+
await f.t.mutation(component.connect.mutations.upsertRefund, {
|
|
209
|
+
stripeRefundId: "re_original",
|
|
210
|
+
stripePaymentIntentId: "pi_original",
|
|
211
|
+
stripeChargeId: "ch_original",
|
|
212
|
+
accountId: "acct_other_original",
|
|
213
|
+
amount: 250,
|
|
214
|
+
currency: "usd",
|
|
215
|
+
status: "pending",
|
|
216
|
+
});
|
|
217
|
+
await expect(f.run()).rejects.toThrow("Original refund identity");
|
|
218
|
+
expect(await f.row()).toMatchObject({
|
|
219
|
+
status: "pending",
|
|
220
|
+
accountId: "acct_other_original",
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("uses the existing immutable allocation seller proof without requiring an unrelated charge GET", async () => {
|
|
225
|
+
const f = fixture();
|
|
226
|
+
await f.t.mutation(
|
|
227
|
+
component.settlement.mutations.reserveSettlementAllocation,
|
|
228
|
+
{
|
|
229
|
+
sourceChargeId: "ch_original",
|
|
230
|
+
paymentIntentId: "pi_original",
|
|
231
|
+
gross: 1000,
|
|
232
|
+
currency: "usd",
|
|
233
|
+
processingFee: 0,
|
|
234
|
+
platformNet: 100,
|
|
235
|
+
transfers: [
|
|
236
|
+
{ destinationAccountId: "acct_original", role: "store", amount: 800 },
|
|
237
|
+
{
|
|
238
|
+
destinationAccountId: "acct_affiliate",
|
|
239
|
+
role: "affiliate",
|
|
240
|
+
amount: 100,
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
},
|
|
244
|
+
);
|
|
245
|
+
expect(await f.run()).toMatchObject({
|
|
246
|
+
status: "accepted",
|
|
247
|
+
refund: { accountId: "acct_original" },
|
|
248
|
+
});
|
|
249
|
+
expect(f.calls.map((call) => call.path)).toEqual(["/v1/refunds/re_original"]);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("preserves an accepted commit when only its response is lost, then observes it without a refund write request", async () => {
|
|
253
|
+
const f = fixture();
|
|
254
|
+
let isLost = false;
|
|
255
|
+
const context: RunCtx = {
|
|
256
|
+
runQuery: f.ctx.runQuery,
|
|
257
|
+
runMutation: async (ref, ...args) => {
|
|
258
|
+
const result = await f.ctx.runMutation!(ref, ...args);
|
|
259
|
+
const path = (ref as Record<symbol, string>)[
|
|
260
|
+
Symbol.for("toReferencePath")
|
|
261
|
+
];
|
|
262
|
+
if (!isLost && path.endsWith("refundSync/mutations/finish")) {
|
|
263
|
+
isLost = true;
|
|
264
|
+
throw new Error("Commit response lost");
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
await expect(f.sdk.syncRefundFromStripe(context, options)).rejects.toThrow(
|
|
270
|
+
"Commit response lost",
|
|
271
|
+
);
|
|
272
|
+
expect(await f.row()).toMatchObject({ status: "succeeded", amount: 250 });
|
|
273
|
+
expect(await f.run()).toMatchObject({
|
|
274
|
+
status: "accepted",
|
|
275
|
+
changed: false,
|
|
276
|
+
generation: 2,
|
|
277
|
+
});
|
|
278
|
+
expect(f.calls).toHaveLength(2);
|
|
279
|
+
expect(f.calls.every((call) => call.method === "GET")).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("observes an expired uncaptured authorization release without importing captured-funds eligibility", async () => {
|
|
283
|
+
const f = fixture();
|
|
284
|
+
await f.t.mutation(component.connect.mutations.upsertPayment, {
|
|
285
|
+
stripePaymentIntentId: "pi_original",
|
|
286
|
+
userId: "buyer",
|
|
287
|
+
amount: 1000,
|
|
288
|
+
currency: "usd",
|
|
289
|
+
status: "succeeded",
|
|
290
|
+
destinationAccountId: "acct_original",
|
|
291
|
+
});
|
|
292
|
+
f.set({ ...refund(), reason: "expired_uncaptured_charge" });
|
|
293
|
+
f.setCharge({
|
|
294
|
+
id: "ch_original",
|
|
295
|
+
object: "charge",
|
|
296
|
+
payment_intent: "pi_original",
|
|
297
|
+
currency: "usd",
|
|
298
|
+
amount: 1000,
|
|
299
|
+
amount_captured: 0,
|
|
300
|
+
transfer_data: { destination: "acct_original" },
|
|
301
|
+
});
|
|
302
|
+
expect(await f.run()).toMatchObject({
|
|
303
|
+
status: "accepted",
|
|
304
|
+
refund: { reason: "expired_uncaptured_charge" },
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("rechecks original seller routing inside the accepting transaction after the provider read", async () => {
|
|
309
|
+
const f = fixture();
|
|
310
|
+
const observation = await prepareRefundSync(
|
|
311
|
+
f.stripe,
|
|
312
|
+
component,
|
|
313
|
+
f.ctx,
|
|
314
|
+
options,
|
|
315
|
+
);
|
|
316
|
+
await f.t.mutation(
|
|
317
|
+
component.settlement.mutations.reserveSettlementAllocation,
|
|
318
|
+
{
|
|
319
|
+
sourceChargeId: "ch_original",
|
|
320
|
+
paymentIntentId: "pi_original",
|
|
321
|
+
gross: 1000,
|
|
322
|
+
currency: "usd",
|
|
323
|
+
processingFee: 0,
|
|
324
|
+
platformNet: 100,
|
|
325
|
+
transfers: [
|
|
326
|
+
{
|
|
327
|
+
destinationAccountId: "acct_different_original",
|
|
328
|
+
role: "store",
|
|
329
|
+
amount: 900,
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
},
|
|
333
|
+
);
|
|
334
|
+
await expect(
|
|
335
|
+
f.t.mutation(
|
|
336
|
+
componentRef(component, "refundSync/mutations/finish"),
|
|
337
|
+
observation,
|
|
338
|
+
),
|
|
339
|
+
).rejects.toThrow("Original refund merchant");
|
|
340
|
+
expect(await f.row()).toBeNull();
|
|
341
|
+
expect(f.calls.map((call) => call.method)).toEqual(["GET"]);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("rolls back the accepting claim, refund and cumulative payment together if the host transaction aborts", async () => {
|
|
345
|
+
const f = fixture();
|
|
346
|
+
await f.t.mutation(component.connect.mutations.upsertPayment, {
|
|
347
|
+
stripePaymentIntentId: "pi_original",
|
|
348
|
+
userId: "buyer",
|
|
349
|
+
amount: 1000,
|
|
350
|
+
currency: "usd",
|
|
351
|
+
status: "succeeded",
|
|
352
|
+
});
|
|
353
|
+
await f.t.mutation(component.connect.mutations.upsertRefund, {
|
|
354
|
+
stripeRefundId: "re_original",
|
|
355
|
+
stripePaymentIntentId: "pi_original",
|
|
356
|
+
stripeChargeId: "ch_original",
|
|
357
|
+
amount: 250,
|
|
358
|
+
currency: "usd",
|
|
359
|
+
status: "failed",
|
|
360
|
+
});
|
|
361
|
+
const observation = await prepareRefundSync(
|
|
362
|
+
f.stripe,
|
|
363
|
+
component,
|
|
364
|
+
f.ctx,
|
|
365
|
+
options,
|
|
366
|
+
);
|
|
367
|
+
await expect(
|
|
368
|
+
f.t.mutation(async (ctx) => {
|
|
369
|
+
const accepted: unknown = await ctx.runMutation(
|
|
370
|
+
componentRef(component, "refundSync/mutations/finish"),
|
|
371
|
+
observation,
|
|
372
|
+
);
|
|
373
|
+
expect(accepted).toMatchObject({ status: "accepted", changed: true });
|
|
374
|
+
throw new Error("Injected host transaction abort after acceptance");
|
|
375
|
+
}),
|
|
376
|
+
).rejects.toThrow("Injected host transaction abort");
|
|
377
|
+
expect(await f.row()).toMatchObject({ status: "failed" });
|
|
378
|
+
expect(
|
|
379
|
+
await f.t.query(component.connect.queries.getPaymentByStripeId, {
|
|
380
|
+
stripePaymentIntentId: "pi_original",
|
|
381
|
+
}),
|
|
382
|
+
).toMatchObject({ refundedAmount: 0 });
|
|
383
|
+
// Reusing the same prepared generation is still a first acceptance after rollback.
|
|
384
|
+
expect(
|
|
385
|
+
await f.t.mutation(
|
|
386
|
+
componentRef(component, "refundSync/mutations/finish"),
|
|
387
|
+
observation,
|
|
388
|
+
),
|
|
389
|
+
).toMatchObject({ status: "accepted", changed: true });
|
|
390
|
+
expect(
|
|
391
|
+
await f.t.query(component.connect.queries.getPaymentByStripeId, {
|
|
392
|
+
stripePaymentIntentId: "pi_original",
|
|
393
|
+
}),
|
|
394
|
+
).toMatchObject({ refundedAmount: 250 });
|
|
395
|
+
expect(f.calls.map((call) => call.method)).toEqual(["GET"]);
|
|
396
|
+
});
|