smladeoye/yii2-paystack

Yii 2 Paystack支付扩展(组件和小部件)

安装: 667

依赖项: 0

建议者: 0

安全: 0

星标: 4

关注者: 4

分支: 8

开放问题: 2

类型:yii2-extension

v1.0.0 2017-02-28 15:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:05:16 UTC


README

YII 2支付集成组件

Latest Stable Version Total Downloads

  • 安装[安装](https://github.com/smladeoye/yii2-paystack#installation)
  • 配置[配置](https://github.com/smladeoye/yii2-paystack#configuration)
  • 使用[使用示例](https://github.com/smladeoye/yii2-paystack#usage-example)
  • 处理Paystack操作的事件[事件处理](https://github.com/smladeoye/yii2-paystack#handling-events)
  • Paystack内联小部件[内联小部件](https://github.com/smladeoye/yii2-paystack#using-the-paystack-inline-payment-widget)

安装

安装此扩展的首选方式是通过composer。

运行

composer require smladeoye/yii2-paystack

或者

将 '"smladeoye/yii2-paystack": "1.0.0"' 添加到 composer.json 文件的 require 部分,然后运行

composer install

配置

在你的配置文件(web.php)中注册组件并配置必要的配置,例如

'components'=>[
    //  ...
    'Paystack' => [
        'class' => 'smladeoye\paystack\Paystack',
    	'environment' => 'test',
    	'testPublicKey'=>'pk_test_365589cc6b0e95f1fb53fa0eeaef6b1819f1b0f2',
    	'testSecretKey'=>'sk_test_1a4a88eaec6f4f3b23771edb2c60fe8d8b95cbe',
    	'livePublicKey'=>'',
    	'liveSecretKey'=>'',
    ],
    //  ...
]

使用示例

// Initializing a payment transaction

$paystack = Yii::$app->Paystack;

$transaction = $paystack->transaction();
$transaction->initialize(['email'=>'smladeoye@gmail.com','amount'=>'100000','currency'=>'NGN']);

// check if an error occured during the operation
if (!$transaction->hasError)
{
    //response property for response gotten for any operation
    $response = $transaction->getResponse()

    // redirect the user to the payment page gotten from the initialization
    $transaction->redirect();
}
else
{
    // display message
    echo $transaction->message;

    // get all the errors information regarding the operation from paystack
    $error = $transaction->getError();
}

有七个操作可以执行,这些操作基于Paystack自身的分组。每个操作都有自己的方法,可以调用以执行不同的操作(创建、列表 - 获取所有、获取、更新等),这些方法可以接受作为数组的所有必要参数。

以下是可以用的操作和方法(所有示例代码均基于上面的示例配置)

  1. customer:初始化任何客户操作
$paystack = Yii::$app->Paystack;
$customer = $paystack->customer();

客户可用的独特方法

  • whitelist --> 将特定客户列入白名单。示例
      $customer->whitelist($customer_id);
  • blacklist --> 将特定客户列入黑名单。示例
      $customer->blacklist($customer_id);
  1. transaction:初始化事务操作
$paystack = Yii::$app->Paystack;
$transaction = $paystack->transaction();

事务可用的独特方法

  • initialize --> 初始化事务;从该方法生成授权URL,然后可以调用重定向方法以重定向到支付页面。示例
$transaction->initialize(['email'=>'smladeoye@gmail.com','amount'=>'10000']);
if (!$transaction->hasError)
      $transaction->redirect();
  • verify --> 验证事务。示例
      $transaction->verify($trans_reference);
  • charge --> 为定期交易授权。示例
$transaction->charge($options = []);
  • timeline --> 特定事务的时间线。示例
$transaction->timeline($trx_id);
  • total --> 获取指定范围内的交易总额。示例
 $transaction->total($from,$to);
 //An array could be provided instead with the available parameters in key => value format.
  • export --> 导出一系列交易详情;从该方法生成一个URL,可以从中下载文件。要获取路径,请简单地调用路径方法或调用下载方法来下载文件。示例
   $transaction->export($options = []);

  //get download link url
  $transaction->getExportUrl();

或要下载文件,请调用

      $transaction->download();
  1. subscription:初始化订阅操作
$paystack = Yii::$app->Paystack;
$subscription = $paystack->subscription();

订阅可用的独特方法

  • enable --> 启用客户订阅。示例
    $subscription->enable($code, $token);
    //an array can be provided instead, containing the necessary parameters as key => value
  • disable --> 禁用客户订阅。示例
    $subscription->disable($code, $token);
    //an array can be provided instead, containing the necessary parameters as key => value
  1. subaccount:初始化子账户操作
$paystack = Yii::$app->Paystack;
$subaccount = $paystack->subaccount();

子账户可用的独特方法

  • listBank --> 列出系统上创建子账户可用的银行。示例
        $subaccount->enable($code, $token);
        //an array can be provided instead, containing the necessary parameters as key => value
  1. plan:初始化计划操作
$paystack = Yii::$app->Paystack;
$plan = $paystack->plan();

计划操作包含所有基本方法(创建、获取、获取所有、更新);

  1. page:初始化页面操作
$paystack = Yii::$app->Paystack;
$page = $paystack->page();

页面可用的独特方法

  • checkAvailability --> 检查特定短语的可用性。示例
        $page->checkAvailability($slud_id);
  1. settlement:初始化结算操作
$paystack = Yii::$app->Paystack;
$settlement = $paystack->settlement();

结算可用的独特方法

  • fetchAll --> 获取所有结算。示例
$settlement->fetchAll($from_date,$to_date);
//an array can be provided instead, containing the necessary parameters as key => value

以下方法也可用

  • fetchAll:fetchall(列表)方法适用于所有操作。示例
        $customer->fetchAll(['page'=>'','perPage'=>'']);
  • create:create方法适用于客户、订阅、子账户、页面和计划操作。示例
        $customer->create(['email'=>'smladeoye@gmail.com']);
  • fetch --> fetch方法适用于所有操作(除结算外)。示例
        $customer->fetch($customer_id);
  • update --> update方法适用于客户、子账户、页面和计划操作。示例
        $customer->update($id,$info = array();

处理事件

该组件还支持在每个请求前后处理事件。您可以在YII2事件[这里](https://yiiframework.cn/doc-2.0/guide-concept-events.html)了解更多。您可以通过配置属性beforeSend -(发送请求之前的事件)和afterSend -(发送请求之后的事件)来设置事件处理器。事件处理器可以设置为所有操作或特定操作;为特定操作设置的事件处理器将覆盖为所有操作设置的事件处理器。示例

'Paystack' => [
            'class' => 'smladeoye\paystack\Paystack',
            ...//other configurations just like example above

            //setting the event handler for all operations before any request is made
            'beforeSend'=>'myFunction',

            //will set the event handler for all operations after any request is made
            'afterSend'=>'myFunction',

            //setting the event handler for the transaction operation; this will overwrite the event handlers above
            'transaction'=>array(
                //handler for the event before any request is made for a transaction operation
                'beforeSend'=>'myFunction',

                //handler for the event after any request is made for a transaction operation
                'afterSend'=>'myFunction',
            )
        ]

使用Paystack内联支付小部件

要使用小部件,请从您的视图中调用小部件并设置小部件参数,例如

use smladeoye\paystack\widget\PaystackWidget;

    echo PaystackWidget::widget(
        [
        //set the text to be displayed on the button
            'buttonText'=>'PAY',
        //array to set other button attributes like id, class,style etc
            'buttonOptions'=>array(
                'class'=>'btn btn-danger',
                'style'=>'width: 80px;',
            ),
        //array to set all necessary paystack inline payment options
        //some values can be set dynamically by passing the element id as value (email,amount,currency,quantity)
            'options'=>[
                //your paystack public key
                'key'=>Yii::$app->Paystack->testPublicKey,
                'email'=>'smladeoye@ymail.com',
                'ref'=>'123456789',
                //'amount' => '#amount' --> the value is gotten from the html element with id = amount
                // OR
                'amount'=>'2000',
                //'currency' => '#currency' --> the value is gotten from the html element with id = currency
                // OR
                'currency' =>'NGN',
                'plan' =>'my-plan',
                //'quantity' => '#quantity' --> the value is gotten from the html element with id = quantity
                // OR
                'quantity' =>'2',
                //callbackUrl can be set, where the tansaction reference would be passed as GET parameter
                'callbackUrl' =>'www.google.com',
                //also u can override the default with the callback option, simply provide javascript anonymous function as a string
                //'callback'=>'function(response){alert(response.trxref);};',
            ],
        ]
    );