Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/provider-split-allocations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@dashcommerce/core": minor
---

Let a payment provider split one payment across vendor payout accounts.

- `capabilities.splits` and `InitCheckoutInput.allocations` / `platformRemainderMinor`. Core computes exact per-vendor minor amounts from trusted cart prices and the vendor's platform fee; a provider only routes them to the account reference core supplies.
- `Vendor.paymentAccounts`, keyed by provider id, for providers other than Stripe: `accountReference`, `status`, and an optional settlement currency and label. Stripe Connect keeps its existing fields.
- `Vendor.stripeAccountId` is optional. A vendor paid through another provider omits it, so a shop without Stripe can create more than one vendor; a missing field is NULL, so the unique index does not clash.
- A cart-wide discount comes out of the platform remainder, matching the Connect plan; a discount larger than that remainder is refused with a readable 409 instead of silently reducing a vendor's payout.
- `admin/vendors/item` accepts `POST` to create a vendor without Stripe, and `PATCH` with `paymentAccount` / `paymentAccounts` to add, replace or remove a provider account. Vendor routes open when Connect is on **or** a split-capable provider is registered.
- Settings exposes each provider's `capabilities`. The Vendors page adds a vendor and edits payout accounts when a split-capable provider is active, without requiring Stripe.
- A vendor cart is refused with a readable 409 when the active provider cannot split, or when a vendor has no active account for it, instead of silently charging the platform account.
2 changes: 2 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ For provider authors, `PaymentProvider` can declare:
- `credentialFields`: the label, accepted prefixes and help text for the settings form.
- `webhookHeaders`: the headers that identify its webhooks, besides `x-<id>-signature`. `verifyWebhook` receives every request header, and `credentials` (the provider's saved secret key and webhook secret) for headers signed with the API key rather than the webhook secret.
- `checkConnection`: one read-only call with the saved keys, behind a "Test connection" button in Settings. Return `{ ok, message }`, with `ok: false` and a message the merchant can act on for a wrong or under-permitted key.
- `capabilities.splits`: core computes `allocations` (`vendorId`, `accountReference`, `amountMinor`) and `platformRemainderMinor` when the cart has vendor-owned items. Vendors store their payout account under `paymentAccounts[providerId]`; the provider only routes the amounts core supplies.
- Keep your own transaction fee on the platform (Paystack's `bearer_type: account`, Flutterwave's default) so each vendor receives exactly the share core computed.

`parseWebhookEvent` may return `refund.updated` to move a pending refund to `succeeded` or `failed`. A failed refund stops counting against its order: the refunded total, payment status and order status are worked out again, and stock the refund put back is taken back off.

Expand Down
79 changes: 45 additions & 34 deletions packages/core/src/admin/fields/VendorSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
* context (field definition, entry metadata) without needing changes here.
*
* Behaviour:
* - Reads `settings:connectEnabled` via the admin settings API on mount.
* - When multi-vendor is off, renders an info banner instead of the picker.
* - When on, fetches `admin/vendors` and renders a searchable Combobox of
* active/pending vendors, dimming entries with `chargesEnabled=false`.
* - Reads `settings:connectEnabled` and the registered providers' `splits`
* capability via the admin settings API on mount.
* - Marketplace mode is Stripe Connect **or** a provider that can split
* payments (Flutterwave subaccounts).
* - When neither is on, renders an info banner instead of the picker.
* - When on, fetches `admin/vendors` and renders a searchable Combobox,
* dimming vendors with no active payout account.
*/

import { useEffect, useMemo, useState } from "react";
import {
Alert,
Combobox,
FormField,
Loading,
usePluginAPI,
type ComboboxOption,
} from "../kit";
import type { Vendor } from "../../types";
import { Alert, Combobox, type ComboboxOption, FormField, Loading, usePluginAPI } from "../kit";

export interface VendorSelectProps {
value?: string | null;
Expand All @@ -35,7 +31,9 @@ export interface VendorSelectProps {
export function VendorSelect(props: VendorSelectProps) {
const api = usePluginAPI();
const [vendors, setVendors] = useState<Vendor[] | null>(null);
const [connectEnabled, setConnectEnabled] = useState<boolean | null>(null);
const [marketplaceEnabled, setMarketplaceEnabled] = useState<boolean | null>(null);
const [connectEnabled, setConnectEnabled] = useState(false);
const [splitsProviders, setSplitsProviders] = useState<Array<{ id: string; label: string }>>([]);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
Expand All @@ -44,22 +42,31 @@ export function VendorSelect(props: VendorSelectProps) {
try {
const settings = await api.get<Record<string, unknown>>("admin/settings");
if (!alive) return;
const on = settings.connectEnabled === true;
setConnectEnabled(on);
if (on) {
const connect = settings.connectEnabled === true;
const providers = Array.isArray(settings._providers)
? (settings._providers as Array<{
id: string;
label: string;
capabilities?: { splits?: boolean };
}>)
: [];
const splits = providers
.filter((provider) => provider.capabilities?.splits === true)
.map((provider) => ({ id: provider.id, label: provider.label }));
const enabled = connect || splits.length > 0;
setConnectEnabled(connect);
setSplitsProviders(splits);
setMarketplaceEnabled(enabled);
if (enabled) {
try {
const res = await api.get<{ items: Vendor[] }>("admin/vendors");
if (alive) setVendors(res.items ?? []);
} catch (err) {
if (alive)
setError(
err instanceof Error ? err.message : "Could not load vendors",
);
if (alive) setError(err instanceof Error ? err.message : "Could not load vendors");
}
}
} catch (err) {
if (alive)
setError(err instanceof Error ? err.message : "Could not load settings");
if (alive) setError(err instanceof Error ? err.message : "Could not load settings");
}
}
load();
Expand All @@ -71,38 +78,42 @@ export function VendorSelect(props: VendorSelectProps) {
const options = useMemo<ComboboxOption[]>(() => {
if (!vendors) return [];
return vendors.map((v) => {
const statusHint =
v.onboardingStatus === "active" && v.chargesEnabled
? "Active"
: v.onboardingStatus === "restricted"
? "Restricted"
: "Pending";
const stripeReady = connectEnabled && v.chargesEnabled;
const providerReady = splitsProviders.some(
(provider) => v.paymentAccounts?.[provider.id]?.status === "active",
);
const ready = stripeReady || providerReady;
const statusHint = ready
? "Active"
: v.onboardingStatus === "restricted"
? "Restricted"
: "Pending";
return {
value: v.id,
label: `${v.name} · ${v.email}`,
hint: statusHint,
disabled: !v.chargesEnabled,
disabled: !ready,
};
});
}, [vendors]);
}, [vendors, connectEnabled, splitsProviders]);

const { value, onChange, field, readOnly } = props;
const label = field?.label ?? "Vendor";

if (connectEnabled === null) {
if (marketplaceEnabled === null) {
return <Loading label="Loading vendor options…" />;
}

if (!connectEnabled) {
if (!marketplaceEnabled) {
return (
<FormField
label={label}
description={field?.help}
{...(field?.required ? { required: true } : {})}
>
<Alert type="info" title="Multi-vendor marketplace is off">
Enable it in Settings → Stripe Connect to assign products to vendors.
Leave this field blank for now.
Enable it in Settings, or install a payment provider that supports split payments, to
assign products to vendors. Leave this field blank for now.
</Alert>
</FormField>
);
Expand Down
Loading