mirror of
				https://gitee.com/technical-laohu/mpay.git
				synced 2025-11-04 16:53:44 +08:00 
			
		
		
		
	优化收银台API逻辑,后台新增监听模式功能
This commit is contained in:
		@@ -109,9 +109,11 @@ class PayController
 | 
			
		||||
        if ($order_id) {
 | 
			
		||||
            $act_order = Order::where('order_id', $order_id)->find();
 | 
			
		||||
            if ($act_order) {
 | 
			
		||||
                $passtime = strtotime($act_order->close_time) - time();
 | 
			
		||||
                $data = [];
 | 
			
		||||
                if ($act_order->state === 0) {
 | 
			
		||||
                    $data['order_id'] = $act_order->order_id;
 | 
			
		||||
                    $data['passtime'] = $passtime > 0 ? $passtime : 0;
 | 
			
		||||
                    $data['state'] = $act_order->state;
 | 
			
		||||
                    return json($data);
 | 
			
		||||
                } elseif ($act_order->state === 1) {
 | 
			
		||||
@@ -125,6 +127,7 @@ class PayController
 | 
			
		||||
                    $res_return_url = $act_order->return_url . '?' . http_build_query($notify);
 | 
			
		||||
                    // 响应消息
 | 
			
		||||
                    $data['order_id'] = $act_order->order_id;
 | 
			
		||||
                    $data['passtime'] = $passtime > 0 ? $passtime : 0;
 | 
			
		||||
                    $data['state'] = $act_order->state;
 | 
			
		||||
                    $data['return_url'] = $res_return_url;
 | 
			
		||||
                    return json($data);
 | 
			
		||||
@@ -245,21 +248,19 @@ class PayController
 | 
			
		||||
        // 检测本账号订单
 | 
			
		||||
        $orders = [];
 | 
			
		||||
        foreach ($order_list as $key => $val) {
 | 
			
		||||
            if ($pid == $val['pid'] && $aid == $val['aid']) {
 | 
			
		||||
            if ($pid == $val['pid'] && $aid == $val['aid'] && $val['patt'] == 1) {
 | 
			
		||||
                $orders[] = $order_list[$key];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (!$orders) {
 | 
			
		||||
            return \json(['code' => 0, 'msg' => '非本账号订单']);
 | 
			
		||||
        }
 | 
			
		||||
        // 收款平台
 | 
			
		||||
        $platform = ['sqbpay' => 'ShouQianBa', 'storepay' => 'ZhiHuiJingYing', 'mqpay' => 'MaQian', 'ysepay' => 'Ysepay'];
 | 
			
		||||
        // 登陆账号
 | 
			
		||||
        $config = ['username' => $pay_config['account'], 'password' => $pay_config['password']];
 | 
			
		||||
        // 收款查询
 | 
			
		||||
        $query = $pay_config['query'];
 | 
			
		||||
        // 实例监听客户端
 | 
			
		||||
        $payclient_name = $platform[$pay_config['platform']];
 | 
			
		||||
        $payclient_name = $pay_config['payclass'];
 | 
			
		||||
        $payclient_path = "\\payclient\\{$payclient_name}";
 | 
			
		||||
        $Payclient = new $payclient_path($config);
 | 
			
		||||
        // 获取支付明细
 | 
			
		||||
@@ -281,7 +282,7 @@ class PayController
 | 
			
		||||
        $is_user = User::checkUser($pid, $sign);
 | 
			
		||||
        $path = '../runtime/order.json';
 | 
			
		||||
        if ($is_user) {
 | 
			
		||||
            $orders = Order::scope('activeOrder')->field('id,pid,aid,cid')->select();
 | 
			
		||||
            $orders = Order::scope('activeOrder')->field('id,pid,aid,cid,patt')->select();
 | 
			
		||||
            if (!file_exists($path)) {
 | 
			
		||||
                file_put_contents($path, '[]');
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -119,6 +119,7 @@ class PayManageController extends BaseController
 | 
			
		||||
            'platform'  =>  $acc->getData('platform'),
 | 
			
		||||
            'account'   =>  $acc->account,
 | 
			
		||||
            'password'  =>  $acc->password,
 | 
			
		||||
            'payclass'  =>  $platform->class_name,
 | 
			
		||||
            'query'     =>  \var_export($query, \true)
 | 
			
		||||
        ];
 | 
			
		||||
        $config = View::fetch('tpl/account_config', $data);
 | 
			
		||||
 
 | 
			
		||||
@@ -44,6 +44,8 @@ class Order extends BaseModel
 | 
			
		||||
            'param'         => serialize(self::getParams($data)),
 | 
			
		||||
            // 等待/过期:0, 支付成功:1
 | 
			
		||||
            'state'         => 0,
 | 
			
		||||
            // 开启监听:1, 关闭监听:0
 | 
			
		||||
            'patt'          => $channel['patt'],
 | 
			
		||||
            // 订单创建时间
 | 
			
		||||
            'create_time'   => self::getFormatTime($my_time),
 | 
			
		||||
            // 订单关闭时间
 | 
			
		||||
@@ -109,9 +111,6 @@ class Order extends BaseModel
 | 
			
		||||
        $order->qrcode = $a_list->payChannel[0]->qrcode ?? '···';
 | 
			
		||||
        return $order->toArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // 选择收款通道
 | 
			
		||||
    private static function setChannel($pid): array
 | 
			
		||||
    {
 | 
			
		||||
@@ -120,7 +119,8 @@ class Order extends BaseModel
 | 
			
		||||
        if (!$channel_info || !$aids) {
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
        $channel = ['aid' => $channel_info->account_id, 'cid' => $channel_info->id];
 | 
			
		||||
        $patt = PayAccount::find($channel_info->account_id);
 | 
			
		||||
        $channel = ['aid' => $channel_info->account_id, 'cid' => $channel_info->id, 'patt' => $patt->pattern];
 | 
			
		||||
        PayChannel::update(['last_time' => self::getFormatTime(), 'id' => $channel['cid']]);
 | 
			
		||||
        return $channel;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@ return [
 | 
			
		||||
    // 用户账号配置
 | 
			
		||||
    'user' => [
 | 
			
		||||
        'pid'       =>  1001,
 | 
			
		||||
        'key'       =>  '7ImzF6Rf8OciQcmRJv8oTNBwIp6uqF0p'
 | 
			
		||||
        'key'       =>  '953c4d682d9ab148277b76a06e215ce7'
 | 
			
		||||
    ],
 | 
			
		||||
    // 收款平台账号配置
 | 
			
		||||
    'pay' => [
 | 
			
		||||
@@ -14,10 +14,12 @@ return [
 | 
			
		||||
        'aid'       =>  40,
 | 
			
		||||
        // 收款平台
 | 
			
		||||
        'platform'  =>  'mqpay',
 | 
			
		||||
        // 收款平台
 | 
			
		||||
        'payclass'  =>  'MaQian',
 | 
			
		||||
        // 账号
 | 
			
		||||
        'account'   =>  '258000000',
 | 
			
		||||
        'account'   =>  '18657945333',
 | 
			
		||||
        // 密码
 | 
			
		||||
        'password'  =>  '123456',
 | 
			
		||||
        'password'  =>  'Aa12345678',
 | 
			
		||||
        // 订单查询参数配置
 | 
			
		||||
        'query'     =>  array (
 | 
			
		||||
  'terminalType' => '',
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1 @@
 | 
			
		||||
{"code":0,"msg":"没有新订单"}
 | 
			
		||||
{"code":1,"msg":"\u67091\u4e2a\u65b0\u8ba2\u5355","orders":[{"id":27409,"pid":1001,"aid":30,"cid":31,"patt":0}]}
 | 
			
		||||
@@ -76,7 +76,7 @@
 | 
			
		||||
        .msg>p {
 | 
			
		||||
            color: red;
 | 
			
		||||
            font-weight: 700;
 | 
			
		||||
            line-height: 1.5em;
 | 
			
		||||
            line-height: 2em;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .msg>p:nth-child(1) {
 | 
			
		||||
@@ -98,7 +98,7 @@
 | 
			
		||||
 | 
			
		||||
        .note {
 | 
			
		||||
            color: red;
 | 
			
		||||
            font-size: 24px;
 | 
			
		||||
            font-size: 22px;
 | 
			
		||||
        }
 | 
			
		||||
    </style>
 | 
			
		||||
</head>
 | 
			
		||||
@@ -135,10 +135,10 @@
 | 
			
		||||
                <div class="qrcode"><img id="qrcode" src="/static/img/loading.gif">
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="msg">
 | 
			
		||||
                    <p>请付款 <span id="s_money" class="note">
 | 
			
		||||
                    <p>请付款 <span class="note">
 | 
			
		||||
                            <?php echo htmlentities($really_price); ?>
 | 
			
		||||
                        </span> 元,注意不能多付或少付</p>
 | 
			
		||||
                    <p>付款后,请等待5秒查看</p>
 | 
			
		||||
                        </span> 元,注意不能多付或少付</p>
 | 
			
		||||
                    <p>付款后,请等待 5 秒查看</p>
 | 
			
		||||
                    <p id="divTime"></p>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="shanxinzha">
 | 
			
		||||
@@ -147,13 +147,11 @@
 | 
			
		||||
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    </div>
 | 
			
		||||
    <script src="/static/js/layui.min.js"></script>
 | 
			
		||||
    <script src="/static/js/awesome-qr.min.js"></script>
 | 
			
		||||
    <script>
 | 
			
		||||
        const payCode = '<?php echo $payUrl; ?>';
 | 
			
		||||
        const payCode = '<?php echo htmlentities($payUrl); ?>';
 | 
			
		||||
        const payType = '<?php echo htmlentities($type); ?>';
 | 
			
		||||
        const order = '<?php echo htmlentities($order_id); ?>';
 | 
			
		||||
        const QR = AwesomeQR.AwesomeQR;
 | 
			
		||||
@@ -170,18 +168,16 @@
 | 
			
		||||
            }
 | 
			
		||||
            // 生成二维码
 | 
			
		||||
            document.getElementById('qrcode').src = await getQrcode(payCode, QR);
 | 
			
		||||
            // 订单倒计时
 | 
			
		||||
            const intDiff = <?php echo strtotime($close_time) - time() > 0 ? strtotime($close_time) - time() : 0; ?>;
 | 
			
		||||
            const timerId = timer(intDiff);
 | 
			
		||||
            // 订单过期时间
 | 
			
		||||
            let passtime = 0;
 | 
			
		||||
            // 订单状态查询
 | 
			
		||||
            let orderTimer = null;
 | 
			
		||||
            let orderTimer = 0;
 | 
			
		||||
            const queryOrder = async (url) => {
 | 
			
		||||
                if (orderTimer) { clearTimeout(orderTimer); }
 | 
			
		||||
                const res = await fetch(url);
 | 
			
		||||
                const jsonData = await res.json();
 | 
			
		||||
                orderTimer = setTimeout(() => { queryOrder(url) }, 1000);
 | 
			
		||||
                if (jsonData.state === 1) {
 | 
			
		||||
                    clearInterval(timerId);
 | 
			
		||||
                    clearTimeout(orderTimer);
 | 
			
		||||
                    document.getElementById('divTime').innerHTML = '<small class="note">订单支付成功</small>';
 | 
			
		||||
                    document.getElementById('qrcode').src = '/static/img/pay_ok.png';// 输出支付成功提示图片
 | 
			
		||||
@@ -189,13 +185,13 @@
 | 
			
		||||
                        location.href = jsonData.return_url;
 | 
			
		||||
                    }, 1500);
 | 
			
		||||
                }
 | 
			
		||||
                if (jsonData.state === 2) {
 | 
			
		||||
                passtime = jsonData.passtime;
 | 
			
		||||
                if (passtime <= 0) {
 | 
			
		||||
                    clearTimeout(orderTimer);
 | 
			
		||||
                }
 | 
			
		||||
                timer(passtime);
 | 
			
		||||
            }
 | 
			
		||||
            if (intDiff > 0) {
 | 
			
		||||
                queryOrder(`/getOrderState/${order}`);
 | 
			
		||||
            }
 | 
			
		||||
            queryOrder(`/getOrderState/${order}`);
 | 
			
		||||
 | 
			
		||||
        })();
 | 
			
		||||
        // 生成二维码
 | 
			
		||||
@@ -208,30 +204,19 @@
 | 
			
		||||
            })
 | 
			
		||||
            return qrcodeUrl;
 | 
			
		||||
        }
 | 
			
		||||
        // 计时
 | 
			
		||||
        function timer(intDiff) {
 | 
			
		||||
            const timerId = setInterval(() => {
 | 
			
		||||
                let day = 0, hour = 0, minute = 0, second = 0;//时间默认值       
 | 
			
		||||
                if (intDiff > 0) {
 | 
			
		||||
                    day = Math.floor(intDiff / (60 * 60 * 24));
 | 
			
		||||
                    hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
 | 
			
		||||
                    minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
 | 
			
		||||
                    second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
 | 
			
		||||
                }
 | 
			
		||||
                if (minute <= 9) minute = '0' + minute;
 | 
			
		||||
                if (second <= 9) second = '0' + second;
 | 
			
		||||
                if (hour <= 0 && minute <= 0 && second <= 0) {
 | 
			
		||||
                    document.getElementById('divTime').innerHTML = '<small class="note">订单二维码已过期</small>';
 | 
			
		||||
                    document.getElementById('qrcode').src = '/static/img/qrcode_timeout.png';// 输出过期二维码提示图片
 | 
			
		||||
                } else {
 | 
			
		||||
                    document.getElementById('divTime').innerHTML = `二维码有效时间: <small class="note">${minute}</small>分<small style="color:red; font-size:24px">${second}</small> 秒,失效勿付`;
 | 
			
		||||
                }
 | 
			
		||||
                if (intDiff < 0) {
 | 
			
		||||
                    clearInterval(timerId);
 | 
			
		||||
                }
 | 
			
		||||
                intDiff--;
 | 
			
		||||
            }, 1000);
 | 
			
		||||
            return timerId;
 | 
			
		||||
        // 倒计时
 | 
			
		||||
        function timer(passtime) {
 | 
			
		||||
            let minute = 0, second = 0; // 时间默认值       
 | 
			
		||||
            if (passtime > 0) {
 | 
			
		||||
                minute = Math.floor(passtime / 60);
 | 
			
		||||
                second = Math.floor(passtime) - (minute * 60);
 | 
			
		||||
            }
 | 
			
		||||
            if (passtime <= 0) {
 | 
			
		||||
                document.getElementById('divTime').innerHTML = '<small class="note">订单二维码已过期</small>';
 | 
			
		||||
                document.getElementById('qrcode').src = '/static/img/qrcode_timeout.png';// 输出过期二维码提示图片
 | 
			
		||||
            } else {
 | 
			
		||||
                document.getElementById('divTime').innerHTML = `二维码有效时间:<small class="note">${minute}</small>分<small class="note">${second}</small> 秒,失效勿付`;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    </script>
 | 
			
		||||
</body>
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,14 @@
 | 
			
		||||
			bottom: 8px;
 | 
			
		||||
			right: 8px;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		.layui-card {
 | 
			
		||||
			border-radius: 10px;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		#mainbox {
 | 
			
		||||
			padding: 16px;
 | 
			
		||||
		}
 | 
			
		||||
	</style>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,8 @@ return [
 | 
			
		||||
        'aid'       =>  <?php echo $aid; ?>,
 | 
			
		||||
        // 收款平台
 | 
			
		||||
        'platform'  =>  '<?php echo $platform; ?>',
 | 
			
		||||
        // 收款平台
 | 
			
		||||
        'payclass'  =>  '<?php echo $payclass; ?>',
 | 
			
		||||
        // 账号
 | 
			
		||||
        'account'   =>  '<?php echo $account; ?>',
 | 
			
		||||
        // 密码
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user