@fantastic.dev/better-stripe 0.59.0 → 0.60.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.
Files changed (124) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +3 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +650 -44
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +309 -6
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +618 -12
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +325 -22
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/index.d.ts +4 -0
  12. package/dist/client/index.d.ts.map +1 -1
  13. package/dist/client/paymentSource/resolver.d.ts +12 -1
  14. package/dist/client/paymentSource/resolver.d.ts.map +1 -1
  15. package/dist/client/paymentSource/resolver.fixtures.d.ts +220 -0
  16. package/dist/client/paymentSource/resolver.fixtures.d.ts.map +1 -0
  17. package/dist/client/paymentSource/resolver.fixtures.js +189 -0
  18. package/dist/client/paymentSource/resolver.fixtures.js.map +1 -0
  19. package/dist/client/paymentSource/resolver.js +28 -3
  20. package/dist/client/paymentSource/resolver.js.map +1 -1
  21. package/dist/client/settlement/execution.d.ts +25 -0
  22. package/dist/client/settlement/execution.d.ts.map +1 -1
  23. package/dist/client/settlement/execution.js +2 -0
  24. package/dist/client/settlement/execution.js.map +1 -1
  25. package/dist/client/usage/catalogPrices.d.ts +1 -1
  26. package/dist/client/usage/catalogPrices.d.ts.map +1 -1
  27. package/dist/client/usage/catalogPrices.js +4 -4
  28. package/dist/client/usage/catalogPrices.js.map +1 -1
  29. package/dist/client/usage/client.d.ts +2 -2
  30. package/dist/client/usage/client.js +2 -2
  31. package/dist/client/usage/creditRevenue.d.ts +22 -0
  32. package/dist/client/usage/creditRevenue.d.ts.map +1 -0
  33. package/dist/client/usage/creditRevenue.js +118 -0
  34. package/dist/client/usage/creditRevenue.js.map +1 -0
  35. package/dist/client/usage/creditSettlement.d.ts +29 -0
  36. package/dist/client/usage/creditSettlement.d.ts.map +1 -0
  37. package/dist/client/usage/creditSettlement.fixtures.d.ts +215 -0
  38. package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
  39. package/dist/client/usage/creditSettlement.fixtures.js +59 -0
  40. package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
  41. package/dist/client/usage/creditSettlement.js +70 -0
  42. package/dist/client/usage/creditSettlement.js.map +1 -0
  43. package/dist/client/usage/creditTransport.fixtures.d.ts +7 -1
  44. package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
  45. package/dist/client/usage/creditTransport.fixtures.js +25 -4
  46. package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
  47. package/dist/client/usage/index.d.ts +4 -0
  48. package/dist/client/usage/index.d.ts.map +1 -1
  49. package/dist/client/usage/invoiceSale.d.ts +22 -0
  50. package/dist/client/usage/invoiceSale.d.ts.map +1 -0
  51. package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
  52. package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
  53. package/dist/client/usage/invoiceSale.fixtures.js +104 -0
  54. package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
  55. package/dist/client/usage/invoiceSale.js +117 -0
  56. package/dist/client/usage/invoiceSale.js.map +1 -0
  57. package/dist/client/usage/observationClient.d.ts +2 -2
  58. package/dist/client/usage/observationClient.d.ts.map +1 -1
  59. package/dist/client/usage/settlement.d.ts +32 -0
  60. package/dist/client/usage/settlement.d.ts.map +1 -0
  61. package/dist/client/usage/settlement.js +60 -0
  62. package/dist/client/usage/settlement.js.map +1 -0
  63. package/dist/client/usage/settlementClient.d.ts +51 -0
  64. package/dist/client/usage/settlementClient.d.ts.map +1 -0
  65. package/dist/client/usage/settlementClient.js +35 -0
  66. package/dist/client/usage/settlementClient.js.map +1 -0
  67. package/dist/component/_generated/component.d.ts +125 -0
  68. package/dist/component/_generated/component.d.ts.map +1 -1
  69. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  70. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  71. package/dist/component/earnings/triggers.d.ts +156 -6
  72. package/dist/component/earnings/triggers.d.ts.map +1 -1
  73. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
  74. package/dist/component/financialObservations/settlementHelpers.js +2 -0
  75. package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
  76. package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
  77. package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
  78. package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
  79. package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
  80. package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
  81. package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
  82. package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
  83. package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
  84. package/dist/component/schema.d.ts +309 -6
  85. package/dist/component/settlement/mutations.d.ts +50 -0
  86. package/dist/component/settlement/mutations.d.ts.map +1 -1
  87. package/dist/component/settlement/mutations.js +8 -1
  88. package/dist/component/settlement/mutations.js.map +1 -1
  89. package/dist/component/settlement/queries.d.ts +25 -0
  90. package/dist/component/settlement/queries.d.ts.map +1 -1
  91. package/dist/component/settlement/schema.d.ts +128 -1
  92. package/dist/component/settlement/schema.d.ts.map +1 -1
  93. package/dist/component/settlement/schema.js +22 -0
  94. package/dist/component/settlement/schema.js.map +1 -1
  95. package/docs/README.md +2 -0
  96. package/docs/settlement.md +4 -0
  97. package/docs/usage-settlement.md +162 -0
  98. package/package.json +2 -1
  99. package/src/client/index.ts +9 -0
  100. package/src/client/paymentSource/resolver.fixtures.ts +216 -0
  101. package/src/client/paymentSource/resolver.test.ts +11 -207
  102. package/src/client/paymentSource/resolver.ts +54 -2
  103. package/src/client/settlement/execution.ts +24 -0
  104. package/src/client/usage/catalogPrices.ts +4 -2
  105. package/src/client/usage/client.ts +2 -2
  106. package/src/client/usage/creditRevenue.test.ts +105 -0
  107. package/src/client/usage/creditRevenue.ts +163 -0
  108. package/src/client/usage/creditSettlement.fixtures.ts +70 -0
  109. package/src/client/usage/creditSettlement.test.ts +66 -0
  110. package/src/client/usage/creditSettlement.ts +94 -0
  111. package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
  112. package/src/client/usage/creditTransport.fixtures.ts +29 -4
  113. package/src/client/usage/index.ts +9 -0
  114. package/src/client/usage/invoiceSale.fixtures.ts +103 -0
  115. package/src/client/usage/invoiceSale.test.ts +184 -0
  116. package/src/client/usage/invoiceSale.ts +155 -0
  117. package/src/client/usage/settlement.test.ts +179 -0
  118. package/src/client/usage/settlement.ts +81 -0
  119. package/src/client/usage/settlementClient.ts +84 -0
  120. package/src/component/_generated/component.ts +110 -0
  121. package/src/component/financialObservations/settlementHelpers.ts +2 -0
  122. package/src/component/settlement/mutations.ts +10 -1
  123. package/src/component/settlement/schema.ts +26 -0
  124. package/src/component/settlement/settlement-claims.test.ts +54 -0
@@ -0,0 +1,216 @@
1
+ import type Stripe from "stripe";
2
+ import { componentsGeneric } from "convex/server";
3
+ import { BetterStripe } from "../index.js";
4
+ import { getStripeClient, type Component, type RunCtx } from "../helpers.js";
5
+
6
+ export interface SourceHttpClient extends Stripe.HttpClient {}
7
+ export function fixture() {
8
+ const key = `sk_test_source_${crypto.randomUUID()}`;
9
+ const apiVersion = "2026-09-30.preview";
10
+ const client = new BetterStripe(
11
+ componentsGeneric().betterStripe as unknown as Component,
12
+ {
13
+ STRIPE_SECRET_KEY: key,
14
+ stripeApiVersion: apiVersion,
15
+ },
16
+ );
17
+ const transaction = {
18
+ id: "txn_source",
19
+ object: "balance_transaction",
20
+ source: "ch_source",
21
+ amount: 1000,
22
+ fee: 50,
23
+ net: 950,
24
+ currency: "usd",
25
+ };
26
+ const payment = {
27
+ id: "pi_source",
28
+ object: "payment_intent",
29
+ status: "succeeded",
30
+ amount: 1000,
31
+ amount_received: 1000,
32
+ currency: "usd",
33
+ customer_account: "acct_customer",
34
+ livemode: false,
35
+ metadata: { stripePriceId: "price_untrusted" },
36
+ latest_charge: {
37
+ id: "ch_source",
38
+ object: "charge",
39
+ payment_intent: "pi_source",
40
+ amount: 1000,
41
+ amount_captured: 1000,
42
+ currency: "usd",
43
+ paid: true,
44
+ captured: true,
45
+ status: "succeeded",
46
+ amount_refunded: 0,
47
+ disputed: false,
48
+ livemode: false,
49
+ balance_transaction: transaction,
50
+ },
51
+ };
52
+ const invoice = {
53
+ id: "in_source",
54
+ object: "invoice",
55
+ status: "paid",
56
+ currency: "usd",
57
+ amount_paid: 1000,
58
+ amount_due: 1000,
59
+ amount_remaining: 0,
60
+ total: 1000,
61
+ amount_overpaid: 0,
62
+ starting_balance: 0,
63
+ ending_balance: 0,
64
+ customer_account: "acct_customer",
65
+ livemode: false,
66
+ parent: {
67
+ type: "subscription_details",
68
+ subscription_details: { subscription: "sub_source" },
69
+ },
70
+ };
71
+ const funding = {
72
+ id: "inpay_source",
73
+ object: "invoice_payment",
74
+ invoice: "in_source",
75
+ status: "paid",
76
+ currency: "usd",
77
+ amount_paid: 1000,
78
+ amount_requested: 1000,
79
+ livemode: false,
80
+ payment: { type: "payment_intent", payment_intent: "pi_source" },
81
+ };
82
+ const line = {
83
+ id: "il_source",
84
+ object: "line_item",
85
+ invoice: "in_source",
86
+ currency: "usd",
87
+ amount: 1000,
88
+ quantity: 1,
89
+ livemode: false,
90
+ parent: {
91
+ type: "subscription_item_details",
92
+ subscription_item_details: {
93
+ subscription: "sub_source",
94
+ subscription_item: "si_source",
95
+ proration: false,
96
+ },
97
+ },
98
+ pricing: {
99
+ type: "price_details",
100
+ price_details: { price: "price_original", product: "prod_original" },
101
+ unit_amount_decimal: "1000",
102
+ },
103
+ };
104
+ const subscription = {
105
+ id: "sub_source",
106
+ object: "subscription",
107
+ customer_account: "acct_customer",
108
+ livemode: false,
109
+ metadata: { sellerId: "untrusted" },
110
+ items: {
111
+ data: [{ price: { id: "price_current", product: "prod_current" } }],
112
+ },
113
+ };
114
+ const history = { inverse: [funding], funding: [funding], lines: [line] };
115
+ const pages = new Map<string, { data: unknown[]; has_more: boolean }>();
116
+ const requests: {
117
+ path: string;
118
+ method: string;
119
+ version: string;
120
+ account: string;
121
+ }[] = [];
122
+ const sdk = getStripeClient(key, apiVersion);
123
+ const extra = new Map<string, unknown>();
124
+ (
125
+ sdk as unknown as { _setApiField: (name: string, value: unknown) => void }
126
+ )._setApiField("httpClient", {
127
+ getClientName: () => "verified-source-contract",
128
+ makeRequest: async (
129
+ ...[, , path, method, headers, body]: Parameters<
130
+ ReturnType<typeof Stripe.createNodeHttpClient>["makeRequest"]
131
+ >
132
+ ) => {
133
+ requests.push({
134
+ path,
135
+ method,
136
+ version: String(headers["Stripe-Version"]),
137
+ account: String(headers["Stripe-Account"]),
138
+ });
139
+ const url = new URL(path, "https://stripe.test");
140
+ const phase = url.pathname.endsWith("/lines")
141
+ ? "lines"
142
+ : url.searchParams.has("invoice")
143
+ ? "funding"
144
+ : "inverse";
145
+ const override = pages.get(
146
+ phase + ":" + (url.searchParams.get("starting_after") ?? ""),
147
+ );
148
+ const handler = extra.get(url.pathname);
149
+ const data =
150
+ (typeof handler === "function" ? handler(method, body) : handler) ??
151
+ (url.pathname === "/v1/payment_intents/pi_source"
152
+ ? payment
153
+ : url.pathname === "/v1/invoices/in_source"
154
+ ? invoice
155
+ : url.pathname === "/v1/subscriptions/sub_source"
156
+ ? subscription
157
+ : {
158
+ object: "list",
159
+ url: url.pathname,
160
+ has_more: override?.has_more ?? false,
161
+ data:
162
+ override?.data ??
163
+ (phase === "lines"
164
+ ? history.lines
165
+ : phase === "funding"
166
+ ? history.funding
167
+ : history.inverse),
168
+ });
169
+ return {
170
+ getStatusCode: () => 200,
171
+ getHeaders: () => ({}),
172
+ getRawResponse: () => ({}),
173
+ toJSON: async () => JSON.parse(JSON.stringify(data)),
174
+ toStream: () => {
175
+ throw new Error("Unexpected stream");
176
+ },
177
+ };
178
+ },
179
+ });
180
+ const options = {
181
+ paymentIntentId: "pi_source",
182
+ customerAccountId: "acct_customer",
183
+ stripeAccountId: "acct_connected",
184
+ gross: 1000,
185
+ currency: "usd",
186
+ };
187
+ const resolve = (
188
+ overrides: Partial<typeof options> & {
189
+ maxHistoryPages?: number;
190
+ maxHistoryItems?: number;
191
+ } = {},
192
+ ) => {
193
+ return client.getVerifiedPaymentSource({} as RunCtx, {
194
+ ...options,
195
+ ...overrides,
196
+ });
197
+ };
198
+ return {
199
+ resolve,
200
+ sdk,
201
+ httpClient: (
202
+ sdk as unknown as { getApiField(name: string): SourceHttpClient }
203
+ ).getApiField("httpClient"),
204
+ extra,
205
+ client,
206
+ options,
207
+ payment,
208
+ invoice,
209
+ funding,
210
+ line,
211
+ subscription,
212
+ history,
213
+ requests,
214
+ pages,
215
+ };
216
+ }
@@ -1,211 +1,6 @@
1
- import type Stripe from "stripe";
2
- import { componentsGeneric } from "convex/server";
1
+ import type { RunCtx } from "../helpers.js";
3
2
  import { expect, it } from "vitest";
4
- import { BetterStripe } from "../index.js";
5
- import { getStripeClient, type Component, type RunCtx } from "../helpers.js";
6
-
7
- function fixture() {
8
- const key = `sk_test_source_${crypto.randomUUID()}`;
9
- const apiVersion = "2026-09-30.preview";
10
- const client = new BetterStripe(
11
- componentsGeneric().betterStripe as unknown as Component,
12
- {
13
- STRIPE_SECRET_KEY: key,
14
- stripeApiVersion: apiVersion,
15
- },
16
- );
17
- const transaction = {
18
- id: "txn_source",
19
- object: "balance_transaction",
20
- source: "ch_source",
21
- amount: 1000,
22
- fee: 50,
23
- net: 950,
24
- currency: "usd",
25
- };
26
- const payment = {
27
- id: "pi_source",
28
- object: "payment_intent",
29
- status: "succeeded",
30
- amount: 1000,
31
- amount_received: 1000,
32
- currency: "usd",
33
- customer_account: "acct_customer",
34
- livemode: false,
35
- metadata: { stripePriceId: "price_untrusted" },
36
- latest_charge: {
37
- id: "ch_source",
38
- object: "charge",
39
- payment_intent: "pi_source",
40
- amount: 1000,
41
- amount_captured: 1000,
42
- currency: "usd",
43
- paid: true,
44
- captured: true,
45
- status: "succeeded",
46
- amount_refunded: 0,
47
- disputed: false,
48
- livemode: false,
49
- balance_transaction: transaction,
50
- },
51
- };
52
- const invoice = {
53
- id: "in_source",
54
- object: "invoice",
55
- status: "paid",
56
- currency: "usd",
57
- amount_paid: 1000,
58
- amount_due: 1000,
59
- amount_remaining: 0,
60
- total: 1000,
61
- amount_overpaid: 0,
62
- starting_balance: 0,
63
- ending_balance: 0,
64
- customer_account: "acct_customer",
65
- livemode: false,
66
- parent: {
67
- type: "subscription_details",
68
- subscription_details: { subscription: "sub_source" },
69
- },
70
- };
71
- const funding = {
72
- id: "inpay_source",
73
- object: "invoice_payment",
74
- invoice: "in_source",
75
- status: "paid",
76
- currency: "usd",
77
- amount_paid: 1000,
78
- amount_requested: 1000,
79
- livemode: false,
80
- payment: { type: "payment_intent", payment_intent: "pi_source" },
81
- };
82
- const line = {
83
- id: "il_source",
84
- object: "line_item",
85
- invoice: "in_source",
86
- currency: "usd",
87
- amount: 1000,
88
- quantity: 1,
89
- livemode: false,
90
- parent: {
91
- type: "subscription_item_details",
92
- subscription_item_details: {
93
- subscription: "sub_source",
94
- subscription_item: "si_source",
95
- proration: false,
96
- },
97
- },
98
- pricing: {
99
- type: "price_details",
100
- price_details: { price: "price_original", product: "prod_original" },
101
- unit_amount_decimal: "1000",
102
- },
103
- };
104
- const subscription = {
105
- id: "sub_source",
106
- object: "subscription",
107
- customer_account: "acct_customer",
108
- livemode: false,
109
- metadata: { sellerId: "untrusted" },
110
- items: {
111
- data: [{ price: { id: "price_current", product: "prod_current" } }],
112
- },
113
- };
114
- const history = { inverse: [funding], funding: [funding], lines: [line] };
115
- const pages = new Map<string, { data: unknown[]; has_more: boolean }>();
116
- const requests: {
117
- path: string;
118
- method: string;
119
- version: string;
120
- account: string;
121
- }[] = [];
122
- const sdk = getStripeClient(key, apiVersion);
123
- (
124
- sdk as unknown as { _setApiField: (name: string, value: unknown) => void }
125
- )._setApiField("httpClient", {
126
- getClientName: () => "verified-source-contract",
127
- makeRequest: async (
128
- ...[, , path, method, headers]: Parameters<
129
- ReturnType<typeof Stripe.createNodeHttpClient>["makeRequest"]
130
- >
131
- ) => {
132
- requests.push({
133
- path,
134
- method,
135
- version: String(headers["Stripe-Version"]),
136
- account: String(headers["Stripe-Account"]),
137
- });
138
- const url = new URL(path, "https://stripe.test");
139
- const phase = url.pathname.endsWith("/lines")
140
- ? "lines"
141
- : url.searchParams.has("invoice")
142
- ? "funding"
143
- : "inverse";
144
- const override = pages.get(
145
- phase + ":" + (url.searchParams.get("starting_after") ?? ""),
146
- );
147
- const data =
148
- url.pathname === "/v1/payment_intents/pi_source"
149
- ? payment
150
- : url.pathname === "/v1/invoices/in_source"
151
- ? invoice
152
- : url.pathname === "/v1/subscriptions/sub_source"
153
- ? subscription
154
- : {
155
- object: "list",
156
- url: url.pathname,
157
- has_more: override?.has_more ?? false,
158
- data:
159
- override?.data ??
160
- (phase === "lines"
161
- ? history.lines
162
- : phase === "funding"
163
- ? history.funding
164
- : history.inverse),
165
- };
166
- return {
167
- getStatusCode: () => 200,
168
- getHeaders: () => ({}),
169
- getRawResponse: () => ({}),
170
- toJSON: async () => data,
171
- toStream: () => {
172
- throw new Error("Unexpected stream");
173
- },
174
- };
175
- },
176
- });
177
- const options = {
178
- paymentIntentId: "pi_source",
179
- customerAccountId: "acct_customer",
180
- stripeAccountId: "acct_connected",
181
- gross: 1000,
182
- currency: "usd",
183
- };
184
- const resolve = (
185
- overrides: Partial<typeof options> & {
186
- maxHistoryPages?: number;
187
- maxHistoryItems?: number;
188
- } = {},
189
- ) => {
190
- return client.getVerifiedPaymentSource({} as RunCtx, {
191
- ...options,
192
- ...overrides,
193
- });
194
- };
195
- return {
196
- resolve,
197
- client,
198
- options,
199
- payment,
200
- invoice,
201
- funding,
202
- line,
203
- subscription,
204
- history,
205
- requests,
206
- pages,
207
- };
208
- }
3
+ import { fixture } from "./resolver.fixtures.js";
209
4
 
210
5
  it("resolves a complete original invoice price through the inherited SDK method, ignoring current prices and metadata", async () => {
211
6
  const s = fixture();
@@ -580,3 +375,12 @@ it("requires original line subscription binding and refuses proration", async ()
580
375
  prorated.line.parent.subscription_item_details.proration = true;
581
376
  expect(await prorated.resolve()).toEqual(terminal("unsupported_source"));
582
377
  });
378
+
379
+ it("retains exact fractional native invoice quantity without rejecting its original cash provenance", async () => {
380
+ const f = fixture();
381
+ Object.assign(f.line, { quantity: null, quantity_decimal: "1.125" });
382
+ expect(await f.resolve()).toMatchObject({
383
+ status: "ready",
384
+ value: { stripePriceId: "price_original", gross: 1000 },
385
+ });
386
+ });
@@ -48,6 +48,15 @@ const integer = (value: number) => Number.isSafeInteger(value) && value >= 0;
48
48
  export async function resolveVerifiedPaymentSource(
49
49
  stripe: Stripe,
50
50
  input: VerifiedPaymentSourceOptions,
51
+ eligibility?: {
52
+ requirePayable: true;
53
+ customerId: string;
54
+ invoiceLine?: {
55
+ id: string;
56
+ subscriptionItemId: string;
57
+ period: { start: number; end: number };
58
+ };
59
+ },
51
60
  ): Promise<FinancialOutcome<VerifiedPaymentSource>> {
52
61
  const parsed = paymentSourceOptions.safeParse(input);
53
62
  if (!parsed.success) return reject("invalid_scope");
@@ -120,6 +129,26 @@ export async function resolveVerifiedPaymentSource(
120
129
  identity(),
121
130
  );
122
131
  const customerAccountId = payment.customer_account!;
132
+ if (eligibility) {
133
+ requireSource(
134
+ charge.amount_refunded === 0 &&
135
+ charge.disputed === false &&
136
+ charge.refunded === false,
137
+ reject("payment_not_payable"),
138
+ );
139
+ requireSource(
140
+ objectId(payment.customer) === eligibility.customerId &&
141
+ objectId(charge.customer) === eligibility.customerId,
142
+ identity(),
143
+ );
144
+ requireSource(
145
+ !payment.transfer_data &&
146
+ !payment.on_behalf_of &&
147
+ !payment.application_fee_amount &&
148
+ !charge.transfer,
149
+ reject("unsupported_source"),
150
+ );
151
+ }
123
152
  const balanceRef = charge.balance_transaction;
124
153
  if (!balanceRef)
125
154
  return { status: "not_ready", reason: "fee_pending", retryable: true };
@@ -201,6 +230,11 @@ export async function resolveVerifiedPaymentSource(
201
230
  invoice.livemode === payment.livemode,
202
231
  identity(),
203
232
  );
233
+ if (eligibility)
234
+ requireSource(
235
+ objectId(invoice.customer) === eligibility.customerId,
236
+ identity(),
237
+ );
204
238
  requireSource(
205
239
  invoice.currency === opts.currency,
206
240
  reject("currency_mismatch"),
@@ -269,6 +303,11 @@ export async function resolveVerifiedPaymentSource(
269
303
  {},
270
304
  requestOptions,
271
305
  );
306
+ if (eligibility)
307
+ requireSource(
308
+ objectId(subscription.customer) === eligibility.customerId,
309
+ identity(),
310
+ );
272
311
  requireSource(
273
312
  subscription.object === "subscription" &&
274
313
  subscription.id === subscriptionId &&
@@ -297,11 +336,24 @@ export async function resolveVerifiedPaymentSource(
297
336
  requireSource(
298
337
  integer(line.amount) &&
299
338
  line.amount > 0 &&
300
- Number.isSafeInteger(line.quantity) &&
301
- line.quantity! > 0,
339
+ (line.quantity_decimal != null
340
+ ? /^\d{1,30}(\.\d{1,12})?$/.test(line.quantity_decimal.toString()) &&
341
+ /[1-9]/.test(line.quantity_decimal.toString())
342
+ : Number.isSafeInteger(line.quantity) && line.quantity! > 0),
302
343
  reject("unsupported_source"),
303
344
  );
304
345
  const parent = line.parent;
346
+ if (eligibility?.invoiceLine) {
347
+ const expected = eligibility.invoiceLine;
348
+ requireSource(
349
+ line.id === expected.id &&
350
+ line.parent?.subscription_item_details?.subscription_item ===
351
+ expected.subscriptionItemId &&
352
+ line.period.start === expected.period.start &&
353
+ line.period.end === expected.period.end,
354
+ identity(),
355
+ );
356
+ }
305
357
  const lineSubscription =
306
358
  parent?.subscription_item_details?.subscription ??
307
359
  parent?.invoice_item_details?.subscription ??
@@ -13,6 +13,28 @@ export type ResolvedSettlement = {
13
13
  sourceChargeId: string;
14
14
  paymentIntentId?: string;
15
15
  sourceInvoiceId?: string;
16
+ usageSource?: {
17
+ stripeAccountId: string;
18
+ customerAccount: string;
19
+ livemode: boolean;
20
+ meterId: string;
21
+ stripePriceId: string;
22
+ stripeProductId: string;
23
+ subscriptionId: string;
24
+ subscriptionItemId: string;
25
+ invoicePaymentId: string;
26
+ period: { start: number; end: number };
27
+ model: "pay_as_you_go" | "included_overage";
28
+ };
29
+ creditSource?: {
30
+ stripeAccountId: string;
31
+ customerAccount: string;
32
+ livemode: boolean;
33
+ purchaseId: string;
34
+ creditGrantId: string;
35
+ stripePriceId: string;
36
+ stripeProductId: string;
37
+ };
16
38
  gross: number;
17
39
  currency: string;
18
40
  processingFee: number;
@@ -53,6 +75,8 @@ export async function executeResolvedTransfers(
53
75
  sourceChargeId: saved.sourceChargeId,
54
76
  paymentIntentId: saved.paymentIntentId,
55
77
  sourceInvoiceId: saved.sourceInvoiceId,
78
+ usageSource: saved.usageSource,
79
+ creditSource: saved.creditSource,
56
80
  gross: saved.gross,
57
81
  currency: saved.currency,
58
82
  processingFee: saved.processingFee,
@@ -83,11 +83,12 @@ function verifyPrice(
83
83
  opts: UsagePriceOptions,
84
84
  rate: string,
85
85
  included: number | undefined,
86
+ requireActive = true,
86
87
  ) {
87
88
  const product =
88
89
  typeof price.product === "string" ? price.product : price.product.id;
89
90
  if (
90
- !price.active ||
91
+ (requireActive && !price.active) ||
91
92
  price.livemode !== opts.scope.livemode ||
92
93
  price.lookup_key !== opts.lookupKey ||
93
94
  product !== opts.stripeProductId ||
@@ -256,6 +257,7 @@ export function verifyPinnedUsagePrice(
256
257
  scope: UsagePriceOptions["scope"],
257
258
  plan: UsagePriceOptions["plan"],
258
259
  meterId: string,
260
+ requireActive = true,
259
261
  ) {
260
262
  const productId =
261
263
  typeof price.product === "string" ? price.product : price.product.id;
@@ -268,5 +270,5 @@ export function verifyPinnedUsagePrice(
268
270
  lookupKey: price.lookup_key,
269
271
  };
270
272
  const { rate, included } = termsFor(opts);
271
- verifyPrice(price, opts, rate, included);
273
+ verifyPrice(price, opts, rate, included, requireActive);
272
274
  }
@@ -1,4 +1,4 @@
1
- import { UsageCorrectionClient } from "./correctionClient.js";
1
+ import { UsageSettlementClient } from "./settlementClient.js";
2
2
  import type { Component, RunCtx } from "../helpers.js";
3
3
  import { runMutationOrThrow } from "../helpers.js";
4
4
  import { componentRef } from "../webhooks/helpers.js";
@@ -46,7 +46,7 @@ function decode({ event, delivery }: StoredView): UsageEventView {
46
46
  };
47
47
  }
48
48
  /** Trusted host facade. Authenticate and authorize each exact scope first. */
49
- export class UsageLedgerClient extends UsageCorrectionClient {
49
+ export class UsageLedgerClient extends UsageSettlementClient {
50
50
  constructor(component: Component) {
51
51
  super(component);
52
52
  }