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; foreach ($params as $key => $value) { $html .= " \n"; } $html .= <<--> eot; return $html; } }