sdelfi/yii2-free-kassa

Yii2 的 FreeKassa 组件

安装: 50

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 2

分支: 6

开放问题: 0

类型:yii2-extension

1.3.0 2022-05-23 17:09 UTC

This package is auto-updated.

Last update: 2024-09-23 22:03:45 UTC


README

只部分迁移到了新 API

Yii2 的 Free Kassa 组件

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

安装

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

运行以下命令之一:

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

或添加以下内容到您的 composer.json 文件的 require 部分。

"sdelfi/yii2-free-kassa": "~0.1"

使用方法

组件配置

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

'freeKassa' => [
    'class' => '\sdelfi\freekassa\Merchant',
    'merchantId' => 'YOUR_MERCHANT_ID',
    'merchantFormSecret' => 'SECRET_1',
    'checkDataSecret' => 'SECRET_2',
    'defaultCurrency' => 'RUB',
    'defaultPayMethod' => 'DEFAULT_PAYMETHOD_ID',
    'defaultLanguage' => 'ru' // or 'en' 
],

重定向到支付系统

要将用户重定向到 PerfectMoney 网站,您需要创建包含 RedirectForm 小部件的页面。页面加载后,用户将被重定向。

<?= \sdelfi\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 sdelfi\freekassa\actions\ResultAction;
use sdelfi\freekassa\events\GatewayEvent;
use sdelfi\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

链接