代码提交
This commit is contained in:
@ -16,12 +16,19 @@ const {loading, hideLoading} = useInterface()
|
||||
export const WechatProvider = 'wxpay'
|
||||
|
||||
// 支付类型(后端用)
|
||||
export const ServicePayType = {
|
||||
0: 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
1: 'weixin_applet', // 微信小程序
|
||||
2: 'weixin_app' // 微信APP
|
||||
export const ServiceFrom = {
|
||||
'h5': 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
'weixin': 'weixin_applet', // 微信小程序
|
||||
'app': 'weixin_app' // 微信APP
|
||||
}
|
||||
|
||||
export const ServicePayType = {
|
||||
'h5': 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
'weixin': 'weixin_applet', // 微信小程序
|
||||
'app': 'weixin_app' // 微信APP
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 支付类型(前端用)
|
||||
export const PayType = {
|
||||
@ -116,12 +123,15 @@ async function _chooseEnvToPayment(options) {
|
||||
*/
|
||||
function _appWechatPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: ServicePayType['2'],
|
||||
paytype: ServicePayType['2'],
|
||||
const payData = {
|
||||
from: ServiceFrom['app'],
|
||||
paytype: ServicePayType['app'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
// 兼容性写法:防止电脑端用户支付后马上关闭支付弹窗导致失败
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
const orderInfo = {
|
||||
appid: res.appId, // 微信开放平台审核通过的移动应用AppID 。
|
||||
prepayid: res.merchant_id, // 请填写商户号mchid对应的值。
|
||||
@ -148,13 +158,15 @@ function _appWechatPay(payInfo) {
|
||||
*/
|
||||
function _miniProgramPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: ServicePayType['1'],
|
||||
paytype: ServicePayType['1'],
|
||||
const payData = {
|
||||
from: ServiceFrom['weixin'],
|
||||
paytype: ServiceFrom['weixin'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
// 兼容性写法:防止电脑端用户支付后马上关闭支付弹窗导致失败
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
uni.requestPayment({
|
||||
timeStamp: res.timeStamp,
|
||||
nonceStr: res.nonceStr,
|
||||
@ -177,12 +189,13 @@ function _miniProgramPay(payInfo) {
|
||||
*/
|
||||
async function _h5InWechatPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
const payData = {
|
||||
from: ServiceFrom['h5'],
|
||||
paytype: ServicePayType['h5'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
/** 注册JS SDK */
|
||||
jweixin.config({
|
||||
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
||||
@ -203,6 +216,7 @@ async function _h5InWechatPay(payInfo) {
|
||||
return reject(createMessage('微信版本过低,请升级微信版本', error))
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
/** 去拉起微信支付 */
|
||||
jweixin.chooseWXPay({
|
||||
timestamp: res.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
|
||||
@ -212,16 +226,6 @@ async function _h5InWechatPay(payInfo) {
|
||||
paySign: res.paySign, // 支付签名
|
||||
success: (res) => {
|
||||
// 支付成功后的回调函数
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(
|
||||
{
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
uni: payInfo.orderId
|
||||
}
|
||||
))
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({url: '/pages/payStatus/index'})
|
||||
}, 3000)
|
||||
return resolve(createMessage('用户支付成功', res))
|
||||
},
|
||||
cancel: (r) => {
|
||||
@ -246,20 +250,15 @@ async function _h5InWechatPay(payInfo) {
|
||||
* @private
|
||||
*/
|
||||
async function _h5OutWechatPay(payInfo) {
|
||||
const res = await _doWechatPayRequest({
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
const payData = {
|
||||
from: ServiceFrom['h5'],
|
||||
paytype: ServicePayType['h5'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
if (res && res.mwebUrl) {
|
||||
// 缓存支付订单数据
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(
|
||||
{
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
uni: payInfo.orderId
|
||||
}
|
||||
))
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
location.replace(res.mwebUrl)
|
||||
return Promise.resolve(createMessage('用户支付成功', {type: 'h5'}))
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user