jtad009/cakephp-paystack

CakePHP 的 Paystack 插件

安装: 88

依赖项: 0

建议者: 0

安全: 0

星星: 2

关注者: 2

分支: 0

开放问题: 0

类型:cakephp-plugin

dev-master 2019-02-08 16:57 UTC

This package is auto-updated.

Last update: 2024-09-24 15:52:49 UTC


README

一个用于制作 Paystack 支付的 Cakephp 3.x 插件。需要 PHP 5.4+ 和 Composer。

要获取 Cakephp Paystack 的最新版本,只需要求它

<?php

    composer require jtad009/cakephp-paystack:dev-master
?>

或者将以下行添加到你的 composer.json 文件的 require 块中。

<?php

    "jtad009/cakephp-paystack": "dev-master"
?>

然后你需要运行 composer install 或 composer update 来下载它并更新自动加载器。

##通用支付流程

尽管有多种支付订单的方式,但大多数支付网关都希望在结账过程中遵循以下流程

###1. 客户被重定向到支付提供者网站 客户完成结账流程并准备支付后,必须将客户重定向到支付提供者网站。

通过提交包含一些隐藏字段的表单来实现重定向。该表单必须提交到支付提供者网站。隐藏字段至少指定必须支付的金额、订单 ID 和一个哈希值。

##使用方法 打开你的 config/path.php 文件,并添加你的公钥、私钥、商户电子邮件和支付 URL,如下所示

<?php 

    define("PaystackPublicKey",xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);
    define("PaystackSecretKey",xxxxxxxxxxxxxxxxxxxxxxxxxx);

?>

##使用此插件进行支付的简单示例 -- 假设一位客户想要支付短信费用

步骤 1:将以下代码包含到你的 AppController.php 中以加载 paystack 组件

   <?php 
        require_once(ROOT . DS . 'vendor' . DS . "jtad009" . DS . "cakephp-     paystack".DS."src".DS."Controller".DS."Component".DS."PayStackComponent.php");
        require_once(ROOT . DS . 'vendor' . DS . "jtad009" . DS . "cakephp-paystack".DS."src".DS."Controller".DS."Component".DS."CurlConnectionComponent.php");
        
        $this->loadComponent('PayStack.PayStack');
        $this->loadComponent('PayStack.CurlConnection');
    ?>

步骤 2:使用以下代码创建视图

<?php
    //Note This form will be setup as per you requirement. in my case i needed to pay for sms units
    echo $this->Form->create(null,['url'=>['controller'=>'as-per-requirement','action'=>'purchase-sms']]);
    echo $this->Form->input('amount',['templates'=>['inputContainer'=>'<div class="form-group">{{content}}<p class="  mb-3 mt-2"> <span  id="allocatedUnits" class="text-danger pull-right small ">0 </span><span class="small pull-right text-muted mr-2 ">UNIT(S) Worth: </span><span class="small text-muted mr-2"> Send to </span><span  id="reach" class="text-danger small ">0 </span></p></div>'],'class'=>'form-control','style'=>'resize:none','maxlength'=>"290",'options'=>['500'=>'500','1000'=>'1000','1500'=>'1500','2000'=>'2000','3000'=>'3000','5000'=>'5000','7000'=>'7000','10000'=>'10000'],'empty'=>'Select amount you want to pay','id'=>'sms-amount']);
   echo $this->Form->submit('PURCHASE UNITS',['class'=>'btn btn-sm btn-danger btn-block mt-2   ']);
   echo $this->Form->end();

?>

步骤 3:在你的控制器中创建一个操作,我的将是 PurchaseSMS()

<?php

    public function purchaseSMS(array $data){
        $postArray = array(
            'description'=>'SMS UNIT PURCHASE',
            'first_name'=>'EXPECTED_FIRST_NAME',//name of the person paying
            'email' => 'EXPECTED EMAIL', //email of the person paying
            'amount' => $data['amount'].'00',
            'callback_url'=>'https://skole.com.ng/phone-manager/success', //this points back to my website i choose to not use the callback on the dev dashboard of paystack you can choose otherwise
            'metadata.cancel_action'=>'https://skole.com.ng/phone-manager/error',
            "reference" => md5(uniqid()));

        $paystackResponse = $this->PayStack->payWithPaystack($postArray); //send payment details to the paystack API
        if($paystackResponse->status):
        //if status is true then get refereence code for confirmation "$paystackResponse->data->reference"
        
        //take us to the payment page to 
            $this->redirect($paystackResponse->data->authorization_url);
        endif;
    }

    //Authorization url will redirect you to this function 
    //$routes->connect('/success/', ['controller' => 'StudentsProfiles', 'action' => 'complete']);
    //I had set up a route to redirect to complete action when the callback_url above is lookedup by paystack
    
    public function complete(){
        
        $reference = isset($_GET['reference']) ? $_GET['reference'] : '';
        if(!$reference){
        die('No reference supplied');
        }else{
            $transactionResponse =  $this->PayStack->callback($reference);
               if(!$transactionResponse->status){
                // there was an error from the API
                $this->flash->error('API returned error: ' . $tranx->message);
              }

              if('success' == $transactionResponse->data->status){
                // transaction was successful...
                // print out reponse and use as required
               debug(transactionResponse);

              }
        }
        
    }
?>

##待办事项 添加对返回客户的收费 实现全面的测试 实现交易仪表板以查看 Cakephp 应用程序中的所有交易 列表/添加客户 创建/获取/更新支付计划 获取所有交易历史记录 管理订阅 将交易导出为 csv

##如何感谢你?为什么不给 GitHub 仓库加星?我很乐意得到关注!为什么不分享这个存储库的链接到 Twitter 或 HackerNews?传播信息!

别忘了在我的 LinkedIn 上关注我

谢谢!以色列·埃迪特。