Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
@carbon/ee/stripe-connect
Stripe Connect integration for Carbon: onboarding connected accounts, resolving Stripe customers for sales invoices, recording payments into the Carbon GL, and syncing missed payments via a background sweep.
Always
- Import
recordStripeConnectPaymentonly from@carbon/ee/stripe-connect.server— it is the single authority for writing Stripe payments into Carbon. Both the webhook handler and the pull sweep call it; adding a third call site MUST go through the same function. recordStripeConnectPaymentis idempotent on Stripe invoice id, but NOT limited to one payment per invoice — a duplicate delivery of the SAMEamount_paidloses the race at the DB via the partial unique index onexternalIntegrationMapping(INTEGRATION="stripe-connect",entityType="payment",allowDuplicateExternalId=false). A delivery with a HIGHER cumulativeamount_paidthan what's already recorded (installment / partial-payment invoices) instead records a new payment for just the delta, linked withallowDuplicateExternalId: true— so one Stripe invoice can map to several Carbon payments. Never guard with an ad-hoc SELECT before calling it, and never assumegetByExternalId(singular) sees every payment for an invoice — usemappingService.getAllByExternalIdfor that.voidStripeConnectPaymentreverses every non-Voided payment mapped to an invoice — it's a FULL reversal only (viapost-payment'svoidop, which mirrors every journal line including the fee). There is no partial reversal capability; a partial refund is logged, not acted on.- All external-ID links go through
createMappingServicefrom@carbon/ee/accounting—mappingService.getByExternalId,mappingService.getEntityId,mappingService.link. Do not add per-entityexternalIdcolumns. - Hooks must stay in
hooks.server.ts— registered under"stripe-connect"inpackages/ee/src/hooks.server.ts. Server-only imports (e.g.@carbon/stripe/connect.server) must not appear inconfig.tsx(bundled for client). StripeConnectconfig andStripeConnectSettingsSchemaare exported from@carbon/ee(the root barrel) — the integration framework reads them there. Do not re-export from a separate subpath.
Ask First
- Changing the accounting accounts that are used for FX gain/loss or service-charge
fees — these are read from
accountDefaultat record time and affect live GL entries. - Adding or removing Stripe capabilities on the account-creation constants in
@carbon/stripe/connect.constants.ts. - Making
stripeConnectOnInstallorstripeConnectOnUninstallnon-stubs — currently no-ops; any side effects must be reviewed against the install lifecycle.
Never
- Do not call
serviceRole.from("payment").delete(...)outside ofrecordStripeConnectPayment— the rollback path inside that function is the only place that deletes a payment row; orphaninginvoiceSettlementrows by deleting their payment elsewhere will corrupt the AR ledger. - Do not advance a pull-sweep cursor on
paid_atwhen the Stripe query filters oncreated— invoices created before the cursor window but paid after it are permanently missed. Advance the cursor on the same field used in the list filter (invoice.created). - Do not surface post-send cleanup failures (PDF storage, notes append) as Stripe
send failures — the invoice is already sent to the customer. Log and continue;
returning
success: falseafter a sent invoice causes the caller to retry and create a duplicate Stripe invoice.
Validation Commands
pnpm --filter @carbon/ee typecheck
pnpm --filter @carbon/ee test
Key Exports
| Subpath | Provides |
|---|---|
@carbon/ee (root) |
StripeConnect integration definition, StripeConnectSettingsSchema |
@carbon/ee/stripe-connect.server |
recordStripeConnectPayment, StripeConnectPaymentResult, voidStripeConnectPayment, StripeConnectVoidResult |
Key Functions
recordStripeConnectPayment(payment.server.ts) — records a paid Stripe invoice as a Carbonpaymentrow, settles it against thesalesInvoice, posts the journal entry (bank debit, AR credit, service-charge expense via thefeefield onpost-payment), and writes toexternalIntegrationMapping. Returns{ status: "recorded" | "skipped" }. Throws on fixable config errors (missing bank account, missing fee account, missing sequence) so Stripe retries. Reconciles against every prior mapping for the invoice before doing anything: a mapped payment stillDraft(a prior delivery that inserted the payment + mapping but crashed before settlement/posting) is RESUMED at its own already- recorded amount; otherwise the delta between the invoice's currentamount_paidand the sum already recorded is what gets recorded (0 or negative → skipped), with the processing fee prorated to that delta's share. The realized settlement-date exchange rate (getConnectInvoicePaymentDetails().exchangeRate, from Stripe'sbalance_transaction.exchange_rate) drivesinvoiceSettlement.sourceExchangeRateandpayment.exchangeRate; the invoice's own booked rate stays ontargetExchangeRate— this is what makestotalFxImpactinpost-paymentnon-zero for a real FX payment instead of structurally suppressed.voidStripeConnectPayment(payment.server.ts) — reverses every non-Voided payment mapped to a Stripe invoice viapost-payment'svoidop (a full reversal that mirrors every journal line, fee included). Called oncharge.refunded(full only),charge.dispute.closed(statuslost), andinvoice.voided. Returns{ status: "voided", paymentIds } | { status: "skipped", reason }; throws on apost-paymentfailure so the webhook returns 500 and Stripe retries.stripeConnectHealthcheck(hooks.server.ts) — called by the integration health system; returnstrueonly whenchargesEnabled && payoutsEnabledon the connected account. Returnsfalsefor missing accounts, Stripe errors, requirement errors, AND the normal mid-onboarding case (account exists, no errors yet, just not fully enabled) — the return type is a plainboolean, so a mid-onboarding account reads as unhealthy (red badge) until onboarding completes.companyIntegration.activegoestrueas soon as the Stripe account is created (seegetOrCreateConnectAccount), before onboarding finishes, so it does NOT gate this neutral state —getStripeConnectAccountId(packages/stripe/src/send-sales-invoice.server.ts) additionally checksmetadata.chargesEnabledfor exactly this reason.stripeConnectOnInstall/stripeConnectOnUninstall(hooks.server.ts) — currently no-ops; registered because theIntegrationServerHookscontract requires them.
Tables Touched by recordStripeConnectPayment
| Table | Operation |
|---|---|
salesInvoices |
SELECT — look up the Carbon invoice by id |
accountDefault |
SELECT — resolve GL accounts for bank, AR, service charge |
companySettings |
SELECT — currency and payment sequence settings |
payment |
INSERT (and DELETE on rollback within the same function), plus a SELECT across all mapped payments for the invoice to compute the resume/delta decision |
invoiceSettlement |
DELETE + INSERT (Kysely transaction) — replace settlements |
externalIntegrationMapping |
SELECT (all mappings for the invoice) + INSERT — links a Stripe invoice to one or more payments |
Cross-References
.claude/rules/billing-system.md— plan/edition gating (INTEGRATION_WHITELIST,FEATURE_PLANS)packages/stripe/AGENTS.md—@carbon/stripe/connect.serverAPI (getConnectAccountStatus,getConnectInvoicePaymentDetails,fromStripeAmount)packages/ee/AGENTS.md—createMappingServicepattern,externalIntegrationMappingtablepackages/jobs/src/inngest/functions/integrations/stripe-connect-pull-sweep.ts— background sweep that callsrecordStripeConnectPaymentfor invoices missed by webhooksapps/erp/app/routes/api+/webhook.stripe-connect.ts— real-time webhook caller; also handlescharge.refunded,charge.dispute.closed,invoice.voided(→voidStripeConnectPayment) andaccount.updated(→ refresheschargesEnabled/payoutsEnabled/requirement errors viagetConnectAccountStatus)