@fantastic.dev/better-stripe 0.59.0 → 0.60.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.
Files changed (230) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +5 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +676 -50
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +322 -9
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +644 -18
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +338 -25
  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/creditCapacity.d.ts.map +1 -1
  32. package/dist/client/usage/creditCapacity.js +7 -1
  33. package/dist/client/usage/creditCapacity.js.map +1 -1
  34. package/dist/client/usage/creditClient.d.ts +3 -0
  35. package/dist/client/usage/creditClient.d.ts.map +1 -1
  36. package/dist/client/usage/creditDelivery.d.ts.map +1 -1
  37. package/dist/client/usage/creditDelivery.js +4 -1
  38. package/dist/client/usage/creditDelivery.js.map +1 -1
  39. package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
  40. package/dist/client/usage/creditLifecycle.js +2 -1
  41. package/dist/client/usage/creditLifecycle.js.map +1 -1
  42. package/dist/client/usage/creditNative.d.ts +3 -0
  43. package/dist/client/usage/creditNative.d.ts.map +1 -1
  44. package/dist/client/usage/creditNative.js +3 -0
  45. package/dist/client/usage/creditNative.js.map +1 -1
  46. package/dist/client/usage/creditProvider.d.ts.map +1 -1
  47. package/dist/client/usage/creditProvider.js +2 -1
  48. package/dist/client/usage/creditProvider.js.map +1 -1
  49. package/dist/client/usage/creditRead.d.ts +1 -0
  50. package/dist/client/usage/creditRead.d.ts.map +1 -1
  51. package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
  52. package/dist/client/usage/creditReconciliation.js +11 -3
  53. package/dist/client/usage/creditReconciliation.js.map +1 -1
  54. package/dist/client/usage/creditRevenue.d.ts +22 -0
  55. package/dist/client/usage/creditRevenue.d.ts.map +1 -0
  56. package/dist/client/usage/creditRevenue.js +118 -0
  57. package/dist/client/usage/creditRevenue.js.map +1 -0
  58. package/dist/client/usage/creditSettlement.d.ts +29 -0
  59. package/dist/client/usage/creditSettlement.d.ts.map +1 -0
  60. package/dist/client/usage/creditSettlement.fixtures.d.ts +216 -0
  61. package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
  62. package/dist/client/usage/creditSettlement.fixtures.js +59 -0
  63. package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
  64. package/dist/client/usage/creditSettlement.js +70 -0
  65. package/dist/client/usage/creditSettlement.js.map +1 -0
  66. package/dist/client/usage/creditTransport.fixtures.d.ts +8 -1
  67. package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
  68. package/dist/client/usage/creditTransport.fixtures.js +27 -5
  69. package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
  70. package/dist/client/usage/index.d.ts +4 -0
  71. package/dist/client/usage/index.d.ts.map +1 -1
  72. package/dist/client/usage/invoiceSale.d.ts +22 -0
  73. package/dist/client/usage/invoiceSale.d.ts.map +1 -0
  74. package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
  75. package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
  76. package/dist/client/usage/invoiceSale.fixtures.js +104 -0
  77. package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
  78. package/dist/client/usage/invoiceSale.js +117 -0
  79. package/dist/client/usage/invoiceSale.js.map +1 -0
  80. package/dist/client/usage/observationClient.d.ts +2 -2
  81. package/dist/client/usage/observationClient.d.ts.map +1 -1
  82. package/dist/client/usage/settlement.d.ts +32 -0
  83. package/dist/client/usage/settlement.d.ts.map +1 -0
  84. package/dist/client/usage/settlement.js +60 -0
  85. package/dist/client/usage/settlement.js.map +1 -0
  86. package/dist/client/usage/settlementClient.d.ts +51 -0
  87. package/dist/client/usage/settlementClient.d.ts.map +1 -0
  88. package/dist/client/usage/settlementClient.js +35 -0
  89. package/dist/client/usage/settlementClient.js.map +1 -0
  90. package/dist/client/usage/subscriptionCancellation.d.ts.map +1 -1
  91. package/dist/client/usage/subscriptionCancellation.js +3 -2
  92. package/dist/client/usage/subscriptionCancellation.js.map +1 -1
  93. package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
  94. package/dist/client/usage/subscriptionCheckout.js +3 -2
  95. package/dist/client/usage/subscriptionCheckout.js.map +1 -1
  96. package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
  97. package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
  98. package/dist/client/usage/subscriptionLifecycle.js +3 -2
  99. package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
  100. package/dist/client/usage/subscriptionProvider.d.ts +2 -0
  101. package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
  102. package/dist/client/usage/subscriptionProvider.js +9 -2
  103. package/dist/client/usage/subscriptionProvider.js.map +1 -1
  104. package/dist/component/_generated/component.d.ts +146 -0
  105. package/dist/component/_generated/component.d.ts.map +1 -1
  106. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  107. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  108. package/dist/component/earnings/triggers.d.ts +172 -12
  109. package/dist/component/earnings/triggers.d.ts.map +1 -1
  110. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
  111. package/dist/component/financialObservations/settlementHelpers.js +2 -0
  112. package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
  113. package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
  114. package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
  115. package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
  116. package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
  117. package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
  118. package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
  119. package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
  120. package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
  121. package/dist/component/schema.d.ts +322 -9
  122. package/dist/component/settlement/mutations.d.ts +50 -0
  123. package/dist/component/settlement/mutations.d.ts.map +1 -1
  124. package/dist/component/settlement/mutations.js +8 -1
  125. package/dist/component/settlement/mutations.js.map +1 -1
  126. package/dist/component/settlement/queries.d.ts +25 -0
  127. package/dist/component/settlement/queries.d.ts.map +1 -1
  128. package/dist/component/settlement/schema.d.ts +128 -1
  129. package/dist/component/settlement/schema.d.ts.map +1 -1
  130. package/dist/component/settlement/schema.js +22 -0
  131. package/dist/component/settlement/schema.js.map +1 -1
  132. package/dist/component/usage/allowance.fixtures.d.ts +4212 -0
  133. package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
  134. package/dist/component/usage/allowance.fixtures.js +67 -0
  135. package/dist/component/usage/allowance.fixtures.js.map +1 -0
  136. package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
  137. package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
  138. package/dist/component/usageCredits/accounting.fixtures.js +89 -0
  139. package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
  140. package/dist/component/usageCredits/helpers.d.ts +7 -0
  141. package/dist/component/usageCredits/helpers.d.ts.map +1 -1
  142. package/dist/component/usageCredits/helpers.js +5 -0
  143. package/dist/component/usageCredits/helpers.js.map +1 -1
  144. package/dist/component/usageCredits/mutations.d.ts +6 -0
  145. package/dist/component/usageCredits/mutations.d.ts.map +1 -1
  146. package/dist/component/usageCredits/mutations.js +70 -7
  147. package/dist/component/usageCredits/mutations.js.map +1 -1
  148. package/dist/component/usageCredits/purchase.d.ts +1 -0
  149. package/dist/component/usageCredits/purchase.d.ts.map +1 -1
  150. package/dist/component/usageCredits/queries.d.ts +10 -0
  151. package/dist/component/usageCredits/queries.d.ts.map +1 -1
  152. package/dist/component/usageCredits/schema.d.ts +24 -4
  153. package/dist/component/usageCredits/schema.d.ts.map +1 -1
  154. package/dist/component/usageCredits/schema.js +4 -0
  155. package/dist/component/usageCredits/schema.js.map +1 -1
  156. package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
  157. package/dist/component/usageSubscriptions/history.js +3 -1
  158. package/dist/component/usageSubscriptions/history.js.map +1 -1
  159. package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
  160. package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
  161. package/dist/component/usageSubscriptions/mutations.js +19 -0
  162. package/dist/component/usageSubscriptions/mutations.js.map +1 -1
  163. package/dist/component/usageSubscriptions/queries.d.ts +2 -0
  164. package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
  165. package/dist/component/usageSubscriptions/schema.d.ts +7 -2
  166. package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
  167. package/dist/component/usageSubscriptions/schema.js +1 -0
  168. package/dist/component/usageSubscriptions/schema.js.map +1 -1
  169. package/docs/README.md +4 -0
  170. package/docs/settlement.md +4 -0
  171. package/docs/usage-credits.md +6 -1
  172. package/docs/usage-security.md +130 -0
  173. package/docs/usage-settlement.md +162 -0
  174. package/package.json +3 -1
  175. package/src/client/index.ts +9 -0
  176. package/src/client/paymentSource/resolver.fixtures.ts +216 -0
  177. package/src/client/paymentSource/resolver.test.ts +11 -207
  178. package/src/client/paymentSource/resolver.ts +54 -2
  179. package/src/client/settlement/execution.ts +24 -0
  180. package/src/client/usage/catalogPrices.ts +4 -2
  181. package/src/client/usage/client.ts +2 -2
  182. package/src/client/usage/creditBackingIsolation.test.ts +110 -0
  183. package/src/client/usage/creditCapacity.test.ts +80 -1
  184. package/src/client/usage/creditCapacity.ts +10 -1
  185. package/src/client/usage/creditDelivery.ts +4 -0
  186. package/src/client/usage/creditLifecycle.ts +2 -0
  187. package/src/client/usage/creditNative.ts +5 -0
  188. package/src/client/usage/creditProofBounds.test.ts +187 -0
  189. package/src/client/usage/creditProvider.test.ts +74 -0
  190. package/src/client/usage/creditProvider.ts +2 -0
  191. package/src/client/usage/creditReconciliation.ts +18 -3
  192. package/src/client/usage/creditRevenue.test.ts +105 -0
  193. package/src/client/usage/creditRevenue.ts +163 -0
  194. package/src/client/usage/creditSettlement.fixtures.ts +70 -0
  195. package/src/client/usage/creditSettlement.test.ts +66 -0
  196. package/src/client/usage/creditSettlement.ts +94 -0
  197. package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
  198. package/src/client/usage/creditTransport.fixtures.ts +31 -5
  199. package/src/client/usage/index.ts +9 -0
  200. package/src/client/usage/invoiceSale.fixtures.ts +103 -0
  201. package/src/client/usage/invoiceSale.test.ts +184 -0
  202. package/src/client/usage/invoiceSale.ts +155 -0
  203. package/src/client/usage/settlement.test.ts +179 -0
  204. package/src/client/usage/settlement.ts +81 -0
  205. package/src/client/usage/settlementClient.ts +84 -0
  206. package/src/client/usage/subscriptionCancellation.ts +3 -2
  207. package/src/client/usage/subscriptionCheckout.ts +7 -2
  208. package/src/client/usage/subscriptionClient.test.ts +60 -1
  209. package/src/client/usage/subscriptionLifecycle.ts +3 -2
  210. package/src/client/usage/subscriptionProvider.ts +27 -3
  211. package/src/component/_generated/component.ts +150 -1
  212. package/src/component/financialObservations/settlementHelpers.ts +2 -0
  213. package/src/component/settlement/mutations.ts +10 -1
  214. package/src/component/settlement/schema.ts +26 -0
  215. package/src/component/settlement/settlement-claims.test.ts +54 -0
  216. package/src/component/usage/allowance.fixtures.ts +77 -0
  217. package/src/component/usage/allowance.test.ts +3 -66
  218. package/src/component/usage/allowanceBounds.test.ts +79 -0
  219. package/src/component/usage/historyIsolation.test.ts +124 -0
  220. package/src/component/usageCredits/accounting.fixtures.ts +96 -0
  221. package/src/component/usageCredits/accounting.test.ts +176 -82
  222. package/src/component/usageCredits/freshness.test.ts +33 -0
  223. package/src/component/usageCredits/helpers.ts +8 -0
  224. package/src/component/usageCredits/mutations.ts +83 -5
  225. package/src/component/usageCredits/quotas.test.ts +63 -0
  226. package/src/component/usageCredits/schema.ts +10 -0
  227. package/src/component/usageSubscriptions/history.test.ts +57 -0
  228. package/src/component/usageSubscriptions/history.ts +3 -1
  229. package/src/component/usageSubscriptions/mutations.ts +23 -0
  230. package/src/component/usageSubscriptions/schema.ts +1 -0
@@ -7715,6 +7715,15 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
7715
7715
  accountId: string;
7716
7716
  allocation?: {
7717
7717
  adjustmentAmount?: number;
7718
+ creditSource?: {
7719
+ creditGrantId: string;
7720
+ customerAccount: string;
7721
+ livemode: boolean;
7722
+ purchaseId: string;
7723
+ stripeAccountId: string;
7724
+ stripePriceId: string;
7725
+ stripeProductId: string;
7726
+ };
7718
7727
  currency: string;
7719
7728
  gross: number;
7720
7729
  paymentId?: string;
@@ -7728,6 +7737,19 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
7728
7737
  destinationAccountId: string;
7729
7738
  role: "store" | "affiliate" | "other";
7730
7739
  }>;
7740
+ usageSource?: {
7741
+ customerAccount: string;
7742
+ invoicePaymentId: string;
7743
+ livemode: boolean;
7744
+ meterId: string;
7745
+ model: "pay_as_you_go" | "included_overage";
7746
+ period: { end: number; start: number };
7747
+ stripeAccountId: string;
7748
+ stripePriceId: string;
7749
+ stripeProductId: string;
7750
+ subscriptionId: string;
7751
+ subscriptionItemId: string;
7752
+ };
7731
7753
  };
7732
7754
  completion?: string;
7733
7755
  currency: string;
@@ -7820,6 +7842,15 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
7820
7842
  activePhase?: "execute" | "reconcile";
7821
7843
  allocation?: {
7822
7844
  adjustmentAmount?: number;
7845
+ creditSource?: {
7846
+ creditGrantId: string;
7847
+ customerAccount: string;
7848
+ livemode: boolean;
7849
+ purchaseId: string;
7850
+ stripeAccountId: string;
7851
+ stripePriceId: string;
7852
+ stripeProductId: string;
7853
+ };
7823
7854
  currency: string;
7824
7855
  gross: number;
7825
7856
  paymentId?: string;
@@ -7833,6 +7864,19 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
7833
7864
  destinationAccountId: string;
7834
7865
  role: "store" | "affiliate" | "other";
7835
7866
  }>;
7867
+ usageSource?: {
7868
+ customerAccount: string;
7869
+ invoicePaymentId: string;
7870
+ livemode: boolean;
7871
+ meterId: string;
7872
+ model: "pay_as_you_go" | "included_overage";
7873
+ period: { end: number; start: number };
7874
+ stripeAccountId: string;
7875
+ stripePriceId: string;
7876
+ stripeProductId: string;
7877
+ subscriptionId: string;
7878
+ subscriptionItemId: string;
7879
+ };
7836
7880
  };
7837
7881
  cancelRequested: boolean;
7838
7882
  completion?: string;
@@ -8132,6 +8176,15 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8132
8176
  "internal",
8133
8177
  {
8134
8178
  adjustmentAmount?: number;
8179
+ creditSource?: {
8180
+ creditGrantId: string;
8181
+ customerAccount: string;
8182
+ livemode: boolean;
8183
+ purchaseId: string;
8184
+ stripeAccountId: string;
8185
+ stripePriceId: string;
8186
+ stripeProductId: string;
8187
+ };
8135
8188
  currency: string;
8136
8189
  gross: number;
8137
8190
  paymentId?: string;
@@ -8145,11 +8198,33 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8145
8198
  destinationAccountId: string;
8146
8199
  role: "store" | "affiliate" | "other";
8147
8200
  }>;
8201
+ usageSource?: {
8202
+ customerAccount: string;
8203
+ invoicePaymentId: string;
8204
+ livemode: boolean;
8205
+ meterId: string;
8206
+ model: "pay_as_you_go" | "included_overage";
8207
+ period: { end: number; start: number };
8208
+ stripeAccountId: string;
8209
+ stripePriceId: string;
8210
+ stripeProductId: string;
8211
+ subscriptionId: string;
8212
+ subscriptionItemId: string;
8213
+ };
8148
8214
  },
8149
8215
  {
8150
8216
  _creationTime: number;
8151
8217
  _id: string;
8152
8218
  adjustmentAmount?: number;
8219
+ creditSource?: {
8220
+ creditGrantId: string;
8221
+ customerAccount: string;
8222
+ livemode: boolean;
8223
+ purchaseId: string;
8224
+ stripeAccountId: string;
8225
+ stripePriceId: string;
8226
+ stripeProductId: string;
8227
+ };
8153
8228
  currency: string;
8154
8229
  gross: number;
8155
8230
  paymentId?: string;
@@ -8163,6 +8238,19 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8163
8238
  destinationAccountId: string;
8164
8239
  role: "store" | "affiliate" | "other";
8165
8240
  }>;
8241
+ usageSource?: {
8242
+ customerAccount: string;
8243
+ invoicePaymentId: string;
8244
+ livemode: boolean;
8245
+ meterId: string;
8246
+ model: "pay_as_you_go" | "included_overage";
8247
+ period: { end: number; start: number };
8248
+ stripeAccountId: string;
8249
+ stripePriceId: string;
8250
+ stripeProductId: string;
8251
+ subscriptionId: string;
8252
+ subscriptionItemId: string;
8253
+ };
8166
8254
  },
8167
8255
  Name
8168
8256
  >;
@@ -8188,6 +8276,15 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8188
8276
  _creationTime: number;
8189
8277
  _id: string;
8190
8278
  adjustmentAmount?: number;
8279
+ creditSource?: {
8280
+ creditGrantId: string;
8281
+ customerAccount: string;
8282
+ livemode: boolean;
8283
+ purchaseId: string;
8284
+ stripeAccountId: string;
8285
+ stripePriceId: string;
8286
+ stripeProductId: string;
8287
+ };
8191
8288
  currency: string;
8192
8289
  gross: number;
8193
8290
  paymentId?: string;
@@ -8201,6 +8298,19 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8201
8298
  destinationAccountId: string;
8202
8299
  role: "store" | "affiliate" | "other";
8203
8300
  }>;
8301
+ usageSource?: {
8302
+ customerAccount: string;
8303
+ invoicePaymentId: string;
8304
+ livemode: boolean;
8305
+ meterId: string;
8306
+ model: "pay_as_you_go" | "included_overage";
8307
+ period: { end: number; start: number };
8308
+ stripeAccountId: string;
8309
+ stripePriceId: string;
8310
+ stripeProductId: string;
8311
+ subscriptionId: string;
8312
+ subscriptionItemId: string;
8313
+ };
8204
8314
  } | null,
8205
8315
  Name
8206
8316
  >;
@@ -8868,6 +8978,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8868
8978
  };
8869
8979
  usageCredits: {
8870
8980
  mutations: {
8981
+ beginObservation: FunctionReference<
8982
+ "mutation",
8983
+ "internal",
8984
+ { inputJson: string },
8985
+ number,
8986
+ Name
8987
+ >;
8871
8988
  cancelPurchase: FunctionReference<
8872
8989
  "mutation",
8873
8990
  "internal",
@@ -8941,6 +9058,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8941
9058
  paymentIntentId?: string;
8942
9059
  paymentKey?: string;
8943
9060
  paymentProofJson?: string;
9061
+ recoveryReason?:
9062
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
8944
9063
  refundedAmount?: number;
8945
9064
  state: "pending" | "ready" | "frozen" | "canceled";
8946
9065
  termsJson: string;
@@ -8990,7 +9109,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
8990
9109
  saveObservation: FunctionReference<
8991
9110
  "mutation",
8992
9111
  "internal",
8993
- { inputJson: string; proofJson: string; startedAt: number },
9112
+ {
9113
+ inputJson: string;
9114
+ proofJson: string;
9115
+ proofStartedAt?: number;
9116
+ startedAt: number;
9117
+ },
8994
9118
  string,
8995
9119
  Name
8996
9120
  >;
@@ -9042,6 +9166,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9042
9166
  historyCount?: number;
9043
9167
  key: string;
9044
9168
  method: "direct" | "checkout";
9169
+ observationStartedAt?: number;
9045
9170
  providerId?: string;
9046
9171
  retiredAt?: number;
9047
9172
  retirementInvoiceId?: string;
@@ -9081,9 +9206,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9081
9206
  nativeExpired?: boolean;
9082
9207
  nativeObservationJson?: string;
9083
9208
  nativeUnused: boolean;
9209
+ observationConflictAt?: number;
9210
+ observationStartedAt?: number;
9084
9211
  observedAt?: number;
9085
9212
  paymentIntentId: string;
9086
9213
  proofJson: string;
9214
+ proofStartedAt?: number;
9087
9215
  purchaseKey: string;
9088
9216
  reserved: string;
9089
9217
  }>;
@@ -9102,6 +9230,10 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9102
9230
  paymentIntentId?: string;
9103
9231
  paymentKey?: string;
9104
9232
  paymentProofJson?: string;
9233
+ recoveryReason?:
9234
+ | "payment_refunded"
9235
+ | "payment_disputed"
9236
+ | "native_state_changed";
9105
9237
  refundedAmount?: number;
9106
9238
  state: "pending" | "ready" | "frozen" | "canceled";
9107
9239
  termsJson: string;
@@ -9127,6 +9259,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9127
9259
  paymentIntentId?: string;
9128
9260
  paymentKey?: string;
9129
9261
  paymentProofJson?: string;
9262
+ recoveryReason?:
9263
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
9130
9264
  refundedAmount?: number;
9131
9265
  state: "pending" | "ready" | "frozen" | "canceled";
9132
9266
  termsJson: string;
@@ -9151,6 +9285,8 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9151
9285
  paymentIntentId?: string;
9152
9286
  paymentKey?: string;
9153
9287
  paymentProofJson?: string;
9288
+ recoveryReason?:
9289
+ "payment_refunded" | "payment_disputed" | "native_state_changed";
9154
9290
  refundedAmount?: number;
9155
9291
  state: "pending" | "ready" | "frozen" | "canceled";
9156
9292
  termsJson: string;
@@ -9203,9 +9339,12 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9203
9339
  nativeExpired?: boolean;
9204
9340
  nativeObservationJson?: string;
9205
9341
  nativeUnused: boolean;
9342
+ observationConflictAt?: number;
9343
+ observationStartedAt?: number;
9206
9344
  observedAt?: number;
9207
9345
  paymentIntentId: string;
9208
9346
  proofJson: string;
9347
+ proofStartedAt?: number;
9209
9348
  purchaseKey: string;
9210
9349
  reserved: string;
9211
9350
  }>;
@@ -9728,6 +9867,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9728
9867
  null,
9729
9868
  Name
9730
9869
  >;
9870
+ beginObservation: FunctionReference<
9871
+ "mutation",
9872
+ "internal",
9873
+ { key: string },
9874
+ number,
9875
+ Name
9876
+ >;
9731
9877
  completeCancellation: FunctionReference<
9732
9878
  "mutation",
9733
9879
  "internal",
@@ -9756,6 +9902,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9756
9902
  historyCount?: number;
9757
9903
  key: string;
9758
9904
  method: "direct" | "checkout";
9905
+ observationStartedAt?: number;
9759
9906
  providerId?: string;
9760
9907
  retiredAt?: number;
9761
9908
  retirementInvoiceId?: string;
@@ -9831,6 +9978,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9831
9978
  historyCount?: number;
9832
9979
  key: string;
9833
9980
  method: "direct" | "checkout";
9981
+ observationStartedAt?: number;
9834
9982
  providerId?: string;
9835
9983
  retiredAt?: number;
9836
9984
  retirementInvoiceId?: string;
@@ -9893,6 +10041,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
9893
10041
  historyCount?: number;
9894
10042
  key: string;
9895
10043
  method: "direct" | "checkout";
10044
+ observationStartedAt?: number;
9896
10045
  providerId?: string;
9897
10046
  retiredAt?: number;
9898
10047
  retirementInvoiceId?: string;
@@ -81,6 +81,8 @@ export async function settlementFingerprint(row: Doc<"settlementAllocations">) {
81
81
  sourceChargeId: row.sourceChargeId,
82
82
  paymentIntentId: row.paymentIntentId,
83
83
  sourceInvoiceId: row.sourceInvoiceId,
84
+ ...(row.usageSource ? { usageSource: row.usageSource } : {}),
85
+ ...(row.creditSource ? { creditSource: row.creditSource } : {}),
84
86
  gross: row.gross,
85
87
  currency: row.currency,
86
88
  processingFee: row.processingFee,
@@ -1,3 +1,4 @@
1
+ import { canonicalFingerprint } from "../operationEvidence/canonical.js";
1
2
  import { observeSettlementAllocation } from "../financialObservations/settlement.js";
2
3
  import { paginator } from "convex-helpers/server/pagination";
3
4
  import schema from "../schema.js";
@@ -16,6 +17,10 @@ export const reserveSettlementAllocation = mutation({
16
17
  args: allocationFields,
17
18
  returns: doc,
18
19
  handler: async (ctx, args) => {
20
+ if (args.usageSource && args.creditSource)
21
+ throw new Error(
22
+ "Settlement cannot combine invoice and credit purchase sources",
23
+ );
19
24
  const existing = await ctx.db
20
25
  .query("settlementAllocations")
21
26
  .withIndex("by_source_charge", (q) =>
@@ -26,7 +31,11 @@ export const reserveSettlementAllocation = mutation({
26
31
  if (
27
32
  existing.currency !== args.currency ||
28
33
  existing.paymentIntentId !== args.paymentIntentId ||
29
- existing.sourceInvoiceId !== args.sourceInvoiceId
34
+ existing.sourceInvoiceId !== args.sourceInvoiceId ||
35
+ (await canonicalFingerprint(existing.usageSource ?? null)) !==
36
+ (await canonicalFingerprint(args.usageSource ?? null)) ||
37
+ (await canonicalFingerprint(existing.creditSource ?? null)) !==
38
+ (await canonicalFingerprint(args.creditSource ?? null))
30
39
  )
31
40
  throw new Error("Settlement source identity cannot change.");
32
41
  await observeSettlementAllocation(ctx, existing);
@@ -9,6 +9,32 @@ export const allocationFields = {
9
9
  sourceChargeId: v.string(),
10
10
  paymentIntentId: v.optional(v.string()),
11
11
  sourceInvoiceId: v.optional(v.string()),
12
+ usageSource: v.optional(
13
+ v.object({
14
+ stripeAccountId: v.string(),
15
+ customerAccount: v.string(),
16
+ livemode: v.boolean(),
17
+ meterId: v.string(),
18
+ stripePriceId: v.string(),
19
+ stripeProductId: v.string(),
20
+ subscriptionId: v.string(),
21
+ subscriptionItemId: v.string(),
22
+ invoicePaymentId: v.string(),
23
+ period: v.object({ start: v.number(), end: v.number() }),
24
+ model: v.union(v.literal("pay_as_you_go"), v.literal("included_overage")),
25
+ }),
26
+ ),
27
+ creditSource: v.optional(
28
+ v.object({
29
+ stripeAccountId: v.string(),
30
+ customerAccount: v.string(),
31
+ livemode: v.boolean(),
32
+ purchaseId: v.string(),
33
+ creditGrantId: v.string(),
34
+ stripePriceId: v.string(),
35
+ stripeProductId: v.string(),
36
+ }),
37
+ ),
12
38
  gross: v.number(),
13
39
  currency: v.string(),
14
40
  processingFee: v.number(),
@@ -74,3 +74,57 @@ it("persists the term, identity and earliest invoice despite replay and term cha
74
74
  }),
75
75
  ).rejects.toThrow();
76
76
  });
77
+
78
+ it("pins original native metering provenance and refuses replay against another meter or invoice payment", async () => {
79
+ const t = convexTest(schema, modules);
80
+ const opts = {
81
+ sourceChargeId: "ch_meter",
82
+ paymentIntentId: "pi_meter",
83
+ sourceInvoiceId: "in_meter",
84
+ gross: 100,
85
+ currency: "usd",
86
+ processingFee: 3,
87
+ platformNet: 7,
88
+ transfers: [
89
+ {
90
+ destinationAccountId: "acct_seller",
91
+ amount: 90,
92
+ role: "store" as const,
93
+ },
94
+ ],
95
+ usageSource: {
96
+ stripeAccountId: "acct_platform",
97
+ customerAccount: "acct_buyer",
98
+ livemode: false,
99
+ meterId: "mtr_calls",
100
+ stripePriceId: "price_calls",
101
+ stripeProductId: "prod_calls",
102
+ subscriptionId: "sub_calls",
103
+ subscriptionItemId: "si_calls",
104
+ invoicePaymentId: "inpay_meter",
105
+ period: { start: 600, end: 1200 },
106
+ model: "pay_as_you_go" as const,
107
+ },
108
+ };
109
+ const first = await t.mutation(
110
+ api.settlement.mutations.reserveSettlementAllocation,
111
+ opts,
112
+ );
113
+ expect(first).toMatchObject({ usageSource: opts.usageSource });
114
+ for (const changed of [
115
+ { meterId: "mtr_other" },
116
+ { invoicePaymentId: "inpay_other" },
117
+ ])
118
+ await expect(
119
+ t.mutation(api.settlement.mutations.reserveSettlementAllocation, {
120
+ ...opts,
121
+ usageSource: { ...opts.usageSource, ...changed },
122
+ }),
123
+ ).rejects.toThrow(/source identity/i);
124
+ await expect(
125
+ t.mutation(api.settlement.mutations.reserveSettlementAllocation, {
126
+ ...opts,
127
+ usageSource: undefined,
128
+ }),
129
+ ).rejects.toThrow(/source identity/i);
130
+ });
@@ -0,0 +1,77 @@
1
+ import { convexTest } from "convex-test";
2
+ import { makeFunctionReference } from "convex/server";
3
+ import { vi } from "vitest";
4
+ import { schema, modules } from "../../test.js";
5
+ import {
6
+ request,
7
+ nativeSubscription,
8
+ } from "../../client/usage/subscription.fixtures.js";
9
+ const mutation = (name: string) =>
10
+ makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
11
+ const policy = {
12
+ reset: "billing_period",
13
+ rollover: "none",
14
+ entitled: true,
15
+ overage: "allow",
16
+ limit: { mode: "hard", quantity: "12" },
17
+ };
18
+ export async function setupAllowance() {
19
+ vi.useFakeTimers();
20
+ vi.setSystemTime(700000);
21
+ const t = convexTest(schema, modules);
22
+ const meter = {
23
+ ...request.meters[0],
24
+ binding: {
25
+ ...request.meters[0].binding,
26
+ plan: {
27
+ key: "calls",
28
+ version: "v1",
29
+ model: "included_overage",
30
+ unit: { name: "call", precision: 0 },
31
+ rate: { currency: "usd", unitAmountDecimal: "2" },
32
+ includedQuantity: "10",
33
+ reset: "billing_period",
34
+ },
35
+ },
36
+ };
37
+ const intent = await t.mutation(
38
+ makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
39
+ {
40
+ termsJson: JSON.stringify({ ...request, meters: [meter] }),
41
+ method: "direct",
42
+ },
43
+ );
44
+ await t.mutation(
45
+ makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
46
+ {
47
+ key: intent.key,
48
+ nativeJson: JSON.stringify(nativeSubscription()),
49
+ startedAt: Date.now(),
50
+ },
51
+ );
52
+ const subscription = {
53
+ subscriptionId: "sub_calls",
54
+ subscriptionItemId: "si_calls",
55
+ stripePriceId: "price_calls",
56
+ period: { start: 600, end: 1200 },
57
+ interval: "month",
58
+ };
59
+ const bindingJson = JSON.stringify({ ...meter, subscription });
60
+ const event = {
61
+ ...meter,
62
+ sourceEventId: "work1",
63
+ quantity: "6",
64
+ occurredAt: 700,
65
+ };
66
+ const args = {
67
+ inputJson: JSON.stringify(event),
68
+ subscriptionJson: JSON.stringify(subscription),
69
+ };
70
+ const configure = (value = policy, expectedRevision = 0) =>
71
+ t.mutation(mutation("configure"), {
72
+ bindingJson,
73
+ policyJson: JSON.stringify(value),
74
+ expectedRevision,
75
+ });
76
+ return { t, meter, event, subscription, bindingJson, args, configure };
77
+ }
@@ -1,11 +1,7 @@
1
- import { convexTest } from "convex-test";
1
+ import { setupAllowance as setup } from "./allowance.fixtures.js";
2
2
  import { makeFunctionReference } from "convex/server";
3
3
  import { afterEach, expect, it, vi } from "vitest";
4
- import { schema, modules } from "../../test.js";
5
- import {
6
- request,
7
- nativeSubscription,
8
- } from "../../client/usage/subscription.fixtures.js";
4
+ import { nativeSubscription } from "../../client/usage/subscription.fixtures.js";
9
5
  const mutation = (name: string) =>
10
6
  makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
11
7
  const status = makeFunctionReference<"query">("usageAllowance/queries:status");
@@ -17,66 +13,7 @@ const policy = {
17
13
  overage: "allow",
18
14
  limit: { mode: "hard", quantity: "12" },
19
15
  };
20
- async function setup() {
21
- vi.useFakeTimers();
22
- vi.setSystemTime(700000);
23
- const t = convexTest(schema, modules);
24
- const meter = {
25
- ...request.meters[0],
26
- binding: {
27
- ...request.meters[0].binding,
28
- plan: {
29
- key: "calls",
30
- version: "v1",
31
- model: "included_overage",
32
- unit: { name: "call", precision: 0 },
33
- rate: { currency: "usd", unitAmountDecimal: "2" },
34
- includedQuantity: "10",
35
- reset: "billing_period",
36
- },
37
- },
38
- };
39
- const intent = await t.mutation(
40
- makeFunctionReference<"mutation">("usageSubscriptions/mutations:prepare"),
41
- {
42
- termsJson: JSON.stringify({ ...request, meters: [meter] }),
43
- method: "direct",
44
- },
45
- );
46
- await t.mutation(
47
- makeFunctionReference<"mutation">("usageSubscriptions/mutations:save"),
48
- {
49
- key: intent.key,
50
- nativeJson: JSON.stringify(nativeSubscription()),
51
- startedAt: Date.now(),
52
- },
53
- );
54
- const subscription = {
55
- subscriptionId: "sub_calls",
56
- subscriptionItemId: "si_calls",
57
- stripePriceId: "price_calls",
58
- period: { start: 600, end: 1200 },
59
- interval: "month",
60
- };
61
- const bindingJson = JSON.stringify({ ...meter, subscription });
62
- const event = {
63
- ...meter,
64
- sourceEventId: "work1",
65
- quantity: "6",
66
- occurredAt: 700,
67
- };
68
- const args = {
69
- inputJson: JSON.stringify(event),
70
- subscriptionJson: JSON.stringify(subscription),
71
- };
72
- const configure = (value = policy, expectedRevision = 0) =>
73
- t.mutation(mutation("configure"), {
74
- bindingJson,
75
- policyJson: JSON.stringify(value),
76
- expectedRevision,
77
- });
78
- return { t, meter, event, subscription, bindingJson, args, configure };
79
- }
16
+
80
17
  afterEach(() => vi.useRealTimers());
81
18
  it("reserves a hard cap atomically and commits included usage and overage exactly once", async () => {
82
19
  const { t, args, configure, bindingJson, event } = await setup();
@@ -0,0 +1,79 @@
1
+ import { makeFunctionReference } from "convex/server";
2
+ import { afterEach, expect, it, vi } from "vitest";
3
+ import { setupAllowance } from "./allowance.fixtures.js";
4
+ const mutation = (name: string) =>
5
+ makeFunctionReference<"mutation">(`usageAllowance/mutations:${name}`);
6
+ const reconciliation = makeFunctionReference<"query">(
7
+ "usageAllowance/queries:reconciliation",
8
+ );
9
+ const record = makeFunctionReference<"mutation">("usage/mutations:record");
10
+ afterEach(() => vi.useRealTimers());
11
+ for (const commit of [false, true])
12
+ it(`matches exactly 500 ${commit ? "accepted events" : "open reservations"} and holds 501 without repairing`, async () => {
13
+ const { t, event, subscription, bindingJson, configure } =
14
+ await setupAllowance();
15
+ await configure({
16
+ reset: "billing_period",
17
+ rollover: "none",
18
+ entitled: true,
19
+ overage: "allow",
20
+ limit: { mode: "hard", quantity: "1000" },
21
+ });
22
+ const add = async (i: number) => {
23
+ const args = {
24
+ inputJson: JSON.stringify({
25
+ ...event,
26
+ sourceEventId: `bound_${i}`,
27
+ quantity: "1",
28
+ }),
29
+ subscriptionJson: JSON.stringify(subscription),
30
+ };
31
+ const reserved = await t.mutation(mutation("reserve"), args);
32
+ expect(reserved.state).toBe("reserved");
33
+ if (commit)
34
+ await t.mutation(record, {
35
+ ...args,
36
+ reservationId: reserved.reservationId,
37
+ });
38
+ };
39
+ for (let i = 0; i < 500; i++) await add(i);
40
+ expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
41
+ status: "matched",
42
+ consumedScaledQuantity: commit ? "500" : "0",
43
+ reservedScaledQuantity: commit ? "0" : "500",
44
+ repaired: false,
45
+ });
46
+ await add(500);
47
+ expect(await t.query(reconciliation, { bindingJson })).toMatchObject({
48
+ status: "held",
49
+ reason: "local_evidence_incomplete",
50
+ consumedScaledQuantity: commit ? "501" : "0",
51
+ reservedScaledQuantity: commit ? "0" : "501",
52
+ repaired: false,
53
+ });
54
+ }, 30000);
55
+ it("commits the 100000th allowance audit entry and rolls back one over the retained quota", async () => {
56
+ const { t, configure, args } = await setupAllowance();
57
+ await configure();
58
+ await t.run(async (ctx) => {
59
+ const row = (await ctx.db.query("usageAllowancePolicies").first())!;
60
+ await ctx.db.patch(row._id, { historyCount: 99999 });
61
+ });
62
+ const reservation = await t.mutation(mutation("reserve"), args);
63
+ expect(reservation.state).toBe("reserved");
64
+ await expect(
65
+ t.mutation(record, { ...args, reservationId: reservation.reservationId }),
66
+ ).rejects.toThrow(/history quota/i);
67
+ await t.run(async (ctx) => {
68
+ expect(await ctx.db.query("usageEvents").first()).toBeNull();
69
+ expect(await ctx.db.query("usageOutbox").first()).toBeNull();
70
+ expect(await ctx.db.query("usageReservations").first()).toMatchObject({
71
+ state: "reserved",
72
+ });
73
+ expect(await ctx.db.query("usageAllowancePolicies").first()).toMatchObject({
74
+ historyCount: 100000,
75
+ consumed: "0",
76
+ reserved: "6",
77
+ });
78
+ });
79
+ });