Browse Source

对接globalpay支付

jace 6 năm trước cách đây
mục cha
commit
bc2a320cb3

+ 2 - 0
normandcrm/crm/backend/models/forms/PayForm.php

@@ -32,6 +32,7 @@ class PayForm extends Model
         14 =>'JD通道A',
         16 =>'对私支付',
         17 =>'对公支付',
+        18 =>'globalpay',
     ]; 
     public static $payMethods = [
         'renren' => 1,
@@ -49,6 +50,7 @@ class PayForm extends Model
         'jiedeng'=>14,
         'duisi'=>16,
         'duigong'=>17,
+        'globalpay'=>18,
 		
     ];
 

+ 4 - 0
normandcrm/crm/backend/modules/user/controllers/PayController.php

@@ -55,6 +55,10 @@ class PayController extends BaseController
                 $data['amount'] = sprintf("%.2f",$data['amount']) + $amout_back/100;
             }
 
+            if ($data['payType'] == 18) {
+                $data['bankCode'] = isset($data['globalpayBankCode']) ? trim($data['globalpayBankCode']) : '';
+            }
+
 			//echo "<pre>";
 			//var_dump($data);die;
 

+ 34 - 0
normandcrm/crm/backend/modules/user/views/bankdeposit/index.php

@@ -228,6 +228,27 @@ $this->title = '入金';
                         </div>
 
 
+                        <div id="globalpay_bank_code" style="display: none">
+                            <br>
+                            <div class="row">
+                                <div class="col-sm-1"></div>
+                                <div class="col-sm-4">
+                                    <div class="input-group">
+                                       <span class="input-group-btn">
+                                           <button type="button" class="btn btn-primary"><i class="fa fa-dollar"></i>支付方式</button>
+                                       </span>
+                                        <select id="globalpayBankCode" name="globalpayBankCode" class="form-control">
+                                            <option value="CNY_PAY">人民币支付</option>
+                                        </select>
+                                    </div>
+                                </div>
+                                <div class="col-sm-4" style="text-align:center;">
+                                   <!-- <span style="line-height: 2.5;color:red">此通道单笔转账金额必须大于7500美元</span> -->
+                                </div>
+                                <div class="col-sm-1"></div>
+                            </div>
+                        </div>
+
 
                         <div id="duisi_bank_code" style="display: none">
                             <br>
@@ -430,6 +451,19 @@ $this->title = '入金';
 			$('.well').text('友情提示:银联入金,1个工作日到账。');
         }
 
+        //globalpay支付
+        if (payMethod == 'globalpay') {
+            $("#globalpay_bank_code").attr("style", "display:block");
+            $('#RMBcode').text('人民币');
+            $('.guojiyinlian').css('display','none');
+			$('.well').text('友情提示: 1个工作日到账。');
+        } else {
+            $("#globalpay_bank_code").attr("style", "display:none");
+            $('#RMBcode').text('人民币');
+			$('.well').text('友情提示:银联入金,1个工作日到账。');
+        }
+        
+
         //对私支付
         if (payMethod == 'duisi') {
             $("#duisi_bank_code").attr("style", "display:block");

+ 6 - 1
normandcrm/service/common/config/params.php

@@ -44,7 +44,12 @@ return [
     "eagle.payUrl" =>'https://api.eaglepay.pro/',                                  //网关地址
     'eagle.merId' => '2',                                                         //商户号码
     'eagle.Key' => 'A7Zs2LCSP1AepR5pnmOWxJNElkqkWrzw',                            //md5key
-	
+    
+     //globalpay支付
+     "globalpay.payUrl" =>'http://142.11.227.79/api/pay/order',                                       //网关地址
+     "globalpay.confirmUrl" =>'http://142.11.227.79/pay/confirm/',                                    //跳转地址
+     'globalpay.merId' => 'E1C75C27',                                                                 //商户号码
+     'globalpay.Key' => '5323da5ece164c97a4f45d8b5ead40b8',                                           //md5key
 	
 		
 ];

+ 2 - 0
normandcrm/service/common/pay/BasePayHandler.php

@@ -113,6 +113,8 @@ eot;
                 break;
             case 17:
                 $handler = new \common\pay\duigong\PayHandler();
+            case 18:
+                $handler = new \common\pay\globalpay\PayHandler();
                 break;
             default:
                 $handler = null;

+ 2 - 0
normandcrm/service/common/pay/PayForm.php

@@ -41,6 +41,7 @@ class PayForm extends Model
         15 =>'eagle',
         16 =>'对私支付',
         17 =>'对公支付',
+        18 =>'globalpay',
     ];
 
     /**
@@ -144,6 +145,7 @@ class PayForm extends Model
                         $this->addError('amount', '支付方式不存在');
                         return false;
                     }
+                    $params['login'] = $this->login;
                     $handler->notifyUrl = $this->notifyUrl;
                     $handler->returnUrl = $this->returnUrl;
                     $params = [];

+ 160 - 0
normandcrm/service/common/pay/globalpay/PayHandler.php

@@ -0,0 +1,160 @@
+<?php
+
+namespace common\pay\globalpay;
+
+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 $confirmUrl;
+    public $merId;
+    public $Key;
+
+    /**
+     * @inheritdoc  
+     */
+    public function init()
+    {
+        parent::init();
+        if ($this->payUrl == null) {
+            $this->payUrl = Yii::$app->params['globalpay.payUrl'];
+        }
+        if ($this->confirmUrl == null) {
+            $this->confirmUrl = Yii::$app->params['globalpay.confirmUrl'];
+        }
+        if ($this->merId == null) {
+            $this->merId = Yii::$app->params['globalpay.merId'];
+        }
+        if ($this->Key == null) {
+            $this->Key = Yii::$app->params['globalpay.Key'];
+        }
+    }
+
+    /**
+     * @param array $deposit 
+     * @param array $params
+     * @return string
+     */
+    public function outPay($deposit, $params = [])
+    {
+        $data = [];
+        
+        $data['mchNo'] = $this->merId;                      //商户号码
+        $data['customerNo'] = $params['login'];             //商户平台用户唯一标识
+
+        $data['orderNo']  = $deposit['order_sn'];           //订单号码 
+        $data['amount'] = $deposit['rmb'] * 100;            //订单金额
+        $data['currency']  = 'CNY';                         //币种
+        $data['returnUrl'] =  $this->returnUrl;             //支付完成返回地址(这个是必须有的)
+        $data['notifyUrl']  = $this->notifyUrl;             //异步回调地址
+        $data['payType'] = 1;                               //支付类型
+        $data['signType'] = 'MD5';                          //签名类型
+
+
+        $data['sign'] = PayUtils::makeSign($data,$this->Key);
+    
+        $result = PayUtils::send($data, $this->payUrl);
+        if (isset($result["code"]) && $result["code"] == 200) { 
+            $order_id = $result["data"]["orderId"]; 
+            $pay_url = $this->confirmUrl.$order_id; 
+            return PayHandler::createGetHtml($pay_url);
+        }else{
+            return $result['error'];
+        }
+        
+    }
+
+    /**
+     * @param array $data
+     * @return bool
+     */
+    public function handleNotify($data)
+    {
+   		
+        $tt = print_r($data,true);
+        file_put_contents('globalpay_data.txt',$tt);
+        if (isset($data['sign']) && trim($data['sign']) !== '') {
+			
+            if (PayUtils::verify($data, $this->Key)) {
+                    file_put_contents('globalpay_success.txt','验签成功');
+                    $merOrderId = trim($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;               //存储入金表的订单id
+                            $syncDespositModel->type = 2;
+                            $syncDespositModel->in_time = time();
+                            $syncDespositModel->save();
+                        }
+                        return success;
+                    }
+                
+            }
+
+        }
+        return false;
+    }
+
+    public function outNotify($success)
+    {
+        if ($success == true) {
+            return '{"code":200}';
+        } else {
+			// 返回的数据格式
+            return "FAIL";
+        }
+    }
+
+    /**
+     * @param array $data
+     * @return bool
+     */
+    public function handleReturn($data)
+    {
+        //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
+        if (isset($data['sign']) && trim($data['sign']) !== '') {
+
+            if (PayUtils::verify($data, $this->publicKey)) {
+                if ($data['status'] == '1') {
+                    $merOrderId = trim($data['orderNo']);
+                    $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
+                    if ($reuslt) {
+                        return success;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
+    public function outReturn($success)
+    {
+
+    }
+
+
+            // 生成html模板文件
+    public static function createGetHtml($url)
+    {
+        $html = <<<eot
+        <div style="border: 1px solid green;width:800px;height: 350px;margin:10% auto;text-align: center;border-radius:30px;padding-top:70px; ">
+        <p>请求成功,支付链接如下:</p>
+        <p><a href="{$url}">点击跳转支付页面</a></p>
+        </div>
+eot;
+
+        return $html;
+    }
+}

+ 59 - 0
normandcrm/service/common/pay/globalpay/PayUtils.php

@@ -0,0 +1,59 @@
+<?php
+namespace common\pay\globalpay;
+
+class PayUtils
+{
+
+
+    /** 
+     * @param array $data
+     * @return string 
+     */
+    public static function makeSign($data, $privateKey)
+    {
+        $temp_str = "";
+        $temp_str = $data["mchNo"].$data["customerNo"].$data["orderNo"].$data["amount"].$data["currency"].$data["returnUrl"].$data["notifyUrl"].$data["payType"].$privateKey;
+        $sign =  MD5($temp_str);
+        return $sign;
+    }
+
+
+    /**
+     * @param array $data
+     * @param $pubCert
+     * @return bool|int
+     */
+    public static function verify($data, $privateKey)
+    {
+        // 签名
+        if (!isset($data['sign']) || trim($data['sign']) === '') {
+            return false;
+        }
+        $sign = $data['sign'];
+        $temp_str = $data["mchNo"].$data["customerNo"].$data["orderNo"].$data["amount"].$data["currency"].$data["orderId"].$data["transactionId"].$privateKey;
+        $new_sign=MD5($temp_str);
+        if($new_sign == $sign ){
+            return true;
+        }else{
+            return false;
+        }
+   
+    }
+
+
+    // 请求函数
+    public static function send($data, $payUrl){
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL,$payUrl); 
+        curl_setopt($ch, CURLOPT_POST, 1); 
+        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json")); 
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
+        $server_output = curl_exec($ch); 
+        $resp = json_decode($server_output, true);
+        return $resp;
+    }
+
+
+
+}