Skip to main content

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 โ€“ stripeConfig initialises 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.

  1. Converts optional date filters into Unix seconds using moment.
  2. Calls stripe.payouts.list with pagination cursors (starting_after, ending_before).
  3. Maps Stripe payload into DTO (amount, created, status, bank info).
  4. Returns { payouts, has_more, first_payout_id, last_payout_id }.

Edge Casesโ€‹

  • Handles empty results by returning null cursor fields.
  • Wraps Stripe connection errors with custom helper 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.

  1. Calls stripe.balanceTransactions.list with payout filter and pagination cursors.
  2. Returns DTO with amount, type, net, and fee for each transaction.
  3. Calculates first_transaction_id and last_transaction_id for subsequent calls.

getSummary({ payoutId, stripeBilling })โ€‹

Aggregates balance transactions by type.

  1. Streams transactions (auto-pagination) to avoid manual pagination loops.
  2. Buckets by charge, refund, adjustment, transfer, topup, and stripe_fee.
  3. Totals count, gross amount, fees, and net value per type.
  4. Computes total as 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 limit to prevent loading excessive transaction history in a single call.
  • Auto-pagination in getSummary iterates up to 100 records per page; consider additional guards if payouts exceed this volume.
๐Ÿ’ฌ

Documentation Assistant

Ask me anything about the docs

Hi! I'm your documentation assistant. Ask me anything about the docs!

I can help you with:
- Code examples
- Configuration details
- Troubleshooting
- Best practices

Try asking: How do I configure the API?
09:31 AM