matscode/paystack

此包用于与 PAYSTACK API 通信

v1.10.1 2024-07-25 10:43 UTC

This package is auto-updated.

Last update: 2024-09-25 11:11:42 UTC


README

此仓库的开发现已停止。从 v2.0.0 版本开始,开发已迁移到 matscode/paystack-php-sdk,具有新的改进结构、文档、示例和整体开发体验。

待办事项 - 路线图

  • 使用 GuzzleHttp
  • 客户
  • 计划
  • 订阅
  • 转账
  • 收费
  • 其他...
此包用于与 PAYSTACK RESTful API 通信。 Paystack

在 PAYSTACK API 上还有其他资源点可用,例如;

  • 交易
  • 客户
  • 计划
  • 订阅
  • 转账
  • 收费
  • 等等

仅举几例,目前此包中仅提供交易资源。开发正在进行中,发布稳定。如果您发现BUG/安全问题,请 kindly 打开一个问题或通过电子邮件 matscode at Gmail dot Com
提示:如果您使用 paystack inline,此库也适用于您。您只需使用 verify() 方法,传递您的交易 $reference|$token|$id 即可,这是强制性的。

要求

  • Curl

安装

通过 Composer

$ composer require matscode/paystack

如果您使用框架,请检查您的文档以了解如何自动加载供应商包,否则请将以下内容添加到您的源文件顶部;

require_once __DIR__ . "/vendor/autoload.php";

进行交易/接收付款

启动 Paystack 交易

use Matscode\Paystack\Transaction;
use Matscode\Paystack\Utility\Debug; // for Debugging purpose
use Matscode\Paystack\Utility\Http;

$secretKey = 'sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// creating the transaction object
$Transaction = new Transaction( $secretKey );

初始化交易

使用初始化请求将数据/有效载荷/请求数据设置为 post。所需的最小数据是电子邮件和金额。

// Set data to post using array
$data = 
[
    'email'  => 'customer@email.com',
    'amount' => 500000 // amount is treated in kobo using this method
];
$response = $Transaction->initialize($data);

或者

// Set data to post using this method
$response =
        $Transaction
            ->setCallbackUrl('http://michaelakanji.com') // to override/set callback_url, it can also be set on your dashboard 
            ->setEmail( 'matscode@gmail.com' )
            ->setAmount( 75000 ) // amount is treated in Naira while using this method
            ->setMetadata(['custom_field1' => 'value1', 'custom_field2' => 'value2'])
            ->initialize();

如果您想获取由 Paystack 发送的原始 200OK 对象,请将 initialize() 方法的第二个参数设置为 true,以下为示例

// Set data to post using this method
$response =
        $Transaction 
            ->setEmail( 'matscode@gmail.com' )
            ->setAmount( 75000 ) // amount is treated in Naira while using this method
            ->initialize([], true);

现在将重定向到付款页面(使用 authorization_url)
注意:建议您调试 $response 或检查是否设置了 authorizationUrl,并保存您的交易参考代码。这对于验证交易状态非常有用。

// recommend to save Transaction reference in database and do a redirect
$reference = $response->reference;
// redirect
Http::redirect($response->authorizationUrl); 

如果您使用框架,建议您使用框架提供的反向路由/重定向功能

验证交易

这部分将位于您的回调文件中,例如 callback.phpwhatsoever_you_name.php
您必须再次创建交易对象。
此方法将返回交易对象,但如果未传递作为参数的 $reference 已保存且无法猜测,则返回 false。使用 verify() 需要您手动检查响应对象。

$response = $Transaction->verify();
// Debuging the $response
Debug::print_r( $response);

或者

// This method does the check for you and return `(bool) true|false` 
$response = $Transaction->isSuccessful();

上述两种方法尝试猜测您的交易 $reference,但强烈建议您将交易 $reference 作为参数传递给该方法,如下所示

// This method does the check for you and return `(bool) true|false`
$response = $Transaction->isSuccessful($reference);

此外,您还可以比较客户支付的实际金额是否与预期的金额相同。此方法仅在调用 verify()isSuccessful() 的同一脚本后生效。如果您使用 paystack inline 初始化交易,则建议这样做。

$amountExpected = 5000; // amount must be in kobo
// returns `(bool) true|false`
$Transaction->amountEquals($amountExpected);

现在您可以处理客户价值。

您可能想要保存当前客户的交易 $authorizationCode 以供后续交易,但这不是必需的。它只对未来此包的更新或如果您选择扩展包才有意义。

// returns Auth_xxxxxxx 
$response = $Transaction->authorizationCode($reference); // can also guess Transaction $reference

雇佣我

如果您需要将 paystack 集成到您的业务网站或类似的东西,我可以提供合同服务。
通过上面的电子邮件联系我 - michaelakanji.com

贡献

如果您似乎理解了架构,欢迎您进行分支和拉取,否则您可以稍等片刻,直到我提供规范文档。

许可证

GNU GPLV3