PHP package for mocking Paystack responses and webhooks.
MockPaystack is a Laravel package designed to simplify testing and development involving Paystack integrations. The MockPaystack package is a tool designed for developers to test and simulate interactions with Paystack APIs in a controlled environment. It provides utility classes, traits, and constants to handle various Paystack API operations, including webhooks, payment initialization, and response simulation.
This package includes utilities for handling HTTP requests and responses, and it offers support for both Illuminate\Http\Client
(Laravel HTTP client) and GuzzleHttp
clients, ensuring compatibility with a wide range of use cases.
- PHP 8.2 or higher
- Laravel 9.x or higher
- Composer
-
Add the package to your Laravel project using Composer:
composer require --dev emmo00/mock-paystack-laravel
-
Use trait in test class
use Emmo00\MockPaystack\MockPaystack; class TransactionControllerTest extends TestCase { use MockPaystack; }
- Simulate Paystack API responses
- Mock webhooks and payment interactions, such as successful payments, failed transactions, and more.
- Validate request payloads and headers
- Utilities to work seamlessly with
GuzzleHttp
andIlluminate\Http\Client
.
Here's a basic example of using the MockPaystack package:
use Emmo00\MockPaystack\MockPaystack;
class ExampleTest extends TestCase
{
use MockPaystack;
public function testInitializePayment()
{
$this->fakeInitializePayment(); // this catches any request made to the paystack api and returns a mock response
// make request to endpoint that calls paystack
$response = $this->get(route('subscriptions.pay'));
// Assert
$response->assertStatus(200);
$response->assertJsonStructure([
'message',
'data' => [
'authorization_url',
],
]);
}
}
This trait provides utility methods for handling HTTP requests, responses and webhook.
methods to simulate and validate Paystack payment initialization requests.
Description: Simulates an initialize payment request, ensuring required properties like email
, amount
, currency
, and Authorization
header are present.
Usage:
$this->fakeInitializePayment();
Description: Simulates a successful initialize payment request.
Usage:
$this->fakeInitializePaymentSuccess();
Description: Simulates a failed initialize payment request.
Usage:
$this->fakeInitializePaymentFailure();
Description: Simulates a request with an invalid Authorization key.
Usage:
$this->fakeInitializePaymentInvalidKey();
Description: Simulates a request with invalid payload or headers.
Usage:
$this->fakeInitializePaymentInvalidRequest();
Description: Simulates a request with an unsupported or missing currency field.
Usage:
$this->fakeInitializePaymentInvalidCurrency();
Description: Simulates a request with an invalid or missing amount field.
Usage:
$this->fakeInitializePaymentInvalidAmount();
Description: Simulates a request with an invalid or missing email field.
Usage:
$this->fakeInitializePaymentInvalidEmail();
MockPaystack
also provides methods to simulate Paystack webhook notifications, like for the charge.success
event.
Description: Sends a fake webhook charge.success
notification to your Paystack webhook handler route.
Parameters:
string $route
: The webhook handler route.string $secret_key
: The secret key for webhook validation.array $metadata
: Additional metadata for the webhook.int $amount
: Transaction amount (default: 10000).string $currency
: Transaction currency (default: 'NGN').string $reference
: Transaction reference (default: 'reference').string $channel
: Payment channel (default: 'card').string $ip_address
: IP address of the transaction source (default: '0.0.0.0').array $customer
: Customer details.array $authorization
: Authorization details.bool $reusable_authorization
: Indicates if the authorization is reusable (default: true).
Returns:
\Illuminate\Testing\TestResponse
: The response from your webhook handler route.
Usage:
$response = $this->fakeWebHookChargeSuccess(
'/webhook-route',
'your-secret-key',
['custom-key' => 'custom-value'],
5000,
'USD',
'unique-reference',
'bank',
'192.168.1.1',
['email' => '[email protected]'],
['card' => 'visa'],
false
);
$response->assertStatus(200);
-
Fork the repository.
-
Create a new branch for your feature or bug fix:
git checkout -b feature/my-new-feature
-
Commit your changes:
git commit -m "Add some feature"
-
Push to the branch:
git push origin feature/my-new-feature
-
Open a pull request.
This package is open-source software licensed under the MIT license.
For questions or support, please reach out to Emmanuel Nwafor.