微信支付需要用到openid,我们可以先从后端的接口先获取请求到参数以及获取到支付接口所需要的参数,根据微信支付的官方文档说明,我们支付接口所需要用到的有:
1.timeStamp 时间戳 2.nonceStr 随机字符数 3.package 统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=***
4.signType 签名算法(MD5) 5.paySign 签名 (这五种的类型都是“字符串类型”)
//示例代码
wx.requestPayment({
timeStamp: '',
nonceStr: '',
package: '',
signType: 'MD5',
paySign: '',
success (res) { },
fail (res) { }
})
以下是我请求支付接口的例子,供大家参考下:
//点击发起支付方式
wx.request({
url: host + "/blue/api/creatOrder" ,
method:"post",
header: {
'content-type': 'application/x-www-form-urlencoded',
},
data: {
deviceNo: that.data.deviceNo,
wxAppid: "wxcbec42f8c5117bdb",
sign:"13e1961f6f374660b80b7e0a6185bd3a",
goodId: that.data.goods_Info[index].id,
openid: app.globalData.openId,
aisleId: that.data.goods_Info[index].aisleId
},
success: res => {
console.log('resss', res)
var pay = res.data.data
if (that.data.goods_Info[index].storage != 0) { //如果有商品时就显示支付
wx.requestPayment({ //微信官方api文档参考链接https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3
timeStamp: pay.timeStamp,
nonceStr: pay.nonceStr,
package: pay.package,
signType: 'MD5',
paySign: pay.paySign,
success(res) {
wx.showToast({
title: '支付成功',
icon: 'none',
duration: 1000
})
},
fail(res) {
wx.showToast({
title: '您已取消支付',
icon: 'none',
duration: 2000
})
}
})
}else{ //否则提醒用户商品已售完
wx.showToast({
title: '您购买的商品已售完',
icon: 'none',
duration: 2000
})
}
}
})