前言
这种情况是不多见的,碰巧本人遇见了,所以记录一下
H5页面
一般是后端把我们需要传递的参数处理好,给到我们前端。调起微信小程序支付的参数有小程序IDappId、时间戳
timeStamp、随机字符串nonceStr、订单详情扩展字符串package、签名方式signType、签名paySign。
大家可以参考官方文档小程序调起支付API,不多说了直接代码。
function topay(){
wx.miniProgram.postMessage({
data: {
message: {
appId: appId,
timeStamp: timeStamp,
nonceStr: nonceStr,
package: packages,
signType: signType,
paySign: paySign,
},
},
});
wx.miniProgram.redirectTo({
url: "../pay/pay",
});
}
微信小程序页面
在web-view组件里定义接收事件
<web-view src="http://******.com" bindmessage="handlePostMessage"></web-view>
handlePostMessage: function (e) {
let datas = e.detail.data[e.detail.data.length - 1].message;
wx.setStorageSync('appId',datas.appId)
wx.setStorageSync('timeStamp',datas.timeStamp)
wx.setStorageSync('nonceStr',datas.nonceStr)
wx.setStorageSync('package',datas.package)
wx.setStorageSync('signType',datas.singType)
wx.setStorageSync('paySign',datas.paySign)
},
支付页面:h5中跳转的就是这个页面
let appId = wx.getStorageSync('appId');
let timeStamp = wx.getStorageSync('timeStamp');
let nonceStr = wx.getStorageSync('nonceStr');
let packages = wx.getStorageSync('package');
let singType = wx.getStorageSync('singType');
let paySign = wx.getStorageSync('paySign');
let data = {
'appId':appId,
"timeStamp": timeStamp,
"nonceStr": nonceStr,
"package": packages,
"signType": singType,
"paySign": paySign,
}
wx.requestPayment
(
{
...data,
"success":function(res){console.log('成功', res)},
"fail":function(res){console.log('取消',res)},
"complete":function(res){console.log('失败',res)}
}
)
|