“小程序·云开发新推出原生微信支付能力支持,拥有简单、安全、免签名、免证书、支付结果可回调云函数的特点。开发者在云开发控制台可直接绑定微信支付商户,在绑定完成后可在云开发中原生接入微信支付。”
小程序端代码
gotopay: function (e) {
//构建时间
const curDate = new Date()
var h = curDate.getHours() + 8;
var m = curDate.getMinutes();
var s = curDate.getSeconds();
s = s < 10 ? "0" + s : s;
m = m < 10 ? "0" + m : m;
h = h < 10 ? "0" + h : h;
const time = `${curDate.getFullYear()}-${curDate.getMonth() + 1}-${curDate.getDate()} ${h}:${m}:${s}`
//单品核销优惠券
const orderlist = this.data.selarr
let body = ''
let goodsdetail_list = []
for (let i = 0; i < orderlist.length; i++) {
body += '/' + orderlist[i].title
//////单品优惠/////////////////////////////////////
let goods_detail = {
"goods_id": orderlist[i]._id,
"quantity": 1,
"price": orderlist[i].demo_price
}
goodsdetail_list.push(goods_detail)
////////////////////////////////////////////////
}
const detail = JSON.stringify({
"goods_detail": goodsdetail_list
})
body = body.substr(1) //不能大于128字符
body = body.substring(0, 128)
wx.cloud.callFunction({
name: 'cloudpay',
data: {
body: body,
totalfee: this.data.demo_total,//元转化为分
detail: detail
},
success: res => {
const payment = res.result.payment
console.log('payment', payment)
wx.requestPayment({
...payment,
success(res) {
console.log('pay success', res)
},
fail(err) {
console.error('pay fail', err)
}
})
},
fail: console.error,
})
}
//云函数端代码
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
const {body,totalfee,detail}=event
console.log('detail:',detail)
const res = await cloud.cloudPay.unifiedOrder({
"body":body,
"detail":detail,
"envId":"云环境ID",
"functionName":"cloudpayreturn",
// "nonceStr":"5K8264GGTKUU16AA3302SI8ZRRTM67MM",
"outTradeNo":`${Date.now().toString().slice(3)}`,
// "outTradeNo":"1217752501201407033299368099",
"spbillCreateIp":"127.0.0.1",
"subMchId":"商户号",
"totalFee":totalfee,
"version":"1.0", //单品券要用的字段///
"tradeType":"JSAPI"
})
return res
}
//以上代码全场券可能核销,单品券不启作用,希望微信支付官方人员指导
您好,具体会用到哪个接口