Back to app

YYY-BATCH-015 — Component-Aware Refund Economics & Reversal Ledger

Status: Implementation Complete Date: September 29, 2026 Author: YYYUUUPPP Engineering


Executive Summary

Batch 015 replaces lump-sum refund logic with a reason-based, component-aware allocation engine. Every refund is decomposed into individual money components (hammer, buyer premium, seller fee, house commission, shipping, tax, promotional credit), each reversed through its own append-only ledger entry. The marketplace never profits from a fully reversed transaction — all fees and commissions are reversed when the underlying sale is reversed.


What Changed

1. Canonical Refund Policy Engine (
base44/shared/refund-policy.ts
)

  • 16 canonical reason codes (SELLER_CANNOT_FULFILL, ITEM_NOT_AS_DESCRIBED, CARRIER_LOST, BUYER_REMORSE, etc.)
  • 9 money components with individual tracking (HAMMER, BUYER_PREMIUM, SELLER_MARKETPLACE_FEE, HOUSE_COMMISSION, SHIPPING, TAX, PROMOTIONAL_CREDIT, PROCESSOR_COST, OTHER_ADJUSTMENT)
  • Server-side allocation generator — client input is never authoritative
  • Policy matrix maps each reason code to its economic consequences
  • Validation layer prevents overflow (refund > remaining)
  • Cash/credit split invariant — cash first, then credit, never exceeds captured

2. Shared Refund Execution (
base44/shared/refund-execution.ts
)

  • Component-aware success processing shared by process-refund and sync-refund-status
  • Individual Transaction entries for each component reversal (append-only)
  • Promotional credit restoration with idempotency key
  • MarketplaceReceivable creation when payout was already released
  • Cumulative tracking on AuctionItem and PaymentExpectation
  • Refund reversal handler — compensating entry, never deletes original

3. Entity Updates

  • RefundRequest: 15 new allocation fields (hammer_refund_minor, buyer_premium_refund_minor, seller_fee_reversal_minor, house_commission_reversal_minor, shipping_refund_minor, tax_refund_minor, cash_refund_minor, credit_restore_minor, etc.)
  • Transaction: New types (fee_reversal, premium_reversal, commission_reversal, tax_reversal, credit_restore, receivable_created) + append-only RLS
  • PaymentExpectation: Cumulative component tracking fields
  • AuctionItem: Cumulative tracking + house commission snapshot (original_house_commission_minor, house_commission_remaining_minor)
  • MarketplaceReceivable: New entity for post-payout debt tracking

4. Backend Functions Updated

  • approve-refund: Generates and persists server-side allocation, validates before approval
  • process-refund: Uses cash_refund_minor from allocation, calls shared execution module
  • sync-refund-status: Uses shared execution module for component-aware success/reversal
  • release-payout: Allocation-aware seller net (fee + commission reversals reduce effective deductions)
  • preview-refund-allocation: Admin preview endpoint showing exact allocation before approval
  • request-refund: Accepts canonical reason_code

5. Admin UI

  • RefundApprovalDialog: New component with reason-code selector, responsible-party selector, and live allocation preview
  • RefundManagementPanel: Shows allocation breakdown for approved refunds, uses new approval dialog

6. Public Copy

  • Terms.jsx: Fixed buyer premium contradiction (was "no buyer premium", now correctly states 1% buyer premium), added Section 11 (Refunds & Cancellations) describing component-aware refund policy

Test Results

Exact Money Tests (12 scenarios, 52 assertions)

  • 47 passed, 0 failed after proportional branch fix
  • Scenarios tested: full seller cancellation, full reversal with credit, tax correction, shipping overcharge, carrier lost, partial refund, duplicate payment, consignment with house commission, cumulative refund sequences, buyer remorse (fail-closed), overflow detection, cash/credit split

Key Invariants Verified

  1. No penny drift — all arithmetic in integer cents
  2. Cash never exceeds captured —
    cash_refund_minor ≤ cash_captured - cumulative_cash_refunded
  3. Credit never exceeds applied —
    credit_restore_minor ≤ credit_applied - cumulative_credit_restored
  4. Full reversal restores all credit — promotional credit is always restored on full reversal
  5. Fee reversal follows responsible party — seller fee reversed when seller is responsible
  6. Commission reversal tracked — house commission reversed and receivable created if already paid
  7. Append-only ledger — original transactions never modified, compensating entries only
  8. Idempotency — deterministic keys prevent duplicate effects

Architecture Decisions

| Decision | Rationale | |---|---| | Server-side allocation generation | Client input is never authoritative — prevents tampering | | Integer minor units (cents) | Eliminates floating-point drift in financial calculations | | Append-only Transaction RLS | Historical records are immutable; corrections use compensating entries | | MarketplaceReceivable entity | Tracks post-payout debt when refund exceeds seller's remaining proceeds | | Canonical reason codes | Enables deterministic, auditable economic classification | | Cash-first credit split | Maximizes buyer's cash recovery, restores credit only for remainder | | Allocation version field | Enables future policy updates without invalidating historical allocations |


Known Limitations

  1. BUYER_REMORSE and CONSIGNMENT_DISPUTE require admin-specified component amounts — the engine does not auto-allocate
  2. OTHER_ADMIN_ADJUSTMENT requires an explanatory admin note
  3. Tax remittance adjustment is tracked but not automated — admin must file with tax authority
  4. Carrier recovery is tracked but not automated — admin must file insurance claims
  5. Processor cost adjustment field exists but is not yet populated (payment processor fees are not reversed)

Files Modified

| File | Change | |---|---| |

base44/shared/refund-policy.ts
| NEW — canonical policy engine | |
base44/shared/refund-execution.ts
| NEW — shared execution module | |
base44/shared/notify.ts
| Extended logTransaction with component fields | |
base44/shared/money.ts
| Proportional reversal math helper | |
base44/entities/RefundRequest.jsonc
| 15 allocation fields | |
base44/entities/Transaction.jsonc
| New types + append-only RLS | |
base44/entities/PaymentExpectation.jsonc
| Cumulative tracking | |
base44/entities/AuctionItem.jsonc
| Cumulative tracking + commission snapshot | |
base44/entities/MarketplaceReceivable.jsonc
| NEW — receivable entity | |
base44/functions/approve-refund/entry.ts
| Server-side allocation generation | |
base44/functions/process-refund/entry.ts
| Uses shared execution + cash_refund_minor | |
base44/functions/sync-refund-status/entry.ts
| Uses shared execution | |
base44/functions/release-payout/entry.ts
| Allocation-aware seller net | |
base44/functions/preview-refund-allocation/entry.ts
| Accepts refund_request_id | |
src/components/RefundApprovalDialog.jsx
| NEW — allocation preview UI | |
src/components/RefundManagementPanel.jsx
| Uses new dialog, shows breakdown | |
src/pages/Terms.jsx
| Fixed buyer premium, added refund section |