现在小程序原生支持支付功能了,非常简单也不需要加密。
云函数
exports.main = async (event, context) => {
const { totalFee } = event
const res = await cloud.cloudPay.unifiedOrder({
"body" : "学校-超市",
"outTradeNo" : "1217752501201407033233368018",
"spbillCreateIp" : "127.0.0.1",
"subMchId" : "1900009231",
"totalFee" : totalFee,
"envId": "test-f0b102",
"functionName": "pay_cb"
})
return res
}
小程序
Page({
doPay: function(totalFee) {
let that = this;
;(async () => {
// 调用云函数
let tradeRes = await wx.cloud.callFunction({
name: 'pay',
data: {
// ...
totalFee: totalFee,
}
})
const payment = tradeRes.result.payment
// 调用微信支付接口
wx.requestPayment({
...tradeRes.result.payment,
success (res) {
console.log('支付成功', res)
},
fail (res) {
console.error('pay fail', err)
}
})
})();
}
})