You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
jwl-applet/src/jtools/pay/index.js

157 lines
3.7 KiB

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';
import {prePay} from '@/jtools/api/pay'
/**
* 支付
*
* @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 = {
"code":this.order.code,
"description": this.order.description,
"money": this.order.money,
"outTradeNo": this.order.outTradeNo,
"userId": this.order.userId
}
if (uni.getStorageSync('openId')) {
params.openId = uni.getStorageSync('openId');
}
prePay(params).then(res => {
if (res.code == '0000') {
resolve(res);
}
})
});
}
// 微信小程序支付
async wxMiniProgramPay() {
let that = this;
let result = await this.prepay();
const params = result.data
uni.requestPayment({
provider: 'wxpay',
...{
appId: params.appId, //公众号名称,由商户传入
timeStamp: params.timeStamp, //时间戳,自1970年以来的秒数
nonceStr: params.nonceStr, //随机串
package: params.packageVal,
signType: params.signType, //微信签名方式:
paySign: params.paySign, //微信签名
},
success: res => {
console.log(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 => {
err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
}
});
}
}
// 微信支付
async wechatPay() {
let that = this;
let result = await this.prepay();
console.log('微信支付');
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 = 'paySuccess'
uni.navigateTo({
url: path
})
}
}