arsik/yii2-free-kassa

Yii2的FreeKassa组件

安装: 20

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 6

类型:yii2-extension

0.13.0 2017-03-11 08:33 UTC

This package is not auto-updated.

Last update: 2024-09-18 22:31:06 UTC


README

Free Kassa服务的支付网关和API客户端。

安装

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

运行

php composer.phar require --prefer-dist yarcode/yii2-free-kassa "~1.0"

或在你的composer.json文件中添加

"yarcode/yii2-free-kassa": "~1.0"

require部分。

使用方法

组件配置

在应用程序的components部分配置freeKassa组件。

'freeKassa' => [
    'class' => '\yarcode\freekassa\Merchant',
    'merchantId' => 'YOUR_MERCHANT_ID',
    'merchantFormSecret' => 'SECRET_1',
    'checkDataSecret' => 'SECRET_2',
    'defaultCurrency' => 'DEFAULT_CURRENCY_ID',
    'defaultLanguage' => 'ru' // or 'en' 
],

重定向到支付系统

要将用户重定向到PerfectMoney网站,你需要创建带有RedirectForm小部件的页面。用户将在页面加载后立即被重定向。

<?= \yarcode\freekassa\RedirectForm::widget([
    'message' => 'Redirecting to payment gateway...',
    'api' => Yii::$app->get('freeKassa'),
    'invoiceId' => $invoice->id,
    'amount' => $invoice->amount,
    'description' => $invoice->description,
    'email' => $invoice->owner->email
]); ?>

网关控制器

你需要创建一个控制器来处理来自PerfectMoney服务的请求结果。示例控制器代码

<?php
namespace frontend\controllers;

use common\models\Invoice;
use yii\base\Event;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
use yii\web\Controller;
use yarcode\freekassa\actions\ResultAction;
use yarcode\freekassa\events\GatewayEvent;
use yarcode\freekassa\Merchant;

class PerfectMoneyController extends Controller
{
    public $enableCsrfValidation = false;

    protected $componentName = 'freeKassa';

    public function init()
    {
        parent::init();
        /** @var Api $pm */
        $freeKassa = \Yii::$app->get($this->componentName);
        $freeKassa->on(GatewayEvent::EVENT_PAYMENT_REQUEST, [$this, 'handlePaymentRequest']);
        $freeKassa->on(GatewayEvent::EVENT_PAYMENT_SUCCESS, [$this, 'handlePaymentSuccess']);
    }

    public function actions()
    {
        return [
            'result' => [
                'class' => ResultAction::className(),
                'componentName' => $this->componentName,
                'redirectUrl' => ['/site/index'],
                'sendConfirmationResponse' => true
            ],
            'success' => [
                'class' => ResultAction::className(),
                'componentName' => $this->componentName,
                'redirectUrl' => ['/site/index'],
                'silent' => true,
                'sendConfirmationResponse' => false
            ],
            'failure' => [
                'class' => ResultAction::className(),
                'componentName' => $this->componentName,
                'redirectUrl' => ['/site/index'],
                'silent' => true,
                'sendConfirmationResponse' => false
            ]
       ];
    }

    /**
     * @param GatewayEvent $event
     * @return bool
     */
    public function handlePaymentRequest($event)
    {
        $invoice = Invoice::findOne(ArrayHelper::getValue($event->gatewayData, 'MERCHANT_ORDER_ID'));

        if (!$invoice instanceof Invoice ||
            $invoice->status != Invoice::STATUS_NEW ||
            ArrayHelper::getValue($event->gatewayData, 'AMOUNT') != $invoice->amount ||
            ArrayHelper::getValue($event->gatewayData, 'MERCHANT_ID') != \Yii::$app->get($this->componentName)->merchantId
        )
            return;

        $invoice->debugData = VarDumper::dumpAsString($event->gatewayData);
        $event->invoice = $invoice;
        $event->handled = true;
    }

    /**
     * @param GatewayEvent $event
     * @return bool
     */
    public function handlePaymentSuccess($event)
    {
        $invoice = $event->invoice;
        
        // TODO: invoice processing goes here 
    }
}

许可

MIT

链接