McTaba Labs logo
Software EngineeringThe WhiteboardProjectsEventsMasterclassesWebinarsLearn
Book Call

500+ developers already in the community

Get answers, find accountability partners, and stay ahead

Join Discord →
McTaba Labs logo

Educating 1 million students in tech by 2030.

Programs

  • HighSchool Graduates Launchpad
  • AI & Business Automation
  • Corporate Training
  • McTaba Story & Mission
  • Full-Stack Curriculum
  • Course Pricing & Plans
  • Frequently Asked Questions

Resources

  • Join Discord Community
  • Tech Events in Nairobi
  • Masterclasses
  • Free Resources
  • Learn
  • Campus
  • Africa Tech Talent Report
  • Partner With Us
  • Start a Project
  • Our Work

Contact Us

  • 124 Manyani East Road, Lavington, Nairobi
  • +254727795603
  • Book a Discovery Call

Office Hours:

Mon-Fri: 9am - 2pm EAT

Sat: 10am - 12pm EAT

© 2027 McTaba Labs - A product of Edtech Industries. All rights reserved.

Privacy PolicyTerms of ServiceRefund Policy
  1. Learn
  2. Paystack for African Developers
  3. Integration Testing Paystack Flows End to End
Bonaventure OgetoBy Bonaventure Ogeto|Updated July 22, 2026

Integration Testing Paystack Flows End to End

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 Test: Full Payment Flow

// 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);
  });
});

Learn More

This guide is part of the Paystack testing guide.

Sign up for the McTaba newsletter

Key Takeaways

  • ✓Integration tests use the real Paystack test API — set PAYSTACK_SECRET_KEY=sk_test_... in your test environment.
  • ✓Test the initialize → webhook → fulfillment flow: call initialize, then simulate the webhook to complete the flow.
  • ✓Use a test database (separate from development) and clean it between tests using transactions or truncation.
  • ✓Webhook simulation in integration tests: construct the event payload and POST it to your handler with a valid HMAC signature.
  • ✓Integration tests are slower than unit tests — run them in CI but not on every file save in development.
  • ✓Test unhappy paths: webhook arriving before order exists, duplicate webhook for same reference, amount mismatch.

Frequently Asked Questions

Should integration tests call the real Paystack test API or use mocks?
Integration tests should call the real Paystack test API — that is the point of integration tests. Mocks belong in unit tests. The Paystack test API is free to use, fast, and does not move real money. If you mock the Paystack API in integration tests, you are just writing slower unit tests.
How do I prevent integration tests from running in production?
Set a NODE_ENV=test environment variable and verify it is set before tests run. Also verify PAYSTACK_SECRET_KEY starts with "sk_test_" — fail fast if a live key is detected. In your CI config, inject the test key explicitly so production never uses it for tests.
Can integration tests run in a GitHub Actions CI pipeline?
Yes. Add your PAYSTACK_TEST_KEY as a GitHub Actions secret, set it as an environment variable in the workflow YAML, and run your integration test suite. Paystack test API is accessible from CI environments — no VPN or special network setup needed.
How do I clean up test data created during integration tests?
Use database transactions that are rolled back after each test (most test frameworks support this). Alternatively, create test-specific records with a known test prefix in your reference or metadata, and delete records with that prefix in afterAll. Avoid sharing test data between tests — isolated state prevents flaky failures.

Learn Paystack Integration

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

Not ready? Create Free Account

Keep reading

Testing Paystack Integrations: The Complete Guide

A complete guide to testing Paystack integrations at every level: test mode, test cards, unit tests with mocks, integration tests, end-to-end flows with Playwright, webhook simulation, contract testing, CI pipelines, staging environments, load testing, and coverage targets for payment code.

Read more

Simulating Declined Cards on Paystack

A card decline is one of the most common failure states in a payment flow, and your application must handle it gracefully. This guide shows how to simulate specific card declines in Paystack test mode and how to verify your decline handling code works correctly.

Read more

Mocking the Paystack API in PHPUnit

PHPUnit tests for Paystack integrations in Laravel or PHP can run without any network calls using Laravel's Http::fake() or Mockery. This guide shows how to mock Paystack responses, test webhook handlers, and verify your payment logic in isolation.

Read more

Seeding Test Data for a Payments Feature

Good test data is essential for testing payment features. This guide covers creating Paystack test mode resources (plans, subaccounts, split codes) and seeding your application database with matching payment records for realistic test scenarios.

Read more

Paystack Invalid Amount Error and Kobo Confusion

Paystack rejects amounts that are not positive integers in the smallest currency unit. This guide covers every way the amount field goes wrong: decimals, strings, Naira-vs-kobo confusion, and floating-point precision traps. Includes a safe conversion function you can drop into any codebase.

Read more

Explore related topics

M-Pesa & African Stack

M-Pesa Daraja API tutorials, USSD development, and WhatsApp Business API guides. The African Stack that global bootcamps do not teach.

Learn to Code in Kenya

Everything a Kenyan needs to know before writing their first line of code. Career paths, real costs, hardware requirements, and honest answers about what it takes.

Learning to Code

Roadmaps, language guides, and practical advice for learning to code and launching a programming career.

Breaking Into Tech

Practical, no-nonsense guides for anyone looking to start or change their career in tech. From choosing your first language to acing interviews.

The African Stack: Skills for This Market

The skills that make Kenyan developers hard to replace: M-Pesa, USSD, WhatsApp, fintech, and building for the African market.

Bonaventure Ogeto

Bonaventure Ogeto

Founder, McTaba Labs

Bonaventure is the founder of McTaba Labs, a coding bootcamp in Nairobi, Kenya. He has spent years building products for the African market and mentoring the next generation of developers across the continent.