askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
payments.json · 6.1 KB · 173 lines
| 1 | { |
| 2 | "name": "payments", |
| 3 | "title": "Payments", |
| 4 | "version": 7, |
| 5 | "storage": "append", |
| 6 | "description": "Money received against an invoice. Append-only (money moves, never mutates -- docs/logic-decisions.md #3): a bounced check is a status transition on the same movement, a correction is a refund record, never an edit to the amount. Rollups on invoices exclude bounced payments. v2: that paragraph was a description and not yet a fact -- the hook gated create only while the owner could PUT the amount and DELETE the row. hook_payments now refuses both for anybody who is not an admin (status and notes stay editable; an admin's edit passes the same ceiling), and amount_cents carries validation.min 1 so no writer, hooked or not, can store a payment of zero or less. v3 adds invoice_seq, unique per invoice: the order in which payments checked against the overpayment ceiling landed on their invoice. The ceiling is checked in a hook, before the write lock, so two payments submitted at once each passed without seeing the other; the hook now stamps the next number from the same read it summed, and the constraint (inside the lock) refuses whichever write no longer holds the next number. Blank on payments no ceiling was checked for (the Stripe webhook, checkout, the allow policy), which are exempt. v4 retires it: the ceiling is a locked check (locked_checks.overpayment -> hook_payments.LOCKED_CHECK) that object_records calls inside the write lock for EVERY writer -- the webhook, checkout and an admin's edit included -- holding the ledger lock that writes to refunds, invoices, invoice_lines and app_settings also take, so neither another payment nor the invoice it measures can move under it. The unique constraint is gone; invoice_seq stays as a column (no data dropped), no longer stamped, and blanked when a client sends one. v5: invoice_id declares relation.requires update. The rules let a registered user create a payment whose owner_id is theirs, and nothing asked about the invoice, so anybody signed in could record a payment against anybody's invoice and move its paid and balance amounts. Over the API a payment now needs a writer the engine would let update the invoice it is against (its owner, an admin, a role granted it); the Stripe webhook and checkout write below the API and are unaffected. v6: the bounce is staff's. received -> bounced was guarded on owner_id = $user_id, and the owner of a payment the Stripe webhook or checkout records is the customer -- so the guard refused the staff who must bounce it, and was the only thing that would have let the customer do it. Who may write a payment is the permission rules now (role:manager, admins); the transition map keeps only which moves exist. v7: credit notes count against the ceiling. A credit note applied to an invoice (an invoice_credits row) cancels that much of the debt, so the overpayment check subtracts it, and reads invoice_credits under the ledger lock so a credit and a payment cannot pass each other.", |
| 7 | "fields": [ |
| 8 | { |
| 9 | "name": "id" |
| 10 | }, |
| 11 | { |
| 12 | "name": "invoice_id", |
| 13 | "subject": "relation", |
| 14 | "required": true, |
| 15 | "label": "Invoice", |
| 16 | "relation": { |
| 17 | "collection": "invoices", |
| 18 | "display_field": "number", |
| 19 | "requires": "update" |
| 20 | } |
| 21 | }, |
| 22 | { |
| 23 | "name": "amount_cents", |
| 24 | "type": "integer", |
| 25 | "required": true, |
| 26 | "label": "Amount (cents)", |
| 27 | "validation": { |
| 28 | "min": 1 |
| 29 | }, |
| 30 | "help": "A positive whole number of cents. Fixed once recorded: a correction is a refund, or a bounce and a new payment." |
| 31 | }, |
| 32 | { |
| 33 | "name": "method", |
| 34 | "type": "enum", |
| 35 | "label": "Method", |
| 36 | "default": "transfer", |
| 37 | "enum": [ |
| 38 | "cash", |
| 39 | "check", |
| 40 | "card", |
| 41 | "transfer", |
| 42 | "other" |
| 43 | ] |
| 44 | }, |
| 45 | { |
| 46 | "name": "received_on", |
| 47 | "type": "date", |
| 48 | "required": true, |
| 49 | "label": "Received" |
| 50 | }, |
| 51 | { |
| 52 | "name": "reference", |
| 53 | "type": "text", |
| 54 | "label": "Reference", |
| 55 | "validation": { |
| 56 | "max_length": 120 |
| 57 | } |
| 58 | }, |
| 59 | { |
| 60 | "name": "notes", |
| 61 | "type": "textarea", |
| 62 | "label": "Notes" |
| 63 | }, |
| 64 | { |
| 65 | "name": "status", |
| 66 | "type": "enum", |
| 67 | "default": "received", |
| 68 | "label": "Status", |
| 69 | "enum": [ |
| 70 | "received", |
| 71 | "bounced" |
| 72 | ], |
| 73 | "transitions": { |
| 74 | "received": [ |
| 75 | "bounced" |
| 76 | ], |
| 77 | "bounced": [] |
| 78 | } |
| 79 | }, |
| 80 | { |
| 81 | "name": "refunded_cents", |
| 82 | "type": "computed", |
| 83 | "label": "Refunded (cents)", |
| 84 | "rollup": { |
| 85 | "collection": "refunds", |
| 86 | "fk_field": "payment_id", |
| 87 | "op": "sum", |
| 88 | "field": "amount_cents" |
| 89 | } |
| 90 | }, |
| 91 | { |
| 92 | "name": "invoice_seq", |
| 93 | "type": "integer", |
| 94 | "label": "Sequence on invoice", |
| 95 | "help": "Retired in v4: payments recorded under v3 carry the order they landed on their invoice; newer ones are blank. Nothing reads it, and nobody edits it." |
| 96 | }, |
| 97 | { |
| 98 | "name": "owner_id", |
| 99 | "type": "text" |
| 100 | }, |
| 101 | { |
| 102 | "name": "created_at", |
| 103 | "type": "datetime", |
| 104 | "read_only": true, |
| 105 | "label": "Created" |
| 106 | } |
| 107 | ], |
| 108 | "forms": { |
| 109 | "default": { |
| 110 | "fields": [ |
| 111 | "invoice_id", |
| 112 | "amount_cents", |
| 113 | "method", |
| 114 | "received_on", |
| 115 | "reference", |
| 116 | "notes" |
| 117 | ] |
| 118 | } |
| 119 | }, |
| 120 | "views": { |
| 121 | "list_mode": "table", |
| 122 | "list_fields": [ |
| 123 | "invoice_id", |
| 124 | "amount_cents", |
| 125 | "method", |
| 126 | "received_on", |
| 127 | "status" |
| 128 | ], |
| 129 | "filter_fields": [ |
| 130 | "status", |
| 131 | "method" |
| 132 | ], |
| 133 | "related": [ |
| 134 | { |
| 135 | "collection": "refunds", |
| 136 | "fk_field": "payment_id" |
| 137 | } |
| 138 | ] |
| 139 | }, |
| 140 | "search": { |
| 141 | "fields": [ |
| 142 | "reference", |
| 143 | "notes" |
| 144 | ] |
| 145 | }, |
| 146 | "locked_checks": [ |
| 147 | { |
| 148 | "name": "overpayment", |
| 149 | "object": "hook_payments", |
| 150 | "function": "LOCKED_CHECK", |
| 151 | "reads": [ |
| 152 | "invoices", |
| 153 | "invoice_lines", |
| 154 | "refunds", |
| 155 | "invoice_credits", |
| 156 | "app_settings" |
| 157 | ], |
| 158 | "lock": "ledger", |
| 159 | "on": { |
| 160 | "create": true, |
| 161 | "update": [ |
| 162 | "amount_cents", |
| 163 | "invoice_id", |
| 164 | "status" |
| 165 | ], |
| 166 | "delete": true |
| 167 | } |
| 168 | } |
| 169 | ], |
| 170 | "hooks": { |
| 171 | "before_write": "hook_payments" |
| 172 | } |
| 173 | } |