@fantastic.dev/better-stripe 0.48.1 → 0.50.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 +34 -0
- package/README.md +1 -1
- package/dist/client/billing/invoicePaymentPages.d.ts +2 -5
- package/dist/client/billing/invoicePaymentPages.d.ts.map +1 -1
- package/dist/client/billing/invoicePaymentPages.js +2 -58
- package/dist/client/billing/invoicePaymentPages.js.map +1 -1
- package/dist/client/billing/invoicePaymentRead.d.ts +17 -0
- package/dist/client/billing/invoicePaymentRead.d.ts.map +1 -0
- package/dist/client/billing/invoicePaymentRead.js +25 -0
- package/dist/client/billing/invoicePaymentRead.js.map +1 -0
- package/dist/client/billing/invoicePaymentShared.d.ts +22 -0
- package/dist/client/billing/invoicePaymentShared.d.ts.map +1 -0
- package/dist/client/billing/invoicePaymentShared.js +62 -0
- package/dist/client/billing/invoicePaymentShared.js.map +1 -0
- package/dist/client/billing/setup-intent-read.d.ts +22 -0
- package/dist/client/billing/setup-intent-read.d.ts.map +1 -0
- package/dist/client/billing/setup-intent-read.js +63 -0
- package/dist/client/billing/setup-intent-read.js.map +1 -0
- package/dist/client/billing/subscription-payment-method.d.ts +28 -0
- package/dist/client/billing/subscription-payment-method.d.ts.map +1 -0
- package/dist/client/billing/subscription-payment-method.js +101 -0
- package/dist/client/billing/subscription-payment-method.js.map +1 -0
- package/dist/client/index.d.ts +9 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +10 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/invoiceReads.d.ts +3 -0
- package/dist/client/invoiceReads.d.ts.map +1 -1
- package/dist/client/invoiceReads.js +5 -0
- package/dist/client/invoiceReads.js.map +1 -1
- package/dist/component/_generated/component.d.ts +1 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/connect/queries.d.ts +1 -0
- package/dist/component/connect/queries.d.ts.map +1 -1
- package/dist/component/connect/queries.js +10 -4
- package/dist/component/connect/queries.js.map +1 -1
- package/dist/component/refundHistory/queries.d.ts +1 -1
- package/docs/invoice-payment-pages.md +62 -0
- package/docs/react-workflows.md +9 -0
- package/docs/read-contracts.md +45 -0
- package/docs/setup-intent-recovery.md +27 -0
- package/docs/subscription-payment-methods.md +121 -0
- package/package.json +4 -2
- package/src/client/billing/invoicePaymentPages.ts +9 -74
- package/src/client/billing/invoicePaymentRead.test.ts +233 -0
- package/src/client/billing/invoicePaymentRead.ts +44 -0
- package/src/client/billing/invoicePaymentReadTransport.test.ts +101 -0
- package/src/client/billing/invoicePaymentShared.ts +84 -0
- package/src/client/billing/setup-intent-read.test.ts +284 -0
- package/src/client/billing/setup-intent-read.ts +97 -0
- package/src/client/billing/subscription-payment-method.test.ts +460 -0
- package/src/client/billing/subscription-payment-method.ts +177 -0
- package/src/client/index.ts +39 -0
- package/src/client/invoiceReads.ts +9 -0
- package/src/component/_generated/component.ts +1 -1
- package/src/component/connect/payment-unique.test.ts +195 -0
- package/src/component/connect/queries.ts +9 -4
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { componentsGeneric } from "convex/server";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { STRIPE_API_VERSION } from "../constants.js";
|
|
6
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
7
|
+
import { BetterStripe } from "../index.js";
|
|
8
|
+
|
|
9
|
+
function fixture() {
|
|
10
|
+
const requests: { method: string; path: string }[] = [];
|
|
11
|
+
const realms: (string | null)[] = [];
|
|
12
|
+
const payloads: string[] = [];
|
|
13
|
+
const setup = {
|
|
14
|
+
id: "seti_original",
|
|
15
|
+
object: "setup_intent",
|
|
16
|
+
customer_account: "acct_original",
|
|
17
|
+
usage: "off_session",
|
|
18
|
+
status: "requires_action",
|
|
19
|
+
client_secret: "seti_original_secret_synthetic",
|
|
20
|
+
metadata: { userId: "owner" },
|
|
21
|
+
};
|
|
22
|
+
let failure: Response | undefined;
|
|
23
|
+
const sdk = new Stripe("sk_test_setup_read_fixture", {
|
|
24
|
+
apiVersion: STRIPE_API_VERSION,
|
|
25
|
+
maxNetworkRetries: 0,
|
|
26
|
+
httpClient: Stripe.createFetchHttpClient(async (url, init) => {
|
|
27
|
+
realms.push(new Headers(init?.headers).get("Stripe-Account"));
|
|
28
|
+
payloads.push(String(init?.body ?? ""));
|
|
29
|
+
requests.push({
|
|
30
|
+
method: init?.method ?? "GET",
|
|
31
|
+
path: new URL(String(url)).pathname,
|
|
32
|
+
});
|
|
33
|
+
if (failure) return failure.clone();
|
|
34
|
+
return new Response(
|
|
35
|
+
JSON.stringify(
|
|
36
|
+
new URL(String(url)).pathname.startsWith("/v1/setup_intents")
|
|
37
|
+
? setup
|
|
38
|
+
: {
|
|
39
|
+
id: "sub_original",
|
|
40
|
+
object: "subscription",
|
|
41
|
+
customer_account: "acct_original",
|
|
42
|
+
status: "active",
|
|
43
|
+
},
|
|
44
|
+
),
|
|
45
|
+
{ status: 200, headers: { "content-type": "application/json" } },
|
|
46
|
+
);
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
class Client extends BetterStripe {
|
|
50
|
+
protected override stripe() {
|
|
51
|
+
return sdk;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const client = new Client(
|
|
55
|
+
componentsGeneric().betterStripe as unknown as Component,
|
|
56
|
+
{ STRIPE_SECRET_KEY: "sk_test_setup_read_fixture" },
|
|
57
|
+
);
|
|
58
|
+
const runQuery = vi
|
|
59
|
+
.fn()
|
|
60
|
+
.mockResolvedValue({ userId: "owner", stripeAccountId: "acct_original" });
|
|
61
|
+
const ctx: RunCtx = { runQuery };
|
|
62
|
+
const opts = {
|
|
63
|
+
userId: "owner",
|
|
64
|
+
customerAccount: "acct_original",
|
|
65
|
+
setupIntentId: "seti_original",
|
|
66
|
+
usage: "off_session" as const,
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
client,
|
|
70
|
+
ctx,
|
|
71
|
+
requests,
|
|
72
|
+
realms,
|
|
73
|
+
payloads,
|
|
74
|
+
setup,
|
|
75
|
+
runQuery,
|
|
76
|
+
opts,
|
|
77
|
+
fail: (response: Response) => {
|
|
78
|
+
failure = response;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
it("retains the public exact subscription provider read as an existing positive", async () => {
|
|
84
|
+
const f = fixture();
|
|
85
|
+
expect(
|
|
86
|
+
await f.client.getSubscriptionFromStripe(f.ctx, {
|
|
87
|
+
stripeSubscriptionId: "sub_original",
|
|
88
|
+
}),
|
|
89
|
+
).toMatchObject({
|
|
90
|
+
id: "sub_original",
|
|
91
|
+
customer_account: "acct_original",
|
|
92
|
+
status: "active",
|
|
93
|
+
});
|
|
94
|
+
expect(f.requests).toEqual([
|
|
95
|
+
{ method: "GET", path: "/v1/subscriptions/sub_original" },
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("provides a public qualified exact existing SetupIntent read for original-request recovery", async () => {
|
|
100
|
+
const f = fixture();
|
|
101
|
+
expect(await f.client.getSetupIntentFromStripe(f.ctx, f.opts)).toEqual({
|
|
102
|
+
setupIntentId: "seti_original",
|
|
103
|
+
customerAccount: "acct_original",
|
|
104
|
+
usage: "off_session",
|
|
105
|
+
status: "requires_action",
|
|
106
|
+
clientSecret: "seti_original_secret_synthetic",
|
|
107
|
+
observedAt: expect.any(Number),
|
|
108
|
+
});
|
|
109
|
+
expect(f.realms).toEqual([null]);
|
|
110
|
+
expect(f.runQuery).toHaveBeenCalledWith(expect.anything(), {
|
|
111
|
+
stripeAccountId: "acct_original",
|
|
112
|
+
requireUnique: true,
|
|
113
|
+
});
|
|
114
|
+
expect(f.requests).toEqual([
|
|
115
|
+
{ method: "GET", path: "/v1/setup_intents/seti_original" },
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it.each([
|
|
120
|
+
null,
|
|
121
|
+
{ userId: "foreign", stripeAccountId: "acct_original" },
|
|
122
|
+
{ userId: "owner", stripeAccountId: "acct_other" },
|
|
123
|
+
])(
|
|
124
|
+
"rejects missing, ambiguous or foreign original account %j before provider access",
|
|
125
|
+
async (account) => {
|
|
126
|
+
const f = fixture();
|
|
127
|
+
f.runQuery.mockResolvedValue(account);
|
|
128
|
+
await expect(
|
|
129
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
130
|
+
).rejects.toThrow(/account/);
|
|
131
|
+
expect(f.requests).toEqual([]);
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
it.each([
|
|
136
|
+
{ id: "seti_other" },
|
|
137
|
+
{ customer_account: "acct_other" },
|
|
138
|
+
{ metadata: { userId: "foreign" } },
|
|
139
|
+
{ usage: "on_session" },
|
|
140
|
+
{ status: "invented" },
|
|
141
|
+
{ metadata: {} },
|
|
142
|
+
])("rejects contradictory existing setup response %j", async (override) => {
|
|
143
|
+
const f = fixture();
|
|
144
|
+
Object.assign(f.setup, override);
|
|
145
|
+
await expect(
|
|
146
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
147
|
+
).rejects.toThrow(/original request/);
|
|
148
|
+
expect(f.requests).toHaveLength(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it.each(["succeeded", "canceled", "processing"])(
|
|
152
|
+
"omits the secret for %s observations",
|
|
153
|
+
async (status) => {
|
|
154
|
+
const f = fixture();
|
|
155
|
+
f.setup.status = status;
|
|
156
|
+
const result = await f.client.getSetupIntentFromStripe(f.ctx, f.opts);
|
|
157
|
+
expect(result.status).toBe(status);
|
|
158
|
+
expect(result).not.toHaveProperty("clientSecret");
|
|
159
|
+
expect(result).not.toHaveProperty("metadata");
|
|
160
|
+
expect(result).not.toHaveProperty("payment_method");
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
it.each([
|
|
165
|
+
"requires_action",
|
|
166
|
+
"requires_confirmation",
|
|
167
|
+
"requires_payment_method",
|
|
168
|
+
])("requires an available transient secret for %s", async (status) => {
|
|
169
|
+
const f = fixture();
|
|
170
|
+
f.setup.status = status;
|
|
171
|
+
f.setup.client_secret = "";
|
|
172
|
+
await expect(
|
|
173
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
174
|
+
).rejects.toThrow(/secret/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("freezes every original primitive selector before the first await without freezing the caller", async () => {
|
|
178
|
+
const f = fixture();
|
|
179
|
+
f.runQuery.mockImplementation(async () => {
|
|
180
|
+
Object.assign(f.opts, {
|
|
181
|
+
userId: "foreign",
|
|
182
|
+
customerAccount: "acct_other",
|
|
183
|
+
setupIntentId: "seti_other",
|
|
184
|
+
usage: "on_session",
|
|
185
|
+
});
|
|
186
|
+
return { userId: "owner", stripeAccountId: "acct_original" };
|
|
187
|
+
});
|
|
188
|
+
expect(await f.client.getSetupIntentFromStripe(f.ctx, f.opts)).toMatchObject({
|
|
189
|
+
setupIntentId: "seti_original",
|
|
190
|
+
customerAccount: "acct_original",
|
|
191
|
+
usage: "off_session",
|
|
192
|
+
});
|
|
193
|
+
expect(f.opts.setupIntentId).toBe("seti_other");
|
|
194
|
+
expect(Object.isFrozen(f.opts)).toBe(false);
|
|
195
|
+
expect(f.requests).toEqual([
|
|
196
|
+
{ method: "GET", path: "/v1/setup_intents/seti_original" },
|
|
197
|
+
]);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("preserves provider failure without searching or creating a setup", async () => {
|
|
201
|
+
const f = fixture();
|
|
202
|
+
f.fail(
|
|
203
|
+
new Response(
|
|
204
|
+
JSON.stringify({
|
|
205
|
+
error: {
|
|
206
|
+
message: "synthetic missing setup",
|
|
207
|
+
type: "invalid_request_error",
|
|
208
|
+
code: "resource_missing",
|
|
209
|
+
},
|
|
210
|
+
}),
|
|
211
|
+
{ status: 404 },
|
|
212
|
+
),
|
|
213
|
+
);
|
|
214
|
+
await expect(
|
|
215
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
216
|
+
).rejects.toMatchObject({ code: "resource_missing" });
|
|
217
|
+
expect(f.requests).toEqual([
|
|
218
|
+
{ method: "GET", path: "/v1/setup_intents/seti_original" },
|
|
219
|
+
]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it.each([
|
|
223
|
+
{ userId: "" },
|
|
224
|
+
{ customerAccount: "acct_bad/realm" },
|
|
225
|
+
{ setupIntentId: "seti_bad?query" },
|
|
226
|
+
{ usage: "unsupported" },
|
|
227
|
+
])(
|
|
228
|
+
"rejects invalid original selectors %j before any read",
|
|
229
|
+
async (override) => {
|
|
230
|
+
const f = fixture();
|
|
231
|
+
Object.assign(f.opts, override);
|
|
232
|
+
await expect(
|
|
233
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
234
|
+
).rejects.toThrow(/selectors/);
|
|
235
|
+
expect(f.runQuery).not.toHaveBeenCalled();
|
|
236
|
+
expect(f.requests).toEqual([]);
|
|
237
|
+
},
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
it.each([undefined, "payment_intent"])(
|
|
241
|
+
"rejects a wrong or missing SetupIntent object discriminator %s",
|
|
242
|
+
async (object) => {
|
|
243
|
+
const f = fixture();
|
|
244
|
+
Object.assign(f.setup, { object });
|
|
245
|
+
await expect(
|
|
246
|
+
f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
247
|
+
).rejects.toThrow(/original request/);
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
it.each(["o".repeat(300), " opaque owner "])(
|
|
252
|
+
"reads an original setup provisioned for opaque host userId %s",
|
|
253
|
+
async (userId) => {
|
|
254
|
+
const f = fixture();
|
|
255
|
+
f.opts.userId = userId;
|
|
256
|
+
f.setup.metadata.userId = userId;
|
|
257
|
+
f.runQuery.mockResolvedValue({
|
|
258
|
+
userId,
|
|
259
|
+
stripeAccountId: f.opts.customerAccount,
|
|
260
|
+
});
|
|
261
|
+
expect(
|
|
262
|
+
await f.client.createSetupIntent(f.ctx, {
|
|
263
|
+
userId,
|
|
264
|
+
customerAccount: f.opts.customerAccount,
|
|
265
|
+
usage: f.opts.usage,
|
|
266
|
+
consent: true,
|
|
267
|
+
idempotencyKey: "original-opaque-user-setup",
|
|
268
|
+
}),
|
|
269
|
+
).toMatchObject({ setupIntentId: f.opts.setupIntentId });
|
|
270
|
+
expect(new URLSearchParams(f.payloads[0]).get("metadata[userId]")).toBe(
|
|
271
|
+
userId,
|
|
272
|
+
);
|
|
273
|
+
expect(
|
|
274
|
+
await f.client.getSetupIntentFromStripe(f.ctx, f.opts),
|
|
275
|
+
).toMatchObject({
|
|
276
|
+
setupIntentId: f.opts.setupIntentId,
|
|
277
|
+
customerAccount: f.opts.customerAccount,
|
|
278
|
+
});
|
|
279
|
+
expect(f.requests).toEqual([
|
|
280
|
+
{ method: "POST", path: "/v1/setup_intents" },
|
|
281
|
+
{ method: "GET", path: "/v1/setup_intents/seti_original" },
|
|
282
|
+
]);
|
|
283
|
+
},
|
|
284
|
+
);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type Stripe from "stripe";
|
|
2
|
+
|
|
3
|
+
import { getAccountByStripeId } from "../core/accounts.js";
|
|
4
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
5
|
+
|
|
6
|
+
/** Host-authenticated immutable selectors for an already acknowledged setup. */
|
|
7
|
+
export type SetupIntentReadOptions = {
|
|
8
|
+
userId: string;
|
|
9
|
+
customerAccount: string;
|
|
10
|
+
setupIntentId: string;
|
|
11
|
+
usage: "on_session" | "off_session";
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type SetupIntentObservation = {
|
|
15
|
+
setupIntentId: string;
|
|
16
|
+
customerAccount: string;
|
|
17
|
+
usage: "on_session" | "off_session";
|
|
18
|
+
status: Stripe.SetupIntent.Status;
|
|
19
|
+
/** Present only for an incomplete confirmation. Never persist or log it. */
|
|
20
|
+
clientSecret?: string;
|
|
21
|
+
/** Time of this provider observation, in Unix milliseconds. */
|
|
22
|
+
observedAt: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function validate(opts: SetupIntentReadOptions) {
|
|
26
|
+
if (
|
|
27
|
+
typeof opts.userId !== "string" ||
|
|
28
|
+
!opts.userId ||
|
|
29
|
+
typeof opts.customerAccount !== "string" ||
|
|
30
|
+
opts.customerAccount.length > 255 ||
|
|
31
|
+
!/^acct_[A-Za-z0-9]+$/.test(opts.customerAccount) ||
|
|
32
|
+
typeof opts.setupIntentId !== "string" ||
|
|
33
|
+
opts.setupIntentId.length > 255 ||
|
|
34
|
+
!/^seti_[A-Za-z0-9]+$/.test(opts.setupIntentId) ||
|
|
35
|
+
(opts.usage !== "on_session" && opts.usage !== "off_session")
|
|
36
|
+
)
|
|
37
|
+
throw new Error("Valid original SetupIntent selectors are required.");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Exact platform-realm GET only. Host authorization is required before and after I/O. */
|
|
41
|
+
export async function getSetupIntentFromStripe(
|
|
42
|
+
stripe: Stripe,
|
|
43
|
+
component: Component,
|
|
44
|
+
ctx: RunCtx,
|
|
45
|
+
input: SetupIntentReadOptions,
|
|
46
|
+
): Promise<SetupIntentObservation> {
|
|
47
|
+
const opts = {
|
|
48
|
+
userId: input.userId,
|
|
49
|
+
customerAccount: input.customerAccount,
|
|
50
|
+
setupIntentId: input.setupIntentId,
|
|
51
|
+
usage: input.usage,
|
|
52
|
+
};
|
|
53
|
+
validate(opts);
|
|
54
|
+
const account = await getAccountByStripeId(component, ctx, {
|
|
55
|
+
stripeAccountId: opts.customerAccount,
|
|
56
|
+
requireUnique: true,
|
|
57
|
+
});
|
|
58
|
+
if (
|
|
59
|
+
!account ||
|
|
60
|
+
account.stripeAccountId !== opts.customerAccount ||
|
|
61
|
+
account.userId !== opts.userId
|
|
62
|
+
)
|
|
63
|
+
throw new Error("Original customer account is unavailable.");
|
|
64
|
+
const setup = await stripe.setupIntents.retrieve(opts.setupIntentId);
|
|
65
|
+
if (
|
|
66
|
+
setup.object !== "setup_intent" ||
|
|
67
|
+
setup.id !== opts.setupIntentId ||
|
|
68
|
+
setup.customer_account !== opts.customerAccount ||
|
|
69
|
+
setup.metadata?.userId !== opts.userId ||
|
|
70
|
+
setup.usage !== opts.usage ||
|
|
71
|
+
![
|
|
72
|
+
"requires_payment_method",
|
|
73
|
+
"requires_confirmation",
|
|
74
|
+
"requires_action",
|
|
75
|
+
"processing",
|
|
76
|
+
"canceled",
|
|
77
|
+
"succeeded",
|
|
78
|
+
].includes(setup.status)
|
|
79
|
+
)
|
|
80
|
+
throw new Error("SetupIntent does not match the original request.");
|
|
81
|
+
const isIncomplete = [
|
|
82
|
+
"requires_payment_method",
|
|
83
|
+
"requires_confirmation",
|
|
84
|
+
"requires_action",
|
|
85
|
+
].includes(setup.status);
|
|
86
|
+
const clientSecret = isIncomplete ? setup.client_secret : undefined;
|
|
87
|
+
if (isIncomplete && (typeof clientSecret !== "string" || !clientSecret))
|
|
88
|
+
throw new Error("Incomplete SetupIntent secret is unavailable.");
|
|
89
|
+
return {
|
|
90
|
+
setupIntentId: opts.setupIntentId,
|
|
91
|
+
customerAccount: opts.customerAccount,
|
|
92
|
+
usage: opts.usage,
|
|
93
|
+
status: setup.status,
|
|
94
|
+
...(typeof clientSecret === "string" ? { clientSecret } : {}),
|
|
95
|
+
observedAt: Date.now(),
|
|
96
|
+
};
|
|
97
|
+
}
|