@fantastic.dev/better-stripe 0.33.0 → 0.35.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 (314) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +30 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +522 -2
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +261 -1
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +522 -2
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +261 -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/core/accountClosures.d.ts +15 -0
  20. package/dist/client/core/accountClosures.d.ts.map +1 -0
  21. package/dist/client/core/accountClosures.js +10 -0
  22. package/dist/client/core/accountClosures.js.map +1 -0
  23. package/dist/client/core/accounts.d.ts.map +1 -1
  24. package/dist/client/core/accounts.js +15 -23
  25. package/dist/client/core/accounts.js.map +1 -1
  26. package/dist/client/core/personalAccountMapping.d.ts +8 -0
  27. package/dist/client/core/personalAccountMapping.d.ts.map +1 -0
  28. package/dist/client/core/personalAccountMapping.js +11 -0
  29. package/dist/client/core/personalAccountMapping.js.map +1 -0
  30. package/dist/client/financialObservations.d.ts +42 -0
  31. package/dist/client/financialObservations.d.ts.map +1 -0
  32. package/dist/client/financialObservations.js +58 -0
  33. package/dist/client/financialObservations.js.map +1 -0
  34. package/dist/client/financialOwnership.d.ts +52 -0
  35. package/dist/client/financialOwnership.d.ts.map +1 -0
  36. package/dist/client/financialOwnership.js +72 -0
  37. package/dist/client/financialOwnership.js.map +1 -0
  38. package/dist/client/index.d.ts +57 -4
  39. package/dist/client/index.d.ts.map +1 -1
  40. package/dist/client/index.js +15 -2
  41. package/dist/client/index.js.map +1 -1
  42. package/dist/client/observationPagination.d.ts +10 -0
  43. package/dist/client/observationPagination.d.ts.map +1 -0
  44. package/dist/client/observationPagination.js +12 -0
  45. package/dist/client/observationPagination.js.map +1 -0
  46. package/dist/client/publicTypes.d.ts +10 -0
  47. package/dist/client/publicTypes.d.ts.map +1 -0
  48. package/dist/client/publicTypes.js +2 -0
  49. package/dist/client/publicTypes.js.map +1 -0
  50. package/dist/client/readContracts.d.ts +29 -0
  51. package/dist/client/readContracts.d.ts.map +1 -1
  52. package/dist/client/readContracts.js +10 -0
  53. package/dist/client/readContracts.js.map +1 -1
  54. package/dist/client/transferObservations.d.ts +59 -0
  55. package/dist/client/transferObservations.d.ts.map +1 -0
  56. package/dist/client/transferObservations.js +82 -0
  57. package/dist/client/transferObservations.js.map +1 -0
  58. package/dist/component/_generated/component.d.ts +594 -0
  59. package/dist/component/_generated/component.d.ts.map +1 -1
  60. package/dist/component/accountClosure/helpers.d.ts +18 -0
  61. package/dist/component/accountClosure/helpers.d.ts.map +1 -0
  62. package/dist/component/accountClosure/helpers.js +18 -0
  63. package/dist/component/accountClosure/helpers.js.map +1 -0
  64. package/dist/component/accountClosure/mutations.d.ts +20 -0
  65. package/dist/component/accountClosure/mutations.d.ts.map +1 -0
  66. package/dist/component/accountClosure/mutations.js +55 -0
  67. package/dist/component/accountClosure/mutations.js.map +1 -0
  68. package/dist/component/accountClosure/queries.d.ts +34 -0
  69. package/dist/component/accountClosure/queries.d.ts.map +1 -0
  70. package/dist/component/accountClosure/queries.js +36 -0
  71. package/dist/component/accountClosure/queries.js.map +1 -0
  72. package/dist/component/accountClosure/schema.d.ts +46 -0
  73. package/dist/component/accountClosure/schema.d.ts.map +1 -0
  74. package/dist/component/accountClosure/schema.js +20 -0
  75. package/dist/component/accountClosure/schema.js.map +1 -0
  76. package/dist/component/affiliateRecovery/mutations.d.ts.map +1 -1
  77. package/dist/component/affiliateRecovery/mutations.js +4 -1
  78. package/dist/component/affiliateRecovery/mutations.js.map +1 -1
  79. package/dist/component/connect/mutations.d.ts.map +1 -1
  80. package/dist/component/connect/mutations.js +18 -2
  81. package/dist/component/connect/mutations.js.map +1 -1
  82. package/dist/component/connect/queries.d.ts +8 -0
  83. package/dist/component/connect/queries.d.ts.map +1 -1
  84. package/dist/component/connect/reversalClaims.d.ts.map +1 -1
  85. package/dist/component/connect/reversalClaims.js +7 -1
  86. package/dist/component/connect/reversalClaims.js.map +1 -1
  87. package/dist/component/connect/reversalReceipts.d.ts.map +1 -1
  88. package/dist/component/connect/reversalReceipts.js +2 -0
  89. package/dist/component/connect/reversalReceipts.js.map +1 -1
  90. package/dist/component/connect/schema.d.ts +11 -1
  91. package/dist/component/connect/schema.d.ts.map +1 -1
  92. package/dist/component/connect/schema.js +9 -1
  93. package/dist/component/connect/schema.js.map +1 -1
  94. package/dist/component/connect/validators.d.ts +9 -1
  95. package/dist/component/connect/validators.d.ts.map +1 -1
  96. package/dist/component/connect/validators.js +2 -0
  97. package/dist/component/connect/validators.js.map +1 -1
  98. package/dist/component/core/mutations.d.ts.map +1 -1
  99. package/dist/component/core/mutations.js +5 -0
  100. package/dist/component/core/mutations.js.map +1 -1
  101. package/dist/component/core/personalAccountMapping.d.ts +95 -0
  102. package/dist/component/core/personalAccountMapping.d.ts.map +1 -0
  103. package/dist/component/core/personalAccountMapping.js +54 -0
  104. package/dist/component/core/personalAccountMapping.js.map +1 -0
  105. package/dist/component/core/queries.d.ts +27 -0
  106. package/dist/component/core/queries.d.ts.map +1 -1
  107. package/dist/component/core/queries.js +7 -0
  108. package/dist/component/core/queries.js.map +1 -1
  109. package/dist/component/earnings/triggers.d.ts +468 -2
  110. package/dist/component/earnings/triggers.d.ts.map +1 -1
  111. package/dist/component/earnings/triggers.js +7 -1
  112. package/dist/component/earnings/triggers.js.map +1 -1
  113. package/dist/component/financialObservations/continuation.d.ts +7 -0
  114. package/dist/component/financialObservations/continuation.d.ts.map +1 -0
  115. package/dist/component/financialObservations/continuation.js +131 -0
  116. package/dist/component/financialObservations/continuation.js.map +1 -0
  117. package/dist/component/financialObservations/generations.d.ts +6 -0
  118. package/dist/component/financialObservations/generations.d.ts.map +1 -0
  119. package/dist/component/financialObservations/generations.js +46 -0
  120. package/dist/component/financialObservations/generations.js.map +1 -0
  121. package/dist/component/financialObservations/mutations.d.ts +62 -0
  122. package/dist/component/financialObservations/mutations.d.ts.map +1 -0
  123. package/dist/component/financialObservations/mutations.js +69 -0
  124. package/dist/component/financialObservations/mutations.js.map +1 -0
  125. package/dist/component/financialObservations/queries.d.ts +307 -0
  126. package/dist/component/financialObservations/queries.d.ts.map +1 -0
  127. package/dist/component/financialObservations/queries.js +219 -0
  128. package/dist/component/financialObservations/queries.js.map +1 -0
  129. package/dist/component/financialObservations/schema.d.ts +182 -0
  130. package/dist/component/financialObservations/schema.d.ts.map +1 -0
  131. package/dist/component/financialObservations/schema.js +53 -0
  132. package/dist/component/financialObservations/schema.js.map +1 -0
  133. package/dist/component/financialObservations/settlement.d.ts +7 -0
  134. package/dist/component/financialObservations/settlement.d.ts.map +1 -0
  135. package/dist/component/financialObservations/settlement.js +118 -0
  136. package/dist/component/financialObservations/settlement.js.map +1 -0
  137. package/dist/component/financialObservations/settlementHelpers.d.ts +46 -0
  138. package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -0
  139. package/dist/component/financialObservations/settlementHelpers.js +70 -0
  140. package/dist/component/financialObservations/settlementHelpers.js.map +1 -0
  141. package/dist/component/financialObservations/transferBatches.d.ts +12 -0
  142. package/dist/component/financialObservations/transferBatches.d.ts.map +1 -0
  143. package/dist/component/financialObservations/transferBatches.js +76 -0
  144. package/dist/component/financialObservations/transferBatches.js.map +1 -0
  145. package/dist/component/financialObservations/transferContinuation.d.ts +5 -0
  146. package/dist/component/financialObservations/transferContinuation.d.ts.map +1 -0
  147. package/dist/component/financialObservations/transferContinuation.js +35 -0
  148. package/dist/component/financialObservations/transferContinuation.js.map +1 -0
  149. package/dist/component/financialObservations/transferContinuationState.d.ts +8 -0
  150. package/dist/component/financialObservations/transferContinuationState.d.ts.map +1 -0
  151. package/dist/component/financialObservations/transferContinuationState.js +69 -0
  152. package/dist/component/financialObservations/transferContinuationState.js.map +1 -0
  153. package/dist/component/financialObservations/transferContinuationSteps.d.ts +5 -0
  154. package/dist/component/financialObservations/transferContinuationSteps.d.ts.map +1 -0
  155. package/dist/component/financialObservations/transferContinuationSteps.js +174 -0
  156. package/dist/component/financialObservations/transferContinuationSteps.js.map +1 -0
  157. package/dist/component/financialObservations/transferHelpers.d.ts +18 -0
  158. package/dist/component/financialObservations/transferHelpers.d.ts.map +1 -0
  159. package/dist/component/financialObservations/transferHelpers.js +37 -0
  160. package/dist/component/financialObservations/transferHelpers.js.map +1 -0
  161. package/dist/component/financialObservations/transferReferenceSchema.d.ts +56 -0
  162. package/dist/component/financialObservations/transferReferenceSchema.d.ts.map +1 -0
  163. package/dist/component/financialObservations/transferReferenceSchema.js +21 -0
  164. package/dist/component/financialObservations/transferReferenceSchema.js.map +1 -0
  165. package/dist/component/financialObservations/transferSchema.d.ts +237 -0
  166. package/dist/component/financialObservations/transferSchema.d.ts.map +1 -0
  167. package/dist/component/financialObservations/transferSchema.js +65 -0
  168. package/dist/component/financialObservations/transferSchema.js.map +1 -0
  169. package/dist/component/financialObservations/transfers.d.ts +7 -0
  170. package/dist/component/financialObservations/transfers.d.ts.map +1 -0
  171. package/dist/component/financialObservations/transfers.js +79 -0
  172. package/dist/component/financialObservations/transfers.js.map +1 -0
  173. package/dist/component/financialOwnership/helpers.d.ts +37 -0
  174. package/dist/component/financialOwnership/helpers.d.ts.map +1 -0
  175. package/dist/component/financialOwnership/helpers.js +76 -0
  176. package/dist/component/financialOwnership/helpers.js.map +1 -0
  177. package/dist/component/financialOwnership/mutations.d.ts +28 -0
  178. package/dist/component/financialOwnership/mutations.d.ts.map +1 -0
  179. package/dist/component/financialOwnership/mutations.js +74 -0
  180. package/dist/component/financialOwnership/mutations.js.map +1 -0
  181. package/dist/component/financialOwnership/queries.d.ts +57 -0
  182. package/dist/component/financialOwnership/queries.d.ts.map +1 -0
  183. package/dist/component/financialOwnership/queries.js +50 -0
  184. package/dist/component/financialOwnership/queries.js.map +1 -0
  185. package/dist/component/financialOwnership/schema.d.ts +140 -0
  186. package/dist/component/financialOwnership/schema.d.ts.map +1 -0
  187. package/dist/component/financialOwnership/schema.js +35 -0
  188. package/dist/component/financialOwnership/schema.js.map +1 -0
  189. package/dist/component/payoutRelease/queries.d.ts +2 -2
  190. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  191. package/dist/component/refundHistory/commandBinding.d.ts +17 -0
  192. package/dist/component/refundHistory/commandBinding.d.ts.map +1 -0
  193. package/dist/component/refundHistory/commandBinding.js +41 -0
  194. package/dist/component/refundHistory/commandBinding.js.map +1 -0
  195. package/dist/component/refundHistory/commandEvidence.d.ts +50 -0
  196. package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -0
  197. package/dist/component/refundHistory/commandEvidence.js +54 -0
  198. package/dist/component/refundHistory/commandEvidence.js.map +1 -0
  199. package/dist/component/refundHistory/commandEvidenceValidator.d.ts +89 -0
  200. package/dist/component/refundHistory/commandEvidenceValidator.d.ts.map +1 -0
  201. package/dist/component/refundHistory/commandEvidenceValidator.js +8 -0
  202. package/dist/component/refundHistory/commandEvidenceValidator.js.map +1 -0
  203. package/dist/component/refundHistory/commandFields.d.ts +8 -0
  204. package/dist/component/refundHistory/commandFields.d.ts.map +1 -0
  205. package/dist/component/refundHistory/commandFields.js +9 -0
  206. package/dist/component/refundHistory/commandFields.js.map +1 -0
  207. package/dist/component/refundHistory/mutations.d.ts +8 -0
  208. package/dist/component/refundHistory/mutations.d.ts.map +1 -0
  209. package/dist/component/refundHistory/mutations.js +25 -0
  210. package/dist/component/refundHistory/mutations.js.map +1 -0
  211. package/dist/component/refundHistory/queries.d.ts +51 -0
  212. package/dist/component/refundHistory/queries.d.ts.map +1 -1
  213. package/dist/component/refundHistory/queries.js +14 -0
  214. package/dist/component/refundHistory/queries.js.map +1 -1
  215. package/dist/component/schema.d.ts +261 -1
  216. package/dist/component/schema.js +14 -0
  217. package/dist/component/schema.js.map +1 -1
  218. package/dist/component/settlement/mutations.d.ts.map +1 -1
  219. package/dist/component/settlement/mutations.js +5 -1
  220. package/dist/component/settlement/mutations.js.map +1 -1
  221. package/dist/shared/refundCommandScope.d.ts +10 -0
  222. package/dist/shared/refundCommandScope.d.ts.map +1 -0
  223. package/dist/shared/refundCommandScope.js +9 -0
  224. package/dist/shared/refundCommandScope.js.map +1 -0
  225. package/docs/financial-observations.md +159 -0
  226. package/docs/financial-ownership.md +54 -0
  227. package/docs/personal-account-mapping.md +54 -0
  228. package/docs/refund-command-evidence.md +49 -0
  229. package/package.json +6 -2
  230. package/src/client/connect/refundCommandEvidence.ts +41 -0
  231. package/src/client/connect/refundCommandNormalization.ts +28 -0
  232. package/src/client/core/account-close-orchestration.test.ts +131 -0
  233. package/src/client/core/account-closure.test.ts +317 -0
  234. package/src/client/core/accountClosures.ts +37 -0
  235. package/src/client/core/accounts.test.ts +0 -111
  236. package/src/client/core/accounts.ts +24 -29
  237. package/src/client/core/personal-account-mapping.test.ts +71 -0
  238. package/src/client/core/personalAccountMapping.ts +23 -0
  239. package/src/client/financialObservations.test.ts +109 -0
  240. package/src/client/financialObservations.ts +106 -0
  241. package/src/client/financialOwnership.test.ts +80 -0
  242. package/src/client/financialOwnership.ts +116 -0
  243. package/src/client/index.ts +38 -35
  244. package/src/client/observationPagination.ts +11 -0
  245. package/src/client/publicTypes.ts +72 -0
  246. package/src/client/readContracts.ts +21 -0
  247. package/src/client/refund-command-evidence.test.ts +68 -0
  248. package/src/client/refund-command-normalization.test.ts +61 -0
  249. package/src/client/transferContinuation.test.ts +94 -0
  250. package/src/client/transferObservations.test.ts +98 -0
  251. package/src/client/transferObservations.ts +180 -0
  252. package/src/component/_generated/api.ts +52 -0
  253. package/src/component/_generated/component.ts +716 -0
  254. package/src/component/accountClosure/helpers.ts +21 -0
  255. package/src/component/accountClosure/mutations.ts +56 -0
  256. package/src/component/accountClosure/queries.ts +43 -0
  257. package/src/component/accountClosure/schema.ts +20 -0
  258. package/src/component/affiliateRecovery/mutations.ts +10 -1
  259. package/src/component/connect/mutations.ts +29 -2
  260. package/src/component/connect/reversalClaims.ts +19 -1
  261. package/src/component/connect/reversalReceipts.ts +2 -0
  262. package/src/component/connect/schema.ts +9 -1
  263. package/src/component/connect/validators.ts +2 -0
  264. package/src/component/core/mutations.ts +3 -0
  265. package/src/component/core/personal-account-mapping.test.ts +173 -0
  266. package/src/component/core/personalAccountMapping.ts +81 -0
  267. package/src/component/core/queries.ts +11 -0
  268. package/src/component/core/reset.test.ts +8 -1
  269. package/src/component/earnings/earnings-totals.test.ts +14 -10
  270. package/src/component/earnings/triggers.ts +9 -1
  271. package/src/component/financialObservations/continuation.test.ts +445 -0
  272. package/src/component/financialObservations/continuation.ts +159 -0
  273. package/src/component/financialObservations/generations.ts +53 -0
  274. package/src/component/financialObservations/mutations.ts +75 -0
  275. package/src/component/financialObservations/observation-boundaries.test.ts +466 -0
  276. package/src/component/financialObservations/queries.ts +246 -0
  277. package/src/component/financialObservations/schema.ts +68 -0
  278. package/src/component/financialObservations/settlement-observations.test.ts +86 -0
  279. package/src/component/financialObservations/settlement.ts +142 -0
  280. package/src/component/financialObservations/settlementHelpers.ts +92 -0
  281. package/src/component/financialObservations/transfer-batches.test.ts +61 -0
  282. package/src/component/financialObservations/transfer-boundaries.test.ts +116 -0
  283. package/src/component/financialObservations/transfer-continuation-guards.test.ts +428 -0
  284. package/src/component/financialObservations/transfer-continuation.test.ts +154 -0
  285. package/src/component/financialObservations/transfer-observations.test.ts +212 -0
  286. package/src/component/financialObservations/transfer-overflow.test.ts +181 -0
  287. package/src/component/financialObservations/transfer-writers.test.ts +162 -0
  288. package/src/component/financialObservations/transferBatches.ts +96 -0
  289. package/src/component/financialObservations/transferContinuation.ts +53 -0
  290. package/src/component/financialObservations/transferContinuationState.ts +85 -0
  291. package/src/component/financialObservations/transferContinuationSteps.ts +232 -0
  292. package/src/component/financialObservations/transferHelpers.ts +38 -0
  293. package/src/component/financialObservations/transferReferenceSchema.ts +20 -0
  294. package/src/component/financialObservations/transferSchema.ts +74 -0
  295. package/src/component/financialObservations/transfers.ts +99 -0
  296. package/src/component/financialOwnership/financial-ownership.test.ts +102 -0
  297. package/src/component/financialOwnership/helpers.ts +98 -0
  298. package/src/component/financialOwnership/mutations.ts +83 -0
  299. package/src/component/financialOwnership/ownership-boundaries.test.ts +308 -0
  300. package/src/component/financialOwnership/queries.ts +65 -0
  301. package/src/component/financialOwnership/schema.ts +35 -0
  302. package/src/component/refundHistory/command-evidence.test.ts +288 -0
  303. package/src/component/refundHistory/command-normalization-resource.test.ts +77 -0
  304. package/src/component/refundHistory/command-normalization.test.ts +229 -0
  305. package/src/component/refundHistory/command-provenance.test.ts +59 -0
  306. package/src/component/refundHistory/commandBinding.ts +54 -0
  307. package/src/component/refundHistory/commandEvidence.ts +71 -0
  308. package/src/component/refundHistory/commandEvidenceValidator.ts +18 -0
  309. package/src/component/refundHistory/commandFields.ts +9 -0
  310. package/src/component/refundHistory/mutations.ts +33 -0
  311. package/src/component/refundHistory/queries.ts +15 -0
  312. package/src/component/schema.ts +24 -0
  313. package/src/component/settlement/mutations.ts +5 -1
  314. package/src/shared/refundCommandScope.ts +19 -0
@@ -0,0 +1,317 @@
1
+ // @vitest-environment edge-runtime
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import type Stripe from "stripe";
4
+ import type { RunCtx } from "../helpers.js";
5
+ import { components, initConvexTest } from "../setup.test.js";
6
+ import schema from "../../component/schema.js";
7
+ import { closeAccount } from "./accounts.js";
8
+ import { BetterStripe } from "../index.js";
9
+
10
+ const modules = import.meta.glob("../../component/**/*.ts");
11
+ function fixture() {
12
+ const t = initConvexTest();
13
+ t.registerComponent("betterStripe", schema, modules);
14
+ const ctx = { runQuery: t.query, runMutation: t.mutation } as RunCtx;
15
+ const close = vi.fn().mockResolvedValue({ closed: true });
16
+ const retrieve = vi.fn().mockResolvedValue({ closed: true });
17
+ const stripe = {
18
+ v2: { core: { accounts: { close, retrieve } } },
19
+ } as unknown as Stripe;
20
+ const client = new BetterStripe(components.betterStripe, {
21
+ STRIPE_SECRET_KEY: "sk_test_bts202_dummy",
22
+ });
23
+ return {
24
+ client,
25
+ t,
26
+ ctx,
27
+ stripe,
28
+ close,
29
+ retrieve,
30
+ component: components.betterStripe,
31
+ };
32
+ }
33
+
34
+ describe("account closure provenance", () => {
35
+ it("does not resurrect a closed recipient on a late account upsert", async () => {
36
+ const f = fixture();
37
+ const account = {
38
+ stripeAccountId: "acct_closed",
39
+ userId: "original",
40
+ appliedConfigurations: ["recipient" as const],
41
+ };
42
+ await f.t.mutation(f.component.core.mutations.upsertAccount, account);
43
+ await closeAccount(f.stripe, f.component, f.ctx, {
44
+ stripeAccountId: account.stripeAccountId,
45
+ });
46
+ await f.t.mutation(f.component.core.mutations.upsertAccountInternal, {
47
+ ...account,
48
+ userId: "replacement",
49
+ onboardingStatus: "complete",
50
+ });
51
+ await f.t.mutation(f.component.core.mutations.upsertAccount, account);
52
+ expect(
53
+ await f.t.query(f.component.core.queries.getAccountByUserId, {
54
+ userId: "original",
55
+ }),
56
+ ).toBeNull();
57
+ expect(
58
+ await f.t.query(f.component.core.queries.getAccountByUserId, {
59
+ userId: "replacement",
60
+ }),
61
+ ).toBeNull();
62
+ expect(
63
+ await f.t.query(f.component.core.queries.getAccountByStripeId, {
64
+ stripeAccountId: account.stripeAccountId,
65
+ }),
66
+ ).toBeNull();
67
+ });
68
+ it("retains original owner history while active lookups move to another account", async () => {
69
+ const f = fixture();
70
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
71
+ stripeAccountId: "acct_old",
72
+ userId: "original",
73
+ appliedConfigurations: ["recipient"],
74
+ });
75
+ await closeAccount(f.stripe, f.component, f.ctx, {
76
+ stripeAccountId: "acct_old",
77
+ });
78
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
79
+ stripeAccountId: "acct_new",
80
+ userId: "original",
81
+ });
82
+ const history = await f.client.listClosedAccountsByUserId(f.ctx, {
83
+ userId: "original",
84
+ paginationOpts: { numItems: 1, cursor: null },
85
+ });
86
+ expect(history.page.map((row) => row.stripeAccountId)).toEqual([
87
+ "acct_old",
88
+ ]);
89
+ expect(history.page[0].userId).toBe("original");
90
+ expect(history.page[0].closedAt).toBeTypeOf("number");
91
+ expect(
92
+ (
93
+ await f.t.query(f.component.core.queries.getAccountByUserId, {
94
+ userId: "original",
95
+ })
96
+ )?.stripeAccountId,
97
+ ).toBe("acct_new");
98
+ expect(
99
+ (
100
+ await f.t.query(
101
+ f.component.accountClosure.queries.listClosedAccountsByUserId,
102
+ {
103
+ userId: "replacement",
104
+ paginationOpts: { numItems: 1, cursor: null },
105
+ },
106
+ )
107
+ ).page,
108
+ ).toEqual([]);
109
+ });
110
+
111
+ it("keeps the live mapping after provider failure and pins its owner during retry", async () => {
112
+ const f = fixture();
113
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
114
+ stripeAccountId: "acct_fail",
115
+ userId: "original",
116
+ appliedConfigurations: [],
117
+ });
118
+ f.close.mockRejectedValueOnce(new Error("provider refused"));
119
+ await expect(
120
+ closeAccount(f.stripe, f.component, f.ctx, {
121
+ stripeAccountId: "acct_fail",
122
+ }),
123
+ ).rejects.toThrow("provider refused");
124
+ expect(
125
+ (
126
+ await f.t.query(f.component.core.queries.getAccountByUserId, {
127
+ userId: "original",
128
+ })
129
+ )?.stripeAccountId,
130
+ ).toBe("acct_fail");
131
+ expect(
132
+ await f.t.query(
133
+ f.component.accountClosure.queries.getClosedAccountByStripeId,
134
+ { stripeAccountId: "acct_fail" },
135
+ ),
136
+ ).toBeNull();
137
+ await expect(
138
+ f.t.mutation(f.component.core.mutations.upsertAccount, {
139
+ stripeAccountId: "acct_fail",
140
+ userId: "replacement",
141
+ }),
142
+ ).rejects.toThrow("ownership is pinned");
143
+ f.retrieve.mockResolvedValueOnce({
144
+ closed: false,
145
+ applied_configurations: [],
146
+ });
147
+ await closeAccount(f.stripe, f.component, f.ctx, {
148
+ stripeAccountId: "acct_fail",
149
+ });
150
+ expect(f.close).toHaveBeenCalledTimes(2);
151
+ expect(f.close.mock.calls[0][2]).toEqual(f.close.mock.calls[1][2]);
152
+ expect(
153
+ (
154
+ await f.t.query(
155
+ f.component.accountClosure.queries.getClosedAccountByStripeId,
156
+ { stripeAccountId: "acct_fail" },
157
+ )
158
+ )?.userId,
159
+ ).toBe("original");
160
+ });
161
+
162
+ it("recovers provider success followed by local completion failure without another close", async () => {
163
+ const f = fixture();
164
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
165
+ stripeAccountId: "acct_retry",
166
+ userId: "original",
167
+ appliedConfigurations: [],
168
+ });
169
+ const runMutation = vi
170
+ .fn(f.t.mutation)
171
+ .mockImplementationOnce(f.t.mutation)
172
+ .mockRejectedValueOnce(new Error("local completion unavailable"));
173
+ const ctx = { ...f.ctx, runMutation } as RunCtx;
174
+ await expect(
175
+ closeAccount(f.stripe, f.component, ctx, {
176
+ stripeAccountId: "acct_retry",
177
+ }),
178
+ ).rejects.toThrow("local completion unavailable");
179
+ await closeAccount(f.stripe, f.component, f.ctx, {
180
+ stripeAccountId: "acct_retry",
181
+ });
182
+ await closeAccount(f.stripe, f.component, f.ctx, {
183
+ stripeAccountId: "acct_retry",
184
+ });
185
+ expect(f.close).toHaveBeenCalledTimes(1);
186
+ expect(f.close.mock.calls[0][2]).toEqual({
187
+ idempotencyKey: "betterstripe-close:acct_retry",
188
+ });
189
+ expect(
190
+ (
191
+ await f.t.query(
192
+ f.component.accountClosure.queries.getClosedAccountByStripeId,
193
+ { stripeAccountId: "acct_retry" },
194
+ )
195
+ )?.userId,
196
+ ).toBe("original");
197
+ });
198
+
199
+ it("does not disclose organization history through personal owner discovery", async () => {
200
+ const f = fixture();
201
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
202
+ stripeAccountId: "acct_org",
203
+ userId: "original",
204
+ orgId: "org_private",
205
+ appliedConfigurations: [],
206
+ });
207
+ await closeAccount(f.stripe, f.component, f.ctx, {
208
+ stripeAccountId: "acct_org",
209
+ });
210
+ expect(
211
+ (
212
+ await f.t.query(
213
+ f.component.accountClosure.queries.listClosedAccountsByUserId,
214
+ {
215
+ userId: "original",
216
+ paginationOpts: { numItems: 10, cursor: null },
217
+ },
218
+ )
219
+ ).page,
220
+ ).toEqual([]);
221
+ expect(
222
+ (
223
+ await f.t.query(
224
+ f.component.accountClosure.queries.getClosedAccountByStripeId,
225
+ { stripeAccountId: "acct_org" },
226
+ )
227
+ )?.orgId,
228
+ ).toBe("org_private");
229
+ });
230
+ it("preserves payout rows and original account references for historical reads", async () => {
231
+ const f = fixture();
232
+ const accountId = "acct_financial_history";
233
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
234
+ stripeAccountId: accountId,
235
+ userId: "original",
236
+ appliedConfigurations: [],
237
+ });
238
+ await f.t.mutation(f.component.connect.mutations.upsertPayout, {
239
+ stripePayoutId: "po_retained",
240
+ accountId,
241
+ amount: 1200,
242
+ currency: "usd",
243
+ status: "paid",
244
+ });
245
+ const original = await f.t.query(
246
+ f.component.connect.queries.getPayoutByStripeId,
247
+ { stripePayoutId: "po_retained" },
248
+ );
249
+ await closeAccount(f.stripe, f.component, f.ctx, {
250
+ stripeAccountId: accountId,
251
+ });
252
+ const owner = await f.client.getClosedAccountByStripeId(f.ctx, {
253
+ stripeAccountId: accountId,
254
+ });
255
+ expect(owner?.userId).toBe("original");
256
+ expect(owner?.orgId).toBeUndefined();
257
+ expect(
258
+ await f.t.query(f.component.connect.queries.listPayouts, {
259
+ accountId: owner!.stripeAccountId,
260
+ }),
261
+ ).toEqual([original]);
262
+ expect(
263
+ await f.t.query(f.component.connect.queries.getPayoutByStripeId, {
264
+ stripePayoutId: "po_retained",
265
+ }),
266
+ ).toEqual(original);
267
+ });
268
+
269
+ it("paginates personal closed history without unrelated users or organization rows", async () => {
270
+ const f = fixture();
271
+ for (const [stripeAccountId, userId, orgId] of [
272
+ ["acct_p1", "original", undefined],
273
+ ["acct_org_private", "original", "org_private"],
274
+ ["acct_p2", "original", undefined],
275
+ ["acct_other", "other", undefined],
276
+ ]) {
277
+ await f.t.mutation(f.component.core.mutations.upsertAccount, {
278
+ stripeAccountId: stripeAccountId!,
279
+ userId: userId!,
280
+ orgId,
281
+ appliedConfigurations: [],
282
+ });
283
+ await closeAccount(f.stripe, f.component, f.ctx, {
284
+ stripeAccountId: stripeAccountId!,
285
+ });
286
+ }
287
+ const first = await f.t.query(
288
+ f.component.accountClosure.queries.listClosedAccountsByUserId,
289
+ { userId: "original", paginationOpts: { numItems: 1, cursor: null } },
290
+ );
291
+ const second = await f.t.query(
292
+ f.component.accountClosure.queries.listClosedAccountsByUserId,
293
+ {
294
+ userId: "original",
295
+ paginationOpts: { numItems: 1, cursor: first.continueCursor },
296
+ },
297
+ );
298
+ expect(
299
+ [...first.page, ...second.page].map((row) => row.stripeAccountId).sort(),
300
+ ).toEqual(["acct_p1", "acct_p2"]);
301
+ expect(first.isDone).toBe(false);
302
+ const end = await f.client.listClosedAccountsByUserId(f.ctx, {
303
+ userId: "original",
304
+ paginationOpts: { numItems: 1, cursor: second.continueCursor },
305
+ });
306
+ expect(end.page).toEqual([]);
307
+ expect(end.isDone).toBe(true);
308
+ for (const numItems of [0, 101, 1.5]) {
309
+ await expect(
310
+ f.client.listClosedAccountsByUserId(f.ctx, {
311
+ userId: "original",
312
+ paginationOpts: { numItems, cursor: null },
313
+ }),
314
+ ).rejects.toThrow("between 1 and 100");
315
+ }
316
+ });
317
+ });
@@ -0,0 +1,37 @@
1
+ import type { Infer } from "convex/values";
2
+ import type { PaginationOptions, PaginationResult } from "convex/server";
3
+ import type { closureDoc } from "../../component/accountClosure/schema.js";
4
+ import type { Component, RunCtx } from "../helpers.js";
5
+ import { componentRef } from "../webhooks/helpers.js";
6
+
7
+ export type StripeAccountClosure = Infer<typeof closureDoc>;
8
+
9
+ /** Host must authenticate the actor and compare both user and organization ownership. */
10
+ export function getClosedAccountByStripeId(
11
+ component: Component,
12
+ ctx: RunCtx,
13
+ opts: { stripeAccountId: string },
14
+ ): Promise<StripeAccountClosure | null> {
15
+ return ctx.runQuery(
16
+ componentRef(
17
+ component,
18
+ "accountClosure/queries/getClosedAccountByStripeId",
19
+ ),
20
+ opts,
21
+ );
22
+ }
23
+
24
+ /** Personal closed accounts only. A caller-supplied userId is not authentication. */
25
+ export function listClosedAccountsByUserId(
26
+ component: Component,
27
+ ctx: RunCtx,
28
+ opts: { userId: string; paginationOpts: PaginationOptions },
29
+ ): Promise<PaginationResult<StripeAccountClosure>> {
30
+ return ctx.runQuery(
31
+ componentRef(
32
+ component,
33
+ "accountClosure/queries/listClosedAccountsByUserId",
34
+ ),
35
+ opts,
36
+ );
37
+ }
@@ -29,7 +29,6 @@ import {
29
29
  addCustomerConfiguration,
30
30
  addRecipientConfiguration,
31
31
  ensureCustomerAccount,
32
- closeAccount,
33
32
  createAccount,
34
33
  createAccountWithOnboarding,
35
34
  getAccount,
@@ -40,7 +39,6 @@ import {
40
39
  getOrCreateAccount,
41
40
  getV2Account,
42
41
  listStripeAccounts,
43
- restartAccountOnboarding,
44
42
  setAccountStatementDescriptor,
45
43
  syncAllAccounts,
46
44
  updateAccount,
@@ -876,115 +874,6 @@ describe("listStripeAccounts", () => {
876
874
  });
877
875
  });
878
876
 
879
- describe("closeAccount / restartAccountOnboarding", () => {
880
- it("uses stored appliedConfigurations from the DB and deletes after Stripe close", async () => {
881
- const stripe = makeStripe();
882
- stripe.v2.core.accounts.close.mockResolvedValue({});
883
- const runQuery = vi.fn().mockResolvedValue({
884
- appliedConfigurations: ["merchant", "recipient"],
885
- });
886
- const runMutation = vi.fn().mockResolvedValue(undefined);
887
- const ctx = { runQuery, runMutation } as unknown as RunCtx;
888
-
889
- const result = await closeAccount(asStripe(stripe), makeComponent(), ctx, {
890
- stripeAccountId: "acct_close",
891
- });
892
-
893
- expect(result).toEqual({ closed: true });
894
- // never falls back to Stripe retrieve when DB has configs
895
- expect(stripe.v2.core.accounts.retrieve).not.toHaveBeenCalled();
896
- expect(stripe.v2.core.accounts.close).toHaveBeenCalledWith("acct_close", {
897
- applied_configurations: ["merchant", "recipient"],
898
- });
899
- expect(runMutation).toHaveBeenCalledWith(
900
- refFor("core/mutations/deleteAccountByStripeId"),
901
- { stripeAccountId: "acct_close" },
902
- );
903
- });
904
-
905
- it("falls back to Stripe retrieve when the DB has no applied configs", async () => {
906
- const stripe = makeStripe();
907
- stripe.v2.core.accounts.retrieve.mockResolvedValue({
908
- applied_configurations: ["merchant"],
909
- });
910
- stripe.v2.core.accounts.close.mockResolvedValue({});
911
- const ctx = {
912
- runQuery: vi.fn().mockResolvedValue(null), // no DB row
913
- runMutation: vi.fn().mockResolvedValue(undefined),
914
- } as unknown as RunCtx;
915
-
916
- await closeAccount(asStripe(stripe), makeComponent(), ctx, {
917
- stripeAccountId: "acct_fb",
918
- });
919
-
920
- expect(stripe.v2.core.accounts.retrieve).toHaveBeenCalledWith("acct_fb");
921
- expect(stripe.v2.core.accounts.close).toHaveBeenCalledWith("acct_fb", {
922
- applied_configurations: ["merchant"],
923
- });
924
- });
925
-
926
- it("closes with an empty config list when the Stripe retrieve fallback throws", async () => {
927
- const stripe = makeStripe();
928
- stripe.v2.core.accounts.retrieve.mockRejectedValue(
929
- new Error("no such account"),
930
- );
931
- stripe.v2.core.accounts.close.mockResolvedValue({});
932
- const ctx = {
933
- runQuery: vi.fn().mockResolvedValue(null),
934
- runMutation: vi.fn().mockResolvedValue(undefined),
935
- } as unknown as RunCtx;
936
-
937
- await closeAccount(asStripe(stripe), makeComponent(), ctx, {
938
- stripeAccountId: "acct_missing",
939
- });
940
-
941
- expect(stripe.v2.core.accounts.close).toHaveBeenCalledWith("acct_missing", {
942
- applied_configurations: [],
943
- });
944
- });
945
-
946
- it("does not delete from the DB if the Stripe close fails", async () => {
947
- const stripe = makeStripe();
948
- stripe.v2.core.accounts.close.mockRejectedValue(new Error("close boom"));
949
- const runMutation = vi.fn().mockResolvedValue(undefined);
950
- const ctx = {
951
- runQuery: vi
952
- .fn()
953
- .mockResolvedValue({ appliedConfigurations: ["merchant"] }),
954
- runMutation,
955
- } as unknown as RunCtx;
956
-
957
- await expect(
958
- closeAccount(asStripe(stripe), makeComponent(), ctx, {
959
- stripeAccountId: "acct_err",
960
- }),
961
- ).rejects.toThrow(/close boom/);
962
-
963
- expect(runMutation).not.toHaveBeenCalled();
964
- });
965
-
966
- it("restartAccountOnboarding delegates to closeAccount", async () => {
967
- const stripe = makeStripe();
968
- stripe.v2.core.accounts.close.mockResolvedValue({});
969
- const ctx = {
970
- runQuery: vi.fn().mockResolvedValue({ appliedConfigurations: [] }),
971
- runMutation: vi.fn().mockResolvedValue(undefined),
972
- } as unknown as RunCtx;
973
-
974
- const result = await restartAccountOnboarding(
975
- asStripe(stripe),
976
- makeComponent(),
977
- ctx,
978
- { stripeAccountId: "acct_restart" },
979
- );
980
-
981
- expect(result).toEqual({ closed: true });
982
- expect(stripe.v2.core.accounts.close).toHaveBeenCalledWith("acct_restart", {
983
- applied_configurations: [],
984
- });
985
- });
986
- });
987
-
988
877
  describe("syncAllAccounts", () => {
989
878
  it("maps a business V2 account into an upsert and counts it synced", async () => {
990
879
  const stripe = makeStripe();
@@ -432,39 +432,34 @@ export async function closeAccount(
432
432
  ctx: RunCtx,
433
433
  opts: { stripeAccountId: string },
434
434
  ): Promise<{ closed: boolean }> {
435
- // First check our DB for stored applied_configurations
436
- let appliedConfigs: string[] = [];
437
-
438
- const dbAccount = await ctx.runQuery(
439
- componentRef(component, "core/queries/getAccountByStripeId"),
440
- { stripeAccountId: opts.stripeAccountId },
435
+ const { closure, isRetry } = await runMutationOrThrow(
436
+ ctx,
437
+ componentRef(component, "accountClosure/mutations/prepare"),
438
+ opts,
441
439
  );
442
- if (dbAccount?.appliedConfigurations) {
443
- appliedConfigs = dbAccount.appliedConfigurations;
444
- } else {
445
- // Fallback: fetch from Stripe
446
- try {
447
- const stripeAccount = await stripe.v2.core.accounts.retrieve(
448
- opts.stripeAccountId,
449
- );
450
- appliedConfigs = stripeAccount.applied_configurations ?? [];
451
- } catch {
452
- // Account may not exist on Stripe
453
- }
440
+ if (closure.state === "closed") return { closed: true };
441
+ let appliedConfigs = closure.appliedConfigurations;
442
+ let isProviderClosed = false;
443
+ if (isRetry || appliedConfigs === undefined) {
444
+ const account = await stripe.v2.core.accounts.retrieve(
445
+ opts.stripeAccountId,
446
+ );
447
+ appliedConfigs ??= account.applied_configurations ?? [];
448
+ isProviderClosed = account.closed === true;
454
449
  }
455
-
456
- // Close the account on Stripe — must pass all applied_configurations
457
- // See: https://docs.stripe.com/api/v2/core/accounts/close
458
- await stripe.v2.core.accounts.close(opts.stripeAccountId, {
459
- applied_configurations: appliedConfigs as V2CloseAppliedConfiguration[],
460
- });
461
-
462
- // Only remove from component DB after Stripe close succeeds
463
- // This prevents Convex/Stripe getting out of sync if close fails
450
+ if (!isProviderClosed)
451
+ await stripe.v2.core.accounts.close(
452
+ opts.stripeAccountId,
453
+ {
454
+ applied_configurations: (appliedConfigs ??
455
+ []) as V2CloseAppliedConfiguration[],
456
+ },
457
+ { idempotencyKey: `betterstripe-close:${opts.stripeAccountId}` },
458
+ );
464
459
  await runMutationOrThrow(
465
460
  ctx,
466
- componentRef(component, "core/mutations/deleteAccountByStripeId"),
467
- { stripeAccountId: opts.stripeAccountId },
461
+ componentRef(component, "accountClosure/mutations/complete"),
462
+ opts,
468
463
  );
469
464
 
470
465
  return { closed: true };
@@ -0,0 +1,71 @@
1
+ // @vitest-environment edge-runtime
2
+ import { describe, expect, it, vi } from "vitest";
3
+
4
+ import schema from "../../component/schema.js";
5
+ import { BetterStripe } from "../index.js";
6
+ import type { RunCtx } from "../helpers.js";
7
+ import { components, initConvexTest } from "../setup.test.js";
8
+
9
+ const modules = import.meta.glob("../../component/**/*.ts");
10
+ function fixture() {
11
+ const t = initConvexTest();
12
+ t.registerComponent("betterStripe", schema, modules);
13
+ const ctx = { runQuery: t.query, runMutation: t.mutation } as RunCtx;
14
+ const client = new BetterStripe(components.betterStripe, {});
15
+ return { t, ctx, client };
16
+ }
17
+
18
+ describe("public personal account mapping", () => {
19
+ it("returns real component proof without a Stripe secret or provider read", async () => {
20
+ const f = fixture();
21
+ await f.client.upsertAccount(f.ctx, {
22
+ stripeAccountId: "acct_original",
23
+ userId: "user_original",
24
+ });
25
+ expect(
26
+ await f.client.getPersonalAccountMapping(f.ctx, {
27
+ userId: "user_original",
28
+ }),
29
+ ).toMatchObject({
30
+ status: "unique",
31
+ account: { stripeAccountId: "acct_original", userId: "user_original" },
32
+ });
33
+ await f.client.upsertAccount(f.ctx, {
34
+ stripeAccountId: "acct_replacement",
35
+ userId: "user_original",
36
+ });
37
+ expect(
38
+ await f.client.getPersonalAccountMapping(f.ctx, {
39
+ userId: "user_original",
40
+ }),
41
+ ).toEqual({ status: "unavailable", reason: "ambiguous" });
42
+ expect(
43
+ (await f.client.getAccountByUserId(f.ctx, { userId: "user_original" }))
44
+ ?.stripeAccountId,
45
+ ).toBe("acct_original");
46
+ });
47
+
48
+ it.each([
49
+ undefined,
50
+ null,
51
+ {},
52
+ { userId: "" },
53
+ { userId: " a" },
54
+ { userId: 123 },
55
+ { userId: "x".repeat(257) },
56
+ { userId: "user_original", stripeAccountId: "acct_browser" },
57
+ ])(
58
+ "rejects malformed options %j before component lookup",
59
+ async (options) => {
60
+ const f = fixture();
61
+ const runQuery = vi.fn();
62
+ const ctx = { runQuery } as RunCtx;
63
+ const read = f.client.getPersonalAccountMapping.bind(f.client) as (
64
+ ctx: RunCtx,
65
+ opts: unknown,
66
+ ) => Promise<unknown>;
67
+ await expect(read(ctx, options)).rejects.toThrow();
68
+ expect(runQuery).not.toHaveBeenCalled();
69
+ },
70
+ );
71
+ });
@@ -0,0 +1,23 @@
1
+ import { z } from "zod";
2
+
3
+ import { assertPersonalAccountUserId } from "../../component/core/personalAccountMapping.js";
4
+ import type { PersonalAccountMapping } from "../../component/core/personalAccountMapping.js";
5
+ import type { Component, RunCtx } from "../helpers.js";
6
+ import { componentRef } from "../webhooks/helpers.js";
7
+
8
+ export type { PersonalAccountMapping } from "../../component/core/personalAccountMapping.js";
9
+ const optionsSchema = z.object({ userId: z.string() }).strict();
10
+
11
+ /** Mapping evidence only. Authenticate the host actor before passing its server-owned identity. */
12
+ export async function getPersonalAccountMapping(
13
+ component: Component,
14
+ ctx: RunCtx,
15
+ input: { userId: string },
16
+ ): Promise<PersonalAccountMapping> {
17
+ const opts = optionsSchema.parse(input);
18
+ assertPersonalAccountUserId(opts.userId);
19
+ return ctx.runQuery(
20
+ componentRef(component, "core/queries/getPersonalAccountMapping"),
21
+ opts,
22
+ );
23
+ }