coderatio / paystack-mirror
适用于 Paystack Api 的实用 PHP 库
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: 7.5.x-dev
This package is auto-updated.
Last update: 2024-09-04 19:02:46 UTC
README
缺失的 Paystack PHP 库。
概述
Paystack Mirror 是一个干净、简单、流畅的 PHP 库,用于 Paystack 支付网关。这个库的产生是因为我需要比现有的更灵活的东西,不仅为自己,也为整个 PHP 社区。
现有的有什么问题吗?
官方的 PHP Paystack 库是 GitHub 上的 yabacon/paystack-php。它很酷。支持几乎所有 Paystack 终端点,还有一些其他酷炫的功能,如专门的 Fee 类、MetaDataBuilder 类和事件处理器类。很多人都在使用这个库,但我希望有一种可以给人们更多自由的东西。我希望有一种可以同时支持多个账户的东西。这就是我决定开发这个库的原因。
这个库的独特之处是什么?
- 将 Paystack 终端点转换为完全可扩展的 PHP 类。
- 支持多个账户
- 干净且更好的事件处理器类
- 流畅的参数构建器类
- 尼日利亚货币转换,例如 1k => 1,000 奈拉和 1,000 奈拉 => 100,000 科博,甚至 (百万(xM),十亿(xB),万亿(xT)) 转换为科博等。
服务器要求
有意要求 PHP 版本 ^7.1.3。原因是它比 5.6 和 7.0 更安全、更好、更快。所以,如果您一直在使用低于该版本的 PHP,请在使用此库之前升级。
- php ^7.1.3
- cURL 扩展启用
- OpenSSL 扩展启用
此库支持所有 Paystack 终端点,我们称之为 操作。 让我们看看可用的操作。
操作组列表
以下是按字母顺序排列的库支持的操作组。
注意:要查看此库中每个操作组下所有操作的列表,请点击这里。
正在安排列出所有这些,就像上面的前两个一样。
安装
composer require coderatio/paystack-mirror
用法
使用单个 Paystack 账户
方法一
require 'venodr/autoload.php'; use Coderatio\PaystackMirror\PaystackMirror; use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions; $queryParams = new ParamsBuilder(); $queryParams->perPage = 10; $result = PaystackMirror::run($secretKey, new ListTransactions($queryParams)); echo $result->getResponse();
方法二
require 'venodr/autoload.php'; use Coderatio\PaystackMirror\PaystackMirror; use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions; $queryParams = new ParamsBuilder(); $queryParams->perPage = 10; $result = PaystackMirror::run($secretKey, ListTransactions::class, $queryParams); echo $result->getResponse();
方法三
require 'venodr/autoload.php'; use Coderatio\PaystackMirror\PaystackMirror; use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions; $queryParams = new ParamsBuilder(); $queryParams->perPage = 10; $result = PaystackMirror::setKey($secretKey)->mirror(new ListTransactions($queryParams)); echo $result->getResponse();
方法四
require 'venodr/autoload.php'; use Coderatio\PaystackMirror\PaystackMirror; use Coderatio\PaystackMirror\Actions\Transactions\ListTransactions; $queryParams = new ParamsBuilder(); $queryParams->perPage = 10; $result = PaystackMirror::setKey($secretKey)->mirror(ListTransactions::class, $queryParams); echo $result->getResponse();
注意:默认情况下,
->getResponse()
返回一个 JSON 对象。但,您可以在运行时链式调用->asArray()
将响应转换为 PHParray
或->asObject()
将响应转换为 PHPobject
。
使用多个 Paystack 账户
使用此库,您可以在多个 Paystack 账户上镜像单个操作。这对于多租户应用程序或拥有多个 Paystack 账户的公司来说非常酷。
让我们看看如何做到这一点。
// Let's use ParamsBuilder to build our data to be sent to paystack. // First account $firstAccountParams = new ParamsBuilder(); $firstAccountParams->first_name = 'Josiah'; $firstAccountParams->last_name = 'Yahaya'; $firstAccountParams->email = 'example1@email.com'; $firstAccountParams->amount = short_naira_to_kobo('25.5k'); $firstAccountParams->reference = PaystackMirror::generateReference(); $firstAccount = new ParamsBuilder(); $firstAccount->key = $firstAccountKey; $firstAccount->data = $firstAccountParams; // Second account $secondAccountParams = new ParamsBuilder(); $secondAccountParams->first_name = 'Ovye'; $secondAccountParams->last_name = 'Yahaya'; $secondAccountParams->email = 'example2@email.com'; $secondAccountParams->amount = short_naira_to_kobo('10k'); $secondAccountParams->reference = PaystackMirror::generateReference(); $secondAccount = new ParamsBuilder(); $secondAccount->key = $secondAccountKey; $secondAccount->data = $firstAccountParams; $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount]) ->mirrorMultipleAccountsOn(new InitializeTransaction()); // OR $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount]) ->mirrorMultipleAccountsOn(InitializeTransaction::class); foreach ($results as $result) { // Do something with $result. ... // The $result variable holds two main object properties; // $result->account which holds an account key and $result->response which holds the response for an account. // The best thing to do is to dump the $result variable to see what's contain there in. }
您可以通过在操作上提供您的参数来覆盖所有账户的数据。完成此操作后,库将使用操作上提供的参数为所有账户使用这些参数,例如:
$actionParams = new ParamsBuilder(); $actionParams->email = 'johndoe@email.com'; $actionParams->amount = naira_to_kobo('1,000'); $actionParams->reference = PaystackMirror::generateReference(); $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount]) ->mirrorMultipleAccountsOn(new InitializeTransaction($actionParams)); // OR $results = PaystackMirror::setAccounts([$firstAccount, $secondAccount]) ->mirrorMultipleAccountsOn(InitializeTransaction::class, $actionParams);
快速笔记:本库中包含了 paystack API 文档网站上使用的所有查询或正文参数。唯一的区别是,它们必须以数组形式或
ParamBuilder::class
对象的形式发送。
创建您的操作
这个库的一个优点是能够轻松插入和播放操作。您可以通过创建自己的操作来替换现有的操作。
<?php use \Coderatio\PaystackMirror\Actions\Action; use Coderatio\PaystackMirror\Services\CurlService; class MyCustomAction extends Action { // The paystack endpoint for this action protected $url = ''; public function handle(CurlService $curlService) : void { // Use the $curlService to handle this action's request. // E.g to send a post request, see below: $curlService->post($this->url, $this->getData()); } }
请注意,
$this->data
属性返回一个数组。如果您想将参数作为 JSON 发送到 paystack,请使用$this->getData()
。
Webhook 事件处理
本库提供了一个流畅的事件处理类,用于在您的 paystack 仪表板上的 webhook URL 中使用。以下是如何监听不同事件的示例。
<?php use Coderatio\PaystackMirror\Events\Event; // $secretKeys array structure should be like this: $secretKeys = [ 'test' => 'sk_testxxxxxxxxxxxxx', 'live' => 'sk_live_xxxxxxxxxxxxxx' ]; $eventData = Event::capture()->thenValidate(string $secretKey or array $secretKeys) ->thenListenOn('subscription.create')->thenGetData(); // Do something with the $eventData
创建单独的事件类
使用本库,您可以为一个单一事件编写一个单独的事件类。例如,可以像这样创建 subscription.create
事件
<?php namespace Coderatio\PaystackMirror\Events; class SubscriptionCreated implements ActionEvent { public static function validate($keys): Event { return Event::capture()->thenValidate($keys) ->thenListenOn('subscription.create'); } }
然后可以这样使用
<?php use Coderatio\PaystackMirror\Events\SubscriptionCreated; $eventData = SubscriptionCreated::validate($key)->thenGetData(); // Or $event = SubscriptionCreated::validate($key)->thenGetEvent();
在这里,我们可以看到我们只是在实现 ActionEvent::class
接口并在 ::validate()
方法上扩展了 Event::class
。
附加组件
本库提供了一些内置功能,可以更快更好地完成任务。让我们来看看它们
1. Nairas 到 kobo
由于 paystack 只接受 kobo 单位金额,应该有一个快速的方法来做。以下是一些辅助函数,可以帮助您完成。
<?php // Normal naira amount to kobo $amount = naira_to_kobo('1000'); // Returns: 100000 // Naira with commas $amount = naira_to_kobo('1,000'); // Returns: 100000 // Human readable nairas to kobo $amount = short_naira_to_kobo('2k'); // Returns: 200000 $amount = short_naira_to_kobo('1.5m'); // Returns: 150000000
注意:
short_naira_to_kobo()
辅助函数仅支持k
作为千位,m
作为百万位,b
作为十亿位和t
作为万亿位表示法。
2. 引用生成器
您可以通过这样做轻松地生成交易参考,特别是
<?php use \Coderatio\PaystackMirror\PaystackMirror; $reference = PaystackMirror::generateReference();
待办事项
- 构建专门的文档网站
测试
composer test
// OR
./vendor/bin/phpunit
贡献
纠正一个拼写错误对这个项目是一个巨大的贡献。请尽力做到这一点。您可以 fork 仓库并发送 pull request,或者通过以下方式轻松地联系我:twitter -> Josiah Ovye Yahaya。
协作者
许可
该项目使用 GPL.3.0
许可证构建和使用。