pay_wx_pub.html 4.3 KB

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