WeChatウィジェットクラウド開発--クラウド関数を使用してWeChat決済を実現
31113 ワード
一.pay/index.jsクラウド関数コード
二.ページjsファイルでクラウド関数を呼び出して支払いを実現
configデータの取得は、微信ウィジェットクラウド開発–微信支払い口座番号、口座鍵などの取得を表示できます.
const config = {
appid: 'XXXXXXXXXXX', // AppId
envName: 'XXXXXXXX', // ID
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');//
// : pay --> --> npm i tenpay -D -->
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,// openId
courseId:event.courseId,// _id
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({
// , seriesLessons+ openID+
// ,
out_trade_no: 'seriesLessons'+ '' + event.timestamp, // '' , 32
body: event._id, // , _id
total_fee: parseInt(event.price)*1, // , , , , 。 *100 , 0.01 。
openid: event.openId // 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, // openId
courseId: that.data._id, // _id
},
success(res) {
console.log(res)
// ,
if (res.result.data.length != 0) {
wx.showToast({
title: ' , 。',
icon: 'none',
duration: 4000
})
// wx.navigateTo({
// url: '../boughtCourse/boughtCourse',
// })
} else {
// ,
wx.cloud.callFunction({
name: 'pay',
data: {
$url: 'topay',
openId: that.data.openId,// openId
_id: that.data._id,// _id
price: 1, // that.data.seriesLessonsData.price
timestamp: timestamp,
},
success(res) {
console.log(res)
// ,
var payData = res.result.result;
//
// wx.requestPayment api
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)
// orders
wx.cloud.callFunction({
name: 'pay',
data: {
$url: 'addorder',
openId: that.data.openId, // openId
courseId: that.data._id, // _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データの取得は、微信ウィジェットクラウド開発–微信支払い口座番号、口座鍵などの取得を表示できます.