egorsmkv-packagist/yii2-perfect-money

Yii2的PerfectMoney组件

安装: 23

依赖项: 0

建议者: 0

安全: 0

星星: 0

观察者: 2

分支: 0

开放问题: 0

类型:yii2-extension

v1.0.0 2017-11-12 23:00 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:20:19 UTC


README

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

安装

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

运行

php composer.phar require --prefer-dist yii-dream-team/yii2-perfect-money "dev-master"

或添加

"yii-dream-team/yii2-perfect-money": "dev-master"

到您的composer.json文件的require部分。

使用方法

组件配置

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

'pm' => [
    'class' => '\yiidreamteam\perfectmoney\Api',
    'accountId' => '1234567',
    'accountPassword' => 'xxxxxxxxx',
    'walletNumber' => 'U1234567',
    'merchantName' => 'My Merchant',
    'alternateSecret' => 'X00O8cT08pOEZTJdFmSiAwxyu', 
    'resultUrl' => ['/perfect-money/result'],
    'successUrl' => ['/site/payment-success'],
    'failureUrl' => ['/site/payment-failure'],
],

重定向到支付系统

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

<?php echo \yiidreamteam\perfectmoney\RedirectForm::widget([
    'api' => Yii::$app->get('pm'),
    'invoiceId' => $invoice->id,
    'amount' => $invoice->amount,
    'description' => $invoice->description,
]); ?>

网关控制器

您需要创建一个控制器来处理来自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 yiidreamteam\perfectmoney\actions\ResultAction;
use yiidreamteam\perfectmoney\Api;
use yiidreamteam\perfectmoney\events\GatewayEvent;

class PerfectMoneyController extends Controller
{
    public $enableCsrfValidation = false;

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

    public function actions()
    {
        return [
            'result' => [
                'class' => ResultAction::className(),
                'componentName' => 'pm',
                'redirectUrl' => ['/site/index'],
            ],
        ];
    }

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

        if (!$invoice instanceof Invoice ||
            $invoice->status != Invoice::STATUS_NEW ||
            ArrayHelper::getValue($event->gatewayData, 'PAYMENT_AMOUNT') != $invoice->amount ||
            ArrayHelper::getValue($event->gatewayData, 'PAYEE_ACCOUNT') != \Yii::$app->get('pm')->walletNumber
        )
            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

链接