第一次提交
This commit is contained in:
91
core/payment/alipay.js
Executable file
91
core/payment/alipay.js
Executable file
@@ -0,0 +1,91 @@
|
||||
import platform from '@/core/platform'
|
||||
import ClientEnum from '@/common/enum/Client'
|
||||
import { PayMethodEnum } from '@/common/enum/payment'
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于H5)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsH5 = option => {
|
||||
const options = { formHtml: '', ...option }
|
||||
// 跳转到支付宝支付页
|
||||
return new Promise((resolve, reject) => {
|
||||
// console.log(options.formHtml)
|
||||
if (options.formHtml) {
|
||||
const div = document.createElement('div')
|
||||
div.innerHTML = options.formHtml
|
||||
document.body.appendChild(div)
|
||||
document.forms[0].submit()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于APP)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsApp = options => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.requestPayment({
|
||||
provider: 'alipay',
|
||||
orderInfo: options.orderInfo,
|
||||
success(res) {
|
||||
// isRequireQuery 是否需要主动查单
|
||||
// outTradeNo 交易订单号
|
||||
const option = {
|
||||
isRequireQuery: true,
|
||||
outTradeNo: options.out_trade_no,
|
||||
method: 'alipay'
|
||||
}
|
||||
resolve({ res, option })
|
||||
},
|
||||
fail: res => reject(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 获取支付完成后跳转的url
|
||||
// #ifdef H5
|
||||
const returnUrl = () => {
|
||||
return window.location.href
|
||||
}
|
||||
// #endif
|
||||
|
||||
/**
|
||||
* 统一下单API
|
||||
*/
|
||||
export const payment = (option) => {
|
||||
const events = {
|
||||
[ClientEnum.H5.value]: paymentAsH5,
|
||||
[ClientEnum.APP.value]: paymentAsApp
|
||||
}
|
||||
return events[platform](option)
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一下单API需要的扩展数据
|
||||
*/
|
||||
export const extraAsUnify = () => {
|
||||
const extra = {}
|
||||
// #ifdef H5
|
||||
extra.returnUrl = returnUrl()
|
||||
// #endif
|
||||
return extra
|
||||
}
|
||||
|
||||
// H5端支付宝支付下单时的数据
|
||||
// 用于从支付宝支付页返回到收银台页面后拿到下单数据
|
||||
// #ifdef H5
|
||||
export const performance = () => {
|
||||
const query = getCurrentQuery()
|
||||
if (query.method && query.method === 'alipay.trade.wap.pay.return') {
|
||||
return { method: PayMethodEnum.ALIPAY.value, outTradeNo: query.out_trade_no }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const getCurrentQuery = () => {
|
||||
const pages = getCurrentPages()
|
||||
return pages[pages.length - 1].$route.query
|
||||
}
|
||||
// #endif
|
||||
4
core/payment/index.js
Executable file
4
core/payment/index.js
Executable file
@@ -0,0 +1,4 @@
|
||||
import * as Alipay from './alipay'
|
||||
import * as Wechat from './wechat'
|
||||
|
||||
export { Alipay, Wechat }
|
||||
180
core/payment/wechat.js
Normal file
180
core/payment/wechat.js
Normal file
@@ -0,0 +1,180 @@
|
||||
import platform from '@/core/platform'
|
||||
import storage from '@/utils/storage'
|
||||
import ClientEnum from '@/common/enum/Client'
|
||||
import { PayMethodEnum } from '@/common/enum/payment'
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于微信小程序)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsWxMp = option => {
|
||||
const options = {
|
||||
timeStamp: '',
|
||||
nonceStr: '',
|
||||
package: '',
|
||||
signType: '',
|
||||
paySign: '',
|
||||
...option
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
timeStamp: options.timeStamp,
|
||||
nonceStr: options.nonceStr,
|
||||
package: options.package,
|
||||
signType: options.signType,
|
||||
paySign: options.paySign,
|
||||
success(res) {
|
||||
const option = {
|
||||
isRequireQuery: true, // 是否需要主动查单
|
||||
outTradeNo: options.out_trade_no, // 交易订单号
|
||||
method: 'wechat'
|
||||
}
|
||||
resolve({ res, option })
|
||||
},
|
||||
fail: res => reject(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于H5)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsH5 = option => {
|
||||
const options = { orderKey: null, mweb_url: '', h5_url: '', ...option }
|
||||
// 记录下单的信息
|
||||
storage.set('tempUnifyData_' + options.orderKey, {
|
||||
method: PayMethodEnum.WECHAT.value,
|
||||
outTradeNo: options.out_trade_no
|
||||
}, 60 * 60)
|
||||
// 跳转到微信支付页
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = options.mweb_url || options.h5_url
|
||||
if (url) {
|
||||
window.location.href = url
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于微信公众号)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsOfficial = option => {
|
||||
const options = {
|
||||
appId: '',
|
||||
timeStamp: '',
|
||||
nonceStr: '',
|
||||
package: '',
|
||||
signType: '',
|
||||
paySign: '',
|
||||
...option
|
||||
}
|
||||
return onBridgeReady(options)
|
||||
if (platform === ClientEnum.H5_WEIXIN.value) {
|
||||
if (typeof WeixinJSBridge != 'undefined') {
|
||||
return onBridgeReady(options)
|
||||
}
|
||||
if (document.addEventListener) {
|
||||
document.addEventListener('WeixinJSBridgeReady', () => onBridgeReady(options), false)
|
||||
} else if (document.attachEvent) {
|
||||
document.attachEvent('WeixinJSBridgeReady', () => onBridgeReady(options))
|
||||
document.attachEvent('onWeixinJSBridgeReady', () => onBridgeReady(options))
|
||||
}
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
reject({ message: '当前不支持WeixinJSBridge11' })
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起支付请求 (用于APP)
|
||||
* @param {Object} option 参数
|
||||
*/
|
||||
const paymentAsApp = options => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
orderInfo: {
|
||||
partnerid: options.partnerid,
|
||||
appid: options.appid,
|
||||
package: 'Sign=WXPay',
|
||||
noncestr: options.noncestr,
|
||||
sign: options.sign,
|
||||
prepayid: options.prepayid,
|
||||
timestamp: options.timestamp
|
||||
},
|
||||
success(res) {
|
||||
// isRequireQuery 是否需要主动查单
|
||||
// outTradeNo 交易订单号
|
||||
resolve({ res, option: { isRequireQuery: true, outTradeNo: options.out_trade_no, method: 'wechat' } })
|
||||
},
|
||||
fail: res => reject(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 微信公众号调用微信支付
|
||||
// #ifdef H5
|
||||
const onBridgeReady = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
WeixinJSBridge.invoke(
|
||||
'getBrandWCPayRequest', {
|
||||
appId: options.appId,
|
||||
timeStamp: options.timeStamp,
|
||||
nonceStr: options.nonceStr,
|
||||
package: options.package,
|
||||
signType: options.signType,
|
||||
paySign: options.paySign,
|
||||
},
|
||||
res => {
|
||||
if (res.err_msg == "get_brand_wcpay_request:ok") {
|
||||
const option = {
|
||||
isRequireQuery: true, // 是否需要主动查单
|
||||
outTradeNo: options.out_trade_no, // 交易订单号
|
||||
method: 'wechat'
|
||||
}
|
||||
resolve({ res, option })
|
||||
} else {
|
||||
reject(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
// #endif
|
||||
|
||||
/**
|
||||
* 统一下单API
|
||||
*/
|
||||
export const payment = (option) => {
|
||||
const events = {
|
||||
[ClientEnum.H5.value]: paymentAsH5,
|
||||
[ClientEnum.MP_WEIXIN.value]: paymentAsWxMp,
|
||||
[ClientEnum.H5_WEIXIN.value]: paymentAsOfficial,
|
||||
[ClientEnum.APP.value]: paymentAsApp
|
||||
}
|
||||
return events[platform](option)
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一下单API需要的扩展数据
|
||||
*/
|
||||
export const extraAsUnify = () => {
|
||||
return {}
|
||||
}
|
||||
|
||||
// H5端微信支付下单时的数据
|
||||
// 用于从微信支付页返回到收银台页面后拿到下单数据
|
||||
// #ifdef H5
|
||||
export const performance = (orderKey) => {
|
||||
if (window.performance && window.performance.navigation.type == 2) {
|
||||
const tempUnifyData = storage.get('tempUnifyData_' + orderKey)
|
||||
if (tempUnifyData) {
|
||||
storage.remove('tempUnifyData_' + orderKey)
|
||||
return tempUnifyData
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
// #endif
|
||||
Reference in New Issue
Block a user