@fantastic.dev/better-stripe 0.82.0 → 0.83.1
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 +23 -0
- package/dist/client/billing/renewalAttach/couponJoins.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/couponJoins.js +76 -0
- package/dist/client/billing/renewalAttach/couponJoins.js.map +1 -0
- package/dist/client/billing/renewalAttach/index.d.ts +3 -0
- package/dist/client/billing/renewalAttach/index.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/index.js +3 -0
- package/dist/client/billing/renewalAttach/index.js.map +1 -0
- package/dist/client/billing/renewalAttach/reader.d.ts +141 -0
- package/dist/client/billing/renewalAttach/reader.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/reader.js +77 -0
- package/dist/client/billing/renewalAttach/reader.js.map +1 -0
- package/dist/client/billing/renewalAttach/request.d.ts +30 -0
- package/dist/client/billing/renewalAttach/request.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/request.js +39 -0
- package/dist/client/billing/renewalAttach/request.js.map +1 -0
- 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/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- 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/index.d.ts +3 -5
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +3 -3
- package/dist/client/index.js.map +1 -1
- package/dist/client/renewal.d.ts +8 -0
- package/dist/client/renewal.d.ts.map +1 -0
- package/dist/client/renewal.js +4 -0
- package/dist/client/renewal.js.map +1 -0
- package/dist/client/renewalContextClient.d.ts +130 -0
- package/dist/client/renewalContextClient.d.ts.map +1 -0
- package/dist/client/renewalContextClient.js +10 -0
- package/dist/client/renewalContextClient.js.map +1 -0
- 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/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +146 -0
- package/docs/renewal-price-replacements.md +55 -16
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/couponEvidence.test.ts +103 -0
- package/src/client/billing/renewalAttach/couponJoins.ts +92 -0
- package/src/client/billing/renewalAttach/index.ts +9 -0
- package/src/client/billing/renewalAttach/reader.test.ts +409 -0
- package/src/client/billing/renewalAttach/reader.ts +125 -0
- package/src/client/billing/renewalAttach/request.ts +49 -0
- package/src/client/billing/renewalAttach/wireJson.test.ts +265 -0
- package/src/client/billing/renewalAttach/wireJson.ts +75 -0
- package/src/client/helpers.ts +2 -4
- package/src/client/index.ts +3 -8
- package/src/client/renewal.ts +13 -0
- package/src/client/renewalContextClient.ts +23 -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,92 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import { equal, object, unsupported } from "../renewalAggregate/shared.js";
|
|
4
|
+
import { readRenewalWireJSON } from "./wireJson.js";
|
|
5
|
+
|
|
6
|
+
const id = z.string().min(1).max(255).regex(/\S/);
|
|
7
|
+
const discount = z
|
|
8
|
+
.object({
|
|
9
|
+
id: z.string().regex(/^di_[A-Za-z0-9_]+$/),
|
|
10
|
+
object: z.literal("discount"),
|
|
11
|
+
deleted: z.literal(false).optional(),
|
|
12
|
+
source: z
|
|
13
|
+
.object({
|
|
14
|
+
type: z.literal("coupon"),
|
|
15
|
+
coupon: z.union([
|
|
16
|
+
id,
|
|
17
|
+
z
|
|
18
|
+
.object({
|
|
19
|
+
id,
|
|
20
|
+
object: z.literal("coupon"),
|
|
21
|
+
deleted: z.literal(false).optional(),
|
|
22
|
+
})
|
|
23
|
+
.passthrough(),
|
|
24
|
+
]),
|
|
25
|
+
})
|
|
26
|
+
.passthrough(),
|
|
27
|
+
})
|
|
28
|
+
.passthrough();
|
|
29
|
+
/** Explicit bounded joins, never a fallback for failed or over-depth expansion. */
|
|
30
|
+
export async function joinRenewalCoupons(
|
|
31
|
+
stripe: Stripe,
|
|
32
|
+
subscription: Record<string, unknown>,
|
|
33
|
+
item: Record<string, unknown>,
|
|
34
|
+
livemode: boolean,
|
|
35
|
+
) {
|
|
36
|
+
const lists = [subscription.discounts, item.discounts].map((value) => {
|
|
37
|
+
const parsed = z.array(discount).max(20).safeParse(value);
|
|
38
|
+
if (!parsed.success)
|
|
39
|
+
unsupported("incomplete_or_unsupported_discount_evidence");
|
|
40
|
+
return parsed.data;
|
|
41
|
+
});
|
|
42
|
+
for (const list of lists)
|
|
43
|
+
for (const redemption of list) {
|
|
44
|
+
const ref = redemption.source.coupon;
|
|
45
|
+
if (
|
|
46
|
+
typeof ref !== "string" &&
|
|
47
|
+
ref.livemode !== undefined &&
|
|
48
|
+
ref.livemode !== livemode
|
|
49
|
+
)
|
|
50
|
+
unsupported("expanded_coupon_mode_mismatch");
|
|
51
|
+
}
|
|
52
|
+
const coupons = new Map<string, Record<string, unknown>>();
|
|
53
|
+
for (const list of lists)
|
|
54
|
+
for (const redemption of list) {
|
|
55
|
+
const couponId =
|
|
56
|
+
typeof redemption.source.coupon === "string"
|
|
57
|
+
? redemption.source.coupon
|
|
58
|
+
: redemption.source.coupon.id;
|
|
59
|
+
let coupon = coupons.get(couponId);
|
|
60
|
+
if (!coupon) {
|
|
61
|
+
coupon = object(await readRenewalWireJSON(stripe, "coupon", couponId));
|
|
62
|
+
if (
|
|
63
|
+
coupon.id !== couponId ||
|
|
64
|
+
coupon.object !== "coupon" ||
|
|
65
|
+
coupon.deleted === true ||
|
|
66
|
+
(coupon.livemode !== undefined && coupon.livemode !== livemode)
|
|
67
|
+
)
|
|
68
|
+
unsupported("coupon_join_identity_mismatch");
|
|
69
|
+
coupons.set(couponId, coupon);
|
|
70
|
+
}
|
|
71
|
+
// A join may expand a string reference, but must never replace facts
|
|
72
|
+
// already present in a full read. Shape drift is conservatively rejected.
|
|
73
|
+
if (
|
|
74
|
+
typeof redemption.source.coupon !== "string" &&
|
|
75
|
+
!equal(redemption.source.coupon, coupon)
|
|
76
|
+
)
|
|
77
|
+
unsupported("expanded_coupon_read_drift");
|
|
78
|
+
}
|
|
79
|
+
const joined = lists.map((list) =>
|
|
80
|
+
list.map((redemption) => {
|
|
81
|
+
const couponId =
|
|
82
|
+
typeof redemption.source.coupon === "string"
|
|
83
|
+
? redemption.source.coupon
|
|
84
|
+
: redemption.source.coupon.id;
|
|
85
|
+
return {
|
|
86
|
+
...redemption,
|
|
87
|
+
source: { ...redemption.source, coupon: coupons.get(couponId)! },
|
|
88
|
+
};
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
return { subscriptionDiscounts: joined[0], itemDiscounts: joined[1] };
|
|
92
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
readRenewalPriceReplacementContext,
|
|
3
|
+
type ReadRenewalPriceReplacementContextOptions,
|
|
4
|
+
} from "./reader.js";
|
|
5
|
+
export {
|
|
6
|
+
prepareRenewalScheduleAttachment,
|
|
7
|
+
type PrepareRenewalScheduleAttachmentOptions,
|
|
8
|
+
type PreparedRenewalScheduleAttachment,
|
|
9
|
+
} from "./request.js";
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { describe, expect, it, vi, afterEach } from "vitest";
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
import { SingleAttemptHttpClient } from "../../singleAttemptHttpClient.js";
|
|
4
|
+
import {
|
|
5
|
+
fixture,
|
|
6
|
+
API_VERSION,
|
|
7
|
+
alter,
|
|
8
|
+
} from "../../../../tests/renewalAggregate/fixtures.js";
|
|
9
|
+
import {
|
|
10
|
+
readRenewalPriceReplacementContext,
|
|
11
|
+
prepareRenewalScheduleAttachment,
|
|
12
|
+
} from "./index.js";
|
|
13
|
+
import { binding } from "../renewalAggregate/shared.js";
|
|
14
|
+
|
|
15
|
+
type Wire = {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
headers: Parameters<Stripe.HttpClient["makeRequest"]>[4];
|
|
19
|
+
body: string;
|
|
20
|
+
};
|
|
21
|
+
function harness(
|
|
22
|
+
options: {
|
|
23
|
+
sub?: unknown;
|
|
24
|
+
customer?: unknown;
|
|
25
|
+
target?: unknown;
|
|
26
|
+
coupon?: (id: string) => unknown;
|
|
27
|
+
fail?: string;
|
|
28
|
+
} = {},
|
|
29
|
+
) {
|
|
30
|
+
const f = fixture();
|
|
31
|
+
const wires: Wire[] = [];
|
|
32
|
+
const sub = structuredClone(
|
|
33
|
+
options.sub ?? f.subscription,
|
|
34
|
+
) as typeof f.subscription;
|
|
35
|
+
const coupons = new Map<string, unknown>();
|
|
36
|
+
for (const d of [...f.subscription.discounts, ...f.originalItem.discounts])
|
|
37
|
+
coupons.set(d.source.coupon.id, d.source.coupon);
|
|
38
|
+
const delegate = {
|
|
39
|
+
getClientName: () => "controlled-renewal-reader",
|
|
40
|
+
async makeRequest(...args: Parameters<Stripe.HttpClient["makeRequest"]>) {
|
|
41
|
+
const wire = {
|
|
42
|
+
path: args[2],
|
|
43
|
+
method: args[3],
|
|
44
|
+
headers: args[4],
|
|
45
|
+
body: args[5],
|
|
46
|
+
};
|
|
47
|
+
wires.push(wire);
|
|
48
|
+
if (options.fail && wire.path.startsWith(options.fail))
|
|
49
|
+
throw Object.assign(new Error("controlled closed response"), {
|
|
50
|
+
code: "ECONNRESET",
|
|
51
|
+
});
|
|
52
|
+
const url = new URL(wire.path, "https://api.stripe.com");
|
|
53
|
+
const data =
|
|
54
|
+
url.pathname === "/v1/subscriptions/sub_original"
|
|
55
|
+
? sub
|
|
56
|
+
: url.pathname === "/v1/prices/price_target"
|
|
57
|
+
? (options.target ?? f.targetPrice)
|
|
58
|
+
: url.pathname === "/v1/customers/cus_buyer"
|
|
59
|
+
? (options.customer ?? f.billingCustomer)
|
|
60
|
+
: url.pathname.startsWith("/v1/coupons/")
|
|
61
|
+
? (options.coupon?.(
|
|
62
|
+
decodeURIComponent(url.pathname.slice(12)),
|
|
63
|
+
) ?? coupons.get(decodeURIComponent(url.pathname.slice(12))))
|
|
64
|
+
: (() => {
|
|
65
|
+
throw Error("unplanned request");
|
|
66
|
+
})();
|
|
67
|
+
return {
|
|
68
|
+
getStatusCode: () => 200,
|
|
69
|
+
getHeaders: () => ({
|
|
70
|
+
"request-id": "req_controlled",
|
|
71
|
+
"stripe-version": API_VERSION,
|
|
72
|
+
}),
|
|
73
|
+
toJSON: async () => structuredClone(data),
|
|
74
|
+
getRawResponse: () => ({ statusCode: 200 }),
|
|
75
|
+
toStream: () => {
|
|
76
|
+
throw Error("not used");
|
|
77
|
+
},
|
|
78
|
+
} as unknown as Stripe.HttpClientResponse;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const stripe = new Stripe("sk_test_CONTROLLED", {
|
|
82
|
+
apiVersion: API_VERSION,
|
|
83
|
+
maxNetworkRetries: 0,
|
|
84
|
+
httpClient: new SingleAttemptHttpClient(delegate),
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
f,
|
|
88
|
+
wires,
|
|
89
|
+
stripe,
|
|
90
|
+
read: () =>
|
|
91
|
+
readRenewalPriceReplacementContext(
|
|
92
|
+
stripe,
|
|
93
|
+
{ expected: f.prepared.expected },
|
|
94
|
+
API_VERSION,
|
|
95
|
+
),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
afterEach(() => vi.restoreAllMocks());
|
|
99
|
+
describe("actual SDK renewal context acquisition", () => {
|
|
100
|
+
it("uses fixed bounded GET plan, joins coupons and returns complete snapshots", async () => {
|
|
101
|
+
const h = harness();
|
|
102
|
+
vi.spyOn(Date, "now").mockReturnValue(h.f.preflightInput.observedAt * 1000);
|
|
103
|
+
const result = await h.read();
|
|
104
|
+
expect(result).toMatchObject({
|
|
105
|
+
version: 1,
|
|
106
|
+
scope: "platform",
|
|
107
|
+
apiVersion: API_VERSION,
|
|
108
|
+
readProfile: "renewal-context-v1",
|
|
109
|
+
providerQualification: "required",
|
|
110
|
+
observedAt: h.f.preflightInput.observedAt,
|
|
111
|
+
subscription: h.f.subscription,
|
|
112
|
+
targetPrice: h.f.targetPrice,
|
|
113
|
+
billingCustomer: h.f.billingCustomer,
|
|
114
|
+
});
|
|
115
|
+
expect(
|
|
116
|
+
h.wires.map((w) => new URL(w.path, "https://api.stripe.com").pathname),
|
|
117
|
+
).toEqual([
|
|
118
|
+
"/v1/subscriptions/sub_original",
|
|
119
|
+
"/v1/prices/price_target",
|
|
120
|
+
"/v1/customers/cus_buyer",
|
|
121
|
+
"/v1/coupons/custom-subscription-offer",
|
|
122
|
+
"/v1/coupons/custom-item-offer",
|
|
123
|
+
]);
|
|
124
|
+
expect(
|
|
125
|
+
Array.from(
|
|
126
|
+
new URL(
|
|
127
|
+
h.wires[0].path,
|
|
128
|
+
"https://api.stripe.com",
|
|
129
|
+
).searchParams.values(),
|
|
130
|
+
),
|
|
131
|
+
).toEqual(["discounts", "items.data.discounts", "items.data.price"]);
|
|
132
|
+
for (const w of h.wires) {
|
|
133
|
+
expect(w.method).toBe("GET");
|
|
134
|
+
expect(w.body).toBe("");
|
|
135
|
+
expect(w.headers["Stripe-Version"]).toBe(API_VERSION);
|
|
136
|
+
expect(w.headers["Stripe-Account"]).toBeUndefined();
|
|
137
|
+
expect(w.headers["Stripe-Context"]).toBeUndefined();
|
|
138
|
+
expect(w.headers["Idempotency-Key"]).toBeUndefined();
|
|
139
|
+
}
|
|
140
|
+
expect(h.wires.slice(1).every((w) => !w.path.includes("?"))).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
it("performs explicit coupon GET joins when refs are strings, never deep expansion", async () => {
|
|
143
|
+
const f = fixture();
|
|
144
|
+
const sub = alter(
|
|
145
|
+
alter(
|
|
146
|
+
f.subscription,
|
|
147
|
+
["discounts", 0, "source", "coupon"],
|
|
148
|
+
"custom-subscription-offer",
|
|
149
|
+
),
|
|
150
|
+
["items", "data", 0, "discounts", 0, "source", "coupon"],
|
|
151
|
+
"custom-item-offer",
|
|
152
|
+
);
|
|
153
|
+
const h = harness({ sub });
|
|
154
|
+
expect((await h.read()).subscription).toEqual(f.subscription);
|
|
155
|
+
expect(h.wires).toHaveLength(5);
|
|
156
|
+
});
|
|
157
|
+
it("deduplicates coupon IDs while retaining independent discount ownership/order", async () => {
|
|
158
|
+
const f = fixture();
|
|
159
|
+
const sub = alter(
|
|
160
|
+
f.subscription,
|
|
161
|
+
["items", "data", 0, "discounts", 0, "source", "coupon"],
|
|
162
|
+
f.subscription.discounts[0].source.coupon,
|
|
163
|
+
);
|
|
164
|
+
const h = harness({ sub });
|
|
165
|
+
const result = await h.read();
|
|
166
|
+
expect(h.wires).toHaveLength(4);
|
|
167
|
+
expect(result.subscription.items.data[0].discounts[0].id).toBe("di_item");
|
|
168
|
+
});
|
|
169
|
+
it("retains actual nonnull schedule association without adopting it or fabricating null", async () => {
|
|
170
|
+
const h = harness({
|
|
171
|
+
sub: { ...fixture().subscription, schedule: "sub_sched_observed" },
|
|
172
|
+
});
|
|
173
|
+
expect((await h.read()).subscription.schedule).toBe("sub_sched_observed");
|
|
174
|
+
expect(h.wires.every((w) => w.method === "GET")).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
it.each([
|
|
177
|
+
[
|
|
178
|
+
"foreign subscription",
|
|
179
|
+
() => harness({ sub: { ...fixture().subscription, id: "sub_foreign" } }),
|
|
180
|
+
],
|
|
181
|
+
[
|
|
182
|
+
"truncated item list",
|
|
183
|
+
() =>
|
|
184
|
+
harness({
|
|
185
|
+
sub: alter(fixture().subscription, ["items", "has_more"], true),
|
|
186
|
+
}),
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
"foreign target",
|
|
190
|
+
() =>
|
|
191
|
+
harness({ target: { ...fixture().targetPrice, id: "price_foreign" } }),
|
|
192
|
+
],
|
|
193
|
+
[
|
|
194
|
+
"foreign customer",
|
|
195
|
+
() =>
|
|
196
|
+
harness({
|
|
197
|
+
customer: {
|
|
198
|
+
...fixture().billingCustomer,
|
|
199
|
+
customer_account: "acct_foreign",
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
],
|
|
203
|
+
[
|
|
204
|
+
"unexpanded discount",
|
|
205
|
+
() =>
|
|
206
|
+
harness({
|
|
207
|
+
sub: alter(
|
|
208
|
+
fixture().subscription,
|
|
209
|
+
["discounts"],
|
|
210
|
+
["di_subscription"],
|
|
211
|
+
),
|
|
212
|
+
}),
|
|
213
|
+
],
|
|
214
|
+
[
|
|
215
|
+
"promotion source",
|
|
216
|
+
() =>
|
|
217
|
+
harness({
|
|
218
|
+
sub: alter(
|
|
219
|
+
fixture().subscription,
|
|
220
|
+
["discounts", 0, "source", "type"],
|
|
221
|
+
"promotion_code",
|
|
222
|
+
),
|
|
223
|
+
}),
|
|
224
|
+
],
|
|
225
|
+
[
|
|
226
|
+
"wrong coupon",
|
|
227
|
+
() =>
|
|
228
|
+
harness({
|
|
229
|
+
coupon: () => ({
|
|
230
|
+
id: "foreign",
|
|
231
|
+
object: "coupon",
|
|
232
|
+
duration: "forever",
|
|
233
|
+
livemode: false,
|
|
234
|
+
}),
|
|
235
|
+
}),
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
"deleted coupon",
|
|
239
|
+
() =>
|
|
240
|
+
harness({
|
|
241
|
+
coupon: (id: string) => ({ id, object: "coupon", deleted: true }),
|
|
242
|
+
}),
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
"foreign coupon mode",
|
|
246
|
+
() =>
|
|
247
|
+
harness({
|
|
248
|
+
coupon: (id: string) => ({
|
|
249
|
+
id,
|
|
250
|
+
object: "coupon",
|
|
251
|
+
livemode: true,
|
|
252
|
+
duration: "forever",
|
|
253
|
+
}),
|
|
254
|
+
}),
|
|
255
|
+
],
|
|
256
|
+
])("rejects %s without POST or alternate fallback", async (_label, build) => {
|
|
257
|
+
const h = build();
|
|
258
|
+
await expect(h.read()).rejects.toThrow();
|
|
259
|
+
expect(h.wires.every((w) => w.method === "GET")).toBe(true);
|
|
260
|
+
});
|
|
261
|
+
it("does not overwrite contradictory expanded coupon mode with a later join", async () => {
|
|
262
|
+
const h = harness({
|
|
263
|
+
sub: alter(
|
|
264
|
+
fixture().subscription,
|
|
265
|
+
["discounts", 0, "source", "coupon", "livemode"],
|
|
266
|
+
true,
|
|
267
|
+
),
|
|
268
|
+
});
|
|
269
|
+
await expect(h.read()).rejects.toThrow();
|
|
270
|
+
expect(
|
|
271
|
+
h.wires.filter((w) => w.path.startsWith("/v1/coupons/")),
|
|
272
|
+
).toHaveLength(0);
|
|
273
|
+
});
|
|
274
|
+
it("does not retry closed connections or fall back after planned read fails", async () => {
|
|
275
|
+
const h = harness({ fail: "/v1/coupons/" });
|
|
276
|
+
await expect(h.read()).rejects.toThrow();
|
|
277
|
+
expect(
|
|
278
|
+
h.wires.filter((w) => w.path.startsWith("/v1/coupons/")),
|
|
279
|
+
).toHaveLength(1);
|
|
280
|
+
expect(h.wires).toHaveLength(4);
|
|
281
|
+
});
|
|
282
|
+
it("rejects unqualified API before reading", async () => {
|
|
283
|
+
const h = harness();
|
|
284
|
+
await expect(
|
|
285
|
+
readRenewalPriceReplacementContext(
|
|
286
|
+
h.stripe,
|
|
287
|
+
{ expected: h.f.prepared.expected },
|
|
288
|
+
"2025-03-31.basil",
|
|
289
|
+
),
|
|
290
|
+
).rejects.toThrow();
|
|
291
|
+
expect(h.wires).toHaveLength(0);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
describe("pure retained attach request", () => {
|
|
295
|
+
function options() {
|
|
296
|
+
const f = fixture();
|
|
297
|
+
return {
|
|
298
|
+
prepared: f.prepared,
|
|
299
|
+
apiVersion: API_VERSION,
|
|
300
|
+
attachKey: "caller:attach",
|
|
301
|
+
firstDispatchedAt: f.preflightInput.observedAt,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
it("binds the exact full stage request with no mutating provider capability", () => {
|
|
305
|
+
const o = options();
|
|
306
|
+
const r = prepareRenewalScheduleAttachment(o);
|
|
307
|
+
const { requestIdentity, ...unsigned } = r;
|
|
308
|
+
expect(unsigned).toEqual({
|
|
309
|
+
version: 1,
|
|
310
|
+
stage: "attach",
|
|
311
|
+
scope: "platform",
|
|
312
|
+
apiVersion: API_VERSION,
|
|
313
|
+
logicalBinding: o.prepared.requestIdentity.binding,
|
|
314
|
+
method: "POST",
|
|
315
|
+
path: "/v1/subscription_schedules",
|
|
316
|
+
payload: { from_subscription: "sub_original" },
|
|
317
|
+
key: o.attachKey,
|
|
318
|
+
firstDispatchedAt: o.firstDispatchedAt,
|
|
319
|
+
});
|
|
320
|
+
expect(requestIdentity).toEqual({
|
|
321
|
+
key: o.attachKey,
|
|
322
|
+
binding: binding("renewal-attach", unsigned),
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
it("permits explicit caller choice of same logical and attach key; never derives one", () => {
|
|
326
|
+
const o = options();
|
|
327
|
+
o.attachKey = o.prepared.requestIdentity.key;
|
|
328
|
+
expect(prepareRenewalScheduleAttachment(o).key).toBe(o.attachKey);
|
|
329
|
+
});
|
|
330
|
+
it.each(["", "x\ny", "x".repeat(256)])(
|
|
331
|
+
"rejects invalid explicit key",
|
|
332
|
+
(key) =>
|
|
333
|
+
expect(() =>
|
|
334
|
+
prepareRenewalScheduleAttachment({ ...options(), attachKey: key }),
|
|
335
|
+
).toThrow(),
|
|
336
|
+
);
|
|
337
|
+
it("rejects forged preparation and arbitrary create options", () => {
|
|
338
|
+
expect(() =>
|
|
339
|
+
prepareRenewalScheduleAttachment({
|
|
340
|
+
...options(),
|
|
341
|
+
prepared: { ...options().prepared, quantity: 9 },
|
|
342
|
+
}),
|
|
343
|
+
).toThrow();
|
|
344
|
+
expect(() =>
|
|
345
|
+
(prepareRenewalScheduleAttachment as (value: unknown) => unknown)({
|
|
346
|
+
...options(),
|
|
347
|
+
metadata: { unexpected: "yes" },
|
|
348
|
+
}),
|
|
349
|
+
).toThrow();
|
|
350
|
+
});
|
|
351
|
+
it("rejects missing key/time and wrong API without generating replacement", () => {
|
|
352
|
+
const invalid = prepareRenewalScheduleAttachment as (
|
|
353
|
+
value: unknown,
|
|
354
|
+
) => unknown;
|
|
355
|
+
const o = options();
|
|
356
|
+
for (const x of [
|
|
357
|
+
{ ...o, attachKey: undefined },
|
|
358
|
+
{ ...o, firstDispatchedAt: undefined },
|
|
359
|
+
{ ...o, firstDispatchedAt: -1 },
|
|
360
|
+
{ ...o, apiVersion: "unknown" },
|
|
361
|
+
])
|
|
362
|
+
expect(() => invalid(x)).toThrow();
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
describe("public server SDK placement", () => {
|
|
367
|
+
it("exposes actual class reader and pure builder, selecting single-attempt client", async () => {
|
|
368
|
+
const publicApi = await import("../../index.js");
|
|
369
|
+
expect(publicApi.prepareRenewalPriceReplacement).toBeTypeOf("function");
|
|
370
|
+
const helpers = await import("../../helpers.js");
|
|
371
|
+
const h = harness();
|
|
372
|
+
const selected = vi
|
|
373
|
+
.spyOn(helpers, "getStripeClient")
|
|
374
|
+
.mockReturnValue(h.stripe);
|
|
375
|
+
const client = new publicApi.BetterStripe(
|
|
376
|
+
{} as ConstructorParameters<typeof publicApi.BetterStripe>[0],
|
|
377
|
+
{
|
|
378
|
+
STRIPE_SECRET_KEY: "sk_test_CONTROLLED",
|
|
379
|
+
stripeApiVersion: API_VERSION,
|
|
380
|
+
},
|
|
381
|
+
);
|
|
382
|
+
const reader = (
|
|
383
|
+
client as unknown as {
|
|
384
|
+
readRenewalPriceReplacementContext: (
|
|
385
|
+
ctx: unknown,
|
|
386
|
+
opts: unknown,
|
|
387
|
+
) => Promise<unknown>;
|
|
388
|
+
}
|
|
389
|
+
).readRenewalPriceReplacementContext;
|
|
390
|
+
expect(typeof reader).toBe("function");
|
|
391
|
+
await reader.call(client, {}, { expected: h.f.prepared.expected });
|
|
392
|
+
expect(selected).toHaveBeenCalledWith("sk_test_CONTROLLED", API_VERSION, {
|
|
393
|
+
singleAttempt: true,
|
|
394
|
+
});
|
|
395
|
+
const exported = (
|
|
396
|
+
publicApi as unknown as {
|
|
397
|
+
prepareRenewalScheduleAttachment: typeof prepareRenewalScheduleAttachment;
|
|
398
|
+
}
|
|
399
|
+
).prepareRenewalScheduleAttachment;
|
|
400
|
+
expect(
|
|
401
|
+
exported({
|
|
402
|
+
prepared: h.f.prepared,
|
|
403
|
+
apiVersion: API_VERSION,
|
|
404
|
+
attachKey: "caller:public",
|
|
405
|
+
firstDispatchedAt: h.f.preflightInput.observedAt,
|
|
406
|
+
}).key,
|
|
407
|
+
).toBe("caller:public");
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
import {
|
|
4
|
+
renewalExpected,
|
|
5
|
+
renewalSubscription,
|
|
6
|
+
renewalPrice,
|
|
7
|
+
renewalBillingCustomer,
|
|
8
|
+
} from "../renewalReplacementSchema.js";
|
|
9
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
10
|
+
import {
|
|
11
|
+
productId,
|
|
12
|
+
referenceId,
|
|
13
|
+
resourceId,
|
|
14
|
+
supportedVersion,
|
|
15
|
+
unsupported,
|
|
16
|
+
timestamp,
|
|
17
|
+
} from "../renewalAggregate/shared.js";
|
|
18
|
+
import { joinRenewalCoupons } from "./couponJoins.js";
|
|
19
|
+
import { readRenewalWireJSON } from "./wireJson.js";
|
|
20
|
+
|
|
21
|
+
const input = z.object({ expected: renewalExpected }).strict();
|
|
22
|
+
export type ReadRenewalPriceReplacementContextOptions = z.infer<typeof input>;
|
|
23
|
+
const observedSchedule = z.union([
|
|
24
|
+
z.null(),
|
|
25
|
+
resourceId("sub_sched"),
|
|
26
|
+
z
|
|
27
|
+
.object({
|
|
28
|
+
id: resourceId("sub_sched"),
|
|
29
|
+
object: z.literal("subscription_schedule"),
|
|
30
|
+
deleted: z.literal(false).optional(),
|
|
31
|
+
})
|
|
32
|
+
.passthrough(),
|
|
33
|
+
]);
|
|
34
|
+
const readSubscription = renewalSubscription.extend({
|
|
35
|
+
schedule: observedSchedule,
|
|
36
|
+
});
|
|
37
|
+
/** Fixed provider read profile. Evidence acquisition is not write qualification. */
|
|
38
|
+
export async function readRenewalPriceReplacementContext(
|
|
39
|
+
stripe: Stripe,
|
|
40
|
+
options: ReadRenewalPriceReplacementContextOptions,
|
|
41
|
+
apiVersion: string,
|
|
42
|
+
) {
|
|
43
|
+
const args = input.parse(renewalSnapshot(options));
|
|
44
|
+
supportedVersion(apiVersion);
|
|
45
|
+
const expected = args.expected;
|
|
46
|
+
const subscription = readSubscription.parse(
|
|
47
|
+
renewalSnapshot(
|
|
48
|
+
await readRenewalWireJSON(
|
|
49
|
+
stripe,
|
|
50
|
+
"subscription",
|
|
51
|
+
expected.subscriptionId,
|
|
52
|
+
),
|
|
53
|
+
),
|
|
54
|
+
);
|
|
55
|
+
const item = subscription.items.data[0];
|
|
56
|
+
const context = {
|
|
57
|
+
customerAccountId: expected.customerAccountId,
|
|
58
|
+
livemode: expected.livemode,
|
|
59
|
+
};
|
|
60
|
+
if (
|
|
61
|
+
subscription.id !== expected.subscriptionId ||
|
|
62
|
+
subscription.customer_account !== expected.customerAccountId ||
|
|
63
|
+
subscription.livemode !== expected.livemode ||
|
|
64
|
+
item.id !== expected.itemId ||
|
|
65
|
+
item.subscription !== expected.subscriptionId ||
|
|
66
|
+
item.price.id !== expected.currentPriceId ||
|
|
67
|
+
item.price.livemode !== expected.livemode ||
|
|
68
|
+
productId(item.price.product, context) !== expected.currentProductId ||
|
|
69
|
+
item.current_period_start !== expected.periodStart ||
|
|
70
|
+
item.current_period_end !== expected.periodEnd
|
|
71
|
+
)
|
|
72
|
+
unsupported("original_read_identity_mismatch");
|
|
73
|
+
const targetPrice = renewalPrice.parse(
|
|
74
|
+
renewalSnapshot(
|
|
75
|
+
await readRenewalWireJSON(stripe, "price", expected.targetPriceId),
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
if (
|
|
79
|
+
targetPrice.id !== expected.targetPriceId ||
|
|
80
|
+
targetPrice.livemode !== expected.livemode ||
|
|
81
|
+
productId(targetPrice.product, context) !== expected.targetProductId
|
|
82
|
+
)
|
|
83
|
+
unsupported("target_read_identity_mismatch");
|
|
84
|
+
const customerId = referenceId(
|
|
85
|
+
subscription.customer,
|
|
86
|
+
"cus",
|
|
87
|
+
"customer",
|
|
88
|
+
context,
|
|
89
|
+
);
|
|
90
|
+
const billingCustomer = renewalBillingCustomer.parse(
|
|
91
|
+
renewalSnapshot(await readRenewalWireJSON(stripe, "customer", customerId)),
|
|
92
|
+
);
|
|
93
|
+
if (
|
|
94
|
+
billingCustomer.id !== customerId ||
|
|
95
|
+
billingCustomer.customer_account !== expected.customerAccountId ||
|
|
96
|
+
billingCustomer.livemode !== expected.livemode
|
|
97
|
+
)
|
|
98
|
+
unsupported("billing_customer_identity_mismatch");
|
|
99
|
+
const joined = await joinRenewalCoupons(
|
|
100
|
+
stripe,
|
|
101
|
+
subscription,
|
|
102
|
+
item,
|
|
103
|
+
expected.livemode,
|
|
104
|
+
);
|
|
105
|
+
const normalizedSubscription = {
|
|
106
|
+
...subscription,
|
|
107
|
+
discounts: joined.subscriptionDiscounts,
|
|
108
|
+
items: {
|
|
109
|
+
...subscription.items,
|
|
110
|
+
data: [{ ...item, discounts: joined.itemDiscounts }],
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
const observedAt = timestamp.parse(Math.floor(Date.now() / 1000));
|
|
114
|
+
return renewalSnapshot({
|
|
115
|
+
version: 1 as const,
|
|
116
|
+
scope: "platform" as const,
|
|
117
|
+
apiVersion,
|
|
118
|
+
readProfile: "renewal-context-v1" as const,
|
|
119
|
+
providerQualification: "required" as const,
|
|
120
|
+
observedAt,
|
|
121
|
+
subscription: normalizedSubscription,
|
|
122
|
+
targetPrice,
|
|
123
|
+
billingCustomer,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { retainedPreparation } from "../renewalAggregate/preflight.js";
|
|
3
|
+
import {
|
|
4
|
+
binding,
|
|
5
|
+
supportedVersion,
|
|
6
|
+
timestamp,
|
|
7
|
+
unsupported,
|
|
8
|
+
} from "../renewalAggregate/shared.js";
|
|
9
|
+
import { idempotencyOptions } from "../../core/idempotency.js";
|
|
10
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
11
|
+
|
|
12
|
+
const input = z
|
|
13
|
+
.object({
|
|
14
|
+
prepared: z.unknown(),
|
|
15
|
+
apiVersion: z.string(),
|
|
16
|
+
attachKey: z.string(),
|
|
17
|
+
firstDispatchedAt: timestamp,
|
|
18
|
+
})
|
|
19
|
+
.strict();
|
|
20
|
+
export type PrepareRenewalScheduleAttachmentOptions = z.infer<typeof input>;
|
|
21
|
+
/** Pure retained request only. It neither authorizes nor dispatches attachment. */
|
|
22
|
+
export function prepareRenewalScheduleAttachment(
|
|
23
|
+
options: PrepareRenewalScheduleAttachmentOptions,
|
|
24
|
+
) {
|
|
25
|
+
const args = input.parse(renewalSnapshot(options));
|
|
26
|
+
supportedVersion(args.apiVersion);
|
|
27
|
+
const prepared = retainedPreparation(args.prepared);
|
|
28
|
+
const key = idempotencyOptions(args.attachKey)?.idempotencyKey;
|
|
29
|
+
if (!key) unsupported("missing_explicit_attach_key");
|
|
30
|
+
const request = {
|
|
31
|
+
version: 1 as const,
|
|
32
|
+
stage: "attach" as const,
|
|
33
|
+
scope: "platform" as const,
|
|
34
|
+
apiVersion: args.apiVersion,
|
|
35
|
+
logicalBinding: prepared.requestIdentity.binding,
|
|
36
|
+
method: "POST" as const,
|
|
37
|
+
path: "/v1/subscription_schedules" as const,
|
|
38
|
+
payload: { from_subscription: prepared.expected.subscriptionId },
|
|
39
|
+
key,
|
|
40
|
+
firstDispatchedAt: args.firstDispatchedAt,
|
|
41
|
+
};
|
|
42
|
+
return renewalSnapshot({
|
|
43
|
+
...request,
|
|
44
|
+
requestIdentity: { key, binding: binding("renewal-attach", request) },
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export type PreparedRenewalScheduleAttachment = ReturnType<
|
|
48
|
+
typeof prepareRenewalScheduleAttachment
|
|
49
|
+
>;
|