提交
This commit is contained in:
165
src/jtools/pay/index.js
Normal file
165
src/jtools/pay/index.js
Normal file
@@ -0,0 +1,165 @@
|
||||
import request from '../request/index.js';
|
||||
// #ifdef H5
|
||||
import wxsdk from '@/jtools/wechat/sdk'
|
||||
// #endif
|
||||
import wechat from '@/jtools/wechat/wechat'
|
||||
import $platform from '@/jtools/platform';
|
||||
|
||||
/**
|
||||
* 支付
|
||||
*
|
||||
* @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
|
||||
* @param {Object} order = {} - 订单详情
|
||||
* @param {String} orderType = ['goods','recharge'] - 订单类型
|
||||
*/
|
||||
|
||||
export default class JtoolsPay {
|
||||
|
||||
|
||||
// wxOfficialAccount wxMiniProgram App H5
|
||||
// wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
|
||||
// alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
|
||||
// wallet v v v v
|
||||
|
||||
|
||||
constructor(payment, order, orderType) {
|
||||
this.payment = payment;
|
||||
this.order = order;
|
||||
this.orderType = orderType;
|
||||
this.platform = $platform.get();
|
||||
let payMehod = this.getPayMethod();
|
||||
payMehod();
|
||||
|
||||
}
|
||||
|
||||
getPayMethod() {
|
||||
var payMethod = {
|
||||
'wxMiniProgram': {
|
||||
'wechat': () => {
|
||||
this.wxMiniProgramPay()
|
||||
},
|
||||
},
|
||||
'App': {
|
||||
'wechat': () => {
|
||||
this.wechatPay()
|
||||
},
|
||||
'alipay': () => {
|
||||
this.aliPay()
|
||||
},
|
||||
},
|
||||
}
|
||||
return payMethod[this.platform][this.payment];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 预支付
|
||||
prepay() {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
const p = $platform.device()
|
||||
const tradeInfoType = p == 'android' ? 'Android' : p == 'ios' ? 'iOS' : 'Wap'
|
||||
let params = {
|
||||
orderId: this.order.orderId,
|
||||
orderPayType: this.order.orderPayType,
|
||||
money: this.order.money,
|
||||
microServiceName: this.order.microServiceName,
|
||||
prepayParamUrl: this.order.prepayParamUrl,
|
||||
paymentType: 'weChatPay',
|
||||
payType: 'JSAPI',
|
||||
tradeInfoType: tradeInfoType,
|
||||
tenantId: '-1',
|
||||
clientType: 'miniWx'
|
||||
}
|
||||
if (uni.getStorageSync('openId')) {
|
||||
params.openId = uni.getStorageSync('openId');
|
||||
}
|
||||
request({
|
||||
url: 'driver-api/applet/pay/prepay',
|
||||
method: 'POST',
|
||||
param,
|
||||
}).then(res => {
|
||||
if (res.code == 'SUCCESS') {
|
||||
resolve(res);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 微信小程序支付
|
||||
async wxMiniProgramPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
const params = result.data.jsApiResult
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
...{
|
||||
appId: params.appId, //公众号名称,由商户传入
|
||||
timeStamp: params.timestamp, //时间戳,自1970年以来的秒数
|
||||
nonceStr: params.nonceStr, //随机串
|
||||
package: `prepay_id=${params.prepay_id}`,
|
||||
signType: params.signType, //微信签名方式:
|
||||
paySign: params.paySign, //微信签名
|
||||
},
|
||||
success: res => {
|
||||
that.payResult('success', result.data.orderPayNo)
|
||||
},
|
||||
fail: err => {
|
||||
console.log('支付取消或者失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 支付宝支付
|
||||
async aliPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
if (result.code === 1) {
|
||||
uni.requestPayment({
|
||||
provider: 'alipay',
|
||||
orderInfo: result.data.pay_data, //支付宝订单数据
|
||||
success: res => {
|
||||
that.payResult('success')
|
||||
},
|
||||
fail: err => {
|
||||
console.log('支付取消或者失败:', err);
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 微信支付
|
||||
async wechatPay() {
|
||||
let that = this;
|
||||
let result = await this.prepay();
|
||||
if (result.code === 1) {
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
|
||||
success: res => {
|
||||
that.payResult('success')
|
||||
},
|
||||
fail: err => {
|
||||
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
|
||||
console.log('支付取消或者失败:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 支付结果跳转,success:成功,fail:失败
|
||||
payResult(resultType, orderPayNo) {
|
||||
const that = this;
|
||||
let path = ''
|
||||
uni.navigateTo({
|
||||
url:path
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user