@fantastic.dev/better-stripe 0.58.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 (294) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +3 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +888 -62
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +428 -15
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +856 -30
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +444 -31
  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/allowanceClient.d.ts +1 -1
  26. package/dist/client/usage/catalogPrices.d.ts +1 -1
  27. package/dist/client/usage/catalogPrices.d.ts.map +1 -1
  28. package/dist/client/usage/catalogPrices.js +4 -4
  29. package/dist/client/usage/catalogPrices.js.map +1 -1
  30. package/dist/client/usage/client.d.ts +2 -2
  31. package/dist/client/usage/client.d.ts.map +1 -1
  32. package/dist/client/usage/client.js +2 -2
  33. package/dist/client/usage/client.js.map +1 -1
  34. package/dist/client/usage/correctionClient.d.ts +33 -0
  35. package/dist/client/usage/correctionClient.d.ts.map +1 -0
  36. package/dist/client/usage/correctionClient.js +27 -0
  37. package/dist/client/usage/correctionClient.js.map +1 -0
  38. package/dist/client/usage/correctionEligibility.d.ts +4 -0
  39. package/dist/client/usage/correctionEligibility.d.ts.map +1 -0
  40. package/dist/client/usage/correctionEligibility.js +7 -0
  41. package/dist/client/usage/correctionEligibility.js.map +1 -0
  42. package/dist/client/usage/correctionInvoice.d.ts +9 -0
  43. package/dist/client/usage/correctionInvoice.d.ts.map +1 -0
  44. package/dist/client/usage/correctionInvoice.js +218 -0
  45. package/dist/client/usage/correctionInvoice.js.map +1 -0
  46. package/dist/client/usage/correctionInvoiceProof.d.ts +29 -0
  47. package/dist/client/usage/correctionInvoiceProof.d.ts.map +1 -0
  48. package/dist/client/usage/correctionInvoiceProof.js +41 -0
  49. package/dist/client/usage/correctionInvoiceProof.js.map +1 -0
  50. package/dist/client/usage/correctionProofs.d.ts +36 -0
  51. package/dist/client/usage/correctionProofs.d.ts.map +1 -0
  52. package/dist/client/usage/correctionProofs.js +41 -0
  53. package/dist/client/usage/correctionProofs.js.map +1 -0
  54. package/dist/client/usage/correctionProvider.d.ts +6 -0
  55. package/dist/client/usage/correctionProvider.d.ts.map +1 -0
  56. package/dist/client/usage/correctionProvider.js +243 -0
  57. package/dist/client/usage/correctionProvider.js.map +1 -0
  58. package/dist/client/usage/correctionRead.d.ts +27 -0
  59. package/dist/client/usage/correctionRead.d.ts.map +1 -0
  60. package/dist/client/usage/correctionRead.js +36 -0
  61. package/dist/client/usage/correctionRead.js.map +1 -0
  62. package/dist/client/usage/correctionTerms.d.ts +64 -0
  63. package/dist/client/usage/correctionTerms.d.ts.map +1 -0
  64. package/dist/client/usage/correctionTerms.js +65 -0
  65. package/dist/client/usage/correctionTerms.js.map +1 -0
  66. package/dist/client/usage/correctionTransport.fixtures.d.ts +37 -0
  67. package/dist/client/usage/correctionTransport.fixtures.d.ts.map +1 -0
  68. package/dist/client/usage/correctionTransport.fixtures.js +322 -0
  69. package/dist/client/usage/correctionTransport.fixtures.js.map +1 -0
  70. package/dist/client/usage/creditClient.d.ts +1 -1
  71. package/dist/client/usage/creditRevenue.d.ts +22 -0
  72. package/dist/client/usage/creditRevenue.d.ts.map +1 -0
  73. package/dist/client/usage/creditRevenue.js +118 -0
  74. package/dist/client/usage/creditRevenue.js.map +1 -0
  75. package/dist/client/usage/creditSettlement.d.ts +29 -0
  76. package/dist/client/usage/creditSettlement.d.ts.map +1 -0
  77. package/dist/client/usage/creditSettlement.fixtures.d.ts +215 -0
  78. package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
  79. package/dist/client/usage/creditSettlement.fixtures.js +59 -0
  80. package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
  81. package/dist/client/usage/creditSettlement.js +70 -0
  82. package/dist/client/usage/creditSettlement.js.map +1 -0
  83. package/dist/client/usage/creditTransport.fixtures.d.ts +7 -1
  84. package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
  85. package/dist/client/usage/creditTransport.fixtures.js +25 -4
  86. package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
  87. package/dist/client/usage/index.d.ts +6 -0
  88. package/dist/client/usage/index.d.ts.map +1 -1
  89. package/dist/client/usage/invoiceSale.d.ts +22 -0
  90. package/dist/client/usage/invoiceSale.d.ts.map +1 -0
  91. package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
  92. package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
  93. package/dist/client/usage/invoiceSale.fixtures.js +104 -0
  94. package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
  95. package/dist/client/usage/invoiceSale.js +117 -0
  96. package/dist/client/usage/invoiceSale.js.map +1 -0
  97. package/dist/client/usage/observationClient.d.ts +20 -0
  98. package/dist/client/usage/observationClient.d.ts.map +1 -1
  99. package/dist/client/usage/observationClient.js +20 -6
  100. package/dist/client/usage/observationClient.js.map +1 -1
  101. package/dist/client/usage/records.d.ts +1 -1
  102. package/dist/client/usage/records.d.ts.map +1 -1
  103. package/dist/client/usage/records.js.map +1 -1
  104. package/dist/client/usage/settlement.d.ts +32 -0
  105. package/dist/client/usage/settlement.d.ts.map +1 -0
  106. package/dist/client/usage/settlement.js +60 -0
  107. package/dist/client/usage/settlement.js.map +1 -0
  108. package/dist/client/usage/settlementClient.d.ts +51 -0
  109. package/dist/client/usage/settlementClient.d.ts.map +1 -0
  110. package/dist/client/usage/settlementClient.js +35 -0
  111. package/dist/client/usage/settlementClient.js.map +1 -0
  112. package/dist/client/usage/summary.d.ts +3 -0
  113. package/dist/client/usage/summary.d.ts.map +1 -1
  114. package/dist/client/usage/summary.js +9 -4
  115. package/dist/client/usage/summary.js.map +1 -1
  116. package/dist/client/usageExports.d.ts +2 -0
  117. package/dist/client/usageExports.d.ts.map +1 -1
  118. package/dist/component/_generated/api.d.ts +12 -0
  119. package/dist/component/_generated/api.d.ts.map +1 -1
  120. package/dist/component/_generated/api.js.map +1 -1
  121. package/dist/component/_generated/component.d.ts +380 -14
  122. package/dist/component/_generated/component.d.ts.map +1 -1
  123. package/dist/component/accountClosure/mutations.d.ts.map +1 -1
  124. package/dist/component/accountClosure/mutations.js +13 -0
  125. package/dist/component/accountClosure/mutations.js.map +1 -1
  126. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  127. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  128. package/dist/component/earnings/triggers.d.ts +368 -32
  129. package/dist/component/earnings/triggers.d.ts.map +1 -1
  130. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
  131. package/dist/component/financialObservations/settlementHelpers.js +2 -0
  132. package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
  133. package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
  134. package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
  135. package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
  136. package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
  137. package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
  138. package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
  139. package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
  140. package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
  141. package/dist/component/schema.d.ts +428 -15
  142. package/dist/component/schema.js +4 -0
  143. package/dist/component/schema.js.map +1 -1
  144. package/dist/component/settlement/mutations.d.ts +50 -0
  145. package/dist/component/settlement/mutations.d.ts.map +1 -1
  146. package/dist/component/settlement/mutations.js +8 -1
  147. package/dist/component/settlement/mutations.js.map +1 -1
  148. package/dist/component/settlement/queries.d.ts +25 -0
  149. package/dist/component/settlement/queries.d.ts.map +1 -1
  150. package/dist/component/settlement/schema.d.ts +128 -1
  151. package/dist/component/settlement/schema.d.ts.map +1 -1
  152. package/dist/component/settlement/schema.js +22 -0
  153. package/dist/component/settlement/schema.js.map +1 -1
  154. package/dist/component/usage/queries.d.ts +2 -2
  155. package/dist/component/usage/schema.d.ts +18 -13
  156. package/dist/component/usage/schema.d.ts.map +1 -1
  157. package/dist/component/usage/schema.js +2 -1
  158. package/dist/component/usage/schema.js.map +1 -1
  159. package/dist/component/usageAllowance/queries.d.ts +1 -1
  160. package/dist/component/usageAllowance/queries.d.ts.map +1 -1
  161. package/dist/component/usageAllowance/queries.js +10 -1
  162. package/dist/component/usageAllowance/queries.js.map +1 -1
  163. package/dist/component/usageAllowance/schema.d.ts +5 -5
  164. package/dist/component/usageAllowance/schema.d.ts.map +1 -1
  165. package/dist/component/usageAllowance/schema.js +1 -1
  166. package/dist/component/usageAllowance/schema.js.map +1 -1
  167. package/dist/component/usageCorrections/accounting.d.ts +5 -0
  168. package/dist/component/usageCorrections/accounting.d.ts.map +1 -0
  169. package/dist/component/usageCorrections/accounting.js +55 -0
  170. package/dist/component/usageCorrections/accounting.js.map +1 -0
  171. package/dist/component/usageCorrections/cancellation.d.ts +6 -0
  172. package/dist/component/usageCorrections/cancellation.d.ts.map +1 -0
  173. package/dist/component/usageCorrections/cancellation.js +29 -0
  174. package/dist/component/usageCorrections/cancellation.js.map +1 -0
  175. package/dist/component/usageCorrections/helpers.d.ts +60 -0
  176. package/dist/component/usageCorrections/helpers.d.ts.map +1 -0
  177. package/dist/component/usageCorrections/helpers.js +52 -0
  178. package/dist/component/usageCorrections/helpers.js.map +1 -0
  179. package/dist/component/usageCorrections/invoice.d.ts +38 -0
  180. package/dist/component/usageCorrections/invoice.d.ts.map +1 -0
  181. package/dist/component/usageCorrections/invoice.js +82 -0
  182. package/dist/component/usageCorrections/invoice.js.map +1 -0
  183. package/dist/component/usageCorrections/mutations.d.ts +121 -0
  184. package/dist/component/usageCorrections/mutations.d.ts.map +1 -0
  185. package/dist/component/usageCorrections/mutations.js +256 -0
  186. package/dist/component/usageCorrections/mutations.js.map +1 -0
  187. package/dist/component/usageCorrections/queries.d.ts +115 -0
  188. package/dist/component/usageCorrections/queries.d.ts.map +1 -0
  189. package/dist/component/usageCorrections/queries.js +84 -0
  190. package/dist/component/usageCorrections/queries.js.map +1 -0
  191. package/dist/component/usageCorrections/schema.d.ts +248 -0
  192. package/dist/component/usageCorrections/schema.d.ts.map +1 -0
  193. package/dist/component/usageCorrections/schema.js +74 -0
  194. package/dist/component/usageCorrections/schema.js.map +1 -0
  195. package/dist/component/usageCredits/queries.d.ts +1 -1
  196. package/dist/component/usageCredits/queries.d.ts.map +1 -1
  197. package/dist/component/usageCredits/queries.js +6 -0
  198. package/dist/component/usageCredits/queries.js.map +1 -1
  199. package/dist/component/usageCredits/schema.d.ts +5 -5
  200. package/dist/component/usageCredits/schema.d.ts.map +1 -1
  201. package/dist/component/usageCredits/schema.js +1 -1
  202. package/dist/component/usageCredits/schema.js.map +1 -1
  203. package/dist/component/usageDelivery/fields.d.ts +2 -2
  204. package/dist/component/usageDelivery/fields.d.ts.map +1 -1
  205. package/dist/component/usageDelivery/fields.js +1 -1
  206. package/dist/component/usageDelivery/fields.js.map +1 -1
  207. package/dist/component/usageDelivery/helpers.d.ts +7 -6
  208. package/dist/component/usageDelivery/helpers.d.ts.map +1 -1
  209. package/dist/component/usageDelivery/mutations.d.ts +6 -6
  210. package/dist/component/usageDelivery/mutations.d.ts.map +1 -1
  211. package/dist/component/usageDelivery/mutations.js +9 -1
  212. package/dist/component/usageDelivery/mutations.js.map +1 -1
  213. package/dist/component/usageDelivery/queries.d.ts +8 -6
  214. package/dist/component/usageDelivery/queries.d.ts.map +1 -1
  215. package/dist/component/usageDelivery/schema.d.ts +4 -4
  216. package/dist/component/usageDelivery/steps.d.ts +2 -2
  217. package/dist/component/usageDelivery/steps.js +1 -1
  218. package/dist/component/usageDelivery/steps.js.map +1 -1
  219. package/dist/component/usageObservations/queries.d.ts.map +1 -1
  220. package/dist/component/usageObservations/queries.js +4 -2
  221. package/dist/component/usageObservations/queries.js.map +1 -1
  222. package/dist/component/usageSubscriptions/retirement.d.ts.map +1 -1
  223. package/dist/component/usageSubscriptions/retirement.js +24 -2
  224. package/dist/component/usageSubscriptions/retirement.js.map +1 -1
  225. package/docs/README.md +4 -0
  226. package/docs/settlement.md +4 -0
  227. package/docs/usage-corrections.md +249 -0
  228. package/docs/usage-limits.md +1 -1
  229. package/docs/usage-settlement.md +162 -0
  230. package/package.json +3 -1
  231. package/src/client/index.ts +9 -0
  232. package/src/client/paymentSource/resolver.fixtures.ts +216 -0
  233. package/src/client/paymentSource/resolver.test.ts +11 -207
  234. package/src/client/paymentSource/resolver.ts +54 -2
  235. package/src/client/settlement/execution.ts +24 -0
  236. package/src/client/usage/catalogPrices.ts +4 -2
  237. package/src/client/usage/client.ts +2 -2
  238. package/src/client/usage/correctionClient.test.ts +153 -0
  239. package/src/client/usage/correctionClient.ts +62 -0
  240. package/src/client/usage/correctionCredits.test.ts +156 -0
  241. package/src/client/usage/correctionEligibility.ts +14 -0
  242. package/src/client/usage/correctionInvoice.test.ts +127 -0
  243. package/src/client/usage/correctionInvoice.ts +269 -0
  244. package/src/client/usage/correctionInvoiceProof.ts +40 -0
  245. package/src/client/usage/correctionProofs.ts +40 -0
  246. package/src/client/usage/correctionProvider.ts +326 -0
  247. package/src/client/usage/correctionRead.ts +73 -0
  248. package/src/client/usage/correctionRecovery.test.ts +113 -0
  249. package/src/client/usage/correctionTerms.ts +98 -0
  250. package/src/client/usage/correctionTransport.fixtures.ts +372 -0
  251. package/src/client/usage/creditRevenue.test.ts +105 -0
  252. package/src/client/usage/creditRevenue.ts +163 -0
  253. package/src/client/usage/creditSettlement.fixtures.ts +70 -0
  254. package/src/client/usage/creditSettlement.test.ts +66 -0
  255. package/src/client/usage/creditSettlement.ts +94 -0
  256. package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
  257. package/src/client/usage/creditTransport.fixtures.ts +29 -4
  258. package/src/client/usage/index.ts +20 -0
  259. package/src/client/usage/invoiceSale.fixtures.ts +103 -0
  260. package/src/client/usage/invoiceSale.test.ts +184 -0
  261. package/src/client/usage/invoiceSale.ts +155 -0
  262. package/src/client/usage/observationClient.ts +20 -6
  263. package/src/client/usage/records.ts +7 -1
  264. package/src/client/usage/settlement.test.ts +179 -0
  265. package/src/client/usage/settlement.ts +81 -0
  266. package/src/client/usage/settlementClient.ts +84 -0
  267. package/src/client/usage/summary.ts +12 -4
  268. package/src/client/usageExports.ts +11 -0
  269. package/src/component/_generated/api.ts +12 -0
  270. package/src/component/_generated/component.ts +440 -2
  271. package/src/component/accountClosure/mutations.ts +17 -0
  272. package/src/component/financialObservations/settlementHelpers.ts +2 -0
  273. package/src/component/schema.ts +8 -0
  274. package/src/component/settlement/mutations.ts +10 -1
  275. package/src/component/settlement/schema.ts +26 -0
  276. package/src/component/settlement/settlement-claims.test.ts +54 -0
  277. package/src/component/usage/schema.ts +2 -0
  278. package/src/component/usageAllowance/queries.ts +8 -1
  279. package/src/component/usageAllowance/schema.ts +1 -0
  280. package/src/component/usageCorrections/accounting.ts +79 -0
  281. package/src/component/usageCorrections/cancellation.ts +32 -0
  282. package/src/component/usageCorrections/corrections.test.ts +389 -0
  283. package/src/component/usageCorrections/helpers.ts +57 -0
  284. package/src/component/usageCorrections/invoice.ts +103 -0
  285. package/src/component/usageCorrections/mutations.ts +278 -0
  286. package/src/component/usageCorrections/queries.ts +94 -0
  287. package/src/component/usageCorrections/schema.ts +90 -0
  288. package/src/component/usageCredits/queries.ts +4 -0
  289. package/src/component/usageCredits/schema.ts +1 -0
  290. package/src/component/usageDelivery/fields.ts +1 -0
  291. package/src/component/usageDelivery/mutations.ts +8 -3
  292. package/src/component/usageDelivery/steps.ts +1 -1
  293. package/src/component/usageObservations/queries.ts +5 -2
  294. package/src/component/usageSubscriptions/retirement.ts +31 -2
@@ -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 { UsageAllowanceClient } from "./allowanceClient.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 UsageAllowanceClient {
49
+ export class UsageLedgerClient extends UsageSettlementClient {
50
50
  constructor(component: Component) {
51
51
  super(component);
52
52
  }
@@ -0,0 +1,153 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ import type { RunCtx } from "../helpers.js";
3
+ import { setupCorrection } from "./correctionTransport.fixtures.js";
4
+ afterEach(() => vi.useRealTimers());
5
+ it("uses the original native identifier and stable operation key without claiming pending cancellation complete", async () => {
6
+ const { f, client, options, correction, receipt, calls } =
7
+ await setupCorrection();
8
+ const result = await f.t.action((ctx) =>
9
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
10
+ );
11
+ expect(result).toMatchObject({
12
+ state: "provider_accepted",
13
+ nativeStatus: "pending",
14
+ nativeAdjustmentId: null,
15
+ restoredQuantity: "0",
16
+ cashConsequence: "none",
17
+ });
18
+ expect(
19
+ await f.t.action((ctx) =>
20
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
21
+ ),
22
+ ).toEqual(result);
23
+ expect(calls).toHaveLength(1);
24
+ expect(calls[0].body.get("cancel[identifier]")).toBe(
25
+ `bs_usage_${receipt.eventId}`,
26
+ );
27
+ expect(calls[0].key).toMatch(/^bs_usage_correction_/);
28
+ });
29
+
30
+ it("retains units if an invoice finalizes while the native cancellation is processing", async () => {
31
+ const { f, client, options, correction, state } = await setupCorrection();
32
+ state.status = "complete";
33
+ state.finalizeOnAdjust = true;
34
+ expect(
35
+ await f.t.action((ctx) =>
36
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
37
+ ),
38
+ ).toMatchObject({
39
+ state: "invoice_adjustment_required",
40
+ nativeStatus: "complete",
41
+ invoiceId: "in_usage",
42
+ restoredQuantity: "0",
43
+ cashConsequence: "operator_resolution_required",
44
+ });
45
+ });
46
+
47
+ it("exposes bounded correction evidence beside original usage and preserves unknown legacy coverage", async () => {
48
+ const { f, client, options, correction, original } = await setupCorrection();
49
+ const before = await f.t.query((ctx) =>
50
+ client.usage.readCorrectionHistory(ctx as RunCtx, {
51
+ scope: correction.scope,
52
+ eventId: correction.eventId,
53
+ limit: 1,
54
+ cursor: null,
55
+ }),
56
+ );
57
+ expect(before).toMatchObject({ records: [], coverage: "not_recorded" });
58
+ await f.t.action((ctx) =>
59
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
60
+ );
61
+ const page = await f.t.query((ctx) =>
62
+ client.usage.readCorrectionHistory(ctx as RunCtx, {
63
+ scope: correction.scope,
64
+ eventId: correction.eventId,
65
+ limit: 1,
66
+ cursor: null,
67
+ }),
68
+ );
69
+ expect(page.records).toHaveLength(1);
70
+ expect(page.isDone).toBe(false);
71
+ expect(page.coverage).toBe("recorded");
72
+ const status = await f.t.query((ctx) =>
73
+ client.usage.getReconciliation(ctx as RunCtx, {
74
+ scope: original.scope,
75
+ binding: original.binding,
76
+ subscription: original.subscription,
77
+ period: original.subscription.period,
78
+ }),
79
+ );
80
+ expect(status.correctionHistory).toMatchObject({
81
+ status: "available",
82
+ value: { coverage: "recorded" },
83
+ });
84
+ });
85
+
86
+ it("adopts a verified partial native invoice credit without erasing valid usage or issuing another refund", async () => {
87
+ const { f, client, options, correction } = await setupCorrection();
88
+ const partial = { ...correction, quantity: "2" };
89
+ expect(
90
+ await f.t.action((ctx) =>
91
+ client.usage.requestCorrection(ctx as RunCtx, partial, options),
92
+ ),
93
+ ).toMatchObject({ state: "unsupported", restoredQuantity: "0" });
94
+ const input = {
95
+ scope: correction.scope,
96
+ eventId: correction.eventId,
97
+ correctionId: correction.correctionId,
98
+ invoiceId: "in_usage",
99
+ creditNoteId: "cn_fix",
100
+ amount: 4,
101
+ };
102
+ const adopted = await f.t.action((ctx) =>
103
+ client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
104
+ );
105
+ expect(adopted).toMatchObject({
106
+ state: "confirmed",
107
+ resolution: "invoice_credit",
108
+ creditNoteId: "cn_fix",
109
+ restoredQuantity: "0",
110
+ cashConsequence: "verified_external_adjustment",
111
+ });
112
+ expect(
113
+ await f.t.action((ctx) =>
114
+ client.usage.observeInvoiceCorrection(ctx as RunCtx, input, options),
115
+ ),
116
+ ).toEqual(adopted);
117
+ expect(
118
+ (
119
+ await f.t.query((ctx) =>
120
+ client.usage.getDelivery(ctx as RunCtx, {
121
+ scope: correction.scope,
122
+ eventId: correction.eventId,
123
+ }),
124
+ )
125
+ ).state,
126
+ ).toBe("provider_accepted");
127
+ });
128
+
129
+ it("restores included units once after complete native cancellation and reconciles the retained original ledger", async () => {
130
+ const { f, client, options, correction, original, state } =
131
+ await setupCorrection(true);
132
+ state.status = "complete";
133
+ expect(
134
+ await f.t.action((ctx) =>
135
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
136
+ ),
137
+ ).toMatchObject({ state: "confirmed", restoredQuantity: "6" });
138
+ expect(
139
+ await f.t.action((ctx) =>
140
+ client.usage.requestCorrection(ctx as RunCtx, correction, options),
141
+ ),
142
+ ).toMatchObject({ restoredQuantity: "6" });
143
+ expect(
144
+ await f.t.query((ctx) =>
145
+ client.usage.getAllowanceStatus(ctx as RunCtx, original),
146
+ ),
147
+ ).toMatchObject({ consumedQuantity: "0", remainingIncludedQuantity: "10" });
148
+ expect(
149
+ await f.t.query((ctx) =>
150
+ client.usage.reconcileAllowance(ctx as RunCtx, original),
151
+ ),
152
+ ).toMatchObject({ status: "matched", acceptedScaledQuantity: "0" });
153
+ });
@@ -0,0 +1,62 @@
1
+ import { observeInvoiceCorrection } from "./correctionInvoice.js";
2
+ import type { UsageInvoiceCorrectionOptions } from "./correctionTerms.js";
3
+ import { readCorrectionEvidence } from "./correctionRead.js";
4
+ import { scopeSchema, type UsageScope } from "./records.js";
5
+ import { componentRef } from "../webhooks/helpers.js";
6
+ import type { Doc } from "../../component/_generated/dataModel.js";
7
+ import { correctionView } from "./correctionTerms.js";
8
+ import type { UsagePeriod } from "./boundaries.js";
9
+ import { UsageAllowanceClient } from "./allowanceClient.js";
10
+ import type { RunCtx } from "../helpers.js";
11
+ import { requestUsageCorrection } from "./correctionProvider.js";
12
+ import type { UsageCorrectionOptions } from "./correctionTerms.js";
13
+ import type { UsageDeliveryProviderOptions } from "./deliveryProvider.js";
14
+ /** Host authenticates and authorizes the actor and original activity before calling. */
15
+ export class UsageCorrectionClient extends UsageAllowanceClient {
16
+ async getCorrection(
17
+ ctx: RunCtx,
18
+ input: { scope: UsageScope; eventId: string; correctionId: string },
19
+ ) {
20
+ const row = (await ctx.runQuery(
21
+ componentRef(this.component, "usageCorrections/queries/get"),
22
+ { ...input, scope: scopeSchema.parse(input.scope) },
23
+ )) as Doc<"usageCorrections"> | null;
24
+ return row ? correctionView(row) : null;
25
+ }
26
+ readCorrectionHistory(
27
+ ctx: RunCtx,
28
+ input: {
29
+ scope: UsageScope;
30
+ eventId: string;
31
+ limit: number;
32
+ cursor: string | null;
33
+ },
34
+ ) {
35
+ return readCorrectionEvidence(this.component, ctx, input);
36
+ }
37
+ readCorrectionPeriodHistory(
38
+ ctx: RunCtx,
39
+ input: {
40
+ scope: UsageScope;
41
+ period: UsagePeriod;
42
+ limit: number;
43
+ cursor: string | null;
44
+ },
45
+ ) {
46
+ return readCorrectionEvidence(this.component, ctx, input);
47
+ }
48
+ observeInvoiceCorrection(
49
+ ctx: RunCtx,
50
+ input: UsageInvoiceCorrectionOptions,
51
+ options: UsageDeliveryProviderOptions,
52
+ ) {
53
+ return observeInvoiceCorrection(this.component, ctx, input, options);
54
+ }
55
+ requestCorrection(
56
+ ctx: RunCtx,
57
+ input: UsageCorrectionOptions,
58
+ options: UsageDeliveryProviderOptions = {},
59
+ ) {
60
+ return requestUsageCorrection(this.component, ctx, input, options);
61
+ }
62
+ }