yii-dream-team / yii2-perfect-money
Yii2的PerfectMoney组件
1.0.2
2020-01-28 08:12 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2
This package is auto-updated.
Last update: 2024-08-28 18:01:29 UTC
README
为PerfectMoney服务提供支付网关和API客户端。
安装
安装此扩展的首选方法是通过composer。
运行以下命令
php composer.phar require --prefer-dist yii-dream-team/yii2-perfect-money "dev-master"
或者将以下内容添加到你的composer.json文件的require部分:
"yii-dream-team/yii2-perfect-money": "dev-master"
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