Test split payment logic at three levels: (1) unit test the commission calculation function in isolation, (2) mock the Paystack API in integration tests to verify the correct percentage_charge or transaction_charge is passed, and (3) end-to-end test using Paystack test mode — create real test subaccounts and verify the settlement breakdown in the Paystack test dashboard.
// commission.ts
export function calculateSplit(totalAmount: number, commissionRate: number) {
const vendorShare = Math.round(totalAmount * (1 - commissionRate) * 100); // kobo
const platformShare = Math.round(totalAmount * commissionRate * 100); // kobo
return { vendorShare, platformShare };
}
// commission.test.ts
describe('calculateSplit', () => {
it('splits 10% commission correctly', () => {
var result = calculateSplit(10000, 0.10);
expect(result.vendorShare).toBe(900000); // NGN 9,000 in kobo
expect(result.platformShare).toBe(100000); // NGN 1,000 in kobo
});
it('handles non-round amounts without floating point errors', () => {
var result = calculateSplit(999.99, 0.10);
// Should not produce fractional kobo
expect(result.vendorShare % 1).toBe(0);
expect(result.platformShare % 1).toBe(0);
});
it('returns full amount to vendor at 0% commission', () => {
var result = calculateSplit(5000, 0);
expect(result.vendorShare).toBe(500000);
expect(result.platformShare).toBe(0);
});
});
// Mock Paystack API in Jest
jest.mock('../lib/paystack', () => ({
initializeTransaction: jest.fn().mockResolvedValue({
status: true,
data: {
authorization_url: 'https://checkout.paystack.com/test',
access_code: 'test_access',
reference: 'test_ref_001',
}
}),
}));
describe('Checkout with vendor split', () => {
it('passes correct percentage_charge to Paystack', async () => {
var paystack = require('../lib/paystack');
var vendor = { subaccount_code: 'SUB_test123', commission_rate: 0.15 };
await checkoutService.initializeSplitPayment({
email: 'buyer@test.com',
amount: 10000,
vendor,
});
expect(paystack.initializeTransaction).toHaveBeenCalledWith(
expect.objectContaining({
subaccount: 'SUB_test123',
percentage_charge: 85, // 1 - 0.15 = 0.85 = 85%
bearer: 'account',
})
);
});
});
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
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