1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- const api = 'https://game.84game.cn/sdk' // 请求地址
- let payParam = ''; appId=""; // 支付参数
- let wechatPayId = "";
- // 关闭
- const closeHandle = () => {
- console.log('关闭')
- document.addEventListener('WeixinJSBridgeReady', () => {
- WeixinJSBridge.call('closeWindow');
- }, false);
- WeixinJSBridge.call('closeWindow');
- }
- // 支付
- const onBridgeReady = () => {
- if (payParam) {
- let { package, nonceStr, paySign, signType, timeStamp } = JSON.parse(payParam)
- WeixinJSBridge.invoke('getBrandWCPayRequest', {
- "appId": appId, //公众号ID,由商户传入 -
- "timeStamp": timeStamp, //时间戳,自1970年以来的秒数
- "nonceStr": nonceStr, //随机串
- "package": `prepay_id=${package.prepay_id}`,
- "signType": signType, //微信签名方式:
- "paySign": paySign //微信签名
- }, res => {
- if (res.err_msg == "get_brand_wcpay_request:ok") {
- // 使用以上方式判断前端返回,微信团队郑重提示:
- // res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
- // 跳转支付成功展示页面
- // closeHandle()
- // window.location.href = window.location.origin + "/html/wechatPay/paySucc.html";
- document.getElementById('succ').style.display = 'block'
- } else if (res.err_msg == "get_brand_wcpay_request:fail") {
- document.getElementById('tip').style.display = 'block'
- document.getElementsByClassName('tipTitle')[0].innerHTML = '该订单已失效,请回复“2”或游戏内重新下单获取最新支付订单';
- } else {
- console.log("取消支付")
- //修改上面生成的预支付订单状态
- // cancelPayMoney();
- }
- });
- } else {
- document.getElementById('tip').style.display = 'block'
- document.getElementsByClassName('tipTitle')[0].innerHTML = '订单异常,请联系客服重新下单';
- }
- }
- window.onload = () => {
- let search = window.location.search.replace('?', '')
- let obj = {}
- search.split('&').forEach(s => {
- let arr = s.split('=')
- obj[arr[0]] = arr[1]
- })
- document.getElementById('money').innerHTML = (obj.amount || '8') + '元'
- document.getElementById('title').innerHTML = decodeURIComponent(obj.description) || '购买8元档充值'
- document.getElementById('orderNo').innerHTML = '订单号:' + (obj.orderId || '2023010711541555400125675')
- if (!obj.appId) {
- alert('请传入appId')
- return
- }
- if (!obj.code) {
- 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`)
- } else {
- console.log('获取信息====>', obj)
- appId = obj.appId
- fetch(api + `/api/pay/param/applet?orderId=${obj.orderId}&code=${obj.code}`, {
- method: 'GET',
- headers: { ['Content-Type']: 'application/json;charset=UTF-8' }
- }).then(res => res.json()).then(res => {
- if (res.success) {
- let { payParam: param } = res.data
- payParam = param
- if (typeof WeixinJSBridge == "undefined") {
- if (document.addEventListener) {
- document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
- } else if (document.attachEvent) {
- document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
- document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
- }
- } else {
- //wechatPayId = result.data.wechatPayId;//微信预支付id 用于用户取消支付时,修改生成的预支付订单状态onBridgeReady(result.data.appId,result.data.timeStamp,result.data.nonceStr,result.data.package,result.data.signType,result.data.paySign);
- onBridgeReady()
- }
- } else {
- document.getElementById('tip').style.display = 'block'
- document.getElementsByClassName('tipTitle')[0].innerHTML = res.msg || '该订单已失效,请回复“2”或游戏内重新下单获取最新支付订单';
- }
- })
- }
- }
|