Run integration tests against the Paystack test API using your sk_test key. Flow: (1) call your checkout endpoint which calls POST /transaction/initialize, (2) simulate webhook delivery by generating a real charge.success event payload and POSTing it to your webhook handler with a valid HMAC signature, (3) verify the database reflects the correct final state. Use a local test database and clean up after each test.
// integration/payment-flow.test.js
// Uses real Paystack test API — requires PAYSTACK_SECRET_KEY=sk_test_...
const crypto = require('crypto');
const request = require('supertest');
const app = require('../app');
const db = require('../db');
describe('Paystack payment flow (integration)', () => {
let orderId;
beforeEach(async () => {
// Create a pending order in the test database
var order = await db.orders.create({
email: 'test@example.com',
amount: 10000, // NGN 100
status: 'pending',
});
orderId = order.id;
});
afterEach(async () => {
await db.orders.destroy({ where: { id: orderId } });
});
it('initializes transaction and returns authorization_url', async () => {
var res = await request(app).post('/api/checkout').send({
email: 'test@example.com',
amount: 10000,
order_id: orderId,
});
expect(res.status).toBe(200);
expect(res.body.authorization_url).toMatch(/checkout.paystack.com/);
});
it('fulfills order when valid charge.success webhook arrives', async () => {
var reference = 'test_ref_' + Date.now();
var payload = {
event: 'charge.success',
data: {
reference,
amount: 1000000, // NGN 10,000 in kobo
status: 'success',
metadata: { order_id: orderId },
customer: { email: 'test@example.com' },
},
};
var body = JSON.stringify(payload);
var sig = crypto
.createHmac('sha512', process.env.PAYSTACK_SECRET_KEY)
.update(body)
.digest('hex');
var res = await request(app)
.post('/webhook/paystack')
.set('x-paystack-signature', sig)
.set('Content-Type', 'application/json')
.send(body);
expect(res.status).toBe(200);
var updated = await db.orders.findByPk(orderId);
expect(updated.status).toBe('paid');
expect(updated.paystack_reference).toBe(reference);
});
});
This guide is part of the Paystack testing 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