@volter/twin-stripe 2.0.38 → 3.0.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 (402) hide show
  1. package/README.md +112 -207
  2. package/dist/generated/pack-facts.json +94 -0
  3. package/dist/journeys/coverage.json +596 -0
  4. package/dist/journeys/customer-life.json +6108 -0
  5. package/dist/journeys/customer-life.outline.md +371 -0
  6. package/dist/journeys/decisions.json +677 -0
  7. package/dist/journeys/demand.json +706 -0
  8. package/dist/journeys/score.json +853 -0
  9. package/dist/journeys/unreachable.json +102 -0
  10. package/dist/journeys/vendor-examples.json +8010 -0
  11. package/dist/spec/SOURCE.md +18 -0
  12. package/dist/spec/doc-examples.json +445 -0
  13. package/dist/spec/fixtures3.json.gz +0 -0
  14. package/dist/spec/openapi.json.gz +0 -0
  15. package/dist/spec/patches.json +34 -0
  16. package/dist/spec/sources.json +306 -0
  17. package/dist/src/cli.d.ts +0 -1
  18. package/dist/src/cli.js +11 -31
  19. package/dist/src/engine/after-payment.d.ts +8 -0
  20. package/dist/src/engine/after-payment.js +27 -0
  21. package/dist/src/engine/apps-secrets.d.ts +1 -0
  22. package/dist/src/engine/apps-secrets.js +3 -0
  23. package/dist/src/engine/balance.d.ts +5 -0
  24. package/dist/src/engine/balance.js +15 -0
  25. package/dist/src/engine/checkout.d.ts +24 -0
  26. package/dist/src/engine/checkout.js +30 -0
  27. package/dist/src/engine/common.d.ts +19 -0
  28. package/dist/src/engine/common.js +34 -0
  29. package/dist/src/engine/connect.d.ts +45 -0
  30. package/dist/src/engine/connect.js +108 -0
  31. package/dist/src/engine/customers.d.ts +4 -0
  32. package/dist/src/engine/customers.js +8 -0
  33. package/dist/src/engine/display.d.ts +11 -0
  34. package/dist/src/engine/display.js +13 -0
  35. package/dist/src/engine/files.d.ts +11 -0
  36. package/dist/src/engine/files.js +23 -0
  37. package/dist/src/engine/invoices.d.ts +21 -0
  38. package/dist/src/engine/invoices.js +84 -0
  39. package/dist/src/engine/issuing.d.ts +33 -0
  40. package/dist/src/engine/issuing.js +54 -0
  41. package/dist/src/engine/ledger.d.ts +8 -0
  42. package/dist/src/engine/ledger.js +8 -0
  43. package/dist/src/engine/oauth.d.ts +4 -0
  44. package/dist/src/engine/oauth.js +24 -0
  45. package/dist/src/engine/onboarding.d.ts +13 -0
  46. package/dist/src/engine/onboarding.js +42 -0
  47. package/dist/src/{stripe-params.d.ts → engine/params.d.ts} +20 -3
  48. package/dist/src/{stripe-params.js → engine/params.js} +7 -60
  49. package/dist/src/engine/payment-intents.d.ts +10 -0
  50. package/dist/src/engine/payment-intents.js +27 -0
  51. package/dist/src/engine/payment-methods.d.ts +16 -0
  52. package/dist/src/engine/payment-methods.js +22 -0
  53. package/dist/src/engine/radar.d.ts +2 -0
  54. package/dist/src/engine/radar.js +5 -0
  55. package/dist/src/engine/refunds.d.ts +1 -0
  56. package/dist/src/engine/refunds.js +3 -0
  57. package/dist/src/engine/renewals.d.ts +36 -0
  58. package/dist/src/engine/renewals.js +65 -0
  59. package/dist/src/{stripe-twin.d.ts → engine/stripe.d.ts} +12 -45
  60. package/dist/src/{stripe-twin.js → engine/stripe.js} +53 -387
  61. package/dist/src/engine/subscriptions.d.ts +11 -0
  62. package/dist/src/engine/subscriptions.js +30 -0
  63. package/dist/src/engine/transfers.d.ts +2 -0
  64. package/dist/src/engine/transfers.js +1 -0
  65. package/dist/src/{stripe-version.d.ts → engine/version.d.ts} +0 -2
  66. package/dist/src/{stripe-version.js → engine/version.js} +21 -9
  67. package/dist/src/engine/webhook-endpoints.d.ts +1 -0
  68. package/dist/src/engine/webhook-endpoints.js +12 -0
  69. package/dist/src/engine/webhooks.d.ts +31 -0
  70. package/dist/src/engine/webhooks.js +68 -0
  71. package/dist/src/fetch.d.ts +2 -0
  72. package/dist/src/fetch.js +26 -0
  73. package/dist/src/generated/surface.gen.json +1 -1
  74. package/dist/src/generated/ui.gen.json +1 -1
  75. package/dist/src/index.d.ts +5 -14
  76. package/dist/src/index.js +10 -75
  77. package/dist/src/manifest.js +478 -876
  78. package/dist/src/screens/billing-portal.d.ts +3 -0
  79. package/dist/src/screens/{portal.js → billing-portal.js} +32 -58
  80. package/dist/src/screens/checkout-js.d.ts +2 -0
  81. package/dist/src/screens/checkout-js.js +84 -0
  82. package/dist/src/screens/checkout.d.ts +2 -31
  83. package/dist/src/screens/checkout.js +32 -121
  84. package/dist/src/screens/connect-onboarding.d.ts +3 -0
  85. package/dist/src/screens/{onboarding.js → connect-onboarding.js} +38 -104
  86. package/dist/src/screens/dashboard.d.ts +3 -0
  87. package/dist/src/screens/dashboard.js +116 -0
  88. package/dist/src/screens/shared.d.ts +27 -0
  89. package/dist/src/screens/shared.js +87 -0
  90. package/dist/src/screens/stripe-js.d.ts +3 -0
  91. package/dist/src/screens/stripe-js.js +95 -0
  92. package/dist/src/semantics/account.d.ts +2 -0
  93. package/dist/src/semantics/account.js +8 -0
  94. package/dist/src/semantics/account_links.d.ts +2 -0
  95. package/dist/src/semantics/account_links.js +22 -0
  96. package/dist/src/semantics/accounts.d.ts +4 -0
  97. package/dist/src/semantics/accounts.js +40 -0
  98. package/dist/src/semantics/apps.d.ts +4 -0
  99. package/dist/src/semantics/{apps-secrets.js → apps.js} +14 -26
  100. package/dist/src/semantics/around.d.ts +2 -0
  101. package/dist/src/semantics/around.js +13 -0
  102. package/dist/src/semantics/balance.d.ts +2 -11
  103. package/dist/src/semantics/balance.js +3 -194
  104. package/dist/src/semantics/balance_transactions.d.ts +2 -0
  105. package/dist/src/semantics/balance_transactions.js +15 -0
  106. package/dist/src/semantics/billing_portal.d.ts +4 -0
  107. package/dist/src/semantics/billing_portal.js +36 -0
  108. package/dist/src/semantics/charges.d.ts +3 -28
  109. package/dist/src/semantics/charges.js +5 -202
  110. package/dist/src/semantics/checkout.d.ts +3 -20
  111. package/dist/src/semantics/checkout.js +21 -271
  112. package/dist/src/semantics/clock.d.ts +2 -0
  113. package/dist/src/semantics/clock.js +15 -0
  114. package/dist/src/semantics/coupons.d.ts +2 -6
  115. package/dist/src/semantics/coupons.js +2 -60
  116. package/dist/src/semantics/customers.d.ts +5 -6
  117. package/dist/src/semantics/customers.js +47 -413
  118. package/dist/src/semantics/doors.d.ts +23 -0
  119. package/dist/src/semantics/doors.js +73 -0
  120. package/dist/src/semantics/entitlements.d.ts +0 -2
  121. package/dist/src/semantics/entitlements.js +4 -95
  122. package/dist/src/semantics/events.d.ts +11 -0
  123. package/dist/src/semantics/events.js +31 -0
  124. package/dist/src/semantics/index.d.ts +2 -0
  125. package/dist/src/semantics/index.js +85 -0
  126. package/dist/src/semantics/invoice_payments.d.ts +2 -0
  127. package/dist/src/semantics/invoice_payments.js +12 -0
  128. package/dist/src/semantics/invoices.d.ts +11 -18
  129. package/dist/src/semantics/invoices.js +121 -440
  130. package/dist/src/semantics/issuing.d.ts +6 -13
  131. package/dist/src/semantics/issuing.js +58 -556
  132. package/dist/src/semantics/payment_intents.d.ts +3 -0
  133. package/dist/src/semantics/payment_intents.js +71 -0
  134. package/dist/src/semantics/payment_methods.d.ts +3 -0
  135. package/dist/src/semantics/payment_methods.js +15 -0
  136. package/dist/src/semantics/payouts.d.ts +3 -0
  137. package/dist/src/semantics/payouts.js +28 -0
  138. package/dist/src/semantics/prices.d.ts +4 -0
  139. package/dist/src/semantics/prices.js +37 -0
  140. package/dist/src/semantics/products.d.ts +4 -2
  141. package/dist/src/semantics/products.js +12 -138
  142. package/dist/src/semantics/promotion_codes.d.ts +3 -0
  143. package/dist/src/semantics/promotion_codes.js +35 -0
  144. package/dist/src/semantics/radar.d.ts +2 -2
  145. package/dist/src/semantics/radar.js +5 -74
  146. package/dist/src/semantics/refunds.d.ts +2 -9
  147. package/dist/src/semantics/refunds.js +3 -193
  148. package/dist/src/semantics/shared.d.ts +558 -55
  149. package/dist/src/semantics/shared.js +2070 -114
  150. package/dist/src/semantics/states.d.ts +462 -0
  151. package/dist/src/semantics/states.js +634 -0
  152. package/dist/src/semantics/subscriptions.d.ts +6 -11
  153. package/dist/src/semantics/subscriptions.js +37 -530
  154. package/dist/src/semantics/test_helpers.d.ts +3 -0
  155. package/dist/src/semantics/test_helpers.js +125 -0
  156. package/dist/src/semantics/transfers.d.ts +2 -2
  157. package/dist/src/semantics/transfers.js +6 -101
  158. package/dist/src/semantics/webhook_endpoints.d.ts +2 -0
  159. package/dist/src/semantics/webhook_endpoints.js +26 -0
  160. package/dist/src/server.d.ts +7 -0
  161. package/dist/src/server.js +7 -0
  162. package/generated/pack-facts.json +94 -0
  163. package/journeys/coverage.json +596 -0
  164. package/journeys/customer-life.json +6108 -0
  165. package/journeys/customer-life.outline.md +371 -0
  166. package/journeys/decisions.json +677 -0
  167. package/journeys/demand.json +706 -0
  168. package/journeys/score.json +853 -0
  169. package/journeys/unreachable.json +102 -0
  170. package/journeys/vendor-examples.json +8010 -0
  171. package/package.json +11 -20
  172. package/spec/SOURCE.md +18 -0
  173. package/spec/doc-examples.json +445 -0
  174. package/spec/fixtures3.json.gz +0 -0
  175. package/spec/openapi.json.gz +0 -0
  176. package/spec/patches.json +34 -0
  177. package/spec/sources.json +306 -0
  178. package/src/cli.ts +11 -28
  179. package/src/engine/after-payment.ts +29 -0
  180. package/src/engine/apps-secrets.ts +4 -0
  181. package/src/engine/balance.ts +32 -0
  182. package/src/engine/checkout.ts +39 -0
  183. package/src/engine/common.ts +49 -0
  184. package/src/engine/connect.ts +106 -0
  185. package/src/engine/customers.ts +10 -0
  186. package/src/engine/display.ts +18 -0
  187. package/src/engine/files.ts +28 -0
  188. package/src/engine/invoices.ts +102 -0
  189. package/src/engine/issuing.ts +66 -0
  190. package/src/engine/ledger.ts +48 -0
  191. package/src/engine/oauth.ts +18 -0
  192. package/src/engine/onboarding.ts +53 -0
  193. package/src/{stripe-params.ts → engine/params.ts} +7 -56
  194. package/src/engine/payment-intents.ts +32 -0
  195. package/src/engine/payment-methods.ts +24 -0
  196. package/src/engine/radar.ts +6 -0
  197. package/src/engine/refunds.ts +3 -0
  198. package/src/engine/renewals.ts +72 -0
  199. package/src/{stripe-twin.ts → engine/stripe.ts} +57 -400
  200. package/src/engine/subscriptions.ts +37 -0
  201. package/src/engine/transfers.ts +22 -0
  202. package/src/{stripe-version.ts → engine/version.ts} +19 -10
  203. package/src/engine/webhook-endpoints.ts +14 -0
  204. package/src/engine/webhooks.ts +70 -0
  205. package/src/fetch.ts +27 -0
  206. package/src/generated/surface.gen.json +1 -1
  207. package/src/generated/ui.gen.json +1 -1
  208. package/src/index.ts +10 -116
  209. package/src/manifest.ts +480 -904
  210. package/src/screens/{portal.tsx → billing-portal.tsx} +35 -42
  211. package/src/screens/checkout-js.ts +80 -0
  212. package/src/screens/checkout.tsx +32 -96
  213. package/src/screens/{onboarding.tsx → connect-onboarding.tsx} +39 -85
  214. package/src/screens/dashboard.tsx +178 -0
  215. package/src/screens/shared.tsx +84 -0
  216. package/src/screens/stripe-js.ts +102 -0
  217. package/src/semantics/account.ts +10 -0
  218. package/src/semantics/account_links.ts +20 -0
  219. package/src/semantics/accounts.ts +44 -0
  220. package/src/semantics/{apps-secrets.ts → apps.ts} +17 -33
  221. package/src/semantics/around.ts +17 -0
  222. package/src/semantics/balance.ts +6 -208
  223. package/src/semantics/balance_transactions.ts +18 -0
  224. package/src/semantics/billing_portal.ts +45 -0
  225. package/src/semantics/charges.ts +8 -212
  226. package/src/semantics/checkout.ts +25 -276
  227. package/src/semantics/clock.ts +19 -0
  228. package/src/semantics/coupons.ts +5 -68
  229. package/src/semantics/customers.ts +48 -414
  230. package/src/semantics/doors.ts +79 -0
  231. package/src/semantics/entitlements.ts +3 -94
  232. package/src/semantics/events.ts +37 -0
  233. package/src/semantics/index.ts +89 -0
  234. package/src/semantics/invoice_payments.ts +16 -0
  235. package/src/semantics/invoices.ts +112 -441
  236. package/src/semantics/issuing.ts +57 -573
  237. package/src/semantics/payment_intents.ts +76 -0
  238. package/src/semantics/payment_methods.ts +20 -0
  239. package/src/semantics/payouts.ts +29 -0
  240. package/src/semantics/prices.ts +41 -0
  241. package/src/semantics/products.ts +12 -146
  242. package/src/semantics/promotion_codes.ts +38 -0
  243. package/src/semantics/radar.ts +8 -79
  244. package/src/semantics/refunds.ts +4 -216
  245. package/src/semantics/shared.ts +2089 -135
  246. package/src/semantics/states.ts +662 -0
  247. package/src/semantics/subscriptions.ts +39 -542
  248. package/src/semantics/test_helpers.ts +119 -0
  249. package/src/semantics/transfers.ts +10 -132
  250. package/src/semantics/webhook_endpoints.ts +27 -0
  251. package/src/server.ts +7 -0
  252. package/client/dashboard-api.ts +0 -286
  253. package/client/stripe-mirror.css +0 -277
  254. package/client/stripe-mirror.tsx +0 -1559
  255. package/dist/client/dashboard-api.d.ts +0 -107
  256. package/dist/client/dashboard-api.js +0 -238
  257. package/dist/client/dashboard-api.ts +0 -286
  258. package/dist/client/stripe-mirror.bundle.js +0 -236
  259. package/dist/client/stripe-mirror.css +0 -277
  260. package/dist/client/stripe-mirror.d.ts +0 -134
  261. package/dist/client/stripe-mirror.js +0 -859
  262. package/dist/client/stripe-mirror.tsx +0 -1559
  263. package/dist/src/screens/connect-oauth.d.ts +0 -27
  264. package/dist/src/screens/connect-oauth.js +0 -414
  265. package/dist/src/screens/connect-settings.d.ts +0 -22
  266. package/dist/src/screens/connect-settings.js +0 -103
  267. package/dist/src/screens/consent-skin.d.ts +0 -4
  268. package/dist/src/screens/consent-skin.js +0 -18
  269. package/dist/src/screens/financial-connections.d.ts +0 -5
  270. package/dist/src/screens/financial-connections.js +0 -90
  271. package/dist/src/screens/identity.d.ts +0 -5
  272. package/dist/src/screens/identity.js +0 -86
  273. package/dist/src/screens/onboarding.d.ts +0 -13
  274. package/dist/src/screens/portal.d.ts +0 -5
  275. package/dist/src/screens/public-details.d.ts +0 -5
  276. package/dist/src/screens/public-details.js +0 -90
  277. package/dist/src/semantics/after-payment.d.ts +0 -22
  278. package/dist/src/semantics/after-payment.js +0 -99
  279. package/dist/src/semantics/apps-secrets.d.ts +0 -2
  280. package/dist/src/semantics/billing.d.ts +0 -2
  281. package/dist/src/semantics/billing.js +0 -220
  282. package/dist/src/semantics/connect.d.ts +0 -5
  283. package/dist/src/semantics/connect.js +0 -493
  284. package/dist/src/semantics/credit-notes.d.ts +0 -2
  285. package/dist/src/semantics/credit-notes.js +0 -172
  286. package/dist/src/semantics/disputes.d.ts +0 -2
  287. package/dist/src/semantics/disputes.js +0 -51
  288. package/dist/src/semantics/ephemeral-keys.d.ts +0 -2
  289. package/dist/src/semantics/ephemeral-keys.js +0 -34
  290. package/dist/src/semantics/files.d.ts +0 -2
  291. package/dist/src/semantics/files.js +0 -125
  292. package/dist/src/semantics/ledger.d.ts +0 -59
  293. package/dist/src/semantics/ledger.js +0 -200
  294. package/dist/src/semantics/payment-intents.d.ts +0 -18
  295. package/dist/src/semantics/payment-intents.js +0 -452
  296. package/dist/src/semantics/payment-links.d.ts +0 -2
  297. package/dist/src/semantics/payment-links.js +0 -133
  298. package/dist/src/semantics/payment-methods.d.ts +0 -20
  299. package/dist/src/semantics/payment-methods.js +0 -140
  300. package/dist/src/semantics/plans.d.ts +0 -5
  301. package/dist/src/semantics/plans.js +0 -121
  302. package/dist/src/semantics/platform.d.ts +0 -9
  303. package/dist/src/semantics/platform.js +0 -206
  304. package/dist/src/semantics/renewals.d.ts +0 -47
  305. package/dist/src/semantics/renewals.js +0 -281
  306. package/dist/src/semantics/setup-intents.d.ts +0 -2
  307. package/dist/src/semantics/setup-intents.js +0 -84
  308. package/dist/src/semantics/subscription-schedules.d.ts +0 -2
  309. package/dist/src/semantics/subscription-schedules.js +0 -119
  310. package/dist/src/semantics/tax.d.ts +0 -2
  311. package/dist/src/semantics/tax.js +0 -197
  312. package/dist/src/semantics/terminal.d.ts +0 -5
  313. package/dist/src/semantics/terminal.js +0 -182
  314. package/dist/src/semantics/test-clocks.d.ts +0 -6
  315. package/dist/src/semantics/test-clocks.js +0 -73
  316. package/dist/src/semantics/tokens.d.ts +0 -4
  317. package/dist/src/semantics/tokens.js +0 -44
  318. package/dist/src/semantics/treasury.d.ts +0 -2
  319. package/dist/src/semantics/treasury.js +0 -377
  320. package/dist/src/semantics/webhook-endpoints.d.ts +0 -3
  321. package/dist/src/semantics/webhook-endpoints.js +0 -85
  322. package/dist/src/stripe-budget.d.ts +0 -55
  323. package/dist/src/stripe-budget.js +0 -155
  324. package/dist/src/stripe-capabilities.d.ts +0 -3
  325. package/dist/src/stripe-capabilities.js +0 -5836
  326. package/dist/src/stripe-conformance.d.ts +0 -43
  327. package/dist/src/stripe-conformance.js +0 -105
  328. package/dist/src/stripe-connector.d.ts +0 -161
  329. package/dist/src/stripe-connector.js +0 -414
  330. package/dist/src/stripe-emit.d.ts +0 -2
  331. package/dist/src/stripe-emit.js +0 -145
  332. package/dist/src/stripe-events.d.ts +0 -93
  333. package/dist/src/stripe-events.js +0 -392
  334. package/dist/src/stripe-js.d.ts +0 -4
  335. package/dist/src/stripe-js.js +0 -70
  336. package/dist/src/stripe-mirror-ui.d.ts +0 -15
  337. package/dist/src/stripe-mirror-ui.js +0 -87
  338. package/dist/src/stripe-perform-harness.d.ts +0 -9
  339. package/dist/src/stripe-perform-harness.js +0 -26
  340. package/dist/src/stripe-server.d.ts +0 -33
  341. package/dist/src/stripe-server.js +0 -429
  342. package/dist/src/stripe-shared.d.ts +0 -109
  343. package/dist/src/stripe-shared.js +0 -276
  344. package/dist/src/stripe-ui-conformance.d.ts +0 -5
  345. package/dist/src/stripe-ui-conformance.js +0 -79
  346. package/dist/src/stripe-ui-structure.d.ts +0 -3
  347. package/dist/src/stripe-ui-structure.js +0 -168
  348. package/dist/test-fixtures/stripe-known-deviations.json +0 -110
  349. package/dist/test-fixtures/stripe-openapi-operations.SOURCE.md +0 -14
  350. package/dist/test-fixtures/stripe-openapi-operations.json +0 -4717
  351. package/dist/test-fixtures/stripe-schemas.SOURCE.md +0 -35
  352. package/dist/test-fixtures/stripe-schemas.json +0 -3813
  353. package/src/screens/connect-oauth.tsx +0 -400
  354. package/src/screens/connect-settings.tsx +0 -121
  355. package/src/screens/consent-skin.ts +0 -20
  356. package/src/screens/financial-connections.tsx +0 -101
  357. package/src/screens/identity.tsx +0 -96
  358. package/src/screens/public-details.tsx +0 -105
  359. package/src/semantics/after-payment.ts +0 -118
  360. package/src/semantics/billing.ts +0 -216
  361. package/src/semantics/connect.ts +0 -487
  362. package/src/semantics/credit-notes.ts +0 -168
  363. package/src/semantics/disputes.ts +0 -62
  364. package/src/semantics/ephemeral-keys.ts +0 -34
  365. package/src/semantics/files.ts +0 -143
  366. package/src/semantics/ledger.ts +0 -253
  367. package/src/semantics/payment-intents.ts +0 -463
  368. package/src/semantics/payment-links.ts +0 -148
  369. package/src/semantics/payment-methods.ts +0 -145
  370. package/src/semantics/plans.ts +0 -131
  371. package/src/semantics/platform.ts +0 -220
  372. package/src/semantics/renewals.ts +0 -279
  373. package/src/semantics/setup-intents.ts +0 -87
  374. package/src/semantics/subscription-schedules.ts +0 -129
  375. package/src/semantics/tax.ts +0 -220
  376. package/src/semantics/terminal.ts +0 -195
  377. package/src/semantics/test-clocks.ts +0 -77
  378. package/src/semantics/tokens.ts +0 -52
  379. package/src/semantics/treasury.ts +0 -383
  380. package/src/semantics/webhook-endpoints.ts +0 -87
  381. package/src/stripe-budget.ts +0 -181
  382. package/src/stripe-capabilities.ts +0 -5651
  383. package/src/stripe-conformance.ts +0 -124
  384. package/src/stripe-connector.ts +0 -462
  385. package/src/stripe-emit.ts +0 -157
  386. package/src/stripe-events.ts +0 -434
  387. package/src/stripe-js.ts +0 -70
  388. package/src/stripe-mirror-ui.ts +0 -90
  389. package/src/stripe-perform-harness.ts +0 -29
  390. package/src/stripe-server.ts +0 -393
  391. package/src/stripe-shared.ts +0 -297
  392. package/src/stripe-ui-conformance.ts +0 -88
  393. package/src/stripe-ui-structure.ts +0 -182
  394. package/test-fixtures/stripe-known-deviations.json +0 -110
  395. package/test-fixtures/stripe-openapi-operations.json +0 -4717
  396. package/test-fixtures/stripe-schemas.json +0 -3813
  397. /package/dist/src/{screens → engine}/industries.d.ts +0 -0
  398. /package/dist/src/{screens → engine}/industries.js +0 -0
  399. /package/dist/src/{semantics → engine}/test-cards.d.ts +0 -0
  400. /package/dist/src/{semantics → engine}/test-cards.js +0 -0
  401. /package/src/{screens → engine}/industries.ts +0 -0
  402. /package/src/{semantics → engine}/test-cards.ts +0 -0
@@ -1,452 +0,0 @@
1
- import { asBool, cardError, chargeDefaults, declineFor, microdepositsNextAction, mintClientSecret, requiresAuthentication, requiresMicrodeposits, threeDsNextAction, validateMoney, verifyMicrodeposits, } from "../stripe-twin.js";
2
- import { afterCharge, mintMandate } from "./after-payment.js";
3
- import { cancelAuthorization, captureAuthorization } from "./charges.js";
4
- import { settleCharge } from "./ledger.js";
5
- import { materializeTestMethod, methodFromData, microdepositsAsked, paymentMethodDetails } from "./payment-methods.js";
6
- import { at_, confirmOnly, created, fail, finder, search } from "./shared.js";
7
- const PI = 'payment_intent';
8
- /** Bookkeeping: the intents whose caller named the methods (payment_method_types, allowed_payment_method_types) on
9
- * create. The twin's default list (['card']) stands for Stripe's choice and restricts nothing, so only a named list
10
- * refuses a confirm with a method of another type. Kept apart from the intent: a `_` subject is never pushed
11
- * (world-core isTwinBookkeeping). */
12
- const TYPES_GIVEN = '_payment_intent_types_given';
13
- /** Whether the intent's caller named its methods: on create (TYPES_GIVEN), or by an update that set its
14
- * payment_method_types ("Alternatively, update the allowed payment_method_types for this PaymentIntent",
15
- * methodNotAllowed below), which the generic update writes as given, so the intent's history holds it. */
16
- const typesGiven = (ctx, id) => ctx.tree().some((r) => r.type === TYPES_GIVEN && r.id === id)
17
- || ctx.history(PI, id).some((e) => e.operation !== 'payment_intent.create' && Array.isArray(e.fields?.payment_method_types));
18
- const send = (ctx, r) => ctx.reply(r.body, r.status);
19
- // ── THE MONEY MODEL, continued: a succeeded intent has a Charge ──
20
- //
21
- // Real Stripe always materializes a Charge when a PaymentIntent succeeds and points the intent's
22
- // `latest_charge` at it: the charge is what a refund lands on, so an intent that succeeds without
23
- // one cannot answer "refund this payment" at all. Idempotent against a repeated confirm: an intent
24
- // that already names a successful charge keeps it (a second one would double the money collected); a
25
- // declined attempt's failed charge is history, and the payment that succeeds makes its own. The id is
26
- // returned so the caller folds `latest_charge` into the same intent write (one write, one event). The
27
- // charge is written as the event Stripe sends for it, `charge.succeeded` ("Occurs whenever a charge is
28
- // successful", docs.stripe.com/api/events/types); Stripe has no `charge.created`.
29
- /** What every charge an intent makes carries of it: its transfer_group, which "identifies the resulting payment as part
30
- * of a group" (docs.stripe.com/api/payment_intents/object), its description and its metadata. */
31
- function intentCarries(pi) {
32
- return {
33
- ...(typeof pi.transfer_group === 'string' ? { transfer_group: pi.transfer_group } : {}),
34
- ...(typeof pi.description === 'string' ? { description: pi.description } : {}),
35
- // "When a PaymentIntent creates a Charge, the metadata copies to the Charge in a one-time snapshot"
36
- // (docs.stripe.com/metadata, "Copy metadata to another object")
37
- ...(pi.metadata && typeof pi.metadata === 'object' ? { metadata: { ...pi.metadata } } : {}),
38
- };
39
- }
40
- export async function mintCharge(ctx, piId, pi, amount) {
41
- const already = ctx.get(PI, piId)?.latest_charge;
42
- if (typeof already === 'string' && ctx.get('charge', already)?.status === 'succeeded')
43
- return already;
44
- const chargeId = ctx.mint('charge');
45
- const bt = await settleCharge(ctx, chargeId, amount, String(pi.currency ?? 'usd'), typeof pi.payment_method === 'string' ? pi.payment_method : undefined);
46
- await created(ctx, 'charge', {
47
- id: chargeId, amount, currency: pi.currency ?? 'usd', payment_intent: piId,
48
- ...(typeof pi.customer === 'string' ? { customer: pi.customer } : {}),
49
- ...(typeof pi.payment_method === 'string' ? { payment_method: pi.payment_method } : {}),
50
- ...(typeof pi.invoice === 'string' ? { invoice: pi.invoice } : {}),
51
- ...intentCarries(pi),
52
- }, { ...chargeDefaults(chargeId, amount, true, ctx.occurredAt), balance_transaction: bt, payment_method_details: paymentMethodDetails(ctx, pi.payment_method) ?? null }, { operation: 'charge.succeeded' });
53
- await afterCharge(ctx, chargeId, { amount, currency: String(pi.currency ?? 'usd'), payment_intent: piId, application_fee_amount: pi.application_fee_amount, destination: pi.transfer_data?.destination }, pi.payment_method);
54
- return chargeId;
55
- }
56
- /** A manual-capture intent's authorization: "Separate payment authorization and capture to create a charge now, but
57
- * capture funds later", and on capture "A partial capture automatically releases the remaining amount"
58
- * (docs.stripe.com/payments/place-a-hold-on-a-payment-method). The charge succeeded and is not captured, holding the
59
- * amount until capture_before, with nothing on the balance yet. Written as
60
- * charge.succeeded, as a charge made with capture=false is (charges.ts); its capture writes charge.captured
61
- * (captureAuthorization). Where the documentation stops and the twin decides: the events page names no event for an
62
- * authorization of its own, so the authorized charge sends charge.succeeded, its status being succeeded. */
63
- export async function authorizeCharge(ctx, piId, pi, amount) {
64
- const chargeId = ctx.mint('charge');
65
- await created(ctx, 'charge', {
66
- id: chargeId, amount, currency: pi.currency ?? 'usd', payment_intent: piId,
67
- ...(typeof pi.customer === 'string' ? { customer: pi.customer } : {}),
68
- ...(typeof pi.payment_method === 'string' ? { payment_method: pi.payment_method } : {}),
69
- ...(typeof pi.invoice === 'string' ? { invoice: pi.invoice } : {}),
70
- ...intentCarries(pi),
71
- }, { ...chargeDefaults(chargeId, amount, false, ctx.occurredAt), balance_transaction: null, payment_method_details: paymentMethodDetails(ctx, pi.payment_method) ?? null }, { operation: 'charge.succeeded' });
72
- return chargeId;
73
- }
74
- /**
75
- * An intent an invoice collects through succeeded (the default_incomplete subscription flow: create
76
- * now, confirm client-side): its invoice is paid and, when it is a subscription's first, the
77
- * subscription leaves incomplete for active. Both are moves the invoice and subscription machines
78
- * declare under the confirming operation. No-op when the intent names no invoice, or the invoice is
79
- * already paid (a duplicate confirm) or void (voiding ends what it collects).
80
- */
81
- async function payInvoiceOf(ctx, pi, operationId, actor) {
82
- const invoiceId = typeof pi.invoice === 'string' ? pi.invoice : undefined;
83
- const invoice = invoiceId ? ctx.get('invoice', invoiceId) : undefined;
84
- if (!invoiceId || !invoice || invoice.status === 'paid' || invoice.status === 'void')
85
- return;
86
- if (ctx.legal('invoice', 'status', operationId, invoice.status, 'paid', invoiceId, actor))
87
- return;
88
- const amount = Number(invoice.total ?? invoice.amount_due) || 0;
89
- // `invoice.pay` is the event the standalone pay action sends: invoice.paid
90
- await ctx.write('invoice', invoiceId, {
91
- status: 'paid', paid: true, amount_paid: amount, amount_remaining: 0,
92
- status_transitions: { ...(invoice.status_transitions ?? {}), paid_at: ctx.now() },
93
- }, 'invoice.pay');
94
- const subId = typeof invoice.subscription === 'string' ? invoice.subscription : undefined;
95
- const sub = subId ? ctx.get('subscription', subId) : undefined;
96
- if (subId && sub && sub.status === 'incomplete' && !ctx.legal('subscription', 'status', operationId, sub.status, 'active', subId, actor)) {
97
- await ctx.write('subscription', subId, { status: 'active' }, 'subscription.update');
98
- }
99
- }
100
- /** The intent the path names, and the machine's refusal when this operation may not move it. */
101
- function load(ctx, operationId) {
102
- const pi = ctx.id ? ctx.get(PI, ctx.id) : undefined;
103
- if (!pi)
104
- return { answer: ctx.notFound(PI, String(ctx.id)) };
105
- const refusal = ctx.legal(PI, 'status', operationId, pi.status);
106
- return refusal ? { answer: ctx.refuse(refusal) } : { pi };
107
- }
108
- const create = async (ctx) => {
109
- const params = ctx.params;
110
- const bad = validateMoney(params);
111
- if (bad)
112
- return send(ctx, bad);
113
- // what describes a confirmation needs one: error_on_requires_action, mandate, mandate_data, off_session and return_url
114
- // are each, in the served spec, a parameter that "can only be used with `confirm=true`" (shared.ts confirmOnly)
115
- const unconfirmed = confirmOnly(ctx, ['error_on_requires_action', 'mandate', 'mandate_data', 'off_session', 'return_url'], asBool(params.confirm));
116
- if (unconfirmed)
117
- return unconfirmed;
118
- // automatic_payment_methods: when enabled, Stripe selects eligible methods and answers the
119
- // normalized { enabled, allow_redirects } object
120
- const apmIn = params.automatic_payment_methods;
121
- const apmEnabled = apmIn && typeof apmIn === 'object' ? asBool(apmIn.enabled) : false;
122
- const automatic_payment_methods = apmEnabled ? { enabled: true, allow_redirects: apmIn.allow_redirects ?? 'always' } : null;
123
- // the methods it may be paid with: allowed_payment_method_types in the served version, payment_method_types for a
124
- // caller pinned to an earlier one (docs.stripe.com/api/payment_intents/create)
125
- const given = Array.isArray(params.allowed_payment_method_types) ? params.allowed_payment_method_types : params.payment_method_types;
126
- const payment_method_types = Array.isArray(given) ? given.map(String) : ['card'];
127
- // `confirm` is an instruction, not a field: `confirm=true` attempts to confirm the intent at once
128
- // (https://docs.stripe.com/api/payment_intents/create#create_payment_intent-confirm)
129
- const { automatic_payment_methods: _a, payment_method_types: _p, allowed_payment_method_types: _ap, id: provided, expand: _e, confirm: confirmNow, payment_method_data: data, mandate_data: _mandate, ...rest } = params;
130
- // a create's payment_method_data makes its method, as a confirm's does (payment-methods.ts methodFromData)
131
- const made = await methodFromData(ctx, data);
132
- if (made)
133
- rest.payment_method = made;
134
- // the id is minted first so the client secret can carry it: Stripe.js parses the id back out
135
- const id = typeof provided === 'string' && provided ? provided : ctx.mint(PI);
136
- // it waits for a payment method until it has one, then for its confirmation (manifest.ts)
137
- const hasMethod = typeof rest.payment_method === 'string' && rest.payment_method !== '';
138
- if (hasMethod)
139
- ctx.legal(PI, 'status', 'PostPaymentIntents', 'requires_payment_method', 'requires_confirmation', id);
140
- const body = await ctx.write(PI, id, {
141
- object: 'payment_intent',
142
- created: ctx.now(),
143
- status: hasMethod ? 'requires_confirmation' : 'requires_payment_method',
144
- client_secret: mintClientSecret(id),
145
- livemode: false,
146
- capture_method: 'automatic',
147
- amount_capturable: 0,
148
- amount_received: 0,
149
- next_action: null,
150
- automatic_payment_methods,
151
- payment_method_types,
152
- ...(Array.isArray(params.allowed_payment_method_types) ? { allowed_payment_method_types: payment_method_types } : {}),
153
- payment_method_options: params.payment_method_options ?? {},
154
- ...rest,
155
- }, 'payment_intent.create');
156
- if (Array.isArray(given))
157
- await ctx.record(TYPES_GIVEN, { payment_intent: id }, id);
158
- if (asBool(confirmNow))
159
- return confirmIntent(ctx, body, {});
160
- return ctx.reply(ctx.expand(PI, body));
161
- };
162
- const confirm = async (ctx) => {
163
- const loaded = load(ctx, 'PostPaymentIntentsIntentConfirm');
164
- if ('answer' in loaded)
165
- return loaded.answer;
166
- const { expand: _e, payment_method_data: data, mandate_data: _mandate, ...params } = ctx.params;
167
- // a confirm's payment_method_data makes the method it pays with; neither it nor mandate_data is a field of the intent
168
- const made = await methodFromData(ctx, data);
169
- return confirmIntent(ctx, loaded.pi, made ? { ...params, payment_method: made } : params);
170
- };
171
- /** The type of the payment method an intent is paid with: its PaymentMethod's, or a documented test name's (pm_card_*,
172
- * tok_* a card; pm_usBankAccount_* a US bank account), else unknown. */
173
- function methodType(ctx, method) {
174
- if (typeof method !== 'string' || !method)
175
- return undefined;
176
- const row = ctx.get('payment_method', method);
177
- if (row)
178
- return typeof row.type === 'string' ? row.type : undefined;
179
- if (/^pm_us_?bank_?account/i.test(method))
180
- return 'us_bank_account';
181
- if (/^(pm_card_|tok_)/.test(method))
182
- return 'card';
183
- return undefined;
184
- }
185
- /** A confirm with a payment method of a type the intent's caller did not allow, refused as Stripe refuses it: "The
186
- * PaymentMethod provided (card_present) is not allowed for this PaymentIntent. Please attach a PaymentMethod of one of
187
- * the following types: card. Alternatively, update the allowed payment_method_types for this PaymentIntent to include
188
- * "card_present"" (a real answer quoted in github.com/stripe/stripe-terminal-react-native/issues/745, an intent made
189
- * with payment_method_types ['card']). Only a list the caller gave restricts: without one Stripe offers the methods
190
- * the account enables, which the twin does not model. Where the evidence stops: that answer's code is not quoted; the
191
- * twin answers payment_intent_invalid_parameter ("One or more provided parameters wasn't allowed for the given
192
- * operation on the PaymentIntent", docs.stripe.com/error-codes), param payment_method. */
193
- function methodNotAllowed(ctx, existing, params) {
194
- const types = Array.isArray(params.payment_method_types) ? params.payment_method_types.map(String)
195
- : Array.isArray(params.allowed_payment_method_types) ? params.allowed_payment_method_types.map(String)
196
- : typesGiven(ctx, String(existing.id)) && Array.isArray(existing.payment_method_types) ? existing.payment_method_types.map(String) : undefined;
197
- if (!types)
198
- return undefined;
199
- const type = methodType(ctx, params.payment_method ?? existing.payment_method);
200
- if (!type || types.includes(type))
201
- return undefined;
202
- return ctx.refuse({ status: 400, code: 'payment_intent_invalid_parameter', param: 'payment_method', message: `The PaymentMethod provided (${type}) is not allowed for this PaymentIntent. Please attach a PaymentMethod of one of the following types: ${types.join(', ')}. Alternatively, update the allowed payment_method_types for this PaymentIntent to include "${type}"` });
203
- }
204
- /** Confirm an intent the confirm machine allows to move: the confirm action, and a create with `confirm=true`. */
205
- async function confirmIntent(ctx, existing, params) {
206
- const id = String(existing.id);
207
- // a payment names a mandate only an active one authorizes: an inactive mandate "was rejected, revoked, or previously
208
- // used, and may not be used to initiate future payments" (docs.stripe.com/api/mandates/object), refused as
209
- // payment_intent_mandate_invalid, "The provided mandate is invalid and can't be used for the payment intent"
210
- // (docs.stripe.com/error-codes)
211
- const named = typeof params.mandate === 'string' ? params.mandate : typeof existing.mandate === 'string' ? existing.mandate : undefined;
212
- if (named && ctx.get('mandate', named)?.status !== 'active')
213
- return fail(ctx, 'The provided mandate is invalid and can\'t be used for the payment intent.', 400, 'payment_intent_mandate_invalid');
214
- const notAllowed = methodNotAllowed(ctx, existing, params);
215
- if (notAllowed)
216
- return notAllowed;
217
- // a declining test card leaves the intent needing a payment method, with the error on it and a 402
218
- const decline = declineFor(finder(ctx), params, existing);
219
- if (decline) {
220
- // Stripe records the attempt: a failed charge the intent's latest_charge names, the declined card on
221
- // last_payment_error (a test name made a real PaymentMethod), and no payment_method left on the intent
222
- // (docs.stripe.com/payments/paymentintents/lifecycle, docs.stripe.com/declines)
223
- const ref = params.payment_method ?? existing.payment_method;
224
- let pmId = typeof ref === 'string' ? ref : undefined;
225
- if (pmId && !ctx.get('payment_method', pmId) && pmId.startsWith('pm_card_'))
226
- pmId = String((await materializeTestMethod(ctx, pmId, typeof existing.customer === 'string' ? existing.customer : null)).id);
227
- const pm = pmId ? ctx.get('payment_method', pmId) : undefined;
228
- const amount = Number(params.amount ?? existing.amount) || 0;
229
- const chargeId = ctx.mint('charge');
230
- await created(ctx, 'charge', {
231
- id: chargeId, amount, currency: existing.currency ?? 'usd', payment_intent: id,
232
- ...(typeof existing.customer === 'string' ? { customer: existing.customer } : {}),
233
- ...(pmId ? { payment_method: pmId } : {}),
234
- ...intentCarries({ ...existing, ...params }),
235
- }, {
236
- ...chargeDefaults(chargeId, amount, false, ctx.occurredAt), status: 'failed', paid: false, captured: false, capture_before: null,
237
- failure_code: decline.code, failure_message: decline.message, balance_transaction: null,
238
- outcome: { type: 'issuer_declined', network_status: 'declined_by_network', reason: decline.decline_code ?? decline.code, risk_level: 'normal', seller_message: 'The bank did not return any further details with this decline.' },
239
- payment_method_details: pmId ? paymentMethodDetails(ctx, pmId) ?? null : null,
240
- }, { operation: 'charge.failed' }); // "Occurs whenever a failed charge attempt occurs." (docs.stripe.com/api/events/types)
241
- const { payment_method: _pm, ...rest } = params;
242
- const last_payment_error = { type: 'card_error', code: decline.code, ...(decline.decline_code ? { decline_code: decline.decline_code } : {}), message: decline.message, param: 'card', charge: chargeId, payment_method: pm ?? null };
243
- const failed = await ctx.write(PI, id, { ...rest, payment_method: null, latest_charge: chargeId, status: 'requires_payment_method', last_payment_error }, 'payment_intent.payment_failed');
244
- return send(ctx, cardError(decline, { payment_intent: failed, charge: chargeId }));
245
- }
246
- // a bank debit that needs micro-deposit verification waits in requires_action
247
- const method = typeof params.payment_method === 'string' ? params.payment_method : typeof existing.payment_method === 'string' ? existing.payment_method : undefined;
248
- // a bank transfer is paid from the customer's cash balance, or waits for the transfer (fundFromCashBalance)
249
- if (method && ctx.get('payment_method', method)?.type === 'customer_balance')
250
- return ctx.reply(ctx.expand(PI, await fundFromCashBalance(ctx, { ...existing, ...params }, params)));
251
- if (requiresMicrodeposits(params, existing) || microdepositsAsked(ctx, params, existing, method)) {
252
- return ctx.reply(ctx.expand(PI, await ctx.write(PI, id, { ...params, status: 'requires_action', next_action: microdepositsNextAction(), last_payment_error: null }, 'payment_intent.requires_action')));
253
- }
254
- // 3DS: the first confirm asks for authentication; a second one completes it
255
- if (existing.status !== 'requires_action' && requiresAuthentication(params, existing)) {
256
- return ctx.reply(ctx.expand(PI, await ctx.write(PI, id, { ...params, status: 'requires_action', next_action: threeDsNextAction(), last_payment_error: null }, 'payment_intent.requires_action')));
257
- }
258
- // manual capture authorizes and waits for /capture: the authorization is a charge, succeeded and not captured, the
259
- // intent's latest_charge
260
- if ((params.capture_method ?? existing.capture_method) === 'manual') {
261
- const amount = Number(existing.amount ?? params.amount ?? 0);
262
- const charge = await authorizeCharge(ctx, id, { ...existing, ...params }, amount);
263
- return ctx.reply(ctx.expand(PI, await ctx.write(PI, id, { ...params, status: 'requires_capture', amount_capturable: amount, amount_received: 0, next_action: null, last_payment_error: null, latest_charge: charge }, 'payment_intent.amount_capturable_updated')));
264
- }
265
- // a bank debit is submitted and settles later (beginBankDebit)
266
- if (isBankDebit(ctx, method))
267
- return ctx.reply(ctx.expand(PI, await beginBankDebit(ctx, id, params)));
268
- const amount = Number(existing.amount ?? params.amount ?? 0);
269
- // a succeeded intent has a Charge, and latest_charge names it: what a refund of it lands on
270
- const charge = await mintCharge(ctx, id, { ...existing, ...params }, amount);
271
- const body = await ctx.write(PI, id, { ...params, status: 'succeeded', amount_received: amount, next_action: null, last_payment_error: null, latest_charge: charge }, 'payment_intent.confirm');
272
- await payInvoiceOf(ctx, body, 'PostPaymentIntentsIntentConfirm');
273
- return ctx.reply(ctx.expand(PI, body));
274
- }
275
- const capture = async (ctx) => {
276
- const loaded = load(ctx, 'PostPaymentIntentsIntentCapture');
277
- if ('answer' in loaded)
278
- return loaded.answer;
279
- const pi = loaded.pi;
280
- const authorized = Number(pi.amount_capturable ?? pi.amount ?? 0);
281
- // amount_to_capture lowers the captured amount (a partial capture); it never raises it
282
- const asked = ctx.params.amount_to_capture !== undefined ? Math.max(0, Math.trunc(Number(ctx.params.amount_to_capture) || 0)) : authorized;
283
- const captured = Math.min(asked, authorized);
284
- // the authorization confirm made is captured; an intent authorized before it was kept is charged as before
285
- const auth = typeof pi.latest_charge === 'string' ? ctx.get('charge', pi.latest_charge) : undefined;
286
- const held = auth && auth.status === 'succeeded' && auth.captured === false ? auth : undefined;
287
- if (held) {
288
- const refused = ctx.legal('charge', 'captured', 'PostPaymentIntentsIntentCapture', 'false', 'true', String(held.id));
289
- if (refused)
290
- return ctx.refuse(refused);
291
- await captureAuthorization(ctx, held, captured, 'PostPaymentIntentsIntentCapture');
292
- await afterCharge(ctx, String(held.id), { amount: captured, currency: String(pi.currency ?? 'usd'), payment_intent: String(pi.id), application_fee_amount: pi.application_fee_amount, destination: pi.transfer_data?.destination }, pi.payment_method);
293
- }
294
- const charge = held ? String(held.id) : await mintCharge(ctx, String(pi.id), pi, captured);
295
- const body = await ctx.write(PI, String(pi.id), { status: 'succeeded', amount_received: captured, amount_capturable: 0, latest_charge: charge }, 'payment_intent.succeeded');
296
- await payInvoiceOf(ctx, body, 'PostPaymentIntentsIntentCapture');
297
- return ctx.reply(ctx.expand(PI, body));
298
- };
299
- /** An increment that does not raise the authorization. */
300
- function incrementTooSmall(ctx) {
301
- return ctx.refuse({ status: 400, code: 'parameter_invalid_integer', message: 'The new amount must be greater than the current amount of the PaymentIntent.' });
302
- }
303
- const incrementAuthorization = async (ctx) => {
304
- const loaded = load(ctx, 'PostPaymentIntentsIntentIncrementAuthorization');
305
- if ('answer' in loaded)
306
- return loaded.answer;
307
- const pi = loaded.pi;
308
- if (ctx.params.amount === undefined)
309
- return ctx.refuse({ status: 400, code: 'parameter_missing', message: 'Missing required param: amount.' });
310
- const amount = Math.trunc(Number(ctx.params.amount) || 0);
311
- if (!Number.isInteger(amount) || amount <= Number(pi.amount ?? 0))
312
- return incrementTooSmall(ctx);
313
- // the authorization the charge holds grows with it: "If the incremental authorization fails ... no other fields on
314
- // the PaymentIntent or Charge update" (docs.stripe.com/api/payment_intents/increment_authorization), so on success the
315
- // charge's amount is the new authorized amount. Where the documentation stops and the twin decides: the charge's
316
- // update sends no event of its own (the events page names none for it).
317
- const held = typeof pi.latest_charge === 'string' ? ctx.get('charge', pi.latest_charge) : undefined;
318
- if (held && held.captured === false)
319
- await ctx.write('charge', String(held.id), { amount }, 'charge.authorization_incremented');
320
- return ctx.reply(ctx.expand(PI, await ctx.write(PI, String(pi.id), { amount, amount_capturable: amount }, 'payment_intent.amount_capturable_updated')));
321
- };
322
- const cancel = async (ctx) => {
323
- const loaded = load(ctx, 'PostPaymentIntentsIntentCancel');
324
- if ('answer' in loaded)
325
- return loaded.answer;
326
- const cancellation_reason = typeof ctx.params.cancellation_reason === 'string' ? ctx.params.cancellation_reason : 'requested_by_customer';
327
- // "For PaymentIntents with a `status` of `requires_capture`, the remaining `amount_capturable` is automatically
328
- // refunded" (docs.stripe.com/api/payment_intents/cancel): the authorization is released (charges.ts)
329
- const held = loaded.pi.status === 'requires_capture' && typeof loaded.pi.latest_charge === 'string' ? ctx.get('charge', loaded.pi.latest_charge) : undefined;
330
- if (held && held.captured === false && held.refunded !== true)
331
- await cancelAuthorization(ctx, held);
332
- return ctx.reply(ctx.expand(PI, await ctx.write(PI, String(loaded.pi.id), { status: 'canceled', cancellation_reason, amount_capturable: 0 }, 'payment_intent.canceled')));
333
- };
334
- const verifyMicrodepositsHandler = async (ctx) => {
335
- const pi = ctx.id ? ctx.get(PI, ctx.id) : undefined;
336
- if (!pi)
337
- return ctx.notFound(PI, String(ctx.id));
338
- // the deposit amounts are checked before the intent's state, as Stripe does
339
- const wrong = verifyMicrodeposits(ctx.params, pi);
340
- if (wrong)
341
- return send(ctx, wrong);
342
- const refusal = ctx.legal(PI, 'status', 'PostPaymentIntentsIntentVerifyMicrodeposits', pi.status);
343
- if (refusal)
344
- return ctx.refuse(refusal);
345
- const amount = Number(pi.amount) || 0;
346
- // a debit saved for reuse ("If you want to reuse the payment method in the future, provide the setup_future_usage
347
- // parameter with the value of off_session", the ACH page) is authorized for many payments (multi_use, "Represents
348
- // permission given for multiple payments"); else for this one (single_use, "a one-time permission given for a single
349
- // payment", docs.stripe.com/api/mandates/object)
350
- const reuse = pi.setup_future_usage === 'off_session' || pi.setup_future_usage === 'on_session';
351
- const mandate = reuse ? await mintMandate(ctx, pi.payment_method, 'multi_use') : await mintMandate(ctx, pi.payment_method, 'single_use', amount, String(pi.currency ?? 'usd'));
352
- // "When the bank account is successfully verified, Stripe returns the PaymentIntent object with a status of
353
- // `processing`" (docs.stripe.com/payments/ach-direct-debit/accept-a-payment?payment-ui=direct-api)
354
- return ctx.reply(ctx.expand(PI, await beginBankDebit(ctx, String(pi.id), { mandate })));
355
- };
356
- // ── a bank debit is submitted, and settles ──
357
- //
358
- // ACH Direct Debit "is a delayed notification payment method ... The PaymentIntent you create initially has a status
359
- // of `processing`. After the payment has succeeded, the PaymentIntent status is updated from `processing` to
360
- // `succeeded`"; verification by micro-deposits returns "a status of `processing`, and sends a payment_intent.processing
361
- // webhook event"; and, the twin being a test-mode account: "Test transactions settle instantly and are added to your
362
- // available test balance. This behavior differs from live mode, where transactions can take multiple days to settle"
363
- // (docs.stripe.com/payments/ach-direct-debit/accept-a-payment?payment-ui=direct-api). A confirm answers processing; the
364
- // debit succeeds at that same instant of the World's clock, caught up before the next request is answered (as
365
- // renewals and payouts are). The page's test accounts give each debit its outcome: pm_usBankAccount_success
366
- // (000123456789) "The payment succeeds"; pm_usBankAccount_processing (000000000009) "The payment stays in processing
367
- // indefinitely". Where the documentation stops and the twin decides: its Charge is made when it succeeds (the page
368
- // names none while it processes); the failing test accounts (closed, no account, insufficient funds, debit not
369
- // authorized, invalid currency, dispute, weekly limit, Radar block) are not modelled and succeed.
370
- /** Whether the method an intent is confirmed with is a bank debit: a us_bank_account PaymentMethod, or one of Stripe's
371
- * test bank accounts named as one (pm_usBankAccount_*, pm_us_bank_account). */
372
- export function isBankDebit(ctx, method) {
373
- if (typeof method !== 'string')
374
- return false;
375
- return ctx.get('payment_method', method)?.type === 'us_bank_account' || /^pm_us_?bank_?account/i.test(method);
376
- }
377
- /** Whether a bank debit stays processing: the page's pm_usBankAccount_processing, or its account 000000000009. */
378
- function staysProcessing(ctx, method) {
379
- if (method === 'pm_usBankAccount_processing')
380
- return true;
381
- const bank = typeof method === 'string' ? ctx.get('payment_method', method)?.us_bank_account : undefined;
382
- return bank?.last4 === '0009';
383
- }
384
- /** A bank debit submitted: the intent processes, nothing received yet, sent as payment_intent.processing; it is due to
385
- * settle at this instant, unless its test account stays processing. */
386
- async function beginBankDebit(ctx, id, fields) {
387
- const method = fields.payment_method ?? ctx.get(PI, id)?.payment_method;
388
- return ctx.write(PI, id, { ...fields, status: 'processing', amount_received: 0, next_action: null, last_payment_error: null, _settles_at: staysProcessing(ctx, method) ? null : Number(ctx.now()) }, 'payment_intent.processing');
389
- }
390
- /** Time's settling of bank debits, caught up to the World's clock: each processing debit whose moment has come succeeds
391
- * then, its Charge made (naming the mandate it ran under) and the invoice it collects for paid. */
392
- export async function settleBankDebits(ctx) {
393
- const now = Number(ctx.now());
394
- const due = ctx.rowsRaw(PI).filter((p) => p.status === 'processing' && typeof p._settles_at === 'number' && p._settles_at <= now).sort((a, b) => Number(a._settles_at) - Number(b._settles_at));
395
- for (const pi of due) {
396
- const id = String(pi.id);
397
- const c = await at_(ctx)(Number(pi._settles_at));
398
- c.legal(PI, 'status', ctx.call.operation.id, 'processing', 'succeeded', id, 'vendor');
399
- const amount = Number(pi.amount) || 0;
400
- const charge = await mintCharge(c, id, pi, amount);
401
- // the charge names the mandate its bank debit ran under (payment_method_details.us_bank_account.mandate)
402
- const details = (c.get('charge', charge)?.payment_method_details ?? null);
403
- const kind = typeof details?.type === 'string' ? details.type : undefined;
404
- if (details && kind && typeof pi.mandate === 'string')
405
- await c.write('charge', charge, { payment_method_details: { ...details, [kind]: { ...(details[kind] ?? {}), mandate: pi.mandate } } }, 'charge.mandate');
406
- const body = await c.write(PI, id, { status: 'succeeded', amount_received: amount, latest_charge: charge, _settles_at: null }, 'payment_intent.succeeded');
407
- // a single-use mandate is spent by its payment: "previously used, and may not be used to initiate future payments"
408
- const used = typeof pi.mandate === 'string' ? c.get('mandate', pi.mandate) : undefined;
409
- if (used && used.type === 'single_use' && used.status === 'active') {
410
- c.legal('mandate', 'status', ctx.call.operation.id, 'active', 'inactive', String(used.id), 'vendor');
411
- await c.write('mandate', String(used.id), { status: 'inactive' }, 'mandate.updated');
412
- }
413
- await payInvoiceOf(c, body, ctx.call.operation.id, 'vendor');
414
- }
415
- }
416
- /** A customer_balance intent funded from the customer's cash balance: paid when the balance covers it ("If the customer
417
- * already has a balance high enough to cover the payment amount, the PaymentIntent immediately succeeds"), else
418
- * waiting for a transfer: "If the customer balance isn’t high enough to cover the request amount, the PaymentIntent
419
- * shows a requires_action status ... next_action ... display_bank_transfer_instructions" with the amount_remaining
420
- * (docs.stripe.com/payments/bank-transfers/accept-a-payment). Answers the intent as written. */
421
- async function fundFromCashBalance(ctx, pi, fields = {}) {
422
- const customer = typeof pi.customer === 'string' ? pi.customer : '';
423
- const currency = String(pi.currency ?? 'usd');
424
- // what the customer's funded cash balance holds in this currency, from its ledger
425
- const available = customer ? ctx.rows('customer_cash_balance_transaction').filter((t) => t.customer === customer && String(t.currency) === currency).reduce((n, t) => n + (Number(t.net_amount) || 0), 0) : 0;
426
- const amount = Number(pi.amount) || 0;
427
- if (available >= amount) {
428
- // the balance pays the intent: its ledger records what was applied
429
- await created(ctx, 'customer_cash_balance_transaction', { customer }, { currency, type: 'applied_to_payment', net_amount: -amount, ending_balance: available - amount, applied_to_payment: { payment_intent: String(pi.id) }, livemode: false });
430
- const charge = await mintCharge(ctx, String(pi.id), { ...pi, ...fields }, amount);
431
- return ctx.write(PI, String(pi.id), { ...fields, status: 'succeeded', next_action: null, amount_received: amount, last_payment_error: null, latest_charge: charge }, 'payment_intent.succeeded');
432
- }
433
- // not enough yet: the intent waits for the rest, with the instructions to send it
434
- const next_action = { type: 'display_bank_transfer_instructions', display_bank_transfer_instructions: { amount_remaining: amount - available, currency, type: 'us_bank_transfer' } };
435
- return ctx.write(PI, String(pi.id), { ...fields, status: 'requires_action', next_action }, 'payment_intent.requires_action');
436
- }
437
- const applyCustomerBalance = async (ctx) => {
438
- const loaded = load(ctx, 'PostPaymentIntentsIntentApplyCustomerBalance');
439
- if ('answer' in loaded)
440
- return loaded.answer;
441
- return ctx.reply(ctx.expand(PI, await fundFromCashBalance(ctx, loaded.pi)));
442
- };
443
- export const paymentIntents = {
444
- PostPaymentIntents: create,
445
- GetPaymentIntentsSearch: async (ctx) => search(ctx, PI),
446
- PostPaymentIntentsIntentConfirm: confirm,
447
- PostPaymentIntentsIntentCapture: capture,
448
- PostPaymentIntentsIntentIncrementAuthorization: incrementAuthorization,
449
- PostPaymentIntentsIntentCancel: cancel,
450
- PostPaymentIntentsIntentVerifyMicrodeposits: verifyMicrodepositsHandler,
451
- PostPaymentIntentsIntentApplyCustomerBalance: applyCustomerBalance,
452
- };
@@ -1,2 +0,0 @@
1
- import type { Semantics } from '@volter/world-core';
2
- export declare const paymentLinks: Record<string, Semantics>;
@@ -1,133 +0,0 @@
1
- import { asBool, lineItemEntries, paginate, resolveLineItem } from "../stripe-twin.js";
2
- import { draftInvoiceFor } from "./invoices.js";
3
- import { at, created, fail, finder, list, newest, path, where } from "./shared.js";
4
- /** The request's line items resolved against their prices, or the refusal for a missing price. */
5
- function resolved(ctx) {
6
- const entries = lineItemEntries(ctx.params);
7
- const items = [];
8
- for (let i = 0; i < entries.length; i++) {
9
- const { item, priceMissing } = resolveLineItem(entries[i], i, 'usd', finder(ctx));
10
- if (priceMissing)
11
- return fail(ctx, `No such price: '${priceMissing}'`, 400, 'resource_missing');
12
- items.push(item);
13
- }
14
- return items;
15
- }
16
- /** A page of the line items a stored object carries. */
17
- function linePage(ctx, holder) {
18
- const li = holder.line_items;
19
- const { page, hasMore } = paginate(Array.isArray(li?.data) ? li.data : [], ctx.params);
20
- return ctx.reply({ object: 'list', url: path(ctx), has_more: hasMore, data: page });
21
- }
22
- const PL = 'payment_link';
23
- const linkMissing = (ctx, id) => fail(ctx, `No such payment link: '${id}'`, 404, 'resource_missing');
24
- const createLink = async (ctx) => {
25
- if (lineItemEntries(ctx.params).length === 0)
26
- return fail(ctx, 'Missing required param: line_items.', 400, 'parameter_missing');
27
- const items = resolved(ctx);
28
- if (items instanceof Response)
29
- return items;
30
- const id = ctx.mint(PL);
31
- const { line_items: _li, ...rest } = ctx.params;
32
- return ctx.reply(await created(ctx, PL, { ...rest, id }, {
33
- active: ctx.params.active !== undefined ? asBool(ctx.params.active) : true,
34
- url: `https://buy.twin.local/${id}`, livemode: false, metadata: {},
35
- // Stripe's defaults for what the link was not given (docs.stripe.com/api/payment-link/create)
36
- allow_promotion_codes: false, automatic_tax: { enabled: false, liability: null }, billing_address_collection: 'auto',
37
- custom_fields: [], custom_text: { after_submit: null, shipping_address: null, submit: null, terms_of_service_acceptance: null },
38
- customer_creation: 'if_required', payment_method_collection: 'always', phone_number_collection: { enabled: false },
39
- shipping_options: [], submit_type: 'auto', tax_id_collection: { enabled: false, required: 'never' },
40
- line_items: { object: 'list', data: items, has_more: false, url: `/v1/payment_links/${id}/line_items` },
41
- currency: items[0]?.currency ?? 'usd',
42
- after_completion: { type: 'redirect', redirect: typeof ctx.params.after_completion === 'object' ? ctx.params.after_completion : null },
43
- }));
44
- };
45
- const listLinks = async (ctx) => list(ctx, PL, where(ctx, newest(ctx, PL), { active: (p, v) => asBool(p.active) === asBool(v) }));
46
- const linkLines = async (ctx) => {
47
- const p = ctx.get(PL, at(ctx, 'payment_link'));
48
- return p ? linePage(ctx, p) : linkMissing(ctx, at(ctx, 'payment_link'));
49
- };
50
- // ── quotes ──
51
- const quoteMissing = (ctx, id) => fail(ctx, `No such quote: '${id}'`, 404, 'resource_missing');
52
- const createQuote = async (ctx) => {
53
- const customer = typeof ctx.params.customer === 'string' ? ctx.params.customer : '';
54
- if (!customer)
55
- return fail(ctx, 'Missing required param: customer.', 400, 'parameter_missing');
56
- if (!ctx.row('customer', customer, { withDeleted: true }))
57
- return fail(ctx, `No such customer: '${customer}'`, 400, 'resource_missing');
58
- const items = resolved(ctx);
59
- if (items instanceof Response)
60
- return items;
61
- const subtotal = items.reduce((s, it) => s + it.amount_subtotal, 0);
62
- const id = ctx.mint('quote');
63
- const { line_items: _li, ...rest } = ctx.params;
64
- return ctx.reply(await created(ctx, 'quote', { ...rest, id }, {
65
- status: 'draft', customer, currency: items[0]?.currency ?? 'usd', livemode: false, metadata: {},
66
- amount_subtotal: subtotal, amount_total: subtotal,
67
- // Stripe's defaults (docs.stripe.com/api/quotes/create); what is paid up front is the whole quote, and the twin bills in classic mode
68
- automatic_tax: { enabled: false, liability: null, provider: null, status: null }, collection_method: 'charge_automatically', discounts: [],
69
- invoice_settings: { days_until_due: null, issuer: { type: 'self' } },
70
- subscription_data: { billing_mode: { type: 'classic' }, description: null, effective_date: null, metadata: null, trial_period_days: null },
71
- computed: { recurring: null, upfront: { amount_subtotal: subtotal, amount_total: subtotal, total_details: { amount_discount: 0, amount_shipping: 0, amount_tax: 0 } } },
72
- line_items: { object: 'list', data: items, has_more: false, url: `/v1/quotes/${id}/line_items` },
73
- total_details: { amount_discount: 0, amount_shipping: 0, amount_tax: 0 },
74
- expires_at: Number(ctx.now()) + 30 * 24 * 3600,
75
- number: null, invoice: null, subscription: null,
76
- status_transitions: { accepted_at: null, canceled_at: null, finalized_at: null },
77
- }));
78
- };
79
- const quoteLines = async (ctx) => {
80
- const q = ctx.get('quote', at(ctx, 'quote'));
81
- return q ? linePage(ctx, q) : quoteMissing(ctx, at(ctx, 'quote'));
82
- };
83
- // Stripe streams the PDF; the twin answers a stable reference to it
84
- const pdf = async (ctx) => {
85
- const id = at(ctx, 'quote');
86
- if (!ctx.get('quote', id))
87
- return quoteMissing(ctx, id);
88
- return ctx.reply({ object: 'quote_pdf', url: `https://files.twin.local/quotes/${id}.pdf` });
89
- };
90
- function move(action, operationId) {
91
- return async (ctx) => {
92
- const id = at(ctx, 'quote');
93
- const q = ctx.get('quote', id);
94
- if (!q)
95
- return quoteMissing(ctx, id);
96
- const refused = ctx.legal('quote', 'status', operationId, q.status, undefined, id);
97
- if (refused)
98
- return ctx.refuse(refused);
99
- const now = ctx.now();
100
- const stamp = action === 'finalize' ? { finalized_at: now } : action === 'accept' ? { accepted_at: now } : { canceled_at: now };
101
- // "Accepted quotes automatically generate an invoice, subscription, or subscription schedule"; "Quotes without
102
- // recurring prices: A draft invoice is created with auto_advance set to false" (docs.stripe.com/quotes/overview),
103
- // the quote its parent ("Details about the quote that generated this invoice", the served spec). Where the
104
- // documentation stops and the twin decides: a quote with recurring prices makes no subscription yet.
105
- let made = {};
106
- if (action === 'accept') {
107
- const lines = q.line_items?.data ?? [];
108
- const recurring = lines.some((l) => l.price && typeof l.price === 'object' && l.price.recurring);
109
- if (!recurring && typeof q.customer === 'string') {
110
- const invoice = await draftInvoiceFor(ctx, q.customer, String(q.currency ?? 'usd'), { type: 'quote_details', quote_details: { quote: id }, subscription_details: null }, lines.map((l) => ({ price: l.price, quantity: Number(l.quantity) || 1, amount: Number(l.amount_subtotal ?? l.amount_total) || 0, description: l.description })));
111
- made = { invoice };
112
- }
113
- }
114
- return ctx.reply(await ctx.write('quote', id, {
115
- status: action === 'finalize' ? 'open' : action === 'accept' ? 'accepted' : 'canceled',
116
- status_transitions: { ...(q.status_transitions ?? {}), ...stamp },
117
- ...(action === 'finalize' ? { number: `QT-${id.toUpperCase()}` } : {}),
118
- ...made,
119
- }, `quote.${action}`));
120
- };
121
- }
122
- export const paymentLinks = {
123
- PostPaymentLinks: createLink,
124
- GetPaymentLinks: listLinks,
125
- GetPaymentLinksPaymentLinkLineItems: linkLines,
126
- PostQuotes: createQuote,
127
- GetQuotesQuoteLineItems: quoteLines,
128
- GetQuotesQuoteComputedUpfrontLineItems: quoteLines,
129
- GetQuotesQuotePdf: pdf,
130
- PostQuotesQuoteFinalize: move('finalize', 'PostQuotesQuoteFinalize'),
131
- PostQuotesQuoteAccept: move('accept', 'PostQuotesQuoteAccept'),
132
- PostQuotesQuoteCancel: move('cancel', 'PostQuotesQuoteCancel'),
133
- };
@@ -1,20 +0,0 @@
1
- import type { Semantics, SemanticsContext } from '@volter/world-core';
2
- import { type Row } from './shared.js';
3
- /** The card a charge was paid with, as its payment_method_details reports it
4
- * (docs.stripe.com/api/charges/object#charge_object-payment_method_details): a stored PaymentMethod's card, a
5
- * documented test name (pm_card_* or tok_*, the same cards), or a raw card; undefined for anything else. */
6
- export declare function paymentMethodDetails(ctx: SemanticsContext, ref: unknown): Row | undefined;
7
- /** Whether a payment method can be attached: a stored one, or a documented test name that attaching mints. */
8
- export declare const attachable: (ctx: SemanticsContext, id: string) => boolean;
9
- export declare function attachPaymentMethod(ctx: SemanticsContext, id: string, customer: string): Promise<Row>;
10
- /** A documented test name (pm_card_visa, pm_card_visa_chargeDeclined, …) made a real PaymentMethod, as Stripe
11
- * does when one is used: the brand and number it stands for (docs.stripe.com/testing#cards) and its outcome. */
12
- export declare function materializeTestMethod(ctx: SemanticsContext, id: string, customer: string | null): Promise<Row>;
13
- export declare const paymentMethods: Record<string, Semantics>;
14
- /** A PaymentMethod made from a confirm's `payment_method_data` ("If provided, this hash will be used to create a
15
- * PaymentMethod", docs.stripe.com/api/setup_intents/confirm, docs.stripe.com/api/payment_intents/confirm), or none. */
16
- export declare function methodFromData(ctx: SemanticsContext, data: unknown): Promise<string | undefined>;
17
- /** Whether an intent sets up or pays with a bank account by micro-deposits because it asks for them:
18
- * payment_method_options[us_bank_account][verification_method]=microdeposits with a us_bank_account method
19
- * ("Microdeposit only verification", docs.stripe.com/payments/ach-direct-debit/set-up-payment). */
20
- export declare function microdepositsAsked(ctx: SemanticsContext, params: Row, intent: Row, pm: string | undefined): boolean;