Browse Source

[更新]修改微信支付demo

邹景立 8 years ago
parent
commit
a6645e84be

+ 33 - 0
application/index/controller/Wap.php

@@ -14,6 +14,8 @@
 namespace app\index\controller;
 
 use controller\BasicWechat;
+use service\DataService;
+use service\PayService;
 
 class Wap extends BasicWechat {
 
@@ -22,4 +24,35 @@ class Wap extends BasicWechat {
     public function index() {
         dump($this->fansinfo);
     }
+
+    public function payqrc() {
+        switch ($this->request->get('action')) {
+            case 'payqrc':
+                $pay = &load_wechat('pay');
+                $order_no = session('pay-test-order-no');
+                if (empty($order_no)) {
+                    $order_no = DataService::createSequence(10, 'wechat-pay-test');
+                    session('pay-test-order-no', $order_no);
+                }
+                if (PayService::isPay($order_no)) {
+                    return json(['code' => 2, 'order_no' => $order_no]);
+                }
+                $url = PayService::createWechatPayQrc($pay, $order_no, 1, '微信扫码支付测试!');
+                if ($url !== false) {
+                    return json(['code' => 1, 'url' => $url, 'order_no' => $order_no]);
+                }
+                $this->error("生成支付二维码失败,{$pay->errMsg}[{$pay->errCode}]");
+                break;
+            case 'reset':
+                session('pay-test-order-no', null);
+                break;
+            default:
+                return view();
+        }
+    }
+
+    public function jspay() {
+
+        return view();
+    }
 }

+ 50 - 0
application/index/view/wap.payqrc.html

@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>扫码支付DEMO</title>
+    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
+</head>
+<body style="text-align:center">
+
+<h3>扫码支付DEMO</h3>
+
+<div id="showbox">
+
+</div>
+
+<p id="orderno">
+
+</p>
+
+<button data-reset class="btn btn-default">重置订单号</button>
+
+</body>
+<script src="//cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
+<script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
+<script>
+    $(function () {
+
+        payTest.call(this), setInterval(payTest, 3000);
+
+        /*! 支付测试动作 */
+        function payTest() {
+            $.get('{:url("payqrc")}', {'action': 'payqrc'}, function (ret) {
+                if (ret.code === 2) {
+                    ret.url = '__STATIC__/theme/default/img/wechat/qrc_payed.jpg';
+                }
+                if (ret.code === 1) {
+                    $('#showbox').html('<img src="' + ret.url + '"/>');
+                }
+                $('#orderno').html('订单号:' + ret.order_no);
+            });
+        }
+
+        $('[data-reset]').on('click', function () {
+            $.get('{:url("payqrc")}', {'action': 'reset'}, function () {
+                window.location.reload();
+            });
+        });
+    });
+</script>
+</html>

+ 3 - 3
extend/controller/BasicWechat.php

@@ -51,7 +51,7 @@ class BasicWechat extends Controller {
         // 当前完整URL地址
         $this->url = $this->request->url(true);
         // 网页授权,并获粉丝信息
-        if ($this->check_auth && $this->_oAuth()) {
+        if ($this->check_auth && $this->oAuth()) {
             $this->assign('jsSign', load_wechat('script')->getJsSign($this->url));
             $this->assign('fansinfo', $this->fansinfo);
         }
@@ -62,10 +62,10 @@ class BasicWechat extends Controller {
      * @param bool $full 获取完整
      * @return string
      */
-    protected function _oAuth($full = true) {
+    protected function oAuth($full = true) {
         // 本地开发调试用户 openid
         if (in_array($this->request->host(), ['127.0.0.1', 'localhost'])) {
-            session('openid', 'o38gps1Unf64JOTdxNdd424lsEmM');
+            session('openid', 'o38gps3vNdCqaggFfrBRCRikwlWY');
         }
         // 检查缓存中 openid 信息是否完整
         if ($this->openid = session('openid')) {