在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
注意:WeixinJSBridge内置对象在其他浏览器中无效。
1、网页端接口请求参数列表
1 2 3 4 5 6 7 8
| { appid: string, timeStamp: string, nonceStr: string, package: string, signType: string, paySign: string }
|
以上参数皆是必填项统一由后端接口返回参数
2、返回结果值说明
1 2 3
| get_brand_wcpay_request:ok get_brand_wcpay_request:cancel get_brand_wcpay_request:fail
|
注:JS API的返回结果get_brand_wcpay_request:ok仅在用户成功完成支付时返回。由于前端交互复杂,get_brand_wcpay_request:cancel或者get_brand_wcpay_request:fail可以统一处理为用户遇到错误或者主动放弃,不必细化区分。
3、前端封装方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| declare let WeixinJSBridge: any;
function onBridgeReady (res: any) { WeixinJSBridge.invoke('getBrandWCPayRequest', { 'appId': res.appId, 'timeStamp': String(res.timeStamp), 'nonceStr': res.nonceStr, 'package': res.package, 'signType': res.signType, 'paySign': res.paySign }, function (result: any) {
if (result.err_msg === 'get_brand_wcpay_request:ok') { console.log('支付成功'); }
if (result.err_msg === 'get_brand_wcpay_request:cancel') { console.log('支付取消'); }
if (result.err_msg === 'get_brand_wcpay_request:fail') { console.log('支付失败'); } }) }
function bootWechatJSAPIPay (res: any) { let document: any; 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 { onBridgeReady(res) } }
export { bootWechatJSAPIPay }
|
以上typScript方法封装
4、前端获取openID方法
1
| window.location.replace(`${baseURL}/api/v1/wechat/oauth?authorization=${sign}&returnUrl=${url.href}`)
|
5、文件内方法调用
1 2
| import { bootWechatJSAPIPay } from 'src/hooks/pay' bootWechatJSAPIPay(param)
|
如果文档内容没有解决您的问题,您还可以前往 微信开放社区 寻求帮助