Payout
๐ Overviewโ
internal/api/v1/billing/services/payout.service.js exposes read-only endpoints that list payouts, retrieve detail records, and summarise balance transactions for Stripe connected accounts. It is used by the billing dashboard to display bank deposits, drill into individual payouts, and display aggregated fee/charge breakdowns.
Primary Controller: payout.controller.js
๐ External Dependenciesโ
- Stripe Payouts API โ Lists and retrieves payouts.
- Stripe Balance Transactions API โ Fetches transactions associated with a payout.
- Moment-Timezone โ Converts ISO dates to Unix timestamps for Stripe filters.
- Stripe Config Utility โ
stripeConfiginitialises the client with connected account tokens.
๐ Data Flowโ
flowchart TD
UI[Billing UI] -->|GET /payouts| Controller
Controller --> Service
Service -->|stripeAccount| Stripe
Stripe --> Payouts
Stripe --> BalanceTransactions
๐งฉ Functionsโ
getPayouts({ startDate, endDate, firstPayoutId, lastPayoutId, limit, stripeBilling })โ
Retrieves paginated payout history.
- Converts optional date filters into Unix seconds using
moment. - Calls
stripe.payouts.listwith pagination cursors (starting_after,ending_before). - Maps Stripe payload into DTO (amount, created, status, bank info).
- Returns
{ payouts, has_more, first_payout_id, last_payout_id }.
Edge Casesโ
- Handles empty results by returning
nullcursor fields. - Wraps Stripe connection errors with
customhelper for consistent messaging.
getPayout({ payoutId, stripeBilling })โ
Fetches a single payout with expanded destination and balance transaction.
- Normalises amount, fees, routing info, and currency into a UI-friendly object.
- Uses
.toFixed(2)to ensure currency precision.
getTransactions({ payoutId, hasMore, firstTransactionId, lastTransactionId, limit, stripeBilling })โ
Paginates transactions tied to a payout.
- Calls
stripe.balanceTransactions.listwith payout filter and pagination cursors. - Returns DTO with amount, type, net, and fee for each transaction.
- Calculates
first_transaction_idandlast_transaction_idfor subsequent calls.
getSummary({ payoutId, stripeBilling })โ
Aggregates balance transactions by type.
- Streams transactions (auto-pagination) to avoid manual pagination loops.
- Buckets by
charge,refund,adjustment,transfer,topup, andstripe_fee. - Totals count, gross amount, fees, and net value per type.
- Computes
totalas sum of net values.
๐งช Testing Notesโ
- Mock Stripe clients when unit testing to simulate pagination responses.
- Prefer integration tests that validate pagination cursors and currency formatting.
โ ๏ธ Operational Considerationsโ
- Controllers should cap
limitto prevent loading excessive transaction history in a single call. - Auto-pagination in
getSummaryiterates up to 100 records per page; consider additional guards if payouts exceed this volume.