@companyio/payments
v0.3.1
Published
Universal settlement payments for multi-tenant CompanyIO products
Readme
@companyio/payments
Universal payment infrastructure for CompanyIO products (Fuel, POS, School, ERP).
Payments owns generic payment transactions, allocations, refunds, and idempotent settlement posting.
Payments does not own:
- invoice / bill / expense balances (Sales, Purchasing, Expenses)
- customers or suppliers (
@companyio/contacts) - the chart of accounts or journals (
@companyio/accounting) - Fuel day closing, cash drawers, or meter cash reconciliation (
@companyio/fuel) - CompanyIO SaaS subscription billing (plans, entitlements, JazzCash/Easypaisa collection for the product itself) — that is a separate planned package; see ADR-PLAT-001
Business fact vs infrastructure
SalesPayment / PurchasingPayment / ExpensesPayment
= domain business fact (what was settled on a document)
PaymentsPayment
= generic payment transaction (how money moved)Do not merge these. Connect them with IDs:
Payments.createPayment(...)
↓
Sales.applyExternalSettlement({ externalPaymentId, amount })
Purchasing.applyExternalSettlement(...)
Expenses.applyExternalSettlement(...)Domain packages still keep their own pay() that writes a local payment row and may post via their Accounting adapter. That path remains valid for simple in-domain collection.
Install
The host owns PostgreSQL, Prisma Client, and migrations.
pnpm exec payments init --schema apps/api/prisma/schema.prisma
pnpm exec payments status
pnpm --filter @companyio/api exec prisma migrate deploy --config prisma7.config.tspayments init copies prisma/migrations/0001_payments_baseline when missing (20260926000500_payments_baseline). Idempotency is unique per (main_business_id, branch_id, idempotencyKey).
Fastify
attachPayments(app, {
store: createPrismaPaymentsStore(prisma),
prefix: '/api/v1/payments',
books: createAccountingPaymentsAdapter(accounting),
contacts: createContactsPaymentsAdapter(contacts),
authenticate,
getContext,
});Tenant ids come from the session. Creation is immediately POSTED (or returned unchanged on idempotent retry). Cancel and refund do not rewrite the original amount.
Frontend adapter
The browser UI ships from two subpaths — never import the package root in a bundle:
@companyio/payments/client— HTTP client + contracts (createPaymentsApi,PAYMENT_METHOD_OPTIONS).@companyio/payments/react—PaymentsProvider, hooks, Platform-UI screens (PaymentsListPage,AddPaymentModal,PaymentDetail,PaymentStatusBadge), andpaymentsQueryKeys.
A host wires its authed requester into the package contract and renders the shared pages:
// 1. createApi — map the host's authed requester into the package contract
const api = createPaymentsApi({
request: async (path, init) => myAuthedJson(path, init), // throws PaymentsApiError on failure
basePath: '/payments',
});
// 2. Provider — api + presentation config
<PaymentsProvider
api={api}
config={{
currencyDisplay: 'PKR',
defaultDirection: 'IN',
canEditPayments, // host RBAC gate for create/cancel/refund
// methodOptions — optional override of PAYMENT_METHOD_OPTIONS
}}
>
<PaymentsListPage
documentTitle="Payments"
listFilter={{
sourceModule: 'fuel',
sourceType: 'customer-payment',
status: 'POSTED',
direction: 'IN',
}}
createDefaults={{ sourceModule: 'fuel', sourceType: 'customer-payment', sourceId: stationId }}
partyOptions={partyOptions}
/>
</PaymentsProvider>;Direction is applied client-side (the list API has no direction param). Mutations invalidate
paymentsQueryKeys.all; dual-invalidate the host's own caches (e.g. ['accounts']) after create/cancel.
Canonical UI labels (shared screens): Add payment, Cancel payment, Refund payment,
Allocate payment. Direction options use Received (in) / Paid (out); method labels come
from PAYMENT_METHOD_OPTIONS (e.g. Online transfer, Bank deposit). Destructive cancel uses
ConfirmDialog from @companyio/platform-ui.
Reference adapters: Fuel Management (products/fuel-management/frontend/src/features/payments) and
ContractFlow (products/contractflow/frontend/src/features/finance).
API Routes
Mounted at /api/v1/payments. Full detail: docs/API_ROUTE_INVENTORY.md.
| Method | Route | Auth | Purpose | | -------- | ------------------------- | ------- | ----------- | | GET | /payments/health | Public | Health | | GET/POST | /payments | Session | List/create | | GET | /payments/:id | Session | Get | | POST | /payments/:id/cancel | Session | Cancel | | GET/POST | /payments/:id/allocations | Session | Allocations | | POST | /payments/:id/refunds | Session | Refund |
Known gaps
- Not a SaaS billing / entitlements package — keep separate from future pricing/billing work.
- Domain document balances stay in Sales/Purchasing/Expenses; Payments is settlement infrastructure only.
- Multi-currency FX and payment-provider adapters are deferred.
Build / test
pnpm --filter @companyio/payments test
pnpm --filter @companyio/payments buildPackage tests cover CLI init, integration flows, external settlement, receivables mapping, and HTTP routes with session-scoped tenant isolation (in-memory store).
