mirror of
https://github.com/hanzoai/commerce.git
synced 2026-11-23 18:55:39 +00:00
* billing: personal plans are sold monthly A purchase or a move onto a personal plan priced over a year is refused with "personal plans are sold monthly", whatever annual price the row carries. planAt asks it first, so every buy path that prices a plan (card, saved card, wallet, POST /v1/billing/subscriptions) reaches it as a client-fixable refusal; a staff record that opens a subscription asks it too, and one that moves a held plan onto its next period does not. A move onto a personal row stated by the year is refused the same way. Renewals bill the subscription's own period and never ask it. The public catalog serves no annual price for a personal plan; business plans keep theirs. Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF * billing: auto-reload is the payer's, guarded per trigger, armed by every purchase (#8) * billing: auto-reload is the payer's, guarded per trigger, armed by every purchase The auto-recharge rule is keyed by its payer: the org's own account (the org slug, which every stored rule already is) or a person's "<org>/<name>" wallet. ReadAutoRecharge(ctx, org, subject) and WriteAutoRecharge(ctx, org, subject, edit) take the payer explicitly ("" is the org's own account; an account outside the org is refused); the HTTP routes use the caller's own payer (userBillingKey) and gate writes on mayChangeWallet. The sweep runs every rule in every org: it reads that payer's balance in the ledger the spend gate reads (prepaid.balance: the host's when one is injected), charges that payer's default card, and credits the reload to the host ledger at the payer's address under the processor's reference before recording it. chargeAndCredit answers the whole receipt, with the processor reference and amount. The duplicate guard is the reload's trigger: a balance below the threshold is a new trigger when the rule has never reloaded or the balance is below what its last reload left (money was spent since). The idempotency key carries the payer, the rule, the trigger (Seq) and the processor's refusals of it (Tries), so overlapping or retried sweeps charge one trigger once, a balance drained again is charged again however soon, and a charge with no answer is asked again under the same key. Square's charge marks its 4xx refusals as processor.Answer, as its authorize and capture do. A plan bounding nothing never stops a reload; a card the processor refuses is asked at most ReloadsPerDay times a day on every plan. Every purchase of a plan whose row states a reload arms the buying payer's rule: new card, saved card, wallet and balance sales (each needs the payer's card on file) and a trial's first paid period in the renewal sweep. Saving a card arms it for a payer on such a plan with no rule on file. A rule the payer switched on is kept. The sale's receipt carries reload {state: armed | kept | needs_card}, and GET /v1/billing/plans serves a plan's reload terms {thresholdCents, amountCents, perDay}. Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF * billing: a reload trigger is asked exactly until it is credited A trigger, once reserved, fixes its charge on the rule: the amount, the payer's card and the balance it was read at (Amount, Card, Base). Every ask of it sends exactly that charge under the same key until the processor answers, whatever the rule or the default card has become. A settled charge is indexed to the payer (paymentorg) and recorded as awaiting its credit (Ref); every sweep credits a charge awaiting one before anything else, by its processor reference, whatever the balance has done since. After is the balance the trigger was read at plus the amount reloaded, so no read decides it. Square's adapter reports IDEMPOTENCY_KEY_REUSED as unknown rather than as a refusal. The sweep charges only rules keyed by the org's own account or an account within it, and logs how many rows are keyed otherwise. A purchase of a plan with a reload, by a payer with no card on file, leaves the rule waiting for one (Plan, Waiting); saving a card switches on only such a rule, on a paid period of that plan. A rule a plan switched on names the plan, and is switched off when the plan is cancelled (now or at its period's end), replaced by a sale of another plan, or moved onto a plan whose row states no reload; a rule the payer set themselves names none and is left as they set it. A refund, or a dispute that withholds the funds, of a payment to a wallet switches that wallet's reload off. A sale made for a subject other than the caller's own payer (SubscribeIn.Delegated) switches no reload on. CheckReload(thresholdCents, amountCents) is the one validator of a plan row's reload figures: amount 0 is no reload; otherwise the top-up's own bounds, which checkRule uses too. Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF