Orders, Transactions & Refunds
Relevant source files
The following files were used as context for generating this wiki page:
Polar implements a robust financial management system designed to handle the complexities of being a Merchant of Record. This system ensures accurate tracking of customer purchases (Orders), the movement of funds (Transactions), and the processing of reversals (Refunds). The architecture is built to maintain consistency across financial states, tax obligations, and product delivery.
The system is primarily composed of SQLAlchemy models for data persistence and specialized service layers for business logic execution. It manages the lifecycle from a successful checkout to potential financial reversals, integrating deeply with external payment processors like Stripe.
Financial Data Models
The core of the system is defined by several key models that track the state and relationships of financial entities.
Order Model
The Order model represents a successful purchase by a customer. It stores total amounts, tax information, and links the customer to the specific product and organization.
| Field | Type | Description |
|---|---|---|
amount | Integer | Total amount paid by the customer in cents. |
tax_amount | Integer | Total tax amount collected. |
currency | String | Three-letter currency code (ISO 4217). |
billing_reason | Enum | Reason for the order (e.g., purchase, subscription_create). |
stripe_invoice_id | String | Reference to the Stripe Invoice. |
Sources: server/polar/models/order.py:10-50
Transaction Model
Transactions represent granular movements of money within the Polar platform. This includes payments, payouts, and platform fees.
The relationship shows how a single Order can result in multiple Transaction records (e.g., payment, fee, and payout).
Order Management Logic
The OrderService handles the creation and retrieval of orders. It is responsible for ensuring that when a payment is successful, the resulting Order is correctly associated with the customer's entitlements.
Key Service Operations
- Creation: Orders are typically generated via Stripe checkout completions.
- Financial Calculations: The model includes properties to calculate the net amount after taxes and fees.
- Remaining Balance: Tracks if an order has been partially or fully refunded.
# From server/polar/models/order.py
def get_remaining_balance(self) -> int:
return self.amount - self.refunded_amount
Sources: server/polar/models/order.py:80-85, server/polar/order/service.py
Refund Processing
The RefundService manages the complex logic of returning funds to customers. This process must account for tax reversals and update the associated order's balance.
Refund Workflow
When a refund is initiated, the system performs several critical checks and updates:
- Validates that the refund amount does not exceed the remaining order balance.
- Interacts with the payment processor (Stripe) to reverse the funds.
- Updates the
Ordermodel'srefunded_amountfield. - Generates a "Refund" type
Transactionfor accounting.
Backoffice Financial UI
For administrators, the project provides a backoffice interface to monitor and manage these financial entities. The implementation uses a modular pattern with specialized data components.
UI Components for Finance
- Datatables: Used to list orders and transactions with sorting and pagination.
- Description Lists: Used in detail pages to show granular financial data like
subtotal,tax_amount, andpayout_status. - Status Badges: Visual indicators for order statuses (e.g.,
paid,pending,refunded).
Sources: server/polar/backoffice/DEVELOPMENT_GUIDE.md:280-330
Summary
The Orders, Transactions & Refunds system provides the financial backbone for the Polar platform. By strictly separating the concepts of an Order (the customer's purchase) from a Transaction (the movement of money), the system maintains high auditability and flexibility. The RefundService ensures that reversals are handled safely, keeping the platform's financial state in sync with external processors and internal accounting requirements.