index.js 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const api = 'https://game.84game.cn/sdk' // 请求地址
  2. let payParam = ''; appId=""; // 支付参数
  3. let wechatPayId = "";
  4. // 关闭
  5. const closeHandle = () => {
  6. console.log('关闭')
  7. document.addEventListener('WeixinJSBridgeReady', () => {
  8. WeixinJSBridge.call('closeWindow');
  9. }, false);
  10. WeixinJSBridge.call('closeWindow');
  11. }
  12. // 支付
  13. const onBridgeReady = () => {
  14. if (payParam) {
  15. let { package, nonceStr, paySign, signType, timeStamp } = JSON.parse(payParam)
  16. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  17. "appId": appId, //公众号ID,由商户传入 -
  18. "timeStamp": timeStamp, //时间戳,自1970年以来的秒数
  19. "nonceStr": nonceStr, //随机串
  20. "package": `prepay_id=${package.prepay_id}`,
  21. "signType": signType, //微信签名方式:
  22. "paySign": paySign //微信签名
  23. }, res => {
  24. if (res.err_msg == "get_brand_wcpay_request:ok") {
  25. // 使用以上方式判断前端返回,微信团队郑重提示:
  26. // res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  27. // 跳转支付成功展示页面
  28. // closeHandle()
  29. // window.location.href = window.location.origin + "/html/wechatPay/paySucc.html";
  30. document.getElementById('succ').style.display = 'block'
  31. } else if (res.err_msg == "get_brand_wcpay_request:fail") {
  32. document.getElementById('tip').style.display = 'block'
  33. document.getElementsByClassName('tipTitle')[0].innerHTML = '该订单已失效,请回复“2”或游戏内重新下单获取最新支付订单';
  34. } else {
  35. console.log("取消支付")
  36. //修改上面生成的预支付订单状态
  37. // cancelPayMoney();
  38. }
  39. });
  40. } else {
  41. document.getElementById('tip').style.display = 'block'
  42. document.getElementsByClassName('tipTitle')[0].innerHTML = '订单异常,请联系客服重新下单';
  43. }
  44. }
  45. window.onload = () => {
  46. let search = window.location.search.replace('?', '')
  47. let obj = {}
  48. search.split('&').forEach(s => {
  49. let arr = s.split('=')
  50. obj[arr[0]] = arr[1]
  51. })
  52. document.getElementById('money').innerHTML = (obj.amount || '8') + '元'
  53. document.getElementById('title').innerHTML = decodeURIComponent(obj.description) || '购买8元档充值'
  54. document.getElementById('orderNo').innerHTML = '订单号:' + (obj.orderId || '2023010711541555400125675')
  55. if (!obj.appId) {
  56. alert('请传入appId')
  57. return
  58. }
  59. if (!obj.code) {
  60. window.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${obj.appId}&redirect_uri=${encodeURIComponent(location.href)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`)
  61. } else {
  62. console.log('获取信息====>', obj)
  63. appId = obj.appId
  64. fetch(api + `/api/pay/param/applet?orderId=${obj.orderId}&code=${obj.code}`, {
  65. method: 'GET',
  66. headers: { ['Content-Type']: 'application/json;charset=UTF-8' }
  67. }).then(res => res.json()).then(res => {
  68. if (res.success) {
  69. let { payParam: param } = res.data
  70. payParam = param
  71. if (typeof WeixinJSBridge == "undefined") {
  72. if (document.addEventListener) {
  73. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  74. } else if (document.attachEvent) {
  75. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  76. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  77. }
  78. } else {
  79. //wechatPayId = result.data.wechatPayId;//微信预支付id 用于用户取消支付时,修改生成的预支付订单状态onBridgeReady(result.data.appId,result.data.timeStamp,result.data.nonceStr,result.data.package,result.data.signType,result.data.paySign);
  80. onBridgeReady()
  81. }
  82. } else {
  83. document.getElementById('tip').style.display = 'block'
  84. document.getElementsByClassName('tipTitle')[0].innerHTML = res.msg || '该订单已失效,请回复“2”或游戏内重新下单获取最新支付订单';
  85. }
  86. })
  87. }
  88. }