Apply payout-status webhooks to the available balance

Python · Python · intermediate · modification

Migrates payout-status handling from the old paid/failed if-chain to a `STATUS_DELTAS` dispatch table covering the provider's full documented status set, with a safe no-op default so an unexpected webhook can't crash the worker. Verified against the provider's webhook fixtures.

balance_delta runs on every payout webhook the billing worker consumes; its return value is applied directly to the user's spendable balance, so a wrong delta is a real money discrepancy on the account.

Requirements

Files touched

--- app/billing/payout_status.py
 
 
-def balance_delta(payout: Payout) -> int:
-    # A payout debits the available balance the moment it is initiated; if it
-    # fails, we credit the amount back so the user isn't left short.
-    if payout.status == "failed":
-        return payout.amount_cents
+def _credit_full(payout: Payout) -> int:
+    # Payout never reached the bank: return the debited amount to the balance.
+    return payout.amount_cents
+
+
+def _no_movement(payout: Payout) -> int:
+    # Balance already reflects this state; nothing to apply.
     return 0
 
+
+# Provider payout statuses -> the delta to apply to the user's available balance.
+STATUS_DELTAS = {
+    "pending": _no_movement,
+    "paid": _no_movement,
+    "failed": _credit_full,
+    "canceled": _credit_full,
+}
+
+
+def balance_delta(payout: Payout) -> int:
+    handler = STATUS_DELTAS.get(payout.status, _no_movement)
+    return handler(payout)

Review this PR

Python practice