When a customer checks out with items from multiple vendors, create a Transaction Split group (POST /split) listing each vendor's subaccount and their flat share (item subtotal minus commission). Pass the resulting split_code when initializing the transaction. On charge.success, fulfill each vendor's order items. Paystack settles each vendor directly into their bank account.
// Cart: items from multiple vendors
// [{ vendor_id, items, subtotal }]
app.post('/api/checkout', async (req, res) => {
var { cart, email } = req.body;
var commissionRate = 0.10;
var total = cart.reduce((s, v) => s + v.subtotal, 0);
// Build subaccounts list for the split
var splitSubaccounts = await Promise.all(
cart.map(async (vendorCart) => {
var vendor = await db.vendors.findById(vendorCart.vendor_id);
var vendorShare = Math.round(vendorCart.subtotal * (1 - commissionRate) * 100); // kobo
return { subaccount: vendor.subaccount_code, share: vendorShare };
})
);
// Create Transaction Split
var splitRes = await fetch('https://api.paystack.co/split', {
method: 'POST',
headers: { Authorization: 'Bearer ' + process.env.PAYSTACK_SECRET_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Cart ' + Date.now(),
type: 'flat',
currency: 'NGN',
subaccounts: splitSubaccounts,
bearer_type: 'account',
}),
});
var splitData = await splitRes.json();
// Initialize transaction with split
var txRes = await fetch('https://api.paystack.co/transaction/initialize', {
method: 'POST',
headers: { Authorization: 'Bearer ' + process.env.PAYSTACK_SECRET_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({
email,
amount: total * 100,
split_code: splitData.data.split_code,
metadata: { cart: cart.map(v => ({ vendor_id: v.vendor_id, subtotal: v.subtotal })) },
}),
});
var txData = await txRes.json();
res.json({ authorization_url: txData.data.authorization_url });
});
This guide is part of the Paystack split payments guide.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course