In Laravel: use Http::fake() to intercept Paystack API calls before they hit the network. Return fake responses for the initialize and verify endpoints. In plain PHP: inject a Paystack client interface and use Mockery::mock() in tests. For webhook testing, generate a valid HMAC signature in your test and POST directly to your webhook controller.
// tests/Feature/PaymentTest.php
use IlluminateSupportFacadesHttp;
use TestsTestCase;
class PaymentTest extends TestCase
{
public function test_checkout_returns_authorization_url(): void
{
Http::fake([
'https://api.paystack.co/transaction/initialize' => Http::response([
'status' => true,
'data' => [
'authorization_url' => 'https://checkout.paystack.com/test',
'access_code' => 'test_access',
'reference' => 'ref_test_001',
],
], 200),
]);
$response = $this->postJson('/api/checkout', [
'email' => 'user@test.com',
'amount' => 10000,
'order_id' => 1,
]);
$response->assertStatus(200)
->assertJson(['authorization_url' => 'https://checkout.paystack.com/test']);
}
public function test_webhook_processes_charge_success(): void
{
$order = Order::factory()->create(['status' => 'pending']);
$secret = config('services.paystack.secret_key');
$payload = json_encode([
'event' => 'charge.success',
'data' => [
'reference' => 'ref_test_001',
'amount' => 1000000,
'status' => 'success',
'metadata' => ['order_id' => $order->id],
],
]);
$signature = hash_hmac('sha512', $payload, $secret);
$response = $this->withHeaders([
'x-paystack-signature' => $signature,
])->postJson('/webhook/paystack', json_decode($payload, true));
$response->assertStatus(200);
$this->assertDatabaseHas('orders', [
'id' => $order->id,
'status' => 'paid',
]);
}
}
// Plain PHP test with Mockery
use PHPUnitFrameworkTestCase;
use Mockery;
class PaymentServiceTest extends TestCase
{
public function test_returns_checkout_url_on_success(): void
{
$paystackMock = Mockery::mock(PaystackClient::class);
$paystackMock->shouldReceive('initializeTransaction')
->once()
->with(Mockery::on(fn($args) => $args['email'] === 'user@test.com'))
->andReturn([
'status' => true,
'data' => ['authorization_url' => 'https://checkout.paystack.com/test'],
]);
$service = new PaymentService($paystackMock);
$result = $service->createCheckout('user@test.com', 10000);
$this->assertEquals('https://checkout.paystack.com/test', $result['url']);
}
protected function tearDown(): void
{
Mockery::close();
}
}
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 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