@fantastic.dev/better-stripe 0.34.0 → 0.37.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 (308) hide show
  1. package/CHANGELOG.md +260 -0
  2. package/README.md +1 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +664 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +332 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +664 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +332 -1
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/connect/refundCommandEvidence.d.ts +14 -0
  12. package/dist/client/connect/refundCommandEvidence.d.ts.map +1 -0
  13. package/dist/client/connect/refundCommandEvidence.js +20 -0
  14. package/dist/client/connect/refundCommandEvidence.js.map +1 -0
  15. package/dist/client/connect/refundCommandNormalization.d.ts +11 -0
  16. package/dist/client/connect/refundCommandNormalization.d.ts.map +1 -0
  17. package/dist/client/connect/refundCommandNormalization.js +12 -0
  18. package/dist/client/connect/refundCommandNormalization.js.map +1 -0
  19. package/dist/client/financialObservations.d.ts +42 -0
  20. package/dist/client/financialObservations.d.ts.map +1 -0
  21. package/dist/client/financialObservations.js +58 -0
  22. package/dist/client/financialObservations.js.map +1 -0
  23. package/dist/client/financialOwnership.d.ts +52 -0
  24. package/dist/client/financialOwnership.d.ts.map +1 -0
  25. package/dist/client/financialOwnership.js +72 -0
  26. package/dist/client/financialOwnership.js.map +1 -0
  27. package/dist/client/index.d.ts +2 -2
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/index.js +2 -2
  30. package/dist/client/index.js.map +1 -1
  31. package/dist/client/observationPagination.d.ts +10 -0
  32. package/dist/client/observationPagination.d.ts.map +1 -0
  33. package/dist/client/observationPagination.js +12 -0
  34. package/dist/client/observationPagination.js.map +1 -0
  35. package/dist/client/operationEvidence.d.ts +37 -0
  36. package/dist/client/operationEvidence.d.ts.map +1 -0
  37. package/dist/client/operationEvidence.js +44 -0
  38. package/dist/client/operationEvidence.js.map +1 -0
  39. package/dist/client/publicTypes.d.ts +6 -0
  40. package/dist/client/publicTypes.d.ts.map +1 -1
  41. package/dist/client/readContracts.d.ts +31 -0
  42. package/dist/client/readContracts.d.ts.map +1 -1
  43. package/dist/client/readContracts.js +14 -0
  44. package/dist/client/readContracts.js.map +1 -1
  45. package/dist/client/transferObservations.d.ts +59 -0
  46. package/dist/client/transferObservations.d.ts.map +1 -0
  47. package/dist/client/transferObservations.js +82 -0
  48. package/dist/client/transferObservations.js.map +1 -0
  49. package/dist/component/_generated/component.d.ts +687 -0
  50. package/dist/component/_generated/component.d.ts.map +1 -1
  51. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  52. package/dist/component/affiliateRecovery/mutations.js +4 -1
  53. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  54. package/dist/component/connect/mutations.d.ts.map +1 -1
  55. package/dist/component/connect/mutations.js +18 -2
  56. package/dist/component/connect/mutations.js.map +1 -1
  57. package/dist/component/connect/queries.d.ts +8 -0
  58. package/dist/component/connect/queries.d.ts.map +1 -1
  59. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  60. package/dist/component/connect/reversalClaims.js +7 -1
  61. package/dist/component/connect/reversalClaims.js.map +1 -1
  62. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  63. package/dist/component/connect/reversalReceipts.js +2 -0
  64. package/dist/component/connect/reversalReceipts.js.map +1 -1
  65. package/dist/component/connect/schema.d.ts +11 -1
  66. package/dist/component/connect/schema.d.ts.map +1 -1
  67. package/dist/component/connect/schema.js +9 -1
  68. package/dist/component/connect/schema.js.map +1 -1
  69. package/dist/component/connect/validators.d.ts +9 -1
  70. package/dist/component/connect/validators.d.ts.map +1 -1
  71. package/dist/component/connect/validators.js +2 -0
  72. package/dist/component/connect/validators.js.map +1 -1
  73. package/dist/component/earnings/pairedAdjustment.d.ts +12 -0
  74. package/dist/component/earnings/pairedAdjustment.d.ts.map +1 -0
  75. package/dist/component/earnings/pairedAdjustment.js +46 -0
  76. package/dist/component/earnings/pairedAdjustment.js.map +1 -0
  77. package/dist/component/earnings/triggers.d.ts +546 -2
  78. package/dist/component/earnings/triggers.d.ts.map +1 -1
  79. package/dist/component/earnings/triggers.js +17 -3
  80. package/dist/component/earnings/triggers.js.map +1 -1
  81. package/dist/component/financialObservations/continuation.d.ts +7 -0
  82. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  83. package/dist/component/financialObservations/continuation.js +131 -0
  84. package/dist/component/financialObservations/continuation.js.map +1 -0
  85. package/dist/component/financialObservations/generations.d.ts +6 -0
  86. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  87. package/dist/component/financialObservations/generations.js +46 -0
  88. package/dist/component/financialObservations/generations.js.map +1 -0
  89. package/dist/component/financialObservations/mutations.d.ts +62 -0
  90. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  91. package/dist/component/financialObservations/mutations.js +69 -0
  92. package/dist/component/financialObservations/mutations.js.map +1 -0
  93. package/dist/component/financialObservations/queries.d.ts +307 -0
  94. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  95. package/dist/component/financialObservations/queries.js +219 -0
  96. package/dist/component/financialObservations/queries.js.map +1 -0
  97. package/dist/component/financialObservations/schema.d.ts +182 -0
  98. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  99. package/dist/component/financialObservations/schema.js +53 -0
  100. package/dist/component/financialObservations/schema.js.map +1 -0
  101. package/dist/component/financialObservations/settlement.d.ts +7 -0
  102. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  103. package/dist/component/financialObservations/settlement.js +118 -0
  104. package/dist/component/financialObservations/settlement.js.map +1 -0
  105. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  106. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  107. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  108. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  109. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  110. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  111. package/dist/component/financialObservations/transferBatches.js +76 -0
  112. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  113. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  114. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  115. package/dist/component/financialObservations/transferContinuation.js +35 -0
  116. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  117. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  118. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  119. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  120. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  121. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  122. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  123. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  124. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  125. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  126. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  127. package/dist/component/financialObservations/transferHelpers.js +37 -0
  128. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  129. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  130. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  131. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  132. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  133. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  134. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  135. package/dist/component/financialObservations/transferSchema.js +65 -0
  136. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  137. package/dist/component/financialObservations/transfers.d.ts +7 -0
  138. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  139. package/dist/component/financialObservations/transfers.js +79 -0
  140. package/dist/component/financialObservations/transfers.js.map +1 -0
  141. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  142. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  143. package/dist/component/financialOwnership/helpers.js +76 -0
  144. package/dist/component/financialOwnership/helpers.js.map +1 -0
  145. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  146. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  147. package/dist/component/financialOwnership/mutations.js +74 -0
  148. package/dist/component/financialOwnership/mutations.js.map +1 -0
  149. package/dist/component/financialOwnership/queries.d.ts +57 -0
  150. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  151. package/dist/component/financialOwnership/queries.js +50 -0
  152. package/dist/component/financialOwnership/queries.js.map +1 -0
  153. package/dist/component/financialOwnership/schema.d.ts +140 -0
  154. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  155. package/dist/component/financialOwnership/schema.js +35 -0
  156. package/dist/component/financialOwnership/schema.js.map +1 -0
  157. package/dist/component/operationEvidence/canonical.d.ts +4 -0
  158. package/dist/component/operationEvidence/canonical.d.ts.map +1 -0
  159. package/dist/component/operationEvidence/canonical.js +8 -0
  160. package/dist/component/operationEvidence/canonical.js.map +1 -0
  161. package/dist/component/operationEvidence/helpers.d.ts +87 -0
  162. package/dist/component/operationEvidence/helpers.d.ts.map +1 -0
  163. package/dist/component/operationEvidence/helpers.js +49 -0
  164. package/dist/component/operationEvidence/helpers.js.map +1 -0
  165. package/dist/component/operationEvidence/maintenance.d.ts +88 -0
  166. package/dist/component/operationEvidence/maintenance.d.ts.map +1 -0
  167. package/dist/component/operationEvidence/maintenance.js +59 -0
  168. package/dist/component/operationEvidence/maintenance.js.map +1 -0
  169. package/dist/component/operationEvidence/mutations.d.ts +97 -0
  170. package/dist/component/operationEvidence/mutations.d.ts.map +1 -0
  171. package/dist/component/operationEvidence/mutations.js +14 -0
  172. package/dist/component/operationEvidence/mutations.js.map +1 -0
  173. package/dist/component/operationEvidence/queries.d.ts +205 -0
  174. package/dist/component/operationEvidence/queries.d.ts.map +1 -0
  175. package/dist/component/operationEvidence/queries.js +75 -0
  176. package/dist/component/operationEvidence/queries.js.map +1 -0
  177. package/dist/component/operationEvidence/schema.d.ts +536 -0
  178. package/dist/component/operationEvidence/schema.d.ts.map +1 -0
  179. package/dist/component/operationEvidence/schema.js +53 -0
  180. package/dist/component/operationEvidence/schema.js.map +1 -0
  181. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  182. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  183. package/dist/component/refundHistory/commandBinding.js +41 -0
  184. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  185. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  186. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  187. package/dist/component/refundHistory/commandEvidence.js +58 -0
  188. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  189. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  190. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  191. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  192. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  193. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  194. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  195. package/dist/component/refundHistory/commandFields.js +9 -0
  196. package/dist/component/refundHistory/commandFields.js.map +1 -0
  197. package/dist/component/refundHistory/mutations.d.ts +8 -0
  198. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  199. package/dist/component/refundHistory/mutations.js +25 -0
  200. package/dist/component/refundHistory/mutations.js.map +1 -0
  201. package/dist/component/refundHistory/originalSeller.d.ts +5 -0
  202. package/dist/component/refundHistory/originalSeller.d.ts.map +1 -0
  203. package/dist/component/refundHistory/originalSeller.js +35 -0
  204. package/dist/component/refundHistory/originalSeller.js.map +1 -0
  205. package/dist/component/refundHistory/queries.d.ts +51 -0
  206. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  207. package/dist/component/refundHistory/queries.js +14 -0
  208. package/dist/component/refundHistory/queries.js.map +1 -1
  209. package/dist/component/schema.d.ts +332 -1
  210. package/dist/component/schema.js +15 -0
  211. package/dist/component/schema.js.map +1 -1
  212. package/dist/component/settlement/mutations.d.ts.map +1 -1
  213. package/dist/component/settlement/mutations.js +5 -1
  214. package/dist/component/settlement/mutations.js.map +1 -1
  215. package/dist/shared/refundCommandScope.d.ts +10 -0
  216. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  217. package/dist/shared/refundCommandScope.js +9 -0
  218. package/dist/shared/refundCommandScope.js.map +1 -0
  219. package/docs/financial-observations.md +244 -0
  220. package/docs/financial-ownership.md +54 -0
  221. package/docs/refund-command-evidence.md +55 -0
  222. package/package.json +5 -2
  223. package/src/client/connect/refundCommandEvidence.ts +41 -0
  224. package/src/client/connect/refundCommandNormalization.ts +28 -0
  225. package/src/client/financialObservations.test.ts +109 -0
  226. package/src/client/financialObservations.ts +106 -0
  227. package/src/client/financialOwnership.test.ts +80 -0
  228. package/src/client/financialOwnership.ts +116 -0
  229. package/src/client/index.ts +2 -2
  230. package/src/client/observationPagination.ts +11 -0
  231. package/src/client/operationEvidence.test.ts +110 -0
  232. package/src/client/operationEvidence.ts +92 -0
  233. package/src/client/publicTypes.ts +45 -0
  234. package/src/client/readContracts.ts +25 -0
  235. package/src/client/refund-command-evidence.test.ts +68 -0
  236. package/src/client/refund-command-normalization.test.ts +61 -0
  237. package/src/client/transferContinuation.test.ts +94 -0
  238. package/src/client/transferObservations.test.ts +98 -0
  239. package/src/client/transferObservations.ts +180 -0
  240. package/src/component/_generated/api.ts +54 -0
  241. package/src/component/_generated/component.ts +797 -0
  242. package/src/component/affiliateRecovery/mutations.ts +10 -1
  243. package/src/component/connect/mutations.ts +29 -2
  244. package/src/component/connect/reversalClaims.ts +19 -1
  245. package/src/component/connect/reversalReceipts.ts +2 -0
  246. package/src/component/connect/schema.ts +9 -1
  247. package/src/component/connect/validators.ts +2 -0
  248. package/src/component/core/reset.test.ts +8 -1
  249. package/src/component/earnings/earnings-totals.test.ts +14 -10
  250. package/src/component/earnings/paired-adjustment.test.ts +258 -0
  251. package/src/component/earnings/pairedAdjustment.ts +77 -0
  252. package/src/component/earnings/triggers.ts +23 -11
  253. package/src/component/financialObservations/continuation.test.ts +445 -0
  254. package/src/component/financialObservations/continuation.ts +159 -0
  255. package/src/component/financialObservations/generations.ts +53 -0
  256. package/src/component/financialObservations/mutations.ts +75 -0
  257. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  258. package/src/component/financialObservations/queries.ts +246 -0
  259. package/src/component/financialObservations/schema.ts +68 -0
  260. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  261. package/src/component/financialObservations/settlement.ts +142 -0
  262. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  263. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  264. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  265. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  266. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  267. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  268. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  269. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  270. package/src/component/financialObservations/transferBatches.ts +96 -0
  271. package/src/component/financialObservations/transferContinuation.ts +53 -0
  272. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  273. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  274. package/src/component/financialObservations/transferHelpers.ts +38 -0
  275. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  276. package/src/component/financialObservations/transferSchema.ts +74 -0
  277. package/src/component/financialObservations/transfers.ts +99 -0
  278. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  279. package/src/component/financialOwnership/helpers.ts +98 -0
  280. package/src/component/financialOwnership/mutations.ts +83 -0
  281. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  282. package/src/component/financialOwnership/queries.ts +65 -0
  283. package/src/component/financialOwnership/schema.ts +35 -0
  284. package/src/component/operationEvidence/canonical.ts +10 -0
  285. package/src/component/operationEvidence/helpers.ts +65 -0
  286. package/src/component/operationEvidence/integrity.test.ts +257 -0
  287. package/src/component/operationEvidence/maintenance.test.ts +152 -0
  288. package/src/component/operationEvidence/maintenance.ts +89 -0
  289. package/src/component/operationEvidence/mutations.ts +14 -0
  290. package/src/component/operationEvidence/queries.ts +108 -0
  291. package/src/component/operationEvidence/reads.test.ts +140 -0
  292. package/src/component/operationEvidence/schema.ts +62 -0
  293. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  294. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  295. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  296. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  297. package/src/component/refundHistory/commandBinding.ts +54 -0
  298. package/src/component/refundHistory/commandEvidence.ts +76 -0
  299. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  300. package/src/component/refundHistory/commandFields.ts +9 -0
  301. package/src/component/refundHistory/mutations.ts +33 -0
  302. package/src/component/refundHistory/original-seller-boundaries.test.ts +138 -0
  303. package/src/component/refundHistory/original-seller.test.ts +175 -0
  304. package/src/component/refundHistory/originalSeller.ts +48 -0
  305. package/src/component/refundHistory/queries.ts +15 -0
  306. package/src/component/schema.ts +28 -0
  307. package/src/component/settlement/mutations.ts +5 -1
  308. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,175 @@
1
+ import { convexTest } from "convex-test";
2
+ import { expect, it } from "vitest";
3
+ import { api } from "../_generated/api.js";
4
+ import { schema, modules } from "../../test.js";
5
+
6
+ const scope = {
7
+ commandKey: "dojo-refund:original_store_gap:v1",
8
+ accountId: "acct_original_store",
9
+ stripePaymentIntentId: "pi_original_split",
10
+ stripeChargeId: "ch_original_split",
11
+ currency: "usd",
12
+ };
13
+ const refund = {
14
+ stripeRefundId: "re_original_split",
15
+ stripePaymentIntentId: scope.stripePaymentIntentId,
16
+ stripeChargeId: scope.stripeChargeId,
17
+ amount: 50,
18
+ currency: "usd",
19
+ status: "pending" as const,
20
+ metadata: { betterStripeRefundCommand: scope.commandKey },
21
+ };
22
+ const payment = {
23
+ stripePaymentIntentId: scope.stripePaymentIntentId,
24
+ userId: "original_buyer",
25
+ accountId: "acct_original_buyer",
26
+ amount: 1000,
27
+ currency: "usd",
28
+ status: "succeeded" as const,
29
+ chargeType: "separate" as const,
30
+ splitRecipients: [
31
+ {
32
+ destinationAccountId: scope.accountId,
33
+ role: "store" as const,
34
+ amount: 700,
35
+ },
36
+ {
37
+ destinationAccountId: "acct_affiliate",
38
+ role: "affiliate" as const,
39
+ amount: 300,
40
+ },
41
+ ],
42
+ };
43
+ const read = api.refundHistory.queries.getRefundCommandEvidence;
44
+
45
+ const allocation = {
46
+ sourceChargeId: scope.stripeChargeId,
47
+ paymentIntentId: scope.stripePaymentIntentId,
48
+ currency: scope.currency,
49
+ gross: 1000,
50
+ processingFee: 0,
51
+ platformNet: 0,
52
+ transfers: payment.splitRecipients,
53
+ };
54
+ it.each([false, true])(
55
+ "qualifies immutable original separate store, refund first=%s, without rewriting history",
56
+ async (isRefundFirst) => {
57
+ const t = convexTest(schema, modules);
58
+ if (isRefundFirst)
59
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
60
+ await t.mutation(api.connect.mutations.upsertPayment, payment);
61
+ await t.mutation(
62
+ api.settlement.mutations.reserveSettlementAllocation,
63
+ allocation,
64
+ );
65
+ if (!isRefundFirst)
66
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
67
+ const before = await t.run((ctx) => ctx.db.query("refunds").take(2));
68
+ expect(await t.query(read, scope)).toMatchObject({
69
+ status: "observed",
70
+ refund: { ...refund, accountId: scope.accountId },
71
+ });
72
+ expect(await t.run((ctx) => ctx.db.query("refunds").take(2))).toEqual(
73
+ before,
74
+ );
75
+ expect(before[0]?.accountId).toBeUndefined();
76
+ },
77
+ );
78
+ it("keeps original allocation authority after normal SAME-PI store reassignment and reserve replay", async () => {
79
+ const t = convexTest(schema, modules);
80
+ await t.mutation(api.connect.mutations.upsertPayment, payment);
81
+ const original = await t.mutation(
82
+ api.settlement.mutations.reserveSettlementAllocation,
83
+ allocation,
84
+ );
85
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
86
+ const changed = [
87
+ {
88
+ destinationAccountId: "acct_replacement",
89
+ role: "store" as const,
90
+ amount: 700,
91
+ },
92
+ payment.splitRecipients[1]!,
93
+ ];
94
+ await t.mutation(api.connect.mutations.upsertPayment, {
95
+ ...payment,
96
+ splitRecipients: changed,
97
+ });
98
+ expect(
99
+ await t.mutation(api.settlement.mutations.reserveSettlementAllocation, {
100
+ ...allocation,
101
+ transfers: changed,
102
+ }),
103
+ ).toEqual(original);
104
+ expect(await t.query(read, scope)).toMatchObject({
105
+ status: "observed",
106
+ refund: { accountId: scope.accountId },
107
+ });
108
+ expect(
109
+ await t.query(read, { ...scope, accountId: "acct_replacement" }),
110
+ ).toMatchObject({ status: "unavailable" });
111
+ expect(
112
+ await t.query(read, { ...scope, accountId: "acct_affiliate" }),
113
+ ).toMatchObject({ status: "unavailable" });
114
+ });
115
+ it.each([false, true])(
116
+ "holds payment-only ownerless legacy provenance, destination=%s",
117
+ async (isDestination) => {
118
+ const t = convexTest(schema, modules);
119
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
120
+ // Late raw history has no immutable capture allocation and no receipt owner.
121
+ await t.run((ctx) =>
122
+ ctx.db.insert(
123
+ "payments",
124
+ isDestination
125
+ ? {
126
+ ...payment,
127
+ chargeType: "destination",
128
+ splitRecipients: undefined,
129
+ destinationAccountId: scope.accountId,
130
+ }
131
+ : payment,
132
+ ),
133
+ );
134
+ expect(await t.query(read, scope)).toMatchObject({
135
+ status: "unavailable",
136
+ reason: "scope_mismatch",
137
+ });
138
+ },
139
+ );
140
+ it("retains persisted destination-account parity", async () => {
141
+ const t = convexTest(schema, modules);
142
+ await t.mutation(api.connect.mutations.upsertPayment, {
143
+ ...payment,
144
+ chargeType: "destination",
145
+ splitRecipients: [],
146
+ destinationAccountId: scope.accountId,
147
+ });
148
+ await t.mutation(api.connect.mutations.upsertRefund, refund);
149
+ expect(await t.query(read, scope)).toMatchObject({
150
+ status: "observed",
151
+ refund: { accountId: scope.accountId },
152
+ });
153
+ });
154
+ it.each(["acct_foreign", ""])(
155
+ "never hides persisted conflicting account %s",
156
+ async (accountId) => {
157
+ const t = convexTest(schema, modules);
158
+ await t.mutation(
159
+ api.settlement.mutations.reserveSettlementAllocation,
160
+ allocation,
161
+ );
162
+ await t.mutation(api.connect.mutations.upsertRefund, {
163
+ ...refund,
164
+ accountId,
165
+ });
166
+ const before = await t.run((ctx) => ctx.db.query("refunds").take(1));
167
+ expect(await t.query(read, scope)).toMatchObject({
168
+ status: "unavailable",
169
+ reason: "scope_mismatch",
170
+ });
171
+ expect(await t.run((ctx) => ctx.db.query("refunds").take(1))).toEqual(
172
+ before,
173
+ );
174
+ },
175
+ );
@@ -0,0 +1,48 @@
1
+ import type { QueryCtx } from "../_generated/server.js";
2
+ import type { Doc } from "../_generated/dataModel.js";
3
+ import type { RefundCommandScope } from "../../shared/refundCommandScope.js";
4
+
5
+ const account = (value: string) => /^acct_[A-Za-z0-9_]{1,128}$/.test(value);
6
+
7
+ /** Immutable allocation routing only. Observation does not authorize a refund. */
8
+ function seller(allocation: Doc<"settlementAllocations">): string | undefined {
9
+ const destinations = new Set<string>();
10
+ let store: string | undefined;
11
+ for (const recipient of allocation.transfers) {
12
+ if (
13
+ !account(recipient.destinationAccountId) ||
14
+ destinations.has(recipient.destinationAccountId) ||
15
+ !Number.isSafeInteger(recipient.amount) ||
16
+ recipient.amount < 0
17
+ )
18
+ return undefined;
19
+ destinations.add(recipient.destinationAccountId);
20
+ if (recipient.role === "store") {
21
+ if (store !== undefined) return undefined;
22
+ store = recipient.destinationAccountId;
23
+ }
24
+ }
25
+ return store;
26
+ }
27
+
28
+ /** At most two exact-charge allocations; mutable payments never establish original ownership. */
29
+ export async function originalRefundSeller(
30
+ ctx: QueryCtx,
31
+ scope: RefundCommandScope,
32
+ ): Promise<string | undefined> {
33
+ const allocations = await ctx.db
34
+ .query("settlementAllocations")
35
+ .withIndex("by_source_charge", (q) =>
36
+ q.eq("sourceChargeId", scope.stripeChargeId),
37
+ )
38
+ .take(2);
39
+ const allocation = allocations[0];
40
+ if (
41
+ allocations.length !== 1 ||
42
+ !allocation ||
43
+ allocation.paymentIntentId !== scope.stripePaymentIntentId ||
44
+ allocation.currency !== scope.currency
45
+ )
46
+ return undefined;
47
+ return seller(allocation);
48
+ }
@@ -1,3 +1,5 @@
1
+ import { refundCommandEvidenceValidator } from "./commandEvidenceValidator.js";
2
+ import { readRefundCommandEvidence } from "./commandEvidence.js";
1
3
  import {
2
4
  paginationOptsValidator,
3
5
  paginationResultValidator,
@@ -97,3 +99,16 @@ function scopedRefunds(
97
99
  return refunds.withIndex("by_status", (q) => q.eq("status", status));
98
100
  return refunds;
99
101
  }
102
+
103
+ /** Exact command evidence is read-only and never authorizes a provider retry. */
104
+ export const getRefundCommandEvidence = query({
105
+ args: {
106
+ commandKey: v.string(),
107
+ accountId: v.string(),
108
+ stripePaymentIntentId: v.string(),
109
+ stripeChargeId: v.string(),
110
+ currency: v.string(),
111
+ },
112
+ returns: refundCommandEvidenceValidator,
113
+ handler: readRefundCommandEvidence,
114
+ });
@@ -1,3 +1,21 @@
1
+ import {
2
+ payloadsTable as operationEvidencePayloads,
3
+ headersTable as operationEvidenceHeaders,
4
+ } from "./operationEvidence/schema.js";
5
+ import { transferBatchReferencesTable } from "./financialObservations/transferReferenceSchema.js";
6
+ import {
7
+ transferObservationsTable,
8
+ transferBatchesTable,
9
+ } from "./financialObservations/transferSchema.js";
10
+ import {
11
+ sourceObservationsTable,
12
+ sourceLegsTable,
13
+ observationGenerationsTable,
14
+ } from "./financialObservations/schema.js";
15
+ import {
16
+ ownershipTable,
17
+ generationsTable,
18
+ } from "./financialOwnership/schema.js";
1
19
  import { accountClosuresTable } from "./accountClosure/schema.js";
2
20
  import { jobsTable as payoutWorkerJobsTable } from "./payoutWorker/schema.js";
3
21
  import {
@@ -60,6 +78,16 @@ import { pricesTable, productsTable } from "./products/schema";
60
78
  import { webhookEventsTable } from "./webhooks/schema";
61
79
 
62
80
  export default defineSchema({
81
+ operationEvidencePayloads,
82
+ operationEvidenceHeaders,
83
+ financialTransferObservations: transferObservationsTable,
84
+ financialTransferBatches: transferBatchesTable,
85
+ financialTransferBatchReferences: transferBatchReferencesTable,
86
+ financialSourceObservations: sourceObservationsTable,
87
+ financialSourceLegs: sourceLegsTable,
88
+ financialObservationGenerations: observationGenerationsTable,
89
+ financialOwnership: ownershipTable,
90
+ financialOwnershipGenerations: generationsTable,
63
91
  payoutWorkerJobs: payoutWorkerJobsTable,
64
92
  payoutFinancialRevisions: financialRevisionsTable,
65
93
  payoutRiskEvents: riskEventsTable,
@@ -1,3 +1,4 @@
1
+ import { observeSettlementAllocation } from "../financialObservations/settlement.js";
1
2
  import { paginator } from "convex-helpers/server/pagination";
2
3
  import schema from "../schema.js";
3
4
  import { updateObligation } from "../affiliateRecovery/accounting.js";
@@ -28,6 +29,7 @@ export const reserveSettlementAllocation = mutation({
28
29
  existing.sourceInvoiceId !== args.sourceInvoiceId
29
30
  )
30
31
  throw new Error("Settlement source identity cannot change.");
32
+ await observeSettlementAllocation(ctx, existing);
31
33
  return existing;
32
34
  }
33
35
  const adjustment = args.adjustmentAmount ?? 0;
@@ -59,7 +61,9 @@ export const reserveSettlementAllocation = mutation({
59
61
  "Settlement allocation must reconcile in integer currency units with unique recipient legs.",
60
62
  );
61
63
  const id = await ctx.db.insert("settlementAllocations", args);
62
- return (await ctx.db.get("settlementAllocations", id))!;
64
+ const stored = (await ctx.db.get("settlementAllocations", id))!;
65
+ await observeSettlementAllocation(ctx, stored);
66
+ return stored;
63
67
  },
64
68
  });
65
69
  export const claimCommissionInvoice = mutation({
@@ -0,0 +1,19 @@
1
+ /** Canonical server-owned selectors. Shape validation is not host authorization or currency support. */
2
+ export type RefundCommandScope = {
3
+ commandKey: string;
4
+ accountId: string;
5
+ stripePaymentIntentId: string;
6
+ stripeChargeId: string;
7
+ currency: string;
8
+ };
9
+
10
+ export function assertRefundCommandScope(input: RefundCommandScope): void {
11
+ if (
12
+ !/^[A-Za-z0-9:_-]{16,200}$/.test(input.commandKey) ||
13
+ !/^acct_[A-Za-z0-9_]{1,128}$/.test(input.accountId) ||
14
+ !/^pi_[A-Za-z0-9_]{1,252}$/.test(input.stripePaymentIntentId) ||
15
+ !/^ch_[A-Za-z0-9_]{1,252}$/.test(input.stripeChargeId) ||
16
+ !/^[a-z]{3}$/.test(input.currency)
17
+ )
18
+ throw new Error("Invalid refund command selectors");
19
+ }