Browse Source

fix #1003 在线支付增加易支付

magicblack 2 years ago
parent
commit
9a4aced7e5

+ 26 - 0
application/admin/view/extend/pay/epay.html

@@ -0,0 +1,26 @@
+<div class="layui-tab-item">
+
+    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
+        <legend>易支付设置</legend>
+    </fieldset>
+
+    <div class="layui-form-item">
+        <label class="layui-form-label">接口地址:</label>
+        <div class="layui-input-inline w400">
+            <input type="text" name="pay[epay][api_url]" value="{$config['pay']['epay']['api_url']}" class="layui-input" placeholder="需保留前面http(s)://和后面/">
+        </div>
+        <div class="layui-form-mid layui-word-aux">如: https://www.epay.com/</div>
+    </div>
+    <div class="layui-form-item">
+        <label class="layui-form-label">商户号:</label>
+        <div class="layui-input-inline w400">
+            <input type="text" name="pay[epay][appid]" placeholder="" value="{$config['pay']['epay']['appid']}" class="layui-input">
+        </div>
+    </div>
+    <div class="layui-form-item">
+        <label class="layui-form-label">密钥:</label>
+        <div class="layui-input-inline w400">
+            <input type="text" name="pay[epay][appkey]" placeholder="" value="{$config['pay']['epay']['appkey']}" class="layui-input">
+        </div>
+    </div>
+</div>

+ 87 - 0
application/common/extend/pay/Epay.php

@@ -0,0 +1,87 @@
+<?php
+namespace app\common\extend\pay;
+
+class Epay {
+
+    public $name = '易支付';
+    public $ver = '1.0';
+
+    public function submit($user,$order,$param)
+    {
+        $pay_type = 0;
+        $pay_type_map = [1 => 'alipay', 2 => 'qqpay', 3 => 'wxpay',];
+        if(!empty($param['paytype']) && isset($pay_type_map[$param['paytype']])){
+            $pay_type = $pay_type_map[$param['paytype']];
+        }
+        //组装参数
+        $epay_config = $GLOBALS['config']['pay']['epay'];
+        $data = array(
+            "pid"          => trim($epay_config['appid']),//你的商户ID
+            "type"         => $pay_type,//1支付宝支付 3微信支付 2QQ钱包
+            "notify_url"   => $GLOBALS['http_type'] . $_SERVER['HTTP_HOST'] . '/index.php/payment/notify/pay_type/epay',//通知地址
+            "return_url"   => $GLOBALS['http_type'] . $_SERVER['HTTP_HOST'] . '/index.php/payment/notify/pay_type/epay',//跳转地址
+            "out_trade_no" => $order['order_code'], //唯一标识 可以是用户ID,用户名,session_id(),订单ID,ip 付款后返回
+            "name"         => '积分充值(UID:'.$user['user_id'].')',
+            "money"        => (float)$order['order_price'],//金额100元
+        );
+        // 跳转至收银台,选择其他方式
+        if (empty($data['type'])) {
+            unset($data['type']);
+        }
+        ksort($data); //重新排序$data数组
+        reset($data); //内部指针指向数组中的第一个元素
+
+        $sign = ''; //初始化需要签名的字符为空
+        $urls = ''; //初始化URL参数为空
+
+        foreach ($data as $key => $val) { //遍历需要传递的参数
+            if ($val == ''||$key == 'sign') continue; //跳过这些不参数签名
+            if ($sign != '') { //后面追加&拼接URL
+                $sign .= "&";
+                $urls .= "&";
+            }
+            $sign .= "$key=$val"; //拼接为url参数形式
+            $urls .= "$key=" . urlencode($val); //拼接为url参数形式并URL编码参数值
+        }
+
+        $query = $urls . '&sign='.md5($sign.trim( $epay_config['appkey'] )); //创建订单所需的参数
+        $url = $epay_config['api_url'] . "submit.php?{$query}"; //支付页面
+        mac_redirect($url);
+    }
+
+    public function notify()
+    {
+        $param = $_REQUEST;
+        // $param['trade_no'] 这是付款人的唯一身份标识或订单ID
+        // $param['out_trade_no'] 这是流水号 没有则表示没有付款成功 流水号不同则为不同订单
+        // $param['money'] 这是付款金额
+
+        unset($param['/payment/notify/pay_type/epay']);
+        unset($param['paytype']);
+        unset($param['s']);
+
+        ksort($param); //排序post参数
+        reset($param); //内部指针指向数组中的第一个元素
+        $sign = '';
+        foreach ($param as $key => $val) {
+            if ($val == '') continue; //跳过空值
+            if ($key != 'sign' && $key != 'sign_type') { //跳过sign
+                $sign .= "$key=$val&"; //拼接为url参数形式
+            }
+        }
+
+        $epay_config = $GLOBALS['config']['pay']['epay'];
+        if (!$param['out_trade_no'] || md5(substr($sign, 0, -1) . trim($epay_config['appkey'])) != $param['sign']) {
+            echo 'fail';
+        }
+        else{
+            $res = model('Order')->notify($param['out_trade_no'], 'epay');
+            if($res['code'] >1){
+                echo 'fail2';
+            }
+            else {
+                echo 'success';
+            }
+        }
+    }
+}

+ 12 - 17
application/index/controller/Payment.php

@@ -12,27 +12,22 @@ class Payment extends Base
 
 
     public function notify()
     public function notify()
     {
     {
-        if (Request()->isPost()) {
-            $param = input();
-            $pay_type = $param['pay_type'];
+        $param = input();
+        $pay_type = $param['pay_type'];
 
 
-            if ($GLOBALS['config']['pay'][$pay_type]['appid'] == '') {
-                echo lang('index/payment_status');
-                exit;
-            }
+        if ($GLOBALS['config']['pay'][$pay_type]['appid'] == '') {
+            echo lang('index/payment_status');
+            exit;
+        }
 
 
-            $cp = 'app\\common\\extend\\pay\\' . ucfirst($pay_type);
-            if (class_exists($cp)) {
-                $c = new $cp;
-                $c->notify();
-            }
-            else{
-                echo lang('index/payment_not');
-                exit;
-            }
+        $cp = 'app\\common\\extend\\pay\\' . ucfirst($pay_type);
+        if (class_exists($cp)) {
+            $c = new $cp;
+            $c->notify();
         }
         }
         else{
         else{
-            return $this->success(lang('index/payment_ok'), url('user/index') );
+            echo lang('index/payment_not');
+            exit;
         }
         }
     }
     }
 }
 }

+ 1 - 1
application/index/controller/User.php

@@ -523,7 +523,7 @@ class User extends Base
         $cp = 'app\\common\\extend\\pay\\' . ucfirst($payment);
         $cp = 'app\\common\\extend\\pay\\' . ucfirst($payment);
         if (class_exists($cp)) {
         if (class_exists($cp)) {
             $c = new $cp;
             $c = new $cp;
-            $payment_res = $c->submit($this->user, $res['info'], $param);
+            $payment_res = $c->submit($GLOBALS['user'], $res['info'], $param);
         }
         }
         //$payment_res = model('Pay' . $payment)->submit($this->user, $res['info'], $param);
         //$payment_res = model('Pay' . $payment)->submit($this->user, $res['info'], $param);
         if ($payment == 'weixin') {
         if ($payment == 'weixin') {