yarcode/yii2-free-kassa

Yii2 的 FreeKassa 组件

安装: 409

依赖: 0

建议者: 0

安全: 0

星星: 5

观察者: 3

分支: 6

开放问题: 2

类型:yii2-extension

0.13.1 2018-11-13 07:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 08:55:51 UTC


README

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

安装

通过以下方式安装此扩展程序是首选方法:composer

运行以下命令:

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

或者将以下内容添加到你的 composer.json 文件的 require 部分:

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

to the require section of your composer.json.

用法

组件配置

在你的应用程序的 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

链接