Donut多端-云开发API_V2微信支付案例
使用的是小程序原生-多端支付
使用条件
1.微信开放平台
2.微信支付(app支付)
3.多端SDK(含支付)
云开发回调这个不知道怎么解决
只能在微信确认支付返回app后确认
有解决方法的可以分享一下
前端js
const payData = {price,body,attach}
cloud('Ger_pay',{
type2:'pay_token',
payData
}).then(res =>{
console.log('获取sign信息',res)
const {timestamp,prepayid,partnerid,appId,noncestr,sign} = res.result;
wx.miniapp.requestPayment({
timeStamp: timestamp,
mchId: partnerid,
prepayId: prepayid,
package: 'Sign=WXPay',
nonceStr: noncestr,
sign: sign,
success: (res) => {
console.warn('wx.miniapp.requestPayment success:', res)
wx.showLoading({
title: '支付中...',
mask: true
})
cloud('Ger_pay',{
type2:'app_vip',
prepayid,
payData
}).then(res =>{
console.log(res)
wx.reLaunch({
url: '../index/index'
})
})
},
fail: (res) => {
console.error('取消支付', res)
wx.showToast({
title: '取消支付',
})
}
})
})
云开发index.js
const cloud = require('wx-server-sdk')
const pay_token = require('./pay_token.js')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const USER_LIST = 'UserList'
async function getpre_order(db,prepayid){
try {
await db.collection('pre_order').where({
prepayid:prepayid
}).update({
data: {
payTime: new Date().getTime(),
payStatus: 1
},
})
} catch (error) {
console.error(error);
}
}
exports.main = async (event,context) => {
try {
const wxContext = cloud.getWXContext()
const db = cloud.database()
const unionid = wxContext.UNIONID
const time = Date.parse(new Date());
const timestamp = time / 1000;
var e = event;
if (e.type2 === 'pay_token') {
return await pay_token.main(event,context);
}
if (e.type2 === 'app_vip') {
const {prepayid,payData} = e
await getpre_order(db,prepayid)
}
}catch (error) {
console.error(error);
return { code: -1, message: '服务器错误' + error};
}
}
const key = "253050DD68FF61CC99D56232E002489"
const mch_id = "160755748"
const cloud = require('wx-server-sdk')
const rp = require('request-promise')
const crypto = require('crypto')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
function getSign(args) {
let arr = new Array();
let num = 0;
for (let i in args) {
arr[num] = i;
num++;
}
let sortArr = arr.sort();
let sortObj = {};
for (let i in sortArr) {
sortObj[sortArr[i]] = args[sortArr[i]];
}
let sortStr = ''
sortArr.forEach(key => {
sortStr += key + "=" + sortObj[key] + "&"
});
sortStr += "key=" + key
const sign = crypto.createHash('md5').update(sortStr, 'utf8').digest('hex').toUpperCase();
return sign;
}
function getXml(args) {
let sa = []
for (let k in args) sa.push('<' + k + '>' + args[k] + '</' + k + '>')
sa.push('<sign>' + getSign(args) + '</sign>')
return '<xml>' + sa.join('') + '</xml>'
}
exports.main = async (event, context) => {
const {total_fee,body,attach} = event.payData
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
const unionid = wxContext. unionid
const appId = appid = 'wx8c699bc1e33333fa9'
const attach = attach || 'app支付'
const notify_url = "https://api.XXX.com/notify"
const spbill_create_ip = "127.0.0.1"
const noncestr = nonceStr = nonce_str = Math.random().toString(36).substr(2, 15)
const timestamp = timeStamp = parseInt(Date.now() / 1000) + ''
const out_trade_no = "otn" + nonce_str + timeStamp
const trade_type = "APP"
const sign_type = 'MD5'
const xmlArgs = {
appid,
attach,
body: body,
mch_id,
nonce_str,
notify_url,
out_trade_no,
spbill_create_ip,
total_fee,
fee_type:'CNY',
trade_type,
sign_type
}
let xml = (await rp({
url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
method: 'POST',
body: getXml(xmlArgs)
}))
if (xml.indexOf('prepay_id') < 0) return xml
let prepay_id = xml.split("<prepay_id><![CDATA[")[1].split("]]></prepay_id>")[0]
let payArgs = {
appid,
noncestr,
partnerid: mch_id,
prepayid:prepay_id,
package: 'Sign=WXPay',
timestamp,
}
await db.collection("pre_order").add({
data: {
...payArgs,
body,
trade_type,
openid: openid,
unionid:unionid,
orderTime: new Date().getTime(),
payStatus: 0 ,
totalFee:total_fee
}
})
return {
...payArgs,
sign: getSign(payArgs)
}
}