定制 emmo00/mock-paystack-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

emmo00/mock-paystack-laravel

Composer 安装命令:

composer require --dev emmo00/mock-paystack-laravel

包简介

PHP package for mocking Paystack responses and webhooks in your Laravel Test suite

README 文档

README

PHP package for mocking Paystack responses and webhooks.

Overview

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.

Installation

Prerequisites

  • PHP 8.2 or higher
  • Laravel 9.x or higher
  • Composer

Installation Steps

  1. Add the package to your Laravel project using Composer:

    composer require --dev emmo00/mock-paystack-laravel
  2. Use trait in test class

    use Emmo00\MockPaystack\MockPaystack;
    
    class TransactionControllerTest extends TestCase 
    {
         use MockPaystack;
    }

Features

  • 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 and Illuminate\Http\Client.

Example

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',
            ],
        ]);
    }
}

Usage

Trait: MockPaystack

This trait provides utility methods for handling HTTP requests, responses and webhook.

Initialize Payment Handler

methods to simulate and validate Paystack payment initialization requests.

Methods

fakeInitializePayment()

Description: Simulates an initialize payment request, ensuring required properties like email, amount, currency, and Authorization header are present.

Usage:

$this->fakeInitializePayment();
fakeInitializePaymentSuccess()

Description: Simulates a successful initialize payment request.

Usage:

$this->fakeInitializePaymentSuccess();
fakeInitializePaymentFailure()

Description: Simulates a failed initialize payment request.

Usage:

$this->fakeInitializePaymentFailure();
fakeInitializePaymentInvalidKey()

Description: Simulates a request with an invalid Authorization key.

Usage:

$this->fakeInitializePaymentInvalidKey();
fakeInitializePaymentInvalidRequest()

Description: Simulates a request with invalid payload or headers.

Usage:

$this->fakeInitializePaymentInvalidRequest();
fakeInitializePaymentInvalidCurrency()

Description: Simulates a request with an unsupported or missing currency field.

Usage:

$this->fakeInitializePaymentInvalidCurrency();
fakeInitializePaymentInvalidAmount()

Description: Simulates a request with an invalid or missing amount field.

Usage:

$this->fakeInitializePaymentInvalidAmount();
fakeInitializePaymentInvalidEmail()

Description: Simulates a request with an invalid or missing email field.

Usage:

$this->fakeInitializePaymentInvalidEmail();

Charge Success Webhook Handler

MockPaystack also provides methods to simulate Paystack webhook notifications, like for the charge.success event.

fakeWebHookChargeSuccess()

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' => 'test@example.com'],
    ['card' => 'visa'],
    false
);

$response->assertStatus(200);

Contributing

  1. Fork the repository.

  2. Create a new branch for your feature or bug fix:

    git checkout -b feature/my-new-feature
  3. Commit your changes:

    git commit -m "Add some feature"
  4. Push to the branch:

    git push origin feature/my-new-feature
  5. Open a pull request.

License

This package is open-source software licensed under the MIT license.

Contact

For questions or support, please reach out to Emmanuel Nwafor.

emmo00/mock-paystack-laravel 适用场景与选型建议

emmo00/mock-paystack-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「mock」 「laravel」 「paystack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 emmo00/mock-paystack-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 emmo00/mock-paystack-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 emmo00/mock-paystack-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 120
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-04