| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace common\pay\trustpay;
- use backend\models\Config;
- use backend\models\Deposit;
- use backend\models\SyncDesposit;
- use common\pay\BasePayHandler;
- use Yii;
- use yii\helpers\VarDumper;
- class PayHandler extends BasePayHandler
- {
- public $payUrl; //网关地址
- public $merId; //商户名称
- public $privateKey; //商户私钥
- public $Key; //商户密钥
- /**
- * @inheritdoc
- */
- public function init()
- {
- parent::init();
- if ($this->payUrl == null) {
- $this->payUrl = Yii::$app->params['trustpay.payUrl']; //网关地址
- }
- if ($this->merId == null) {
- $this->merId = Yii::$app->params['trustpay.merId']; //商户号码
- }
- if ($this->privateKey == null) {
- $this->privateKey = __DIR__ . '/prem/trustpay_private.pem'; //私钥
- }
- if ($this->Key == null) {
- $this->Key = __DIR__ . '/prem/trustpay_public.pem'; //公钥
- }
- }
- /**
- * @param array $deposit
- * @param array $params
- * @return string
- */
- public function outPay($deposit, $params = [])
- {
- $data = []; //存储数据的容器
- $data['MerchantID'] = $this->merId; //商户号
- $data['OrderNo'] = $deposit['order_sn']; //订单号
- $data['ProductName'] = $deposit['order_sn']; //商品名称
- $data['Amount'] = sprintf("%.2f",$deposit['amount']); //金额(数量和美元是挂钩的)
- $data['CallBackUrl'] = $this->notifyUrl; //回调地址(异步回调)
- $data['Sign'] = PayUtils::makeSign($data, $this->privateKey); //签名 (数据,公钥)
-
- //sign最终请求参数
- $data['Sign'] = UrlEncode($data['Sign']); //最终请求参数sign需要进行UrlEncode
- file_put_contents('tp_sign_after.txt',$data['Sign']);
- $result = static::createGetHtml($data, $this->payUrl); //发起get请求
- return $result;
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleNotify($data)
- {
- $tt = print_r($data,true);
- file_put_contents("trustpaydata.txt",$tt); //记录返回的数据
- if (PayUtils::verify($data, $this->Key)) { //验签函数通过
- file_put_contents('truspay.txt','验签成功');
- if($data['Status']==1){ //判断交易的状态(暂时文档并没有说明是什么)
- $merOrderId = $data['OrderNo']; //订单号码
- $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;
- }
- }else{
- return false;
- }
-
- }else{
- return false;
- }
- }
- public function outNotify($success)
- {
- if ($success == true) {
- return "true";
- } else {
- // 返回的数据格式
- return "false";
- }
- }
- /**
- * @param array $data
- * @return bool
- */
- public function handleReturn($data)
- {
- if (PayUtils::verify($data,$this->Key)) {
- if($data['Status']==1){ //判断交易的状态(暂时文档并没有说明是什么)
- $merOrderId = $data['OrderNo']; //订单号码
- $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
- if ($reuslt) {
- return true;
- }
- }else{
- return false;
- }
-
- }else{
- return false;
- }
- }
- public function outReturn($success)
- {
- }
- // 发起get请求
- 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;
- }
- }
|