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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
payments.py · 3.2 KB · 94 lines
| 1 | """Pre-write hook for payments: the overpayment gate. |
| 2 | |
| 3 | Cross-record rule the schema can't express (docs/business-logic-patterns.md): |
| 4 | a new payment may not take an invoice past its total — received payments minus |
| 5 | refunds plus this payment must stay <= invoice.total_cents. Configurable via |
| 6 | app_settings key `payments.overpayment_policy` ("reject", the default, or |
| 7 | "allow" for shops that keep customer credit balances). Also covers the |
| 8 | numeric-range gap: amount_cents must be positive (logic-decisions: count this |
| 9 | repeat toward a future declarative min/max rule). |
| 10 | |
| 11 | Gates sum the records directly — never derived rollup values (a gate must be |
| 12 | authoritative even when a derived caption is stale or empty). |
| 13 | """ |
| 14 | |
| 15 | import os |
| 16 | |
| 17 | import object_records |
| 18 | |
| 19 | |
| 20 | def _base_dir(): |
| 21 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 22 | |
| 23 | |
| 24 | def _setting(key, default): |
| 25 | try: |
| 26 | for row in object_records.read_collection_records("app_settings", base_dir=_base_dir()): |
| 27 | if row.get("key") == key and row.get("value"): |
| 28 | return row["value"] |
| 29 | except Exception: |
| 30 | pass |
| 31 | return default |
| 32 | |
| 33 | |
| 34 | def BEFORE_WRITE(request): |
| 35 | if request.get("action") != "create": |
| 36 | return None |
| 37 | record = request.get("record") or {} |
| 38 | |
| 39 | try: |
| 40 | amount = int(record.get("amount_cents") or 0) |
| 41 | except ValueError: |
| 42 | return None # schema validation reports the type error properly |
| 43 | if amount <= 0: |
| 44 | return {"error": "Payment amount must be a positive number of cents.", "status": 400} |
| 45 | |
| 46 | if (record.get("status") or "received") != "received": |
| 47 | return None |
| 48 | invoice_id = record.get("invoice_id") |
| 49 | if not invoice_id: |
| 50 | return None # required + relation validation own this |
| 51 | |
| 52 | if _setting("payments.overpayment_policy", "reject") == "allow": |
| 53 | return None |
| 54 | |
| 55 | base = _base_dir() |
| 56 | try: |
| 57 | invoice = object_records.get_collection_record("invoices", invoice_id, base_dir=base) |
| 58 | except Exception: |
| 59 | return None # relation validation owns a missing invoice |
| 60 | try: |
| 61 | total = int(invoice.get("total_cents") or 0) |
| 62 | except ValueError: |
| 63 | return None |
| 64 | |
| 65 | received = 0 |
| 66 | for p in object_records.read_collection_records("payments", base_dir=base): |
| 67 | if p.get("invoice_id") == invoice_id and (p.get("status") or "received") == "received": |
| 68 | try: |
| 69 | received += int(p.get("amount_cents") or 0) |
| 70 | except ValueError: |
| 71 | continue |
| 72 | refunded = 0 |
| 73 | try: |
| 74 | for r in object_records.read_collection_records("refunds", base_dir=base): |
| 75 | if r.get("invoice_id") == invoice_id: |
| 76 | try: |
| 77 | refunded += int(r.get("amount_cents") or 0) |
| 78 | except ValueError: |
| 79 | continue |
| 80 | except Exception: |
| 81 | pass |
| 82 | |
| 83 | remaining = total - (received - refunded) |
| 84 | if amount > remaining: |
| 85 | return { |
| 86 | "error": ( |
| 87 | f"Payment of {amount} cents exceeds the remaining balance of " |
| 88 | f"{max(remaining, 0)} cents on invoice {invoice_id}. " |
| 89 | "(Set app_settings payments.overpayment_policy=allow to permit " |
| 90 | "customer credit balances.)" |
| 91 | ), |
| 92 | "status": 409, |
| 93 | } |
| 94 | return None |