wxpay.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>充值</title>
  8. <style></style>
  9. </head>
  10. <body></body>
  11. <script src="http://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  12. <script>
  13. let appId = ""; //公众号ID,由商户传入
  14. let timeStamp = ""; //时间戳,自1970年以来的秒数
  15. let nonceStr = ""; //随机串
  16. let package = "";
  17. let signType = ""; //微信签名方式:
  18. let paySign = ""; //微信签名
  19. if (location.search) {
  20. ajax(
  21. "https://api.huiyaohuyu.com/pay/getOrder" + location.search,
  22. (res) => {
  23. let data = JSON.parse(res).data.pay_info;
  24. appId = data.appId;
  25. timeStamp = data.timeStamp;
  26. nonceStr = data.nonceStr;
  27. package = data.package;
  28. signType = data.signType;
  29. paySign = data.paySign;
  30. if (typeof WeixinJSBridge == "undefined") {
  31. if (document.addEventListener) {
  32. document.addEventListener(
  33. "WeixinJSBridgeReady",
  34. onBridgeReady,
  35. false
  36. );
  37. } else if (document.attachEvent) {
  38. document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
  39. document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
  40. }
  41. } else {
  42. onBridgeReady();
  43. }
  44. }
  45. );
  46. }
  47. function ajax(url, callback) {
  48. let xhr = new XMLHttpRequest();
  49. xhr.open("GET", url);
  50. xhr.onreadystatechange = function () {
  51. if (xhr.readyState == 4) {
  52. if (xhr.status == 200 || xhr.status == 304) {
  53. callback(xhr.responseText);
  54. } else {
  55. alert("支付失败");
  56. closePage();
  57. }
  58. }
  59. };
  60. xhr.send();
  61. }
  62. function onBridgeReady() {
  63. WeixinJSBridge.invoke(
  64. "getBrandWCPayRequest",
  65. {
  66. appId: appId, //公众号ID,由商户传入
  67. timeStamp: timeStamp, //时间戳,自1970年以来的秒数
  68. nonceStr: nonceStr, //随机串
  69. package: package,
  70. signType: signType, //微信签名方式:
  71. paySign: paySign, //微信签名
  72. },
  73. function (res) {
  74. if (res.err_msg == "get_brand_wcpay_request:ok") {
  75. // 使用以上方式判断前端返回,微信团队郑重提示:
  76. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  77. alert("支付成功");
  78. }
  79. closePage();
  80. }
  81. );
  82. }
  83. function closePage() {
  84. var userAgent = navigator.userAgent;
  85. if (
  86. userAgent.indexOf("Firefox") != -1 ||
  87. userAgent.indexOf("Chrome") != -1
  88. ) {
  89. // Firefox或Chrome中关闭
  90. window.location.href = "about:blank";
  91. } else {
  92. window.opener = null;
  93. window.open("", "_self");
  94. window.close();
  95. }
  96. if (WeixinJSBridge) {
  97. // 微信中关闭
  98. wx.closeWindow();
  99. }
  100. }
  101. </script>
  102. </html>