Skip to content

feat(core): split payments across vendor payout accounts - #101

Open
alexasomba wants to merge 4 commits into
emdashCommerce:mainfrom
alexasomba:feat/provider-split-allocations
Open

alexasomba wants to merge 4 commits into
emdashCommerce:mainfrom
alexasomba:feat/provider-split-allocations

Conversation

@alexasomba

@alexasomba alexasomba commented Nov 24, 2026 •

Copy link
Copy Markdown

Part of the Flutterwave set described in #97. Item 4 — a generic core seam; no Flutterwave code in core.

Why

A cart with vendor-owned items could not be paid by a non-Stripe provider at all, and Stripe Connect deliberately rejects multi-vendor carts. Flutterwave supports splitting one payment into collection subaccounts with an exact flat_subaccount amount, but core had no way to hand a provider per-vendor amounts.

This adds that seam. Core owns the arithmetic; a provider only routes what core supplies.

What

  • capabilities.splits on PaymentProvider, and splits in unsupportedHostedCheckoutFeatures. A vendor cart is now refused with a readable 409 for a provider that cannot split, instead of silently charging the platform account.
  • InitCheckoutInput.allocations (vendorId, accountReference, amountMinor) and platformRemainderMinor. Core computes them from trusted cart prices and the vendor's platform fee; providers must not reprice.
  • Vendor.paymentAccounts, keyed by provider id, for providers other than Stripe: accountReference, status, optional settlement currency and label. Stripe Connect keeps its existing fields.
  • computeProviderSplit: groups cart lines by vendor, requires an active account in the cart's currency, applies the vendor/global platform fee, and returns exact minor amounts. It throws merchant-readable errors for a missing vendor, a missing/pending/disabled account, a currency mismatch, a non-positive share, or a cart-wide discount larger than the platform remainder.
  • Admin API: POST admin/vendors/item creates a vendor without Stripe; PATCH accepts paymentAccount or paymentAccounts to add, replace or remove an account. Vendor routes open when Connect is on or a split-capable provider is registered.
  • Settings exposes each provider's capabilities, and the Vendors page adds a vendor and edits payout accounts when a split-capable provider is active — no Stripe onboarding needed. The Stripe onboarding/refresh/payout routes still require Connect.
  • Product editor: the vendor picker and the save-time validation now accept a split-capable provider's active payout account, not only Stripe chargesEnabled. A Flutterwave-only store can assign products to vendors without turning on Stripe Connect.

What does not change

  • Stripe Connect: single-vendor destination charges are untouched; multi-vendor Stripe carts still return 501.
  • A cart without vendor items sends no allocations.
  • Vendor records created by Stripe onboarding keep working; paymentAccounts is additive.

Tests

  • 345 core tests pass, including 18 new ones for split computation and the vendor API, 5 for product-to-vendor assignment, and 4 that run the real checkout/create-session route:
    • two vendors at 10% → 135000 each, platform remainder 180000 (platform item + fees);
    • no vendor items → no allocations;
    • a provider without splits → 409 naming splits;
    • a vendor with no account → 409 naming the vendor.
  • Split computation covers fees, overrides, tax/shipping/platform items, missing vendors, pending/disabled accounts, currency mismatch, zero shares, a platform-absorbed discount and a discount larger than the platform remainder.
  • Vendor API covers the gate, create, add/replace/remove, unknown/non-splitting providers, bad references/status, Stripe status untouched, search by account reference, and creating two vendors without a Stripe account.
  • tsc --noEmit clean; Biome clean on the touched files.

Next

The Flutterwave package is #107, which builds on this. #103 (payout onboarding) and #105 (settlement) also build on this.

Review response (moset15)

  • Vendor unique index: Vendor.stripeAccountId is now optional and createVendor omits it for non-Stripe vendors. A missing field is NULL, so a shop without Stripe can create any number of vendors; two vendors still cannot share an acct_…. Test: two vendors through admin/vendors/item. The Stripe Refresh action is hidden when a vendor has no Stripe account.
  • Generic comments: accountReference and capabilities.splits now name Paystack's ACCT_… as well as Flutterwave's RS_….
  • Fee bearer: the README provider-author section says to keep the fee on the platform so vendor shares stay exact.
  • Coupons: intended. Shares are computed on pre-discount line subtotals, matching split.ts; the platform absorbs a cart-wide discount, and a discount larger than the platform remainder is refused with a readable 409 rather than silently reducing a vendor's payout. Tests cover both cases. If you want vendors to bear part of the discount, that is a separate policy change.

Review notes

@moset15 moset15 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this against Paystack's split API, since Paystack will build on the same seam.

It fits Paystack as is. allocations maps onto Paystack's dynamic split at initialise: split: { type: "flat", bearer_type: "account", subaccounts: [{ subaccount: accountReference, share: amountMinor }] }. Flat shares are in subunits and must sum to at most the amount, which platformRemainderMinor >= 0 already guarantees. bearer_type: "account" keeps Paystack's fee on the platform, the same as Connect, so a vendor gets exactly their share. A multi-vendor cart is one split with several subaccounts. paymentAccounts[providerId].accountReference holds Paystack's ACCT_… code.

Two small points:

  1. The comments on accountReference and capabilities.splits name Flutterwave only. Something like "a provider subaccount, e.g. Flutterwave RS_… or Paystack ACCT_…" keeps the type generic.
  2. Fee bearer isn't part of the seam, which is right, but it's worth one line in the README's provider-author section: a provider should keep its fee on the platform so vendor shares stay exact. Paystack and Flutterwave both default that way, but a later provider might not.

One question, not a blocker: shares are computed on pre-discount line subtotals, so a cart-wide coupon comes out of the platform's remainder, and a large one makes the allocations exceed the total (409). That matches the Connect plan in split.ts. Is that the intended rule for coupons on vendor items?

@moset15

moset15 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

One more from testing against Paystack, separate from my earlier review: a shop without Stripe can only create one vendor.

createVendor stores stripeAccountId: "", and vendors.stripeAccountId has a unique index. EmDash builds that as a SQL unique index on json_extract(data, '$.stripeAccountId'), so the second empty value fails: UNIQUE constraint failed (reproduced in SQLite, which D1 uses).

Suggested fix: make stripeAccountId optional and leave it out when the vendor has no Stripe account. A missing JSON field is NULL, and NULLs don't clash. I checked: any number of vendors without a Stripe id save fine, and the same acct_… used twice is still refused. Existing rows with "" would need the field dropped, or only the first one keeps working.

A test that creates two vendors through admin/vendors/item would cover it.

createVendor stored stripeAccountId: "" and the field has a unique
index, so the second vendor failed. The field is now optional and
omitted when there is no Stripe account; a missing field is NULL.

Also documents the coupon rule for split allocations (the platform
absorbs a cart-wide discount; a larger one is a 409) and makes the
allocation comments generic for Paystack.
@alexasomba

alexasomba commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

@moset15 thanks for the review and the two-vendor catch — both are addressed.

  • Vendor unique index: Vendor.stripeAccountId is now optional and createVendor omits it for non-Stripe vendors. A missing field is NULL, so a shop without Stripe can create any number of vendors; two vendors still cannot share an acct_…. Added the two-vendor test through admin/vendors/item; the Stripe Refresh action is hidden when a vendor has no Stripe account.
  • Generic comments: accountReference and capabilities.splits now name Paystack's ACCT_… alongside Flutterwave's RS_…, and the README provider-author section says to keep the fee on the platform so vendor shares stay exact.
  • Coupons: yes, intended. Shares are computed on pre-discount line subtotals, matching split.ts; the platform absorbs a cart-wide discount, and a discount larger than the platform remainder is refused with a readable 409 instead of silently reducing a vendor's payout. Tests cover both the platform-absorbed and over-discount cases. If you'd rather vendors bear part of the discount, that's a policy change worth a separate issue.

Pushed in 3650bba (fix) and e6ba737 (notes); #103/#105/#107 are merged forward. Thanks for checking the Paystack mapping too — the flat-share mapping you described is exactly what the seam was meant to support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants