统一下单
除付款码支付场景以外,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易会话标识后再按Native、JSAPI、APP等不同场景生成交易串调起支付。 官方JSAPI文档 Native官方文档 官方APP文档 官方H5文档 官方小程序支付文档
js
const { Formatter, Hash } = require('wechatpay-axios-plugin')
// 直连模式 NATIVE 支付场景
wxpay.v2.pay.unifiedorder.post({
mch_id,
appid,
body,
out_trade_no,
notify_url,
total_fee,
trade_type: 'NATIVE',
})
.then(
({
data: {
return_code,
result_code,
trade_type,
code_url,
}
}) => code_url
)
// 直连模式 JSAPI 支付场景
wxpay.v2.pay.unifiedorder.post({
mch_id,
appid,
body,
out_trade_no,
notify_url,
total_fee,
trade_type: 'JSAPI',
sign_type,
})
.then(
({
data: {
return_code,
result_code,
trade_type,
prepay_id,
}
}) => {
const nonceStr = Formatter.nonce();
const timeStamp = '' + Formatter.timestamp();
const packageStr = 'prepay_id=' + prepay_id;
const signType = previousSignType || 'MD5';
return {
appId,
timeStamp,
nonceStr,
package: packageStr,
signType,
paySign: Hash.sign(
signType,
{ appId, timeStamp, nonceStr, package: packageStr, signType },
apiv2Secret
)
}
})
// 直连模式 APP 支付场景
wxpay.v2.pay.unifiedorder.post({
mch_id,
appid,
body,
out_trade_no,
notify_url,
total_fee,
trade_type: 'APP',
sign_type,
})
.then(
({
data: {
return_code,
result_code,
trade_type,
prepay_id: prepayid,
}
}) => {
const noncestr = Formatter.nonce();
const timestamp = '' + Formatter.timestamp();
const packageStr = 'Sign=WXPay';
const signType = previousSignType || 'MD5';
return {
appid,
partnerid,
prepayid,
package: packageStr,
timestamp,
noncestr,
sign: Hash.sign(
signType,
{ appid, partnerid, prepayid, package: packageStr, timestamp, noncestr },
apiv2Secret
)
}
})
// 直连模式 MWEB 支付场景
wxpay.v2.pay.unifiedorder.post({
mch_id,
appid,
body,
scene_info,
out_trade_no,
notify_url,
total_fee,
trade_type: 'MWEB',
})
.then(
({
data: {
return_code,
result_code,
trade_type,
mweb_url,
}
}) => mweb_url
)