amirasaran/zarinpal

为 Yii2 的在线 Zarinpal 支付扩展

安装次数: 1,897

依赖项: 0

建议者: 0

安全: 0

星星: 13

关注者: 2

分支: 3

开放问题: 0

类型:yii2-extension

2.0.2 2019-01-09 06:40 UTC

This package is auto-updated.

Last update: 2024-09-09 18:38:51 UTC


README

为 Yii2 的在线 Zarinpal 支付扩展

安装

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

运行以下命令之一:

php composer.phar require amirasaran/zarinpal:"*"

或者在您的 composer.json 文件的 require 部分添加

"amirasaran/zarinpal": "*"

到您的 composer.json 文件。

如何配置支付组件

将以下代码添加到您的 common/config/main.php 文件中的 components

    'components' => [
         ....
        'zarinpal' => [
            'class' => 'amirasaran\zarinpal\Zarinpal',
            'merchant_id' => 'XXXXXXX-XXX-XXXX-XXXXXXXXXXXX',
            'callback_url' => 'http://site.com/payment/verify',
            'testing' => true, // if you are testing zarinpal set it true, else set to false
        ],
        .... 
    ]
        

如何使用此组件

例如,假设您有一个名为 PaymentController 的控制器,首先您需要 2 个动作,一个是请求支付,另一个是验证支付。

您需要一个存储来保存您的支付和支付状态。

PaymentController.php

..... 

public function actionRequest()
{
    /** @var Zarinpal $zarinpal */
    $zarinpal = Yii::$app->zarinpal ;
    /*
    * if you whant, you can pass $callbackParams as array to request method for additional params send to your callback url
    */
    if($zarinpal->request(100,'Test Payment description',null,null,['parameter'=>'value','parameter2'=>'value2'])->getStatus() == '100'){
        /*
        * You can save your payment request data to the database in here before rediract user
        * to get authority code you can use $zarinpal->getAuthority()
        */
        return $this->redirect($zarinpal->getRedirectUrl());
    }
    echo "Error !";
}

/*
* $parameter and $parameter2 are optional parameter that set in request method $callbackParams
*/
public function actionVerify($Authority, $Status , $parameter , $parameter2){

    if($Status != "OK")
        return ; //Payment canceled by user 

    /** @var Zarinpal $zarinpal */
    $zarinpal = Yii::$app->zarinpal ;
    
    if($zarinpal->verify($Authority, 100)->getStatus() == '100'){
        //User payment successfully verified!
        echo "payment successfully";
    }
    elseif($zarinpal->getStatus() == '101') {
        //User payment successfuly verified but user try to verified more than one 
        echo  "duplicated verify payment";
    } 
    else
        echo "payment error !";
}

.....