yarcode / yii2-adv-cash
为 Yii2 框架提供的 AdvCash 组件
1.0.2
2017-06-28 16:20 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2
This package is not auto-updated.
Last update: 2024-09-23 15:10:28 UTC
README
AdvCash 服务的支付网关和 API 客户端。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist yarcode/yii2-adv-cash
或者
"yarcode/yii2-adv-cash": "*"
将以下内容添加到您的 composer.json 文件中的 require
部分。
使用方法
组件配置
在应用程序的 components
部分配置 advCash
组件。
'advCash' => [ 'class' => '\yarcode\advcash\Merchant' 'accountEmail' => null, 'merchantName' => null, 'merchantPassword' => null, 'walletNumber' => null, 'sciCurrency' => \yarcode\advcash\Merchant::CURRENCY_USD, 'sciCheckSign' => true, 'sciDefaultPs' => null, 'successUrl' => null, 'failureUrl' => null, 'resultUrl' => null, 'successUrlMethod' => null, 'failureUrlMethod' => null, 'resultUrlMethod' => null ]
重定向到支付系统
要将用户重定向到 AdvCash 网站,您需要创建带有 RedirectForm 小部件的页面。用户将在页面加载后立即被重定向。
<?php echo \yarcode\advcash\RedirectForm::widget([ 'api' => Yii::$app->get('advCash'), 'invoiceId' => $invoice->id, 'amount' => $invoice->amount, 'description' => $invoice->description, ]); ?>
网关控制器
您需要创建一个控制器来处理来自 AdvCash 服务的请求结果。示例控制器代码
<?php namespace yarcode\advcash\controllers; use common\models\billing\Invoice; use yarcode\advcash\actions\ResultAction; use yarcode\advcash\events\GatewayEvent; use yarcode\advcash\Merchant; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; use yii\web\Controller; class GatewayController extends Controller { /** @inheritdoc */ public $enableCsrfValidation = false; /** @var string Your component configuration name */ public $componentName = 'advCash'; /** @var Merchant */ protected $component; /** * @inheritdoc */ public function init() { parent::init(); $this->component = \Yii::$app->get($this->componentName); $this->component->on(GatewayEvent::EVENT_PAYMENT_REQUEST, [$this, 'handlePaymentRequest']); $this->component->on(GatewayEvent::EVENT_PAYMENT_SUCCESS, [$this, 'handlePaymentSuccess']); } public function actions() { return [ 'result' => [ 'class' => ResultAction::className(), 'componentName' => $this->componentName, 'redirectUrl' => ['/billing'], ], 'success' => [ 'class' => ResultAction::className(), 'componentName' => $this->componentName, 'redirectUrl' => ['/billing'], 'silent' => true, ], 'failure' => [ 'class' => ResultAction::className(), 'componentName' => $this->componentName, 'redirectUrl' => ['/billing'], 'silent' => true, ] ]; } /** * @param GatewayEvent $event * @return bool */ public function handlePaymentRequest($event) { $invoice = Invoice::findOne(ArrayHelper::getValue($event->gatewayData, 'ac_order_id')); if (!$invoice instanceof Invoice || $invoice->status != Invoice::STATUS_NEW || ArrayHelper::getValue($event->gatewayData, 'ac_amount') != $invoice->amount || ArrayHelper::getValue($event->gatewayData, 'ac_transaction_status') != Merchant::TRANSACTION_STATUS_COMPLETED || ArrayHelper::getValue($event->gatewayData, 'ac_sci_name') != $this->component->merchantName ) { return; } $invoice->debugData = VarDumper::dumpAsString($event->gatewayData); $event->invoice = $invoice; $event->handled = true; } /** * @param GatewayEvent $event * @return bool */ public function handlePaymentSuccess($event) { /** @var Invoice $invoice */ $invoice = $event->invoice; // TODO: invoice processing goes here } }
许可
MIT