一. pay/index.js 云函数代码
const config = {
appid: 'XXXXXXXXXXX',
envName: 'XXXXXXXX',
mchid: 'XXXXXXXXX',
partnerKey: 'XXXXXXXXXXXXXXXXX',
notify_url: 'https://mp.weixin.qq.com',
spbill_create_ip: '127.0.0.1'
};
const cloud = require('wx-server-sdk');
cloud.init({
env: config.envName
})
const db = cloud.database();
const TcbRouter = require('tcb-router');
const rq = require('request');
const tenpay = require('tenpay');
exports.main = async (event, context) => {
const app = new TcbRouter({
event
});
app.router('selectorder',async(ctx)=>{
const data=await db.collection('orders').where({
openId:event.openId,
courseId:event.courseId
}).get()
ctx.body = data;
});
app.router('selectAllOrders',async(ctx)=>{
const data=await db.collection('orders').where({
openId:event.openId,
}).get()
ctx.body = data;
});
app.router('addorder',async(ctx)=>{
await db.collection('orders').add({
data:{
openId:event.openId,
courseId:event.courseId,
coursePrice:event.coursePrice,
courseTitle:event.courseTitle,
courseClassName:event.courseClassName,
createTime:event.createTime,
timestamp:event.timestamp,
payData:event.payData,
}
})
});
app.router('topay', async (ctx) => {
const api = tenpay.init(config)
let result = await api.getPayParams({
out_trade_no: 'seriesLessons'+ '' + event.timestamp,
body: event._id,
total_fee: parseInt(event.price)*1,
openid: event.openId
});
ctx.body = {
result,event};
});
return app.serve();
}
二 . 页面js文件中调用云函数实现支付
goPay: function () {
let that = this;
console.log('点击了立即支付')
var createTime = days();
var timestamp = (new Date()).getTime();
wx.cloud.callFunction({
name: 'pay',
data: {
$url: 'selectorder',
openId: that.data.openId,
courseId: that.data._id,
},
success(res) {
console.log(res)
if (res.result.data.length != 0) {
wx.showToast({
title: '您已经购买过这个课程了,无需重复购买。',
icon: 'none',
duration: 4000
})
} else {
wx.cloud.callFunction({
name: 'pay',
data: {
$url: 'topay',
openId: that.data.openId,
_id: that.data._id,
price: 1,
timestamp: timestamp,
},
success(res) {
console.log(res)
var payData = res.result.result;
wx.requestPayment({
timeStamp: res.result.result.timeStamp,
nonceStr: res.result.result.nonceStr,
package: res.result.result.package,
signType: 'MD5',
paySign: res.result.result.paySign,
success(re) {
console.log(re)
wx.cloud.callFunction({
name: 'pay',
data: {
$url: 'addorder',
openId: that.data.openId,
courseId: that.data._id,
coursePrice: that.data.seriesLessonsData.price,
courseTitle: that.data.seriesLessonsData.coverTitle,
courseClassName: that.data.seriesLessonsData.className,
createTime: createTime,
timestamp: timestamp,
payData: payData,
},
success(e) {
console.log(e)
wx.showToast({
title: '支付成功',
icon: 'success'
})
},
fail(err) {
console.log(err)
wx.showToast({
title: '网络波动,请重试',
icon: 'none'
})
}
})
},
fail(err) {
console.log(err)
wx.showToast({
title: '网络波动,请重试',
icon: 'none'
})
}
})
},
fail(err) {
console.log(err);
wx.showToast({
title: '网络波动,请重试',
icon: 'none'
})
}
})
}
},
fail(err) {
console.log(err)
wx.showToast({
title: '网络波动,请重试',
icon: 'none'
})
}
})
},
config数据的获取可以查看 微信小程序云开发–微信支付商户号,商户密钥等的获取