Minor-unit multi-currency Money math, penny-exact allocation, transactional SQLite double-entry ledger, and .secret Stripe/LemonSqueezy SDK.
Exact multi-currency minor-unit Money arithmetic, penny-exact largest-remainder allocation, transactional immutable double-entry SQLite ledger, and .secret-aware Stripe and LemonSqueezy SDK helpers.
payments_ledger eliminates floating-point rounding bugs, unbalanced accounting mutations, duplicate webhook credits, and timing side-channels in financial applications:
payments_ledger: Checked i64 minor-unit Money (money, money-add, money-sub, money-mul-bps, Hamilton largest-remainder money-allocate, ISO-4217 format-money) and an ACID append-only double-entry ledger backed by Zero-FFI db/sqlite (open-ledger, close-ledger, create-account, post-transaction, account-balance, trial-balance, reverse-transaction).payments_ledger/stripe: .secret-compatible HMAC-SHA256 webhook signature generators and verifiers for Stripe (Stripe-Signature: t=...,v1=... with replay tolerance window and multi-key rotation) and LemonSqueezy (X-Signature) using constant-time bits/ct-eq?, plus RFC 3986 form-urlencoded request builders for Stripe Checkout Sessions, PaymentIntents, and Refunds and a live net/client transport helper.veko pkg add payments_ledger@1.0.0Import the ledger and payment provider modules:
use [
pay = payments_ledger
stripe = payments_ledger/stripe
]Money amount is stored in integer ISO-4217 minor units (0 decimals for JPY/KRW, 3 for BHD/KWD/OMR, 2 for USD/EUR/GBP), guarded by checked 64-bit arithmetic (math/add-checked, math/sub-checked, math/mul-checked).money-allocate distributes integer quotients first and assigns leftover pennies in descending order of exact fractional remainders, guaranteeing $\sum \text{parts} = \text{total}$ ($10.00 -> $3.34, $3.33, $3.33).post-transaction executes inside a SQLite BEGIN ... COMMIT B-tree transaction, verifying sum(debits) == sum(credits) > 0, uniform currency across all legs and accounts, idempotency_key replay deduplication, and bounded-account overdraft protection (allow_overdraft == false). Corrections never mutate or delete historical rows; reverse-transaction appends a compensating entry that flips .debit and .credit legs..secret & Constant-Time Webhook Hygiene: verify-stripe-webhook and verify-lemonsqueezy-webhook accept .secret-tainted keys (bytes/with-secret or os/getenv/secret) without exposing key material to serialization sinks, validate timestamp skew before JSON decoding, and compare 32-byte HMAC-SHA256 digests with bits/ct-eq?.payments_ledger| Type / Signature | Description |
|---|---|
Money = struct { amount i64, currency str } | Exact minor-unit monetary value with 3-letter ISO-4217 currency. |
Account = struct { id str, name str, kind enum, currency str, allow_overdraft bool } | Ledger chart-of-accounts definition (.asset, .liability, .equity, .revenue, .expense). |
LedgerEntry = struct { account_id str, direction enum, amount i64, currency str } | Single .debit or .credit posting leg. |
money = fn [amount i64, currency str] Money | Constructs a validated Money value. |
money-add = fn [a Money, b Money] Money / money-sub = fn [a Money, b Money] Money | Checked same-currency addition and subtraction. |
money-mul-bps = fn [val Money, bps i64] Money | Basis-points fee calculation (290 bps = 2.90%) with half-away-from-zero rounding. |
money-allocate = fn [total Money, weights block[i64]] block[Money] | Largest-remainder proportional split with zero lost cents. |
format-money = fn [val Money] str | Formats Money according to ISO-4217 minor-unit precision. |
debit = fn [account_id str, val Money] LedgerEntry / credit = fn [account_id str, val Money] LedgerEntry | Leg constructors for post-transaction. |
open-ledger = fn [db_path str] Ledger / close-ledger = fn [mut ledger Ledger] enum | Opens and closes a :memory: or file-backed SQLite ledger. |
create-account = fn [mut ledger Ledger, id str, name str, kind enum, currency str /bounded bounded bool = false] Account | Creates an account (/bounded true forbids negative normal balances). |
post-transaction = fn [mut ledger Ledger, tx_id str, description str, entries block /idempotency_key idem_key str = '' /timestamp ts_unix i64 = 0] map | Atomically posts a balanced multi-leg transaction. |
account-balance = fn [ledger Ledger, account_id str] Money | Computes the exact normal-side Money balance of account_id. |
trial-balance = fn [ledger Ledger /currency filter_ccy str = ''] map | Computes global debit/credit totals and proves total_debits == total_credits. |
reverse-transaction = fn [mut ledger Ledger, orig_tx_id str, reversal_tx_id str, reason str /timestamp ts_unix i64 = 0] map | Posts an immutable compensating reversal transaction. |
payments_ledger/stripe| Type / Signature | Description |
|---|---|
CheckoutItem = struct { price_id str, name str, unit_amount i64, currency str, quantity i64 } | Line item for build-checkout-form. |
sign-stripe-webhook = fn [secret_key any, timestamp_sec i64, payload str] str | Generates a t=...,v1=... Stripe-Signature header. |
verify-stripe-webhook = fn [payload str, sig_header str, secret_key any, now_unix i64 /tolerance tolerance_sec i64 = 300] map | Verifies Stripe-Signature HMAC-SHA256 + timestamp replay window using bits/ct-eq?. |
sign-lemonsqueezy-webhook / verify-lemonsqueezy-webhook | Generates and verifies LemonSqueezy X-Signature HMAC-SHA256 headers. |
build-checkout-form / build-payment-intent-form / build-refund-form | Encodes nested bracket application/x-www-form-urlencoded Stripe payloads. |
stripe-post-form = fn [api_key str, endpoint_path str, form_body str /idempotency_key idem_key str = '' /base_url base_url str = 'https://api.stripe.com'] map | Sends an authenticated Stripe API request via net/client. |
use [pay = payments_ledger]
shares = pay/money-allocate (pay/money 1000 'USD') [1 1 1]
// -> ['$3.34', '$3.33', '$3.33']
mut ledger = pay/open-ledger ':memory:'
_ = pay/create-account mut ledger 'asset:cash' 'Operating Cash' .asset 'USD'
_ = pay/create-account mut ledger 'rev:subs' 'Subscriptions' .revenue 'USD'
_ = pay/post-transaction/idempotency_key mut ledger 'tx-1' 'Invoice #42' [
(pay/debit 'asset:cash' (pay/money 4900 'USD'))
(pay/credit 'rev:subs' (pay/money 4900 'USD'))
] 'inv-42'
tb = pay/trial-balance ledger
println `Balanced: {tb.balanced}, Cash: {pay/format-money (pay/account-balance ledger 'asset:cash')}`veko check packages/payments_ledger/src/stripe.vk
veko check packages/payments_ledger/src/mod.vk
veko check packages/payments_ledger/examples/demo.vk
veko run -C all packages/payments_ledger/examples/demo.vkhttps://veko-lang.org/pkg/payments_ledger/1.0.0.vkpca48704f41133090ebf1274ef6d7e936212190149cebc4a7f857a9b522673f7acc6c41e75ebff0be94b4a60f59df11e20f7ae8273052d57de9fd005f93b284eced25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70{"name":"payments_ledger","version":"1.0.0","tier":4.0,"tier_label":"Tier 4: FinTech, GraphQL, CDP & SSO","category":"Double-Entry Ledger & Stripe","description":"Minor-unit multi-currency Money math, penny-exact allocation, transactional SQLite double-entry ledger, and .secret Stripe/LemonSqueezy SDK.","modules":["payments_ledger","payments_ledger/stripe"],"homepage":"https://veko-lang.org/packages/payments_ledger"}| Bundle Entry Path | Size | Leaf SHA-256 Digest |
|---|---|---|
| README.md | 7 KB | 55b1414948f65cf7a498b4fc3b553f1550fbdd28b8ded04b4dfbb84aa71fab03 |
| examples/demo.vk | 9 KB | 98177a24beb5ca059bb16eeb67fa97fde6e1e91c1f04881f2e0762c23e631752 |
| mod.vk | 20 KB | fcb1ff2f4663f4d8dde57372ff6da2d0efd2352a214dc5e836b6e9d29f8fd231 |
| stripe.vk | 10 KB | 08fdd3dd83cf14eb25392fe112af0e167c21c9a6cebb977a858715e1786e3632 |