| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace common\pay\yilai;
- use backend\models\Config;
- use backend\models\Deposit;
- use backend\models\SyncDesposit;
- use common\helpers\Utils;
- use common\pay\BasePayHandler;
- use Yii;
- class PayHandler extends BasePayHandler
- {
- public $payUrl;
- public $merNo;
- public $secretKey;
- /**
- * @inheritdoc
- */
- public function init()
- {
- parent::init();
- if ($this->payUrl == null) {
- $this->payUrl = Yii::$app->params['yilai.payUrl'];
- }
- if ($this->merNo == null) {
- $this->merNo = Yii::$app->params['yilai.merNo'];
- }
- if ($this->secretKey == null) {
- $this->secretKey = Yii::$app->params['yilai.secretKey'];
- }
- }
- /**
- * @param array $deposit
- * @param array $params
- * @return string
- */
- public function outPay($deposit, $params = [])
- {
- $data = [];
- //提交的参数
- $data['mch_id'] = $this->merNo; //商户号码
- $data['order_num'] = $deposit['order_sn']; //订单号码
- $data['pay_amount'] = $deposit['rmb'] * 100; //订单金额
- $data['notify_url'] = $this->notifyUrl; //异步地址
- $data['return_url'] = $this->returnUrl; //同步地址,留空即可
- $data['ext'] = $deposit['order_sn']; //备注信息
- $data['pay_type'] = 'pay_wyzf'; //网银支付
- $data['bank_code'] = $params['bankCode']; //网银支付(额外参数:什么银行)
- $data['sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名
- return self::createGetHtml($data, $this->payUrl);
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleNotify($data)
- {
-
- if (PayUtils::checkSign($data, $this->secretKey)) {
-
- if ( isset($data['pay_status']) && $data['pay_status'] == "success" ) {
- $merOrderId = trim($data['order_num']);
- /** @var Deposit $model */
- $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
- if ($reuslt['type'] != 1) {
- $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
- $configData = Config::find()->asArray()->one();
- if ($configData['auto_deposit'] == 1 && $res) {
- $syncDespositModel = new SyncDesposit();
- $syncDespositModel->login = $reuslt['login'];
- $syncDespositModel->amount = $reuslt['amount'];
- $syncDespositModel->comment = 'Deposit';
- $syncDespositModel->memo = $merOrderId;
- $syncDespositModel->type = 2;
- $syncDespositModel->in_time = time();
- $syncDespositModel->save();
- }
- }
- return true;
- }
- }
- return false;
- }
- public function outNotify($success)
- {
- if ($success == true) {
- return "success";
- } else {
- return "FAIL";
- }
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleReturn($data)
- {
- if (PayUtils::checkSign($data, $this->secretKey)) {
- if (isset($data['pay_status']) && $data['pay_status'] == "success") {
- $merOrderId = trim($data['order_num']);
- /** @var Deposit $model */
- $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
- if ($model) {
- return true;
- }
- }
- }
- return false;
- }
- public function outReturn($success)
- {
- }
- public static function createGetHtml($params, $action)
- {
- $encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
- $html = <<<eot
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
- </head>
- <body onload="javascript:document.pay_form.submit();">
- <form id="pay_form" name="pay_form" action="{$action}" method="get">
- eot;
- foreach ($params as $key => $value) {
- $html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
- }
- $html .= <<<eot
- <!-- <input type="submit" type="hidden">-->
- </form>
- </body>
- </html>
- eot;
- return $html;
- }
- }
|