garung / paystack
帮助您进行Paystack API调用,返回stdClass对象。
Requires (Dev)
- guzzlehttp/guzzle: ^6.2
- phpunit/phpunit: ~5.3
- scrutinizer/ocular: ^1.1
- squizlabs/php_codesniffer: ^2.3
- vlucas/phpdotenv: ^2.2
This package is not auto-updated.
Last update: 2024-09-15 04:20:23 UTC
README
这是一个针对Paystack的PHP API包装器。
要求
- Curl 7.34.0或更高版本(除非使用Guzzle)
- PHP 5.4.0或更高版本
- OpenSSL v1.0.1或更高版本
安装
通过Composer
$ composer require garung/paystack
通过下载
从发布页面下载一个版本。解压后
require 'path/to/src/autoload.php';
重要
版本2与版本1的代码不兼容!如果cURL有问题或Paystack API在响应体中返回了错误状态,则会抛出错误。
用法
将重定向到从交易/初始化端点收到的授权URL。此URL仅限一次使用,因此请确保每个交易生成一个新URL。
当支付成功时,我们将调用您的回调URL(在您的仪表板中设置或在初始化交易时设置)并返回第一步中发送的引用作为查询参数。
如果您使用的是测试密钥,我们将调用您的测试回调URL,否则,我们将调用您的实时回调URL。
0. 前提条件
确认您的服务器可以与Paystack服务器建立TLSv1.2连接。大多数最新软件都有此功能。如果您有任何SSL错误,请联系您的服务提供商以获取指导。不要禁用SSL端点验证!
1. 准备您的参数
email
和amount
是最常见的必填参数。请为每个客户发送唯一的电子邮件。如果您的客户不提供唯一的电子邮件,请制定策略为每个客户设置一个。以下任何一种方式都可以。我们接受的所有端点的金额以科博为单位,必须为整数。例如,要接受456奈拉,78科博
,请发送45678
作为金额。
2. 初始化交易
通过调用我们的API初始化交易。
$paystack = new Yabacon\Paystack(SECRET_KEY); try { $tranx = $paystack->transaction->initialize([ 'amount'=>$amount, // in kobo 'email'=>$email, // unique to customers 'reference'=>$reference, // unique to transactions ]); } catch(\Yabacon\Paystack\Exception\ApiException $e){ print_r($e->getResponseObject()); die($e->getMessage()); } // store transaction reference so we can query in case user never comes back // perhaps due to network issue save_last_transaction_reference($tranx->data->reference); // redirect to page so User can pay header('Location: ' . $tranx->data->authorization_url);
当用户输入他们的卡详细信息时,Paystack将验证并扣款。它将执行以下所有操作
重定向回初始化交易时设置的回调URL或您的仪表板中的https://dashboard.paystack.co/#/settings/developer 。如果两者都没有设置,客户将看到“交易成功”的消息。
向您的Webhook URL发送charge.success事件,该URL设置为https://dashboard.paystack.co/#/settings/developer
如果未关闭收据,将向客户的电子邮件发送HTML收据。
在向客户支付之前,请通过我们的验证端点在服务器端调用以确认交易的状态和属性。
3. 处理charge.success事件
我们将向为您的交易域设置的Webhook URL发布charge.success事件。如果是实时交易,我们将向您的实时Webhook URL发布,反之亦然。
- 如果使用.htaccess,请记住将尾部/添加到您设置的URL中。
- 向您的URL进行测试POST,并确保脚本接收到了POST正文。
- 公开可用的URL(https://无法接收!)
// Retrieve the request's body and parse it as JSON $event = Yabacon\Paystack\Event::capture(); http_response_code(200); /* It is a important to log all events received. Add code * * here to log the signature and body to db or file */ openlog('MyPaystackEvents', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER | LOG_PERROR); syslog(LOG_INFO, $event->raw); closelog(); /* Verify that the signature matches one of your keys*/ $my_keys = [ 'live'=>'sk_live_blah', 'test'=>'sk_test_blah', ]; $owner = $event->discoverOwner($my_keys); if(!$owner){ // None of the keys matched the event's signature die(); } // Do something with $event->obj // Give value to your customer but don't give any output // Remember that this is a call from Paystack's servers and // Your customer is not seeing the response here at all switch($event->obj->event){ // charge.success case 'charge.success': if('success' === $event->obj->data->status){ // TIP: you may still verify the transaction // via an API call before giving value. } break; }
4. 验证交易
在重定向到您的回调URL后,请在给值之前验证交易。
$reference = isset($_GET['reference']) ? $_GET['reference'] : ''; if(!$reference){ die('No reference supplied'); } // initiate the Library's Paystack Object $paystack = new Yabacon\Paystack(SECRET_KEY); try { // verify using the library $tranx = $pasytack->transaction->verify([ 'reference'=>$reference, // unique to transactions ]); } catch(\Yabacon\Paystack\Exception\ApiException $e){ print_r($e->getResponseObject()); die($e->getMessage()); } if ('success' === $tranx->data->status) { // transaction was successful... // please check other things like whether you already gave value for this ref // if the email matches the customer who owns the product etc // Give value }
5. 结束语
通常,在构建Paystack对象后进行API请求,请按以下方式调用资源/方法:$paystack->{resource}->{method}()
;对于GET请求,使用$paystack->{resource}(id)
,要列出资源:使用$paystack->{resource}s()
。
目前,我们支持:'customer', 'page', 'plan', 'subscription', 'transaction' 和 'subaccount'。检查我们的API参考(link-paystack-api-reference)以获取支持的方法。要指定参数,请发送数组。
查看SAMPLES以获取更多示例调用
附加功能
有一些类可以帮助开发者完成在Paystack上经常需要做的某些任务
费用
此类与金额和您的Paystack费用一起工作。使用时,创建一个新的Fee对象
$fee = new Yabacon\Paystack\Fee();
配置它
$fee->withPercentage(0.015); // 1.5% $fee->withAdditionalCharge(10000); // plus 100 NGN $fee->withThreshold(250000); // when total is above 2,500 NGN $fee->withCap(200000); // capped at 2000
计算费用
$chargeOn300naira = $fee->calculateFor(30000); $chargeOn7000naira = $fee->calculateFor(700000);
当您想结算特定金额时,了解您需要发送到API的内容
$iWant100Naira = $fee->addFor(10000); $iWant4000Naira = $fee->addFor(400000);
事件
此类可帮助您捕获和验证我们的事件
$event = Yabacon\Paystack\Event::capture();
与密钥进行验证
if($event->validFor($mySecretKey)) { // awesome }
发现拥有它的密钥(如果同一webhook为多个集成接收)。这对于多租户系统很有用。或者,如果您想拥有相同的测试和实时webhook URL。
$my_keys = [ 'live'=>'sk_live_blah', 'test'=>'sk_test_blah', ]; $owner = $event->discoverOwner($my_keys); if(!$owner){ // None of my keys matched the event's signature die(); }
将事件转发到另一个URL。如果您想通知其他系统相同的精确事件。
$evt->forwardTo('http://another-webhook.url');
MetadataBuilder
此类可帮助您构建在发起交易请求时发送的有效JSON元数据字符串。
$builder = new MetadataBuilder();
添加元数据
通过调用withKeyName
魔术函数添加元数据(这些不会显示在仪表板上)。不要使用CustomFields
作为键名
$builder->withQuoteId(10); // will add as quote_id: 10 unless you turn auto_snake_case off $builder->withMobileNumber(08012345678); // will add as mobile_number: 08012345678 $builder->withCSRF('dontacceptpaymentunlessthismatches');
要在开始向$builder
添加元数据之前关闭键名的自动蛇形命名,请执行以下操作:
MetadataBuilder::$auto_snake_case = false;
。
添加自定义字段
通过调用withCustomField
函数添加自定义字段(这些将显示在仪表板上)。
$builder->withCustomField('Mobile Number', '080123456789'); $builder->withCustomField('See Me', 'I\'m Visible on your Dashboard');
构建JSON
最后调用build()
以获取您的JSON元数据字符串。
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
测试
$ composer test
贡献
有关详细信息,请参阅CONTRIBUTING和CONDUCT。检查我们的待办事项列表以获取已打算的功能。
安全
如果您发现任何与安全相关的问题,请通过电子邮件yabacon.valley@gmail.com联系,而不是使用问题跟踪器。
鸣谢
- Yabacon
- Issa Jubril
- Akachukwu Okafor
- Ibrahim Lawal
- Opeyemi Obembe - 按照他在创建NodeJS Wrapper时使用的风格
- 所有贡献者
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。