Blame view

yudao-user-server/src/main/resources/static/pay.html 4.26 KB
60f5898c   YunaiV   增加 pay.html 前端,对接...
1
2
3
4
  <!DOCTYPE html>
  <html>
  <head>
      <meta charset="UTF-8">
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
5
      <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
60f5898c   YunaiV   增加 pay.html 前端,对接...
6
7
8
9
10
11
12
13
14
15
16
      <title>支付测试页</title>
      <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
      <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  </head>
  <body>
  <div>点击如下按钮,发起支付的测试</div>
  <div>
      <button id="wx_pub">微信公众号</button>
  </div>
  </body>
  <script>
72438ed5   YunaiV   增加 pay.html 前端,对接...
17
18
      let shopOrderId = undefined;
      let payOrderId = undefined;
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
19
20
      // let server = 'http://127.0.0.1:28080';
      let server = 'http://niubi.natapp1.cc';
20628987   YunaiV   增加支付回调地址的配置
21
22
      // TODO openid
      let openid = "ockUAwIZ-0OeMZl9ogcZ4ILrGba0";
60f5898c   YunaiV   增加 pay.html 前端,对接...
23
24
      $(function() {
          // 获得 JsapiTicket
72438ed5   YunaiV   增加 pay.html 前端,对接...
25
          // 参考 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html 文档
60f5898c   YunaiV   增加 pay.html 前端,对接...
26
          $.ajax({
72438ed5   YunaiV   增加 pay.html 前端,对接...
27
              url: server + "/api/wx/mp/create-jsapi-signature?url=" + document.location.href,
60f5898c   YunaiV   增加 pay.html 前端,对接...
28
29
30
31
32
33
34
              method: 'POST',
              success: function( result ) {
                  if (result.code !== 0) {
                      alert('获取 JsapiTicket 失败,原因:' + result.msg)
                      return;
                  }
                  var jsapiTicket = result.data;
72438ed5   YunaiV   增加 pay.html 前端,对接...
35
36
                  jsapiTicket.jsApiList = ['chooseWXPay'];
                  jsapiTicket.debug = false;
60f5898c   YunaiV   增加 pay.html 前端,对接...
37
38
39
40
41
  
                  // 初始化 JS
                  wx.config(jsapiTicket);
              }
          });
72438ed5   YunaiV   增加 pay.html 前端,对接...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  
          // 自动发起商城订单编号
          $.ajax({
              url: server + "/api/shop/order/create",
              method: 'POST',
              success: function( result ) {
                  if (result.code !== 0) {
                      alert('创建商城订单失败,原因:' + result.msg)
                      return;
                  }
                  shopOrderId = result.data.id;
                  payOrderId = result.data.payOrderId;
                  console.log("商城订单:" + shopOrderId)
                  console.log("支付订单:" + payOrderId)
              }
          })
60f5898c   YunaiV   增加 pay.html 前端,对接...
58
59
      })
  
72438ed5   YunaiV   增加 pay.html 前端,对接...
60
      // 微信公众号
60f5898c   YunaiV   增加 pay.html 前端,对接...
61
      $( "#wx_pub").on( "click", function() {
72438ed5   YunaiV   增加 pay.html 前端,对接...
62
          if (typeof WeixinJSBridge == "undefined") {
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
63
64
65
66
67
68
69
70
71
72
73
74
              // if (document.addEventListener) {
              //     document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
              // } else if (document.attachEvent) {
              //     document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
              //     document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
              // }
              alert('微信支付,只支持在微信客户端中使用!');
              return;
          }
          if (navigator.userAgent.indexOf('wechatdevtools') >= 0) {
              alert('微信支付,无法在微信开发者工具中使用!请使用微信客户端!');
              return;
72438ed5   YunaiV   增加 pay.html 前端,对接...
75
76
          }
  
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
77
          // 提交支付
20628987   YunaiV   增加支付回调地址的配置
78
79
          // 参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 文档
          // 参考 https://segmentfault.com/a/1190000020704650 文档
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
80
81
82
83
84
85
86
87
          $.ajax({
              url: server + "/api/pay/order/submit",
              method: 'POST',
              dataType: "json",
              contentType: "application/json",
              data: JSON.stringify({
                  "id": payOrderId,
                  "channelCode": 'wx_pub',
20628987   YunaiV   增加支付回调地址的配置
88
89
90
                  "channelExtras": {
                      "openid": openid
                  }
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
91
92
93
94
95
              }),
              success: function( result ) {
                  if (result.code !== 0) {
                      alert('提交支付订单失败,原因:' + result.msg)
                      return;
72438ed5   YunaiV   增加 pay.html 前端,对接...
96
                  }
1b0aaec9   YunaiV   完成微信支付的支付流程,美滋滋
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                  alert('点击确定,开始微信支付');
                  // 开始调用微信支付
                  let data = result.data.invokeResponse;
                  wx.chooseWXPay({
                      timestamp: data.timeStamp,
                      nonceStr: data.nonceStr,
                      package: data.packageValue,
                      signType: data.signType,
                      paySign: data.paySign,
                      success: function (res) {
                          alert(JSON.stringify(res));
                      },
                      error: function(e) {
                          alert(JSON.stringify(e));
                      }
                  });
              }
          })
60f5898c   YunaiV   增加 pay.html 前端,对接...
115
116
117
      });
  </script>
  </html>