2023-05-26 11:32:01 +08:00
|
|
|
|
import util from 'utils/util'
|
|
|
|
|
import store from '@/store'
|
|
|
|
|
|
|
|
|
|
const payTypes = ['WXPAY', 'ALIPAY']
|
|
|
|
|
/**
|
|
|
|
|
* 统一支付处理方法
|
|
|
|
|
* @param payInfo // 支付下单信息
|
|
|
|
|
* @param payType // 支付类型
|
|
|
|
|
* @param createOrderFun // 预下单方法
|
2023-05-26 16:28:53 +08:00
|
|
|
|
* @param payResultHandle // 支付参数返回处理方法,不同的业务逻辑下单后返回的支付参数字段可能不一样,需要处理成统一格式
|
|
|
|
|
* {
|
|
|
|
|
jsapiResult: '', 微信jsapi支付参数
|
|
|
|
|
body: '', 支付宝支付参数
|
|
|
|
|
}
|
2023-05-26 11:53:18 +08:00
|
|
|
|
* @param wxPaySuccess // 微信支付下单支付成功回调
|
|
|
|
|
* @param aliPaySuccess // 支付宝支付下单成功回调
|
2023-05-26 11:32:01 +08:00
|
|
|
|
*/
|
|
|
|
|
let _options = {
|
|
|
|
|
payInfo: {},
|
|
|
|
|
payType: '',
|
|
|
|
|
createOrderFun: () => {},
|
2023-05-26 16:28:53 +08:00
|
|
|
|
payResultHandle: (res) => res,
|
2023-05-26 11:32:01 +08:00
|
|
|
|
wxPaySuccess: () => {},
|
|
|
|
|
aliPaySuccess: () => {}
|
|
|
|
|
}
|
2023-05-29 18:31:06 +08:00
|
|
|
|
|
|
|
|
|
const unityPay = (options) => {
|
|
|
|
|
|
2023-05-26 11:32:01 +08:00
|
|
|
|
Object.assign(_options, options)
|
|
|
|
|
if (!_options.payType) {
|
|
|
|
|
return util.Tips({
|
|
|
|
|
title: '请选择支付方式'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if (!payTypes.includes(_options.payType)) {
|
|
|
|
|
throw new Error('支付方式参数值为 WXPAY、ALIPAY')
|
|
|
|
|
}
|
|
|
|
|
// 目前微信只支持JSAPI,h5支付开通后取消该验证
|
|
|
|
|
const openId = store.state.app.openId || ''
|
|
|
|
|
if (_options.payType === 'WXPAY' && !openId) {
|
|
|
|
|
return util.Tips({
|
|
|
|
|
title: '请在微信客户端进行支付操作'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
_options.payInfo.openid = openId
|
2023-05-26 17:06:03 +08:00
|
|
|
|
if (_options.payType === 'WXPAY') {
|
2023-05-29 09:51:19 +08:00
|
|
|
|
_options.payInfo.payChannel = 'public'
|
2023-05-26 17:06:03 +08:00
|
|
|
|
} else if (_options.payType === 'ALIPAY') {
|
2023-05-29 09:51:19 +08:00
|
|
|
|
_options.payInfo.payChannel = 'appAliPay'
|
2023-05-26 17:06:03 +08:00
|
|
|
|
}
|
2023-05-26 11:32:01 +08:00
|
|
|
|
uni.showLoading({
|
|
|
|
|
title: '支付中',
|
|
|
|
|
mask: true
|
|
|
|
|
})
|
|
|
|
|
_options.createOrderFun({
|
|
|
|
|
..._options.payInfo,
|
|
|
|
|
payType: _options.payType
|
|
|
|
|
}).then(res => {
|
2023-05-26 16:28:53 +08:00
|
|
|
|
const payConfig = _options.payResultHandle(res.data)
|
2023-05-26 11:32:01 +08:00
|
|
|
|
switch (_options.payType) {
|
|
|
|
|
// 微信JSAPI支付
|
|
|
|
|
case 'WXPAY':
|
|
|
|
|
wxPayJSAPI(payConfig.jsapiResult)
|
|
|
|
|
break
|
|
|
|
|
case 'ALIPAY':
|
|
|
|
|
_options.aliPaySuccess(payConfig.body)
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-05-29 21:28:55 +08:00
|
|
|
|
}).catch((error) => {
|
2023-05-26 11:32:01 +08:00
|
|
|
|
util.Tips({
|
2023-05-29 21:28:55 +08:00
|
|
|
|
title: error
|
2023-05-26 11:32:01 +08:00
|
|
|
|
})
|
|
|
|
|
}).finally(uni.hideLoading)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wxPayJSAPI = (payData) => {
|
|
|
|
|
function onBridgeReady() {
|
|
|
|
|
WeixinJSBridge.invoke('getBrandWCPayRequest', {
|
|
|
|
|
"appId": payData.appId, //公众号ID,由商户传入
|
|
|
|
|
"timeStamp": payData.timeStamp, //时间戳,自1970年以来的秒数
|
|
|
|
|
"nonceStr": payData.nonceStr, //随机串
|
|
|
|
|
"package": payData.packageValue,
|
|
|
|
|
"signType": payData.signType, //微信签名方式:
|
|
|
|
|
"paySign": payData.paySign //微信签名
|
|
|
|
|
},
|
|
|
|
|
function(res) {
|
|
|
|
|
if (res.err_msg == "get_brand_wcpay_request:ok") {
|
|
|
|
|
_options.wxPaySuccess()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if (typeof WeixinJSBridge == "undefined") {
|
|
|
|
|
if (document.addEventListener) {
|
|
|
|
|
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
|
|
|
|
|
} else if (document.attachEvent) {
|
|
|
|
|
document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
|
|
|
|
|
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
onBridgeReady()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default unityPay
|