@hyperscale0/hsx 2.4.1 → 3.0.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 (249) hide show
  1. package/CHANGELOG.md +2 -408
  2. package/README.md +3 -92
  3. package/dist/src/ast.d.ts +99 -310
  4. package/dist/src/ast.d.ts.map +1 -1
  5. package/dist/src/ast.js +16 -69
  6. package/dist/src/ast.js.map +1 -1
  7. package/dist/src/cli.d.ts +6 -19
  8. package/dist/src/cli.d.ts.map +1 -1
  9. package/dist/src/cli.js +76 -315
  10. package/dist/src/cli.js.map +1 -1
  11. package/dist/src/compile.d.ts +23 -73
  12. package/dist/src/compile.d.ts.map +1 -1
  13. package/dist/src/compile.js +1113 -148
  14. package/dist/src/compile.js.map +1 -1
  15. package/dist/src/cost.d.ts +8 -77
  16. package/dist/src/cost.d.ts.map +1 -1
  17. package/dist/src/cost.js +13 -422
  18. package/dist/src/cost.js.map +1 -1
  19. package/dist/src/format.d.ts +8 -7
  20. package/dist/src/format.d.ts.map +1 -1
  21. package/dist/src/format.js +9 -175
  22. package/dist/src/format.js.map +1 -1
  23. package/dist/src/headers.d.ts +35 -0
  24. package/dist/src/headers.d.ts.map +1 -0
  25. package/dist/src/headers.js +108 -0
  26. package/dist/src/headers.js.map +1 -0
  27. package/dist/src/index.d.ts +6 -12
  28. package/dist/src/index.d.ts.map +1 -1
  29. package/dist/src/index.js +4 -9
  30. package/dist/src/index.js.map +1 -1
  31. package/dist/src/lex.d.ts +8 -28
  32. package/dist/src/lex.d.ts.map +1 -1
  33. package/dist/src/lex.js +88 -146
  34. package/dist/src/lex.js.map +1 -1
  35. package/dist/src/parse.d.ts +4 -17
  36. package/dist/src/parse.d.ts.map +1 -1
  37. package/dist/src/parse.js +402 -1040
  38. package/dist/src/parse.js.map +1 -1
  39. package/dist/src/std-bundle.d.ts.map +1 -1
  40. package/dist/src/std-bundle.js +12 -20
  41. package/dist/src/std-bundle.js.map +1 -1
  42. package/dist/src/std-library.d.ts +1 -1
  43. package/dist/src/std-library.d.ts.map +1 -1
  44. package/dist/src/std-library.js +1 -5
  45. package/dist/src/std-library.js.map +1 -1
  46. package/dist/src/tunables.d.ts +7 -0
  47. package/dist/src/tunables.d.ts.map +1 -0
  48. package/dist/src/tunables.js +26 -0
  49. package/dist/src/tunables.js.map +1 -0
  50. package/dist/src/version.d.ts +2 -4
  51. package/dist/src/version.d.ts.map +1 -1
  52. package/dist/src/version.js +2 -4
  53. package/dist/src/version.js.map +1 -1
  54. package/docs/README.md +179 -41
  55. package/docs/headers.md +44 -0
  56. package/examples/cost-table.json +99 -751
  57. package/examples/library.hsx +59 -0
  58. package/package.json +9 -17
  59. package/src/ast.ts +82 -441
  60. package/src/cli.ts +77 -362
  61. package/src/compile.ts +1535 -247
  62. package/src/cost.ts +28 -671
  63. package/src/format.ts +12 -256
  64. package/src/headers.ts +126 -0
  65. package/src/index.ts +7 -39
  66. package/src/lex.ts +99 -195
  67. package/src/parse.ts +403 -1209
  68. package/src/std-bundle.ts +12 -20
  69. package/src/std-library.ts +2 -7
  70. package/src/tunables.ts +31 -0
  71. package/src/version.ts +2 -5
  72. package/std/approvals.hsx +17 -0
  73. package/std/cards.hsx +63 -0
  74. package/std/collections.hsx +31 -0
  75. package/std/escrow.hsx +43 -0
  76. package/std/financing.hsx +372 -0
  77. package/std/insurance.hsx +59 -0
  78. package/std/lending.hsx +115 -0
  79. package/std/marketplace.hsx +24 -0
  80. package/std/money.hsx +195 -0
  81. package/std/savings.hsx +44 -0
  82. package/std/travel.hsx +98 -0
  83. package/std/wallet.hsx +41 -0
  84. package/dist/src/diagnostics.d.ts +0 -13
  85. package/dist/src/diagnostics.d.ts.map +0 -1
  86. package/dist/src/diagnostics.js +0 -587
  87. package/dist/src/diagnostics.js.map +0 -1
  88. package/dist/src/emit.d.ts +0 -51
  89. package/dist/src/emit.d.ts.map +0 -1
  90. package/dist/src/emit.js +0 -192
  91. package/dist/src/emit.js.map +0 -1
  92. package/dist/src/entry-overrides.d.ts +0 -58
  93. package/dist/src/entry-overrides.d.ts.map +0 -1
  94. package/dist/src/entry-overrides.js +0 -284
  95. package/dist/src/entry-overrides.js.map +0 -1
  96. package/dist/src/ir.d.ts +0 -73
  97. package/dist/src/ir.d.ts.map +0 -1
  98. package/dist/src/ir.js +0 -15
  99. package/dist/src/ir.js.map +0 -1
  100. package/dist/src/limits.d.ts +0 -23
  101. package/dist/src/limits.d.ts.map +0 -1
  102. package/dist/src/limits.js +0 -23
  103. package/dist/src/limits.js.map +0 -1
  104. package/dist/src/lsp/server.d.ts +0 -32
  105. package/dist/src/lsp/server.d.ts.map +0 -1
  106. package/dist/src/lsp/server.js +0 -391
  107. package/dist/src/lsp/server.js.map +0 -1
  108. package/dist/src/modules.d.ts +0 -38
  109. package/dist/src/modules.d.ts.map +0 -1
  110. package/dist/src/modules.js +0 -372
  111. package/dist/src/modules.js.map +0 -1
  112. package/dist/src/typecheck.d.ts +0 -8
  113. package/dist/src/typecheck.d.ts.map +0 -1
  114. package/dist/src/typecheck.js +0 -4160
  115. package/dist/src/typecheck.js.map +0 -1
  116. package/docs/guide/01-first-program.md +0 -24
  117. package/docs/guide/02-money.md +0 -25
  118. package/docs/guide/03-instruments.md +0 -93
  119. package/docs/guide/04-lifecycles.md +0 -40
  120. package/docs/guide/05-fees-and-splits.md +0 -45
  121. package/docs/guide/06-schedules.md +0 -26
  122. package/docs/guide/07-composition.md +0 -52
  123. package/docs/guide/08-writing-a-module.md +0 -85
  124. package/docs/guide/09-cost.md +0 -24
  125. package/docs/guide/10-diagnostics.md +0 -27
  126. package/docs/llms-full.txt +0 -3684
  127. package/docs/llms.txt +0 -44
  128. package/docs/piece-plans.md +0 -120
  129. package/docs/playground.md +0 -55
  130. package/docs/reference/cli.md +0 -36
  131. package/docs/reference/diagnostics.md +0 -901
  132. package/docs/reference/grammar.md +0 -38
  133. package/docs/reference/std/advance.md +0 -112
  134. package/docs/reference/std/cancellable_booking.md +0 -159
  135. package/docs/reference/std/captured_payment.md +0 -115
  136. package/docs/reference/std/conditional_disbursement.md +0 -101
  137. package/docs/reference/std/credit_facility.md +0 -108
  138. package/docs/reference/std/held_payment.md +0 -173
  139. package/docs/reference/std/instant_transfer.md +0 -97
  140. package/docs/reference/std/metered.md +0 -89
  141. package/docs/reference/std/pooled_split.md +0 -95
  142. package/docs/reference/std/premium_forward.md +0 -119
  143. package/docs/reference/std/reconciled_payout.md +0 -103
  144. package/docs/reference/std/rotating_pool.md +0 -130
  145. package/docs/reference/std/scheduled.md +0 -136
  146. package/docs/reference/std/security_deposit.md +0 -138
  147. package/docs/reference/std/settlement_batch.md +0 -123
  148. package/docs/reference/std/swap.md +0 -133
  149. package/docs/reference/std/threshold_pool.md +0 -138
  150. package/docs/reference/std/weighted_distribution.md +0 -135
  151. package/docs/reference/types.md +0 -61
  152. package/docs/reference/udl-output.md +0 -13
  153. package/examples/01-first-program/README.md +0 -45
  154. package/examples/01-first-program/tip-jar.hsx +0 -17
  155. package/examples/02-imports-and-modules/README.md +0 -96
  156. package/examples/02-imports-and-modules/photo-booth.hsx +0 -37
  157. package/examples/03-diagnostics/README.md +0 -68
  158. package/examples/03-diagnostics/corner-shop-fixed.hsx +0 -22
  159. package/examples/03-diagnostics/corner-shop.hsx +0 -19
  160. package/examples/04-complete-product/README.md +0 -84
  161. package/examples/04-complete-product/study-hall.hsx +0 -67
  162. package/examples/05-authored-instrument/README.md +0 -5
  163. package/examples/05-authored-instrument/payment.hsx +0 -37
  164. package/examples/05-watch-club/README.md +0 -6
  165. package/examples/05-watch-club/watch-club.hsx +0 -123
  166. package/examples/README.md +0 -48
  167. package/examples/advance/README.md +0 -3
  168. package/examples/advance/advance.hsx +0 -13
  169. package/examples/advance/advance.udl +0 -367
  170. package/examples/cancellable_booking/README.md +0 -3
  171. package/examples/cancellable_booking/cancellable_booking.hsx +0 -14
  172. package/examples/cancellable_booking/cancellable_booking.udl +0 -413
  173. package/examples/captured_payment/README.md +0 -3
  174. package/examples/captured_payment/captured_payment.hsx +0 -17
  175. package/examples/captured_payment/captured_payment.udl +0 -686
  176. package/examples/conditional_disbursement/README.md +0 -3
  177. package/examples/conditional_disbursement/conditional_disbursement.hsx +0 -15
  178. package/examples/conditional_disbursement/conditional_disbursement.udl +0 -418
  179. package/examples/credit_facility/README.md +0 -3
  180. package/examples/credit_facility/credit_facility.hsx +0 -24
  181. package/examples/credit_facility/credit_facility.udl +0 -1396
  182. package/examples/held_payment/README.md +0 -3
  183. package/examples/held_payment/held_payment.hsx +0 -13
  184. package/examples/held_payment/held_payment.udl +0 -569
  185. package/examples/instant_transfer/README.md +0 -3
  186. package/examples/instant_transfer/instant_transfer.hsx +0 -16
  187. package/examples/instant_transfer/instant_transfer.udl +0 -376
  188. package/examples/metered/README.md +0 -3
  189. package/examples/metered/metered.hsx +0 -13
  190. package/examples/metered/metered.udl +0 -253
  191. package/examples/pooled_split/README.md +0 -3
  192. package/examples/pooled_split/pooled_split.hsx +0 -15
  193. package/examples/pooled_split/pooled_split.udl +0 -432
  194. package/examples/premium_forward/README.md +0 -3
  195. package/examples/premium_forward/premium_forward.hsx +0 -19
  196. package/examples/premium_forward/premium_forward.udl +0 -604
  197. package/examples/reconciled_payout/README.md +0 -3
  198. package/examples/reconciled_payout/reconciled_payout.hsx +0 -13
  199. package/examples/reconciled_payout/reconciled_payout.udl +0 -312
  200. package/examples/rotating_pool/README.md +0 -3
  201. package/examples/rotating_pool/rotating_pool.hsx +0 -18
  202. package/examples/rotating_pool/rotating_pool.udl +0 -4370
  203. package/examples/scheduled/README.md +0 -3
  204. package/examples/scheduled/scheduled.hsx +0 -12
  205. package/examples/scheduled/scheduled.udl +0 -355
  206. package/examples/security_deposit/README.md +0 -3
  207. package/examples/security_deposit/security_deposit.hsx +0 -21
  208. package/examples/security_deposit/security_deposit.udl +0 -320
  209. package/examples/settlement_batch/README.md +0 -3
  210. package/examples/settlement_batch/settlement_batch.hsx +0 -19
  211. package/examples/settlement_batch/settlement_batch.udl +0 -757
  212. package/examples/swap/README.md +0 -3
  213. package/examples/swap/swap.hsx +0 -19
  214. package/examples/swap/swap.udl +0 -892
  215. package/examples/threshold_pool/README.md +0 -3
  216. package/examples/threshold_pool/threshold_pool.hsx +0 -16
  217. package/examples/threshold_pool/threshold_pool.udl +0 -680
  218. package/examples/weighted_distribution/README.md +0 -3
  219. package/examples/weighted_distribution/weighted_distribution.hsx +0 -20
  220. package/examples/weighted_distribution/weighted_distribution.udl +0 -348
  221. package/skills/hsx/SKILL.md +0 -449
  222. package/src/diagnostics.ts +0 -620
  223. package/src/emit.ts +0 -282
  224. package/src/entry-overrides.ts +0 -428
  225. package/src/ir.ts +0 -113
  226. package/src/limits.ts +0 -22
  227. package/src/lsp/server.ts +0 -460
  228. package/src/modules.ts +0 -452
  229. package/src/typecheck.ts +0 -5515
  230. package/std/SEMANTICS.md +0 -44
  231. package/std/money_flows/advance.hsx +0 -235
  232. package/std/money_flows/cancellable_booking.hsx +0 -402
  233. package/std/money_flows/captured_payment.hsx +0 -220
  234. package/std/money_flows/conditional_disbursement.hsx +0 -226
  235. package/std/money_flows/credit_facility.hsx +0 -214
  236. package/std/money_flows/held_payment.hsx +0 -812
  237. package/std/money_flows/index.hsx +0 -4
  238. package/std/money_flows/instant_transfer.hsx +0 -231
  239. package/std/money_flows/metered.hsx +0 -104
  240. package/std/money_flows/pooled_split.hsx +0 -109
  241. package/std/money_flows/premium_forward.hsx +0 -307
  242. package/std/money_flows/reconciled_payout.hsx +0 -119
  243. package/std/money_flows/rotating_pool.hsx +0 -305
  244. package/std/money_flows/scheduled.hsx +0 -599
  245. package/std/money_flows/security_deposit.hsx +0 -248
  246. package/std/money_flows/settlement_batch.hsx +0 -173
  247. package/std/money_flows/swap.hsx +0 -1192
  248. package/std/money_flows/threshold_pool.hsx +0 -307
  249. package/std/money_flows/weighted_distribution.hsx +0 -237
@@ -1,402 +0,0 @@
1
- module std.money_flows.cancellable_booking
2
-
3
- // Time-anchored reservation holding booking funds in escrow with dynamically quoted cancellation penalties.
4
- //
5
- // ### Purpose
6
- // `cancellable_booking` holds booking fees in escrow from a guest for a host until a scheduled start date.
7
- // It fits hospitality, travel reservations, studio rentals, event tickets, and appointments where guests
8
- // can cancel prior to start, but cancellation penalties depend on how close to the start date cancellation occurs.
9
- //
10
- // ### Selection guidance
11
- // - vs `held_payment`: Both hold money in escrow and both quote a cancellation before it is spent.
12
- // `cancellable_booking` prices the penalty against the time left before `starts_at`, so the charge moves
13
- // as the start approaches; `held_payment` has no start date and quotes one flat charge.
14
- // Choose `cancellable_booking` whenever cancellation fees are time-sensitive.
15
- // - vs `security_deposit`: `security_deposit` holds funds to cover damages claimed by the holder.
16
- // `cancellable_booking` holds the service fee itself and releases to the host upon `starts_at` or refunds net of penalty.
17
- //
18
- // ### Parameters
19
- // - `guest`: The booking customer paying the fee and receiving refunds.
20
- // - `host`: The service provider receiving the payout or retained cancellation penalty.
21
- // - `amount`: Total booking price in minor units of currency `C`.
22
- // - `starts_at`: Stored ISO 8601 date when the booking begins.
23
- // - `late_penalty_bps`: Penalty in basis points applied when cancelling within the late window.
24
- // - `late_within`: ISO 8601 duration defining the late window before `starts_at` (e.g. `"P2D"` for 2 days).
25
- // - `early_penalty_bps`: Penalty in basis points applied when cancelling before the late window.
26
- // - `offer_life`: ISO 8601 duration defining how long a quoted cancellation offer remains valid (e.g. `"PT30M"`).
27
- //
28
- // ### Decision ports
29
- // None. Action transitions are driven by guest API calls (`take`, `cancel`, `confirm`) and scheduled completion (`complete` due at `starts_at`).
30
- //
31
- // ### Example
32
- // ```hsx
33
- // program studio_booking "Studio booking"
34
- // import { cancellable_booking } from "std/money_flows"
35
- // party guest: person
36
- // party studio: business
37
- // settlement studio_session = cancellable_booking {
38
- // guest: guest
39
- // host: studio
40
- // amount: sessionPrice: money(SAR)
41
- // starts_at: startsAt
42
- // late_penalty_bps: 5000
43
- // late_within: "P2D"
44
- // early_penalty_bps: 1000
45
- // offer_life: "PT30M"
46
- // }
47
- // ```
48
- // ### Finite cancellation bands
49
- // Supply cancel_bands to select deposit/balance custody instead of quoted cancellation.
50
- // Each band has name, refund (full, balance or none), starts_at after the first band,
51
- // and supplier_bps when money is retained. The unpaid_band index selects a retained
52
- // deposit policy. Omit it to return the deposit; a full-refund band also returns it. price_value and supplier_cost fix the price; deposit_bps derives
53
- // the deposit. balance_before and confirmation_window are positive fixed durations
54
- // relative to departure. tax_bps applies to margin after the supplier share.
55
- // authority_type and supplier_role bind release to a consumed confirmation decision.
56
- // collects optionally cascades named actions to linked instruments on deposit.
57
- // acceptance optionally supplies an aggregate gate. All actions are private until
58
- // expose selects them. Ordered band starts and deadlines prevent a late sweep from
59
- // extending an earlier band's cancellation rights. Full timeout refund returns price.
60
- // Seven derivations suffice for three bands; larger finite policies use up to the
61
- // UDL bound of 64 derived amounts. shift_date(date, duration, "before") subtracts a
62
- // fixed duration; omitting the direction adds it. Invalid dates or durations refuse.
63
- //
64
- export instrument cancellable_booking<C>(
65
- guest: optional<party>,
66
- host: optional<party>,
67
- amount: optional<money<C>>,
68
- starts_at: optional<date>,
69
- late_penalty_bps: optional<integer>,
70
- late_within: optional<text>,
71
- early_penalty_bps: optional<integer>,
72
- offer_life: optional<text>,
73
- cancel_bands: optional<list<block>>,
74
- price_value: optional<text>, supplier_cost: optional<text>, departure: optional<text>,
75
- balance_before: optional<text>, confirmation_window: optional<text>, unpaid_band: optional<integer>,
76
- acceptance: optional<block>, deposit_bps: optional<integer>, tax_bps: optional<integer>,
77
- authority_type: optional<text>, supplier_role: optional<text>,
78
- collects: optional<json>,
79
- ) {
80
- when_not(cancel_bands) {
81
- let(guest_account_field): camel(concat(guest, "_account_id"));
82
- let(host_account_field): camel(concat(host, "_account_id"));
83
- let(reference_filter_key): "x-hyperscale-reference-filter";
84
- let(owner_id_key): "owner.id";
85
- let(owner_type_key): "owner.type";
86
- fields {
87
- amount { type: money<C>; description: concat("Booking price in minor units, held away from both the ", words(guest), " and the ", words(host), " until the booking ends"); }
88
- currency { type: text; description: "ISO 4217 currency code"; minLength: 3; maxLength: 3; pattern: "^[A-Z]{3}$"; }
89
- starts_at { type: date; description: "When the booking starts; the cancellation penalty is priced against the time left before it"; }
90
- [guest_account_field] { type: account<C>; description: concat("The ", words(guest), " account the booking is taken from and the refund returns to"); [reference_filter_key]: { column: role; values: [customer_balance]; }; }
91
- [host_account_field] { type: account<C>; description: concat("The ", words(host), " account the booking and any penalty end up in"); [reference_filter_key]: { column: role; values: [customer_balance]; }; }
92
- }
93
- lifecycle {
94
- states created held cancellation_quoted canceled settled completed;
95
- initial created;
96
- on take: created -> held;
97
- on complete: held | cancellation_quoted -> completed;
98
- on cancel: held -> cancellation_quoted;
99
- on confirm: cancellation_quoted -> canceled;
100
- on retain: canceled -> settled;
101
- }
102
- parties { beneficiary: host_account_field; payer: guest_account_field; }
103
- required: [amount, currency, guest_account_field, host_account_field, starts_at];
104
- title: sentence(instrument);
105
- agent_description: concat("Reach for cancellable booking when the ", words(guest), " pays up front and can back out at a price they see before they agree to it. The money sits in a holding account, the penalty is priced against the time left before the start, and the quote is frozen until it expires. Pick held payment when there is no cancellation price to quote.");
106
- id_prefix: prefix(instrument);
107
- summary: concat("Booking the ", words(guest), " can cancel at a price they see before they agree to it");
108
- description: "A booking whose cancellation is quoted rather than argued: the penalty is priced against the time left before the start, frozen with the terms it was read from, and paid only if the guest confirms while the offer is still alive";
109
- action create {
110
- agent_description: concat("Open the booking and provision the holding account the price sits in. No money moves. The price, the currency, the start moment, and both party accounts are fixed here. Call take next to collect from the ", words(guest), ".");
111
- summary: concat("Open a ", words(instrument), " and its holding account");
112
- moves: [];
113
- steps: [{ operation: "account.escrow.provision"; bind: { currency: { from: instance; path: fields.currency; }; [owner_id_key]: { from: instance; path: productId; }; [owner_type_key]: { from: const; value: product; }; productId: { from: instance; path: productId; }; role: { from: const; value: product_escrow; }; }; capture: { bookingHoldingAccountId: accountId; }; }];
114
- }
115
- action take {
116
- agent_description: concat("Move the whole booking price out of the ", words(guest), " account into the holding account. This moves money. Only a created booking takes. Neither party can spend the money until the booking completes or a cancellation is confirmed.");
117
- summary: concat("Take the booking price from the ", words(guest), " into the holding account");
118
- moves: [{ key: booking; operation: internal_transfer.create; bind: { amount: { from: instance; path: concat("fields.", amount); }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: refs.bookingHoldingAccountId; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", guest_account_field); }; }; capture: { bookingTransferId: transferId; }; }];
119
- sandbox_failure_point: funding;
120
- steps: [];
121
- }
122
- action complete {
123
- summary: concat("Release the whole booking to the ", words(host), " once the stay has started, even with a cancellation quote parked un-acted");
124
- due { field: starts_at; }
125
- moves: [{ key: release; operation: internal_transfer.create; bind: { amount: { from: instance; path: concat("fields.", amount); }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", host_account_field); }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: refs.bookingHoldingAccountId; }; }; capture: { releaseTransferId: transferId; }; }];
126
- steps: [];
127
- }
128
- action cancel {
129
- agent_description: "Price the cancellation and freeze that price. No money moves. The penalty is computed from the time left before the start, and the quote expires on its own, after which a new cancel prices it again. Call confirm to spend the quote.";
130
- summary: "Price the cancellation and hold that price open";
131
- quote {
132
- anchorField: starts_at;
133
- baseField: amount;
134
- chargeRef: cancellationPenaltyAmount;
135
- charges: [{ bps: late_penalty_bps; withinOffset: late_within; }, { bps: early_penalty_bps; }];
136
- expires { offset: offer_life; }
137
- fixes: [amount, guest_account_field];
138
- netDestinationField: guest_account_field;
139
- netRef: cancellationRefundAmount;
140
- }
141
- moves: []; steps: [];
142
- }
143
- action confirm {
144
- agent_description: concat("Spend the frozen quote and return the net refund to the ", words(guest), ". This moves money and does not reverse. It requires an un-expired quote from cancel, and the penalty the refund left behind is paid to the ", words(host), " by retain.");
145
- summary: concat("Spend the quoted cancellation and return the net to the ", words(guest));
146
- commit: cancel;
147
- moves: [{ key: refund; operation: internal_transfer.create; bind: { amount: { from: instance; path: refs.cancellationRefundAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", guest_account_field); }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: refs.bookingHoldingAccountId; }; }; capture: { cancellationTransferId: transferId; }; }];
148
- steps: [];
149
- }
150
- action retain {
151
- agent_description: concat("Pay the quoted cancellation penalty out of the holding account to the ", words(host), ". This moves money and does not reverse. The booking must already be canceled, so the penalty figure is the one the quote fixed.");
152
- summary: concat("Pay the quoted penalty that the refund left behind to the ", words(host));
153
- moves: [{ key: penalty; operation: internal_transfer.create; bind: { amount: { from: instance; path: refs.cancellationPenaltyAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", host_account_field); }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: refs.bookingHoldingAccountId; }; }; capture: { penaltyTransferId: transferId; }; }];
154
- steps: [];
155
- }
156
- }
157
- when(cancel_bands) {
158
- let(band_count): len(cancel_bands);
159
- for band in cancel_bands {
160
- let(refund): get(band, "refund");
161
- let(not_full): if_eq(refund, "full", false, true);
162
- let(not_balance): if_eq(refund, "balance", false, true);
163
- let(not_none): if_eq(refund, "none", false, true);
164
- when(not_full) { when(not_balance) { when(not_none) {
165
- unsupported { code: HSX1407; message: "Unknown cancellation refund policy"; fix: "Choose full, balance or none"; }
166
- } } }
167
- }
168
- let(deposit_states): names("deposit_", len(cancel_bands), "");
169
- let(full_states): names("full_", len(cancel_bands), "");
170
- fields {
171
- bookingKey { type: text; min_length: 1; }
172
- signedTermsReference { type: text; min_length: 1; }
173
- currency { type: text; const: C; }
174
- customerAccountId { type: account<C>; "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
175
- supplierAccountId { type: account<C>; "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
176
- operatorAccountId { type: account<C>; "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
177
- taxAccountId { type: account<C>; "x-hyperscale-reference-filter": { column: role; values: [customer_balance]; }; }
178
- authorityId { type: ref<authority_type>; optional: true; }
179
- supplierRole { type: text; const: supplier_role; }
180
- travellerCount { type: integer; minimum: 1; maximum: 20; }
181
- departureAt { type: date; const: departure; }
182
- balanceDueAt { type: date; const: shift_date(departure, balance_before, "before"); }
183
- confirmationDeadline { type: date; const: shift_date(departure, confirmation_window); }
184
- price { type: money<C>; const: price_value; }
185
- supplierCost { type: money<C>; const: supplier_cost; }
186
- depositAmount { type: money<C>; optional: true; allow_zero: true; }
187
- balanceAmount: money<C>;
188
- margin: money<C>;
189
- operatorNet: money<C>;
190
- marginVat { type: money<C>; optional: true; allow_zero: true; }
191
- for i in len(cancel_bands) {
192
- let(band): at(cancel_bands, i);
193
- let(refund_mode): get(band, "refund");
194
- let(full_refund): if_eq(refund_mode, "full", true, false);
195
- let(balance_refund): if_eq(refund_mode, "balance", true, false);
196
- let(retains): if_eq(refund_mode, "full", false, true);
197
- let(later): if_eq(i, 1, false, true);
198
- when(later) { band[i]Start { type: date; const: get(band, "starts_at"); } }
199
-
200
- when(retains) {
201
- band[i]Supplier { type: money<C>; optional: true; allow_zero: true; }
202
- let(portions): if_eq(refund_mode, "balance", ["deposit"], ["deposit", "full"]);
203
- for paid in portions {
204
- let(is_full): if_eq(paid, "full", true, false);
205
- let(portion): if_eq(refund_mode, "balance", "deposit", paid);
206
- let(margin_field): concat("band", i, title(portion), "Margin");
207
- let(net_field): concat("band", i, title(portion), "Net");
208
- let(vat_field): concat("band", i, title(portion), "Vat");
209
- [margin_field]: money<C>;
210
- [net_field]: money<C>;
211
- [vat_field] { type: money<C>; optional: true; allow_zero: true; }
212
- }
213
- }
214
- }
215
- }
216
- parties { payer: customerAccountId; beneficiary: supplierAccountId; fee: operatorAccountId; tax: taxAccountId; }
217
- distinct_parties: true;
218
- computes derived: { field: depositAmount; source_field: price; rule: { kind: percentage_of; bps: deposit_bps; }; rounding: floor; };
219
- computes derived: { field: marginVat; source_field: margin; rule: { kind: percentage_of; bps: tax_bps; }; rounding: floor; };
220
- for i in len(cancel_bands) {
221
- let(band): at(cancel_bands, i);
222
- let(refund_mode): get(band, "refund");
223
- let(balance_refund): if_eq(refund_mode, "balance", true, false);
224
- when(balance_refund) {
225
- partitions: { total_field: price; piece_fields: [balanceAmount, concat("band", i, "Supplier"), concat("band", i, "DepositNet"), concat("band", i, "DepositVat")]; };
226
- }
227
- }
228
- partitions: { total_field: price; piece_fields: [supplierCost, operatorNet, marginVat]; };
229
- partitions: { total_field: price; piece_fields: [depositAmount, balanceAmount]; };
230
- partitions: { total_field: price; piece_fields: [supplierCost, margin]; };
231
- partitions: { total_field: margin; piece_fields: [operatorNet, marginVat]; };
232
- date order: { before_field: balanceDueAt; after_field: departureAt; operator: "<"; };
233
- date order: { before_field: departureAt; after_field: confirmationDeadline; operator: "<"; };
234
- for i in len(cancel_bands) {
235
- let(band): at(cancel_bands, i);
236
- let(refund_mode): get(band, "refund");
237
- let(full_refund): if_eq(refund_mode, "full", true, false);
238
- let(balance_refund): if_eq(refund_mode, "balance", true, false);
239
- let(retains): if_eq(refund_mode, "full", false, true);
240
- let(later): if_eq(i, 1, false, true);
241
- let(band_start): concat("band", i, "Start");
242
- when(later) {
243
- date order: { before_field: band_start; after_field: departureAt; operator: "<"; };
244
- let(after_second): if_eq(i, 2, false, true);
245
- when(after_second) { date order: { before_field: concat("band", sub(i, 1), "Start"); after_field: band_start; operator: "<"; }; }
246
- }
247
-
248
- when(retains) {
249
- let(supplier_field): concat("band", i, "Supplier");
250
- computes derived: { field: supplier_field; source_field: supplierCost; rule: { kind: percentage_of; bps: get(band, "supplier_bps"); }; rounding: floor; };
251
- let(portions): if_eq(refund_mode, "balance", ["deposit"], ["deposit", "full"]);
252
- for paid in portions {
253
- let(is_full): if_eq(paid, "full", true, false);
254
- let(portion): if_eq(refund_mode, "balance", "deposit", paid);
255
- let(margin_field): concat("band", i, title(portion), "Margin");
256
- let(net_field): concat("band", i, title(portion), "Net");
257
- let(vat_field): concat("band", i, title(portion), "Vat");
258
- let(retained): if_eq(paid, "deposit", "depositAmount", if_eq(refund_mode, "balance", "depositAmount", "price"));
259
- computes derived: { field: vat_field; source_field: margin_field; rule: { kind: percentage_of; bps: tax_bps; }; rounding: floor; };
260
- partitions: { total_field: retained; piece_fields: [supplier_field, net_field, vat_field]; };
261
- partitions: { total_field: retained; piece_fields: [supplier_field, margin_field]; };
262
- partitions: { total_field: margin_field; piece_fields: [net_field, vat_field]; };
263
- }
264
- }
265
- }
266
- agent_description: "Hold deposit and balance in one booking account. Apply a finite dated cancellation policy and release the supplier, operator and tax partition only after the named authority confirms. Refund the entire price when confirmation times out.";
267
- lifecycle {
268
- states created awaiting_confirmation confirmed released refunded canceled expired;
269
- let(states_key): "states";
270
- [states_key]: concat_lists(deposit_states, full_states);
271
- initial created;
272
- on deposit: created -> deposit_1;
273
- on expire: created -> expired;
274
- for i in len(cancel_bands) {
275
- let(band): at(cancel_bands, i);
276
- let(refund_mode): get(band, "refund");
277
- let(full_refund): if_eq(refund_mode, "full", true, false);
278
- let(balance_refund): if_eq(refund_mode, "balance", true, false);
279
- let(retains): if_eq(refund_mode, "full", false, true);
280
- let(band_name): get(band, "name");
281
- let(deposit_state): concat("deposit_", i);
282
- let(full_state): concat("full_", i);
283
- let(balance_action): concat("balance_", band_name);
284
- let(cancel_deposit): concat("cancel_deposit_", band_name);
285
- let(cancel_full): concat("cancel_full_", band_name);
286
- on [balance_action]: deposit_state -> full_state;
287
- on [cancel_deposit]: deposit_state -> canceled;
288
- on [cancel_full]: full_state -> canceled;
289
- let(later): if_eq(i, 1, false, true);
290
- when(later) {
291
- let(previous_deposit): names("deposit_", sub(i, 1), "");
292
- let(previous_full): names("full_", sub(i, 1), "");
293
- let(open_deposit): concat("open_deposit_", band_name);
294
- let(open_full): concat("open_full_", band_name);
295
- on [open_deposit]: previous_deposit -> deposit_state;
296
- on [open_full]: previous_full -> full_state;
297
- }
298
- }
299
- on cancel_unpaid: deposit_states -> canceled;
300
- on await_confirmation: full_states -> awaiting_confirmation;
301
- on confirm: awaiting_confirmation -> confirmed;
302
- on release: confirmed -> released;
303
- on refund_unconfirmed: full_states | awaiting_confirmation -> refunded;
304
- }
305
- caller_parked_states: { confirmed: "Release pays the confirmed supplier and frozen partition."; };
306
- action create {
307
- public: none;
308
- agent_description: "Freeze the booking terms and provision its holding account.";
309
- unique: { namespace: instrument; by_fields: [bookingKey]; };
310
- deadline: { field: balanceDueAt; };
311
- steps: { operation: "account.escrow.provision"; bind: { currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; "owner.id": { from: instance; path: productId; }; "owner.type": { from: const; value: product; }; role: { from: const; value: product_escrow; }; }; capture: { heldAccountId: accountId; }; };
312
- }
313
- action deposit {
314
- public: none;
315
- agent_description: "Collect the derived deposit after the acceptance requirements pass.";
316
- when(collects) { cascade: collects; }
317
- when(acceptance) { requires aggregate: acceptance; }
318
- deadline: { field: balanceDueAt; }; port: { allowedParties: [payer]; };
319
- moves: { key: deposit; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "depositAmount"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "fields.customerAccountId"; }; destinationAccountId: { from: instance; path: "refs.heldAccountId"; }; }; };
320
- steps: [];
321
- }
322
- action expire { public: none; agent_description: "Close an unfunded booking."; due: { field: balanceDueAt; }; requires_drained: { path: refs.heldAccountId; }; steps: []; }
323
- for i in len(cancel_bands) {
324
- let(band): at(cancel_bands, i);
325
- let(refund_mode): get(band, "refund");
326
- let(full_refund): if_eq(refund_mode, "full", true, false);
327
- let(balance_refund): if_eq(refund_mode, "balance", true, false);
328
- let(retains): if_eq(refund_mode, "full", false, true);
329
- let(band_name): get(band, "name");
330
- let(balance_action): concat("balance_", band_name);
331
- let(next_index): add(i, 1);
332
- let(next_field): concat("band", next_index, "Start");
333
- let(band_end): if_eq(i, band_count, "departureAt", next_field);
334
- let(later): if_eq(i, 1, false, true);
335
- action [balance_action] {
336
- public: none; agent_description: "Collect the remaining balance once before its deadline.";
337
- deadline: { field: balanceDueAt; }; port: { allowedParties: [payer]; };
338
- moves: { key: balance; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "balanceAmount"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "fields.customerAccountId"; }; destinationAccountId: { from: instance; path: "refs.heldAccountId"; }; }; };
339
- steps: [];
340
- }
341
- for paid in ["deposit", "full"] {
342
- let(is_full): if_eq(paid, "full", true, false);
343
- when(later) {
344
- let(open_action): concat("open_", paid, "_", band_name);
345
- action [open_action] { public: none; agent_description: "Open the next dated cancellation band."; due: { field: concat("band", i, "Start"); }; steps: []; }
346
- }
347
- let(portion): if_eq(refund_mode, "balance", "deposit", paid);
348
- let(cancel_action): concat("cancel_", paid, "_", band_name);
349
- action [cancel_action] {
350
- public: none; agent_description: "Settle this paid amount under the active cancellation band.";
351
- deadline: { field: band_end; }; port: { allowedParties: [payer]; };
352
- when(full_refund) { moves: { key: refund; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", if_eq(paid, "deposit", "depositAmount", "price")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.customerAccountId"; }; }; }; }
353
- when(retains) {
354
- when(is_full) { when(balance_refund) { moves: { key: refund; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "balanceAmount"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.customerAccountId"; }; }; }; } }
355
- moves: { key: supplier; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", i, "Supplier")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.supplierAccountId"; }; }; };
356
- moves: { key: operator; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", i, title(portion), "Net")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.operatorAccountId"; }; }; };
357
- moves: { key: vat; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", i, title(portion), "Vat")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.taxAccountId"; }; }; };
358
- }
359
- steps: [];
360
- }
361
- }
362
- }
363
- action cancel_unpaid {
364
- public: none; agent_description: "Settle the deposit under the declared unpaid-balance band and end the remaining obligation.";
365
- due: { field: balanceDueAt; };
366
- when_not(unpaid_band) { moves: { key: refund; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: "fields.depositAmount"; }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.customerAccountId"; }; }; }; }
367
- when(unpaid_band) {
368
- let(unpaid_policy): at(cancel_bands, unpaid_band);
369
- let(unpaid_full_refund): if_eq(get(unpaid_policy, "refund"), "full", true, false);
370
- when(unpaid_full_refund) { moves: { key: refund; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: "fields.depositAmount"; }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.customerAccountId"; }; }; }; }
371
- when_not(unpaid_full_refund) {
372
- moves: { key: supplier; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", unpaid_band, "Supplier")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.supplierAccountId"; }; }; };
373
- moves: { key: operator; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", unpaid_band, "DepositNet")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.operatorAccountId"; }; }; };
374
- moves: { key: vat; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", concat("band", unpaid_band, "DepositVat")); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.taxAccountId"; }; }; };
375
- }
376
- }
377
- steps: [];
378
- }
379
- action await_confirmation { public: none; agent_description: "Open confirmation at the booked departure."; due: { field: departureAt; }; steps: []; }
380
- action confirm {
381
- public: none; agent_description: "Consume the named supplier's approved decision for this booking before timeout.";
382
- deadline: { field: confirmationDeadline; };
383
- input: { type: object; additionalProperties: false; properties: { authorityId: { type: string; pattern: concat("^", prefix(authority_type), "_(sandbox|live)_[a-z0-9]{8,64}$"); }; }; required: [authorityId]; };
384
- updates: [authorityId];
385
- requires refs: { field: authorityId; statuses: [approved]; match: { instrumentInstanceId: fields.requestInstanceId; "fields.supplierAccountId": fields.decidingActorAccountId; "fields.supplierRole": fields.decidingRole; }; attests: { instrument: fields.requestInstrumentId; party: decider; action: fields.requestAction; digest: fields.inputDigest; role: fields.decidingRole; expires_at: fields.expiresAt; consume: consume; }; };
386
- steps: [];
387
- }
388
- action release {
389
- public: none; agent_description: "Pay supplier cost, operator net and VAT after confirmation.";
390
- moves: { key: supplier; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "supplierCost"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.supplierAccountId"; }; }; };
391
- moves: { key: operator; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "operatorNet"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.operatorAccountId"; }; }; };
392
- moves: { key: vat; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "marginVat"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.taxAccountId"; }; }; };
393
- steps: [];
394
- }
395
- action refund_unconfirmed {
396
- public: none; agent_description: "Return the entire booking price when supplier confirmation times out.";
397
- due: { field: confirmationDeadline; };
398
- moves: { key: refund; operation: "internal_transfer.create"; bind: { amount: { from: instance; path: concat("fields.", "price"); }; currency: { from: instance; path: fields.currency; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: "refs.heldAccountId"; }; destinationAccountId: { from: instance; path: "fields.customerAccountId"; }; }; };
399
- steps: [];
400
- }
401
- }
402
- }
@@ -1,220 +0,0 @@
1
- module std.money_flows.captured_payment
2
-
3
- // Two-phase authorization and capture payment flow for card and merchant processing.
4
- //
5
- // ### Purpose
6
- // `captured_payment` reserves funds against a payer's account and allows the payee to capture the authorized
7
- // balance in one or multiple slices before a reservation expiry date (`reserve_until`). It fits ecommerce checkouts,
8
- // card processing, hotel authorizations, and pay-at-pump fuel payments where final amounts vary or settle later.
9
- //
10
- // ### Selection guidance
11
- // - vs `instant_transfer`: `instant_transfer` immediately transfers money from payer to payee in a single irreversible
12
- // step without reservation or settlement delays. `captured_payment` separates authorization from capture, allowing
13
- // incremental captures, voids, amount corrections via the `correction` port, and external network reversals via `external_reversal`.
14
- // - vs `held_payment`: `held_payment` holds the full amount in third-party escrow pending release. `captured_payment`
15
- // reserves funds directly on payer balance and settles incrementally directly to payee.
16
- //
17
- // ### Parameters
18
- // - `payer`: Party whose account balance is reserved during authorization.
19
- // - `payee`: Beneficiary party capturing authorized funds.
20
- // - `amount`: Maximum authorized reservation in minor units of currency `C`.
21
- // - `reserve_until`: Expiration date for the authorization hold.
22
- // - `correction`: Condition port allowing post-settlement amount corrections.
23
- // - `external_reversal`: Condition port allowing external processor chargebacks/reversals.
24
- // - `derived_amount`: Optional block declaring percentage fee calculation.
25
- //
26
- // ### Decision ports
27
- // - `correction`: Condition allowing payee or processor to submit an amount correction after settlement.
28
- // - `external_reversal`: Condition allowing bank or card network to execute an external reversal.
29
- //
30
- // ### Example
31
- // ```hsx
32
- // program captured_payment_example "Captured payment example"
33
- // import { captured_payment } from "std/money_flows"
34
- // party payer: person
35
- // party payee: business
36
- // settlement card_payment = captured_payment {
37
- // payer: payer
38
- // payee: payee
39
- // amount: authorizedAmount: money(SAR)
40
- // reserve_until: reserveUntil
41
- // correction: port correct_capture
42
- // external_reversal: port reverse_capture within P14D
43
- // }
44
- // port correct_capture { allowed: [payee] }
45
- // port reverse_capture {
46
- // allowed: [payee]
47
- // shape: { externalReference: text }
48
- // }
49
- // ```
50
- export instrument captured_payment<C>(payer: party, payee: party, amount: money<C>, reserve_until: date, correction: condition, external_reversal: condition, derived_amount: optional<block>) {
51
- let(correction_name): correction;
52
- let(reversal_name): external_reversal;
53
- let(reversal_window): external_reversal_within;
54
- let(instrument_words): words(instrument);
55
- when(derived_amount) {
56
- let(derived_field): get(derived_amount, "field");
57
- let(derived_source): get(derived_amount, "source");
58
- let(derived_rule): get(derived_amount, "rule");
59
- let(derived_bearer): get(derived_amount, "bearer");
60
- }
61
- agent_description: "Reach for captured payment when the {payee} reserves against the {payer} first and then takes the money in slices before {reserve_until}. Pick instant transfer when the whole amount moves at once, and deposit when the reservation is claimed or returned whole.";
62
- summary: "Capture reservation from {payer} to {payee}";
63
- title: sentence(instrument);
64
- description: "Payer reservation captured by the payee in slices within a fixed window";
65
- fields {
66
- when(derived_amount) {
67
- let(fee_collection_key): "x-hyperscale-fee-collection-port";
68
- let(reference_filter_key): "x-hyperscale-reference-filter";
69
- platformAccountId {
70
- type: account<C>;
71
- description: "The product fee collection account";
72
- [fee_collection_key]: true;
73
- [reference_filter_key]: { column: role; values: [customer_balance, product_revenue]; };
74
- }
75
- }
76
- amount { type: money<C>; description: "Maximum captured amount in {C} minor units"; }
77
- reserve_until { type: date; description: "Reservation expiry that releases any uncaptured remainder"; }
78
- reversalUntil { type: date; description: "Machine-owned external reversal cutoff anchored when settlement completes"; optional: true; }
79
- when(derived_amount) {
80
- [derived_field] { type: money<C>; description: "Machine-computed {derived_rule} of {derived_source}; callers never supply it"; pattern: "^[1-9][0-9]{0,17}$"; optional: true; }
81
- }
82
- }
83
- lifecycle {
84
- states created authorized partially_captured settled voided expired corrected reversed;
85
- initial created;
86
- on authorize: created -> authorized;
87
- on capture: authorized -> partially_captured;
88
- on capture_more: partially_captured -> partially_captured;
89
- on settle: authorized | partially_captured -> settled;
90
- on void: authorized -> voided;
91
- on expire: authorized -> expired;
92
- on settle_on_expiry: partially_captured -> settled;
93
- on [correction_name]: settled -> corrected;
94
- on [reversal_name]: settled -> reversed;
95
- }
96
- parties {
97
- payer: payer;
98
- beneficiary: payee;
99
- }
100
- when(derived_amount) {
101
- computes derived: { field: derived_field; source_field: derived_source; rounding: floor; rule: { kind: percentage_of; bps: derived_rule; }; };
102
- }
103
- action create {
104
- let(metadata_instrument): "metadata.instrumentId";
105
- let(metadata_instance): "metadata.instrumentInstanceId";
106
- let(metadata_phase): "metadata.phase";
107
- agent_description: "Open the payment row before anything is reserved. The caller supplies the {amount}, the currency, and both party accounts. This reserves nothing, call authorize next.";
108
- summary: "Create a {instrument_words}";
109
- when(derived_amount) {
110
- moves: [{ key: "derived_amount"; operation: internal_transfer.create; bind: { amount: { from: instance; path: concat("fields.", derived_field); }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: fields.platformAccountId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: create; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", camel(derived_bearer), "AccountId"); }; }; capture: { createDerivedAmountTransferId: transferId; }; }];
111
- }
112
- steps: [];
113
- }
114
- action authorize {
115
- agent_description: "Reserve the {amount} against the {payer} in the {payee}'s favor until {reserve_until}. The payment must be created. The money is held, not paid: capture and settle post it, void and expiry release it.";
116
- summary: "Reserve the {amount} until {reserve_until}";
117
- moves: [{ key: reservation; amount: amount; from: payer; to: beneficiary; operation: reserve; }];
118
- sandbox_failure_point: funding;
119
- steps: [];
120
- }
121
- action capture {
122
- agent_description: "Post the first capture slice out of the live reservation. The payment must be authorized and the caller passes captureAmount in minor units. The slice settles for good and void can no longer release the reservation. Call before {reserve_until}.";
123
- summary: "Post one strict partial capture slice";
124
- deadline { field: reserve_until; };
125
- input { type: object; additional_properties: false; properties: { captureAmount: { description: "Positive partial capture amount in minor units"; pattern: "^[1-9][0-9]{0,17}$"; type: string; }; }; required: [captureAmount]; }
126
- moves: [{ key: post; operation: internal_transfer.post; bind: { amount: { from: input; path: captureAmount; }; currency: { from: instance; path: fields.currency; }; postMode: { from: const; value: partial_only; }; transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; capture: { capturedAmount: postedAmount; }; }];
127
- sandbox_failure_point: "release";
128
- steps: [];
129
- }
130
- action capture_more {
131
- agent_description: "Post another capture slice out of a partly captured reservation. Call this only after capture took the first slice. Each slice settles for good, and the uncaptured remainder still releases at {reserve_until}.";
132
- summary: "Post another strict partial capture slice";
133
- deadline { field: reserve_until; };
134
- input { type: object; additional_properties: false; properties: { captureAmount: { description: "Positive partial capture amount in minor units"; pattern: "^[1-9][0-9]{0,17}$"; type: string; }; }; required: [captureAmount]; }
135
- moves: [{ key: post; operation: internal_transfer.post; bind: { amount: { from: input; path: captureAmount; }; currency: { from: instance; path: fields.currency; }; postMode: { from: const; value: partial_only; }; transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; capture: { capturedAmount: postedAmount; }; }];
136
- steps: [];
137
- }
138
- action settle {
139
- agent_description: "Post the whole reserved remainder and finish the payment. The payment must be authorized or partly captured. The money settles and the external reversal window opens on reversalUntil.";
140
- summary: "Post the full reserved remainder and settle";
141
- deadline { field: reserve_until; };
142
- sets(at) { field: reversalUntil; offset: reversal_window; };
143
- moves: [{ key: post; operation: internal_transfer.post; bind: { transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; capture: { capturedAmount: postedAmount; }; }];
144
- steps: [];
145
- }
146
- action void {
147
- agent_description: "Release the reservation whole and cancel the payment. Only an authorized reservation with nothing captured can void. Once any slice is captured, settle it or let it expire instead.";
148
- summary: "Release an entirely uncaptured reservation";
149
- moves: [{ key: void; operation: internal_transfer.void; bind: { reason: { from: const; value: "Reservation voided before any capture"; }; transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; }];
150
- steps: [];
151
- }
152
- action expire {
153
- summary: "Release an uncaptured reservation at expiry";
154
- due { field: reserve_until; };
155
- moves: [{ key: void; operation: internal_transfer.void; bind: { reason: { from: const; value: "Uncaptured reservation expired"; }; transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; }];
156
- steps: [];
157
- }
158
- action settle_on_expiry {
159
- summary: "Release the uncaptured remainder and settle captured slices";
160
- due { field: reserve_until; };
161
- sets(at) { field: reversalUntil; offset: reversal_window; };
162
- moves: [{ key: void; operation: internal_transfer.void; bind: { reason: { from: const; value: "Uncaptured remainder released at expiry"; }; transferId: { from: instance; path: refs.authorizeReservationTransferId; }; }; }];
163
- steps: [];
164
- }
165
- action [correction_name] {
166
- let(metadata_clawback): "metadata.clawbackOfTransferId";
167
- let(metadata_instrument): "metadata.instrumentId";
168
- let(metadata_instance): "metadata.instrumentInstanceId";
169
- let(metadata_phase): "metadata.phase";
170
- let(correction_capture): camel(concat(correction_name, "_transfer_transfer_id"));
171
- agent_description: "Return the whole captured amount to the {payer} after the payment settled. Only a settled payment corrects, and only the party the product authorized may call it. The money moves back and there is no undo.";
172
- summary: "Return the full captured amount on payee correction";
173
- when(correction_fields) {
174
- input {
175
- type: object;
176
- additional_properties: false;
177
- properties {
178
- for input_name in keys(correction_fields) {
179
- [input_name]: get(correction_fields, input_name);
180
- }
181
- }
182
- required: keys(correction_fields);
183
- }
184
- capture_input: {
185
- for item in keys(correction_fields) { [item]: item; }
186
- };
187
- }
188
- port { allowed_parties: correction_allowed; };
189
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: refs.capturedAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", payer, "AccountId"); }; [metadata_clawback]: { from: instance; path: refs.authorizeReservationTransferId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: correction_name; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payee, "AccountId"); }; }; capture: { [correction_capture]: transferId; }; }];
190
- steps: [];
191
- }
192
- action [reversal_name] {
193
- let(metadata_clawback): "metadata.clawbackOfTransferId";
194
- let(metadata_instrument): "metadata.instrumentId";
195
- let(metadata_instance): "metadata.instrumentInstanceId";
196
- let(metadata_phase): "metadata.phase";
197
- let(reversal_capture): camel(concat(reversal_name, "_transfer_transfer_id"));
198
- agent_description: "Return the whole captured amount to the {payer} on an external reversal. The payment must be settled and the call must land before reversalUntil. The caller passes externalReference, which stays in the receipt. There is no undo.";
199
- summary: "Return the full captured amount on an external reversal";
200
- deadline { field: reversalUntil; };
201
- when(external_reversal_fields) {
202
- capture_input: {
203
- for item in keys(external_reversal_fields) { [item]: item; }
204
- };
205
- input {
206
- type: object;
207
- additional_properties: false;
208
- properties {
209
- for input_name in keys(external_reversal_fields) {
210
- [input_name]: get(external_reversal_fields, input_name);
211
- }
212
- }
213
- required: keys(external_reversal_fields);
214
- }
215
- }
216
- port { allowed_parties: external_reversal_allowed; };
217
- moves: [{ key: transfer; operation: internal_transfer.create; bind: { amount: { from: instance; path: refs.capturedAmount; }; currency: { from: instance; path: fields.currency; }; destinationAccountId: { from: instance; path: concat("fields.", payer, "AccountId"); }; [metadata_clawback]: { from: instance; path: refs.authorizeReservationTransferId; }; [metadata_instrument]: { from: const; value: instrument; }; [metadata_instance]: { from: instance; path: instrumentInstanceId; }; [metadata_phase]: { from: const; value: reversal_name; }; productId: { from: instance; path: productId; }; sourceAccountId: { from: instance; path: concat("fields.", payee, "AccountId"); }; }; capture: { [reversal_capture]: transferId; }; }];
218
- steps: [];
219
- }
220
- }