@fantastic.dev/better-stripe 0.83.0 → 0.84.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 +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/couponJoins.d.ts +1 -1
- package/dist/client/billing/renewalAttach/couponJoins.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/couponJoins.js +3 -3
- package/dist/client/billing/renewalAttach/couponJoins.js.map +1 -1
- package/dist/client/billing/renewalAttach/reader.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/reader.js +5 -8
- package/dist/client/billing/renewalAttach/reader.js.map +1 -1
- package/dist/client/billing/renewalAttach/wireJson.d.ts +24 -0
- package/dist/client/billing/renewalAttach/wireJson.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/wireJson.js +51 -0
- package/dist/client/billing/renewalAttach/wireJson.js.map +1 -0
- package/dist/client/billing/renewalV2/binding.d.ts +3 -0
- package/dist/client/billing/renewalV2/binding.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/binding.js +18 -0
- package/dist/client/billing/renewalV2/binding.js.map +1 -0
- package/dist/client/billing/renewalV2/index.d.ts +38 -0
- package/dist/client/billing/renewalV2/index.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/index.js +70 -0
- package/dist/client/billing/renewalV2/index.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/helpers.d.ts.map +1 -1
- package/dist/client/helpers.js +2 -2
- package/dist/client/helpers.js.map +1 -1
- package/dist/client/renewal.d.ts +2 -0
- package/dist/client/renewal.d.ts.map +1 -1
- package/dist/client/renewal.js +1 -0
- package/dist/client/renewal.js.map +1 -1
- package/dist/client/singleAttemptHttpClient.d.ts +1 -1
- package/dist/client/singleAttemptHttpClient.d.ts.map +1 -1
- package/dist/client/singleAttemptHttpClient.js +1 -1
- package/dist/client/singleAttemptHttpClient.js.map +1 -1
- package/dist/client/strictStripeTransport.d.ts +4 -0
- package/dist/client/strictStripeTransport.d.ts.map +1 -0
- package/dist/client/strictStripeTransport.js +7 -0
- package/dist/client/strictStripeTransport.js.map +1 -0
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/docs/renewal-context-reads.md +101 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/couponEvidence.test.ts +1 -2
- package/src/client/billing/renewalAttach/couponJoins.ts +2 -5
- package/src/client/billing/renewalAttach/reader.test.ts +1 -1
- package/src/client/billing/renewalAttach/reader.ts +6 -12
- package/src/client/billing/renewalAttach/wireJson.test.ts +265 -0
- package/src/client/billing/renewalAttach/wireJson.ts +75 -0
- package/src/client/billing/renewalV2/binding.ts +25 -0
- package/src/client/billing/renewalV2/index.test.ts +414 -0
- package/src/client/billing/renewalV2/index.ts +111 -0
- package/src/client/helpers.ts +2 -4
- package/src/client/renewal.ts +10 -0
- package/src/client/singleAttemptHttpClient.ts +1 -1
- package/src/client/strictStripeTransport.test.ts +73 -0
- package/src/client/strictStripeTransport.ts +11 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { it, expect } from "vitest";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { SingleAttemptHttpClient } from "../../singleAttemptHttpClient.js";
|
|
4
|
+
import {
|
|
5
|
+
fixture,
|
|
6
|
+
API_VERSION,
|
|
7
|
+
} from "../../../../tests/renewalAggregate/fixtures.js";
|
|
8
|
+
import { readRenewalPriceReplacementContext } from "./reader.js";
|
|
9
|
+
import { readRenewalWireJSON } from "./wireJson.js";
|
|
10
|
+
export function wireHarness(status = 200, withDecimals = true) {
|
|
11
|
+
const f = fixture();
|
|
12
|
+
const sub = JSON.parse(JSON.stringify(f.subscription));
|
|
13
|
+
const p = {
|
|
14
|
+
...sub.items.data[0].price,
|
|
15
|
+
unit_amount: 1000,
|
|
16
|
+
unit_amount_decimal: withDecimals ? "1000.00" : null,
|
|
17
|
+
created: 1,
|
|
18
|
+
metadata: { keep: "wire" },
|
|
19
|
+
nickname: null,
|
|
20
|
+
tiers_mode: null,
|
|
21
|
+
transform_quantity: null,
|
|
22
|
+
};
|
|
23
|
+
sub.items.data[0].price = p;
|
|
24
|
+
sub.items.data[0].plan = {
|
|
25
|
+
id: p.id,
|
|
26
|
+
object: "plan",
|
|
27
|
+
active: p.active,
|
|
28
|
+
amount: 1000,
|
|
29
|
+
amount_decimal: withDecimals ? "1000.00" : null,
|
|
30
|
+
billing_scheme: p.billing_scheme,
|
|
31
|
+
created: 1,
|
|
32
|
+
currency: p.currency,
|
|
33
|
+
interval: p.recurring.interval,
|
|
34
|
+
interval_count: p.recurring.interval_count,
|
|
35
|
+
livemode: p.livemode,
|
|
36
|
+
metadata: { keep: "wire" },
|
|
37
|
+
meter: null,
|
|
38
|
+
nickname: null,
|
|
39
|
+
product: p.product,
|
|
40
|
+
tiers_mode: null,
|
|
41
|
+
transform_usage: null,
|
|
42
|
+
trial_period_days: null,
|
|
43
|
+
usage_type: "licensed",
|
|
44
|
+
};
|
|
45
|
+
sub.opaque = { facts: ["keep", "all"] };
|
|
46
|
+
const target = {
|
|
47
|
+
...f.targetPrice,
|
|
48
|
+
unit_amount_decimal: withDecimals ? "2000.00" : null,
|
|
49
|
+
opaque: { keep: "yes" },
|
|
50
|
+
};
|
|
51
|
+
const calls: {
|
|
52
|
+
path: string;
|
|
53
|
+
method: string;
|
|
54
|
+
version: string | null;
|
|
55
|
+
connected: string | null;
|
|
56
|
+
body: string;
|
|
57
|
+
}[] = [];
|
|
58
|
+
const coupons = new Map(
|
|
59
|
+
[...f.subscription.discounts, ...f.originalItem.discounts].map((d) => [
|
|
60
|
+
d.source.coupon.id,
|
|
61
|
+
d.source.coupon,
|
|
62
|
+
]),
|
|
63
|
+
);
|
|
64
|
+
const stripe = new Stripe("sk_test_ONLY_CONTROLLED_WIRE", {
|
|
65
|
+
apiVersion: API_VERSION,
|
|
66
|
+
maxNetworkRetries: 0,
|
|
67
|
+
httpClient: new SingleAttemptHttpClient(
|
|
68
|
+
Stripe.createFetchHttpClient(async (url, init) => {
|
|
69
|
+
const u = new URL(String(url)),
|
|
70
|
+
headers = new Headers(init?.headers);
|
|
71
|
+
calls.push({
|
|
72
|
+
path: u.pathname + u.search,
|
|
73
|
+
method: String(init?.method),
|
|
74
|
+
version: headers.get("stripe-version"),
|
|
75
|
+
connected: headers.get("stripe-account"),
|
|
76
|
+
body: String(init?.body ?? ""),
|
|
77
|
+
});
|
|
78
|
+
const body = u.pathname.startsWith("/v1/subscriptions/")
|
|
79
|
+
? sub
|
|
80
|
+
: u.pathname.startsWith("/v1/prices/")
|
|
81
|
+
? target
|
|
82
|
+
: u.pathname.startsWith("/v1/customers/")
|
|
83
|
+
? f.billingCustomer
|
|
84
|
+
: coupons.get(decodeURIComponent(u.pathname.slice(12)));
|
|
85
|
+
return new Response(JSON.stringify(body), {
|
|
86
|
+
status,
|
|
87
|
+
headers: {
|
|
88
|
+
"content-type": "application/json",
|
|
89
|
+
"request-id": "req_controlledwire",
|
|
90
|
+
"stripe-version": API_VERSION,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
f,
|
|
98
|
+
sub,
|
|
99
|
+
target,
|
|
100
|
+
calls,
|
|
101
|
+
stripe,
|
|
102
|
+
read: () =>
|
|
103
|
+
readRenewalPriceReplacementContext(
|
|
104
|
+
stripe,
|
|
105
|
+
{ expected: f.prepared.expected },
|
|
106
|
+
API_VERSION,
|
|
107
|
+
),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
it("reader preserves actualSDK nativePlan/Price decimal stringlexemes and every opaqueJSONfact", async () => {
|
|
111
|
+
const h = wireHarness();
|
|
112
|
+
const r = await h.read();
|
|
113
|
+
expect(
|
|
114
|
+
(
|
|
115
|
+
(r.subscription.items.data[0] as Record<string, unknown>).plan as {
|
|
116
|
+
amount_decimal: string;
|
|
117
|
+
}
|
|
118
|
+
).amount_decimal,
|
|
119
|
+
).toBe("1000.00");
|
|
120
|
+
expect(r.subscription.items.data[0].price.unit_amount_decimal).toBe(
|
|
121
|
+
"1000.00",
|
|
122
|
+
);
|
|
123
|
+
expect(r.subscription).toEqual(h.sub);
|
|
124
|
+
expect(r.targetPrice).toEqual(h.target);
|
|
125
|
+
expect(h.calls).toHaveLength(5);
|
|
126
|
+
expect(
|
|
127
|
+
h.calls.every(
|
|
128
|
+
(c) =>
|
|
129
|
+
c.method === "GET" &&
|
|
130
|
+
c.version === API_VERSION &&
|
|
131
|
+
c.connected === null &&
|
|
132
|
+
c.body === "",
|
|
133
|
+
),
|
|
134
|
+
).toBe(true);
|
|
135
|
+
const first = new URL(h.calls[0].path, "https://api.stripe.com");
|
|
136
|
+
expect([...first.searchParams]).toEqual([
|
|
137
|
+
["expand[0]", "discounts"],
|
|
138
|
+
["expand[1]", "items.data.discounts"],
|
|
139
|
+
["expand[2]", "items.data.price"],
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
it.each([201, 204, 299])(
|
|
143
|
+
"admitsvalid2xx JSON or failsclosed onbodyless%s",
|
|
144
|
+
async (status) => {
|
|
145
|
+
// 201/299 are valid2xx. The fixedbody stillneeds schema; unlike204 they preserveJSON.
|
|
146
|
+
const h = wireHarness(status);
|
|
147
|
+
if (status === 204) await expect(h.read()).rejects.toBeDefined();
|
|
148
|
+
else expect((await h.read()).subscription.id).toBe("sub_original");
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
it.each([307, 308, 400, 500])(
|
|
152
|
+
"rejects fulfillednon2xx JSONstatus%s before acquiring otherresources",
|
|
153
|
+
async (status) => {
|
|
154
|
+
const h = wireHarness(status, false);
|
|
155
|
+
await expect(h.read()).rejects.toBeDefined();
|
|
156
|
+
expect(h.calls).toHaveLength(1);
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
it.each([
|
|
161
|
+
{},
|
|
162
|
+
{ status: "200" },
|
|
163
|
+
{ statusCode: "200" },
|
|
164
|
+
{ status: 200, statusCode: 201 },
|
|
165
|
+
{ status: 200, statusCode: 500 },
|
|
166
|
+
{ status: NaN },
|
|
167
|
+
])("rejects missing/invalid/contradictory statusmetadata %j", async (meta) => {
|
|
168
|
+
const f = fixture();
|
|
169
|
+
const stripe = new Stripe("sk_test_CONTROLLED_META", {
|
|
170
|
+
apiVersion: API_VERSION,
|
|
171
|
+
maxNetworkRetries: 0,
|
|
172
|
+
httpClient: {
|
|
173
|
+
getClientName: () => "controlledmeta",
|
|
174
|
+
makeRequest: async () => ({
|
|
175
|
+
getStatusCode: () => 200,
|
|
176
|
+
getHeaders: () => ({ "request-id": "req_meta" }),
|
|
177
|
+
getRawResponse: () => meta,
|
|
178
|
+
toJSON: async () => JSON.parse(JSON.stringify(f.billingCustomer)),
|
|
179
|
+
toStream: () => {
|
|
180
|
+
throw Error("unused");
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
await expect(
|
|
186
|
+
readRenewalWireJSON(stripe, "customer", "cus_buyer"),
|
|
187
|
+
).rejects.toMatchObject({
|
|
188
|
+
message: expect.stringContaining("provider_http_status"),
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
it.each([
|
|
192
|
+
{ status: 200 },
|
|
193
|
+
{ statusCode: 200 },
|
|
194
|
+
{ status: 200, statusCode: 200 },
|
|
195
|
+
])("admits supported successfultransportmetadata %j", async (meta) => {
|
|
196
|
+
const f = fixture();
|
|
197
|
+
const stripe = new Stripe("sk_test_CONTROLLED_META_VALID", {
|
|
198
|
+
apiVersion: API_VERSION,
|
|
199
|
+
maxNetworkRetries: 0,
|
|
200
|
+
httpClient: {
|
|
201
|
+
getClientName: () => "controlledmeta",
|
|
202
|
+
makeRequest: async () => ({
|
|
203
|
+
getStatusCode: () => 200,
|
|
204
|
+
getHeaders: () => ({ "request-id": "req_meta" }),
|
|
205
|
+
getRawResponse: () => meta,
|
|
206
|
+
toJSON: async () => JSON.parse(JSON.stringify(f.billingCustomer)),
|
|
207
|
+
toStream: () => {
|
|
208
|
+
throw Error("unused");
|
|
209
|
+
},
|
|
210
|
+
}),
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
expect(await readRenewalWireJSON(stripe, "customer", "cus_buyer")).toEqual(
|
|
214
|
+
f.billingCustomer,
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
it("encodes arbitrary supported customcoupon IDs into one fixedpath segment", async () => {
|
|
218
|
+
const paths: string[] = [];
|
|
219
|
+
const stripe = new Stripe("sk_test_CONTROLLED_ENCODE", {
|
|
220
|
+
apiVersion: API_VERSION,
|
|
221
|
+
maxNetworkRetries: 0,
|
|
222
|
+
httpClient: {
|
|
223
|
+
getClientName: () => "controlledencoding",
|
|
224
|
+
makeRequest: async (...args) => {
|
|
225
|
+
paths.push(args[2]);
|
|
226
|
+
return {
|
|
227
|
+
getStatusCode: () => 200,
|
|
228
|
+
getHeaders: () => ({}),
|
|
229
|
+
getRawResponse: () => ({ statusCode: 200 }),
|
|
230
|
+
toJSON: async () => ({ id: "custom /?#%", object: "coupon" }),
|
|
231
|
+
toStream: () => {
|
|
232
|
+
throw Error("unused");
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
expect(await readRenewalWireJSON(stripe, "coupon", "custom /?#%")).toEqual({
|
|
239
|
+
id: "custom /?#%",
|
|
240
|
+
object: "coupon",
|
|
241
|
+
});
|
|
242
|
+
expect(paths).toEqual(["/v1/coupons/custom%20%2F%3F%23%25"]);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it.each([".", ".."])(
|
|
246
|
+
"rejects dot-only coupon selector %s before actualSDK issues anyrequest",
|
|
247
|
+
async (id) => {
|
|
248
|
+
let requests = 0;
|
|
249
|
+
const stripe = new Stripe("sk_test_CONTROLLED_DOT_SEGMENT", {
|
|
250
|
+
apiVersion: API_VERSION,
|
|
251
|
+
maxNetworkRetries: 0,
|
|
252
|
+
httpClient: Stripe.createFetchHttpClient(async () => {
|
|
253
|
+
requests++;
|
|
254
|
+
return new Response(JSON.stringify({ id, object: "coupon" }), {
|
|
255
|
+
status: 200,
|
|
256
|
+
headers: { "content-type": "application/json" },
|
|
257
|
+
});
|
|
258
|
+
}),
|
|
259
|
+
});
|
|
260
|
+
await expect(
|
|
261
|
+
readRenewalWireJSON(stripe, "coupon", id),
|
|
262
|
+
).rejects.toBeDefined();
|
|
263
|
+
expect(requests).toBe(0);
|
|
264
|
+
},
|
|
265
|
+
);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
4
|
+
import { resourceId, unsupported } from "../renewalAggregate/shared.js";
|
|
5
|
+
|
|
6
|
+
const resources = {
|
|
7
|
+
subscription: { path: "subscriptions", id: resourceId("sub") },
|
|
8
|
+
price: { path: "prices", id: resourceId("price") },
|
|
9
|
+
customer: { path: "customers", id: resourceId("cus") },
|
|
10
|
+
coupon: {
|
|
11
|
+
path: "coupons",
|
|
12
|
+
id: z
|
|
13
|
+
.string()
|
|
14
|
+
.min(1)
|
|
15
|
+
.max(255)
|
|
16
|
+
.regex(/\S/)
|
|
17
|
+
.refine(
|
|
18
|
+
(value) => value !== "." && value !== "..",
|
|
19
|
+
"Coupon ID must not be a URL dot segment.",
|
|
20
|
+
),
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
const expansions = ["discounts", "items.data.discounts", "items.data.price"];
|
|
24
|
+
|
|
25
|
+
/** Fixed renewal GETs only. rawRequest bypasses typed resource Decimal coercion. */
|
|
26
|
+
export async function readRenewalWireJSON(
|
|
27
|
+
stripe: Stripe,
|
|
28
|
+
resource: keyof typeof resources,
|
|
29
|
+
id: string,
|
|
30
|
+
) {
|
|
31
|
+
const spec = resources[resource];
|
|
32
|
+
const selectedId = spec.id.parse(id);
|
|
33
|
+
const query =
|
|
34
|
+
resource === "subscription"
|
|
35
|
+
? `?${new URLSearchParams(expansions.map((value, i) => [`expand[${i}]`, value]))}`
|
|
36
|
+
: "";
|
|
37
|
+
const reply: unknown = await stripe.rawRequest(
|
|
38
|
+
"GET",
|
|
39
|
+
`/v1/${spec.path}/${encodeURIComponent(selectedId)}${query}`,
|
|
40
|
+
undefined,
|
|
41
|
+
{ apiVersion: "2026-09-30.endive", maxNetworkRetries: 0 },
|
|
42
|
+
);
|
|
43
|
+
if (!reply || typeof reply !== "object")
|
|
44
|
+
unsupported("missing_provider_http_status");
|
|
45
|
+
const descriptor = Object.getOwnPropertyDescriptor(reply, "lastResponse");
|
|
46
|
+
if (
|
|
47
|
+
!descriptor ||
|
|
48
|
+
descriptor.enumerable ||
|
|
49
|
+
!("value" in descriptor) ||
|
|
50
|
+
!descriptor.value ||
|
|
51
|
+
typeof descriptor.value !== "object"
|
|
52
|
+
)
|
|
53
|
+
unsupported("missing_provider_http_status");
|
|
54
|
+
// Fetch's public Response uses status; Node IncomingMessage uses statusCode.
|
|
55
|
+
// Neither is provider JSON. If both are present they must agree.
|
|
56
|
+
const meta = descriptor.value as { status?: unknown; statusCode?: unknown };
|
|
57
|
+
const codes = [meta.status, meta.statusCode].filter(
|
|
58
|
+
(code) => code !== undefined,
|
|
59
|
+
);
|
|
60
|
+
if (
|
|
61
|
+
!codes.length ||
|
|
62
|
+
codes.some(
|
|
63
|
+
(code) =>
|
|
64
|
+
typeof code !== "number" ||
|
|
65
|
+
!Number.isInteger(code) ||
|
|
66
|
+
code < 200 ||
|
|
67
|
+
code >= 300,
|
|
68
|
+
) ||
|
|
69
|
+
codes.some((code) => code !== codes[0])
|
|
70
|
+
)
|
|
71
|
+
unsupported("unverified_provider_http_status");
|
|
72
|
+
// Strict JSON copy remains unchanged. Nonenumerable transport metadata is
|
|
73
|
+
// excluded; every enumerable wire-body fact, including unknown fields, remains.
|
|
74
|
+
return renewalSnapshot(reply);
|
|
75
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
2
|
+
|
|
3
|
+
/** Internal fixed-family encoding. This is consistency, not authorization. */
|
|
4
|
+
export async function renewalBindingV2(
|
|
5
|
+
family: "renewal" | "renewal-attach",
|
|
6
|
+
preimage: unknown,
|
|
7
|
+
): Promise<string> {
|
|
8
|
+
// Finish all input capture and canonical bytes before the first await.
|
|
9
|
+
const text = JSON.stringify(
|
|
10
|
+
renewalSnapshot(["betterstripe-renewal-binding", 2, family, preimage]),
|
|
11
|
+
);
|
|
12
|
+
const bytes = new TextEncoder().encode(text);
|
|
13
|
+
if (bytes.byteLength > 500_000)
|
|
14
|
+
throw new Error("Renewal V2 binding exceeds its UTF-8 preimage budget.");
|
|
15
|
+
const subtle = globalThis.crypto?.subtle;
|
|
16
|
+
if (!subtle || typeof subtle.digest !== "function")
|
|
17
|
+
throw new Error("Renewal V2 requires Web Crypto SHA-256.");
|
|
18
|
+
const digest = await subtle.digest("SHA-256", bytes);
|
|
19
|
+
if (!(digest instanceof ArrayBuffer) || digest.byteLength !== 32)
|
|
20
|
+
throw new Error("Renewal V2 received an invalid SHA-256 result.");
|
|
21
|
+
const hex = Array.from(new Uint8Array(digest), (byte) =>
|
|
22
|
+
byte.toString(16).padStart(2, "0"),
|
|
23
|
+
).join("");
|
|
24
|
+
return `${family}:v2:sha256:${hex}`;
|
|
25
|
+
}
|