394 行
9.2 KiB
JavaScript
394 行
9.2 KiB
JavaScript
import sheep from '@/sheep';
|
|
// #ifdef H5
|
|
import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
|
|
// #endif
|
|
import {
|
|
getRootUrl
|
|
} from '@/sheep/helper';
|
|
|
|
/**
|
|
* 支付
|
|
*
|
|
* @param {String} payment = ['wechat','alipay','wallet','offline'] - 支付方式
|
|
* @param {String} orderType = ['goods','recharge','groupon'] - 订单类型
|
|
* @param {String} orderSN - 订单号
|
|
*/
|
|
|
|
export default class SheepPay {
|
|
constructor(payment, orderType, orderSN, options = {}) {
|
|
this.payment = payment;
|
|
this.orderSN = orderSN;
|
|
this.orderType = orderType;
|
|
this.options = options || {};
|
|
this.payAction();
|
|
}
|
|
|
|
payAction() {
|
|
const payAction = {
|
|
WechatOfficialAccount: {
|
|
wechat: () => {
|
|
this.wechatOfficialAccountPay();
|
|
},
|
|
alipay: () => {
|
|
this.redirectPay(); // 现在公众号可以直接跳转支付宝页面
|
|
},
|
|
money: () => {
|
|
this.moneyPay();
|
|
},
|
|
offline: () => {
|
|
this.offlinePay();
|
|
}
|
|
},
|
|
WechatMiniProgram: {
|
|
wechat: () => {
|
|
this.wechatMiniProgramPay();
|
|
},
|
|
alipay: () => {
|
|
this.copyPayLink();
|
|
},
|
|
money: () => {
|
|
this.moneyPay();
|
|
},
|
|
offline: () => {
|
|
this.offlinePay();
|
|
}
|
|
},
|
|
App: {
|
|
wechat: () => {
|
|
this.wechatAppPay();
|
|
},
|
|
alipay: () => {
|
|
this.alipay();
|
|
},
|
|
money: () => {
|
|
this.moneyPay();
|
|
},
|
|
offline: () => {
|
|
this.offlinePay();
|
|
}
|
|
},
|
|
H5: {
|
|
wechat: () => {
|
|
this.wechatWapPay();
|
|
},
|
|
alipay: () => {
|
|
this.redirectPay();
|
|
},
|
|
money: () => {
|
|
this.moneyPay();
|
|
},
|
|
offline: () => {
|
|
this.offlinePay();
|
|
}
|
|
},
|
|
};
|
|
return payAction[sheep.$platform.name][this.payment]();
|
|
}
|
|
|
|
// 预支付
|
|
prepay() {
|
|
return new Promise((resolve, reject) => {
|
|
let data = {
|
|
order_sn: this.orderSN,
|
|
payment: this.payment,
|
|
};
|
|
if (this.options && Number(this.options.money) > 0) {
|
|
data.money = Number(this.options.money);
|
|
}
|
|
const localOpenid = uni.getStorageSync('openid');
|
|
if (localOpenid) {
|
|
data.openid = localOpenid;
|
|
}
|
|
sheep.$api.pay.prepay(data).then((res) => {
|
|
if (res && res.code === 1) {
|
|
resolve(res);
|
|
} else {
|
|
if (res && res.data === -1 && res.msg === 'miss_openid') {
|
|
uni.showModal({
|
|
title: '微信支付',
|
|
content: '未检测到微信授权绑定,请先绑定微信后再进行支付',
|
|
confirmText: '前往绑定',
|
|
success: async (modalRes) => {
|
|
if (modalRes.confirm) {
|
|
try {
|
|
const bindResult = await sheep.$platform.useProvider('wechat').bind();
|
|
if (bindResult) {
|
|
sheep.$helper.toast('微信绑定成功');
|
|
}
|
|
} catch (bindErr) {
|
|
console.log('绑定微信取消或失败', bindErr);
|
|
}
|
|
}
|
|
},
|
|
});
|
|
}
|
|
reject(res);
|
|
}
|
|
}).catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
// #ifdef H5
|
|
// 微信公众号JSSDK支付
|
|
async wechatOfficialAccountPay() {
|
|
let that = this;
|
|
try {
|
|
let {
|
|
code,
|
|
data,
|
|
msg
|
|
} = await this.prepay();
|
|
if (code !== 1) {
|
|
console.log('支付错误', msg);
|
|
return;
|
|
}
|
|
if (!data?.pay_data) {
|
|
that.payResult('success');
|
|
return;
|
|
}
|
|
$wxsdk.wxpay(data.pay_data, {
|
|
success: () => {
|
|
that.payResult('success');
|
|
},
|
|
cancel: () => {
|
|
sheep.$helper.toast('支付已手动取消');
|
|
},
|
|
fail: () => {
|
|
that.payResult('fail');
|
|
},
|
|
});
|
|
} catch (e) {
|
|
console.log('预支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
//浏览器微信H5支付
|
|
async wechatWapPay() {
|
|
try {
|
|
const {
|
|
code,
|
|
data
|
|
} = await this.prepay();
|
|
if (code === 1) {
|
|
if (!data?.pay_data) {
|
|
this.payResult('success');
|
|
return;
|
|
}
|
|
const redirect_url = `${getRootUrl()}pages/pay/result?orderSN=${this.orderSN}&payment=${this.payment
|
|
}&orderType=${this.orderType}`;
|
|
location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
|
|
}
|
|
} catch (e) {
|
|
console.log('预支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 支付链接
|
|
async redirectPay() {
|
|
try {
|
|
let {
|
|
code,
|
|
data
|
|
} = await this.prepay();
|
|
if (code === 1) {
|
|
if (!data?.pay_data) {
|
|
this.payResult('success');
|
|
return;
|
|
}
|
|
const redirect_url = `${getRootUrl()}pages/pay/result?orderSN=${this.orderSN}&payment=${this.payment
|
|
}&orderType=${this.orderType}`;
|
|
location.href = data.pay_data + encodeURIComponent(redirect_url);
|
|
}
|
|
} catch (e) {
|
|
console.log('预支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
// #endif
|
|
|
|
// 微信小程序支付
|
|
async wechatMiniProgramPay() {
|
|
let that = this;
|
|
try {
|
|
let result = await this.prepay();
|
|
if (result.code !== 1) {
|
|
const errMsg = result?.msg || '预支付发起失败,请重试';
|
|
sheep.$helper.toast(errMsg);
|
|
return;
|
|
}
|
|
if (!result.data?.pay_data) {
|
|
that.payResult('success');
|
|
return;
|
|
}
|
|
const payData = result.data.pay_data;
|
|
// 严格按微信小程序原生参数规范整理,确保 timeStamp 为 string,过滤多余字段
|
|
const paymentParams = {
|
|
provider: 'wxpay',
|
|
timeStamp: String(payData.timeStamp || ''),
|
|
nonceStr: String(payData.nonceStr || ''),
|
|
package: String(payData.package || ''),
|
|
signType: payData.signType || 'RSA',
|
|
paySign: String(payData.paySign || ''),
|
|
success: (res) => {
|
|
that.payResult('success');
|
|
},
|
|
fail: (err) => {
|
|
console.error('微信支付调用失败:', err);
|
|
if (err.errMsg === 'requestPayment:fail cancel') {
|
|
sheep.$helper.toast('支付已手动取消');
|
|
} else {
|
|
// 提示具体错误原因,方便用户与管理员定位
|
|
const errHint = err.errMsg ? err.errMsg.replace('requestPayment:fail', '').trim() : '';
|
|
if (errHint) {
|
|
uni.showModal({
|
|
title: '支付拉取失败',
|
|
content: '微信支付未成功调起:' + errHint,
|
|
showCancel: false,
|
|
confirmText: '我知道了',
|
|
success: () => {
|
|
that.payResult('fail');
|
|
}
|
|
});
|
|
} else {
|
|
that.payResult('fail');
|
|
}
|
|
}
|
|
},
|
|
};
|
|
uni.requestPayment(paymentParams);
|
|
} catch (e) {
|
|
if (e?.msg !== 'miss_openid') {
|
|
console.error('预支付未完成异常:', e);
|
|
const hint = e?.msg || (typeof e === 'string' ? e : '支付请求失败,请稍后重试');
|
|
sheep.$helper.toast(hint);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 余额支付
|
|
async moneyPay() {
|
|
try {
|
|
const {
|
|
code
|
|
} = await this.prepay();
|
|
code === 1 && this.payResult('success');
|
|
} catch (e) {
|
|
console.log('余额支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 货到付款
|
|
async offlinePay() {
|
|
try {
|
|
const {
|
|
code
|
|
} = await this.prepay();
|
|
code === 1 && this.payResult('success');
|
|
} catch (e) {
|
|
console.log('货到付款未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 支付宝复制链接支付
|
|
async copyPayLink() {
|
|
let that = this;
|
|
try {
|
|
let {
|
|
code,
|
|
data
|
|
} = await this.prepay();
|
|
if (code === 1) {
|
|
if (!data?.pay_data) {
|
|
that.payResult('success');
|
|
return;
|
|
}
|
|
// 引入showModal 点击确认 复制链接;
|
|
uni.showModal({
|
|
title: '支付宝支付',
|
|
content: '复制链接到外部浏览器',
|
|
confirmText: '复制链接',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
sheep.$helper.copyText(data.pay_data);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.log('复制支付链接未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 支付宝支付
|
|
async alipay() {
|
|
let that = this;
|
|
try {
|
|
const {
|
|
code,
|
|
data
|
|
} = await this.prepay();
|
|
if (code === 1) {
|
|
if (!data?.pay_data) {
|
|
that.payResult('success');
|
|
return;
|
|
}
|
|
uni.requestPayment({
|
|
provider: 'alipay',
|
|
orderInfo: data.pay_data, //支付宝订单数据
|
|
success: (res) => {
|
|
that.payResult('success');
|
|
},
|
|
fail: (err) => {
|
|
if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {
|
|
sheep.$helper.toast('支付已手动取消');
|
|
} else {
|
|
that.payResult('fail');
|
|
}
|
|
},
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.log('支付宝预支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 微信支付
|
|
async wechatAppPay() {
|
|
let that = this;
|
|
try {
|
|
let {
|
|
code,
|
|
data
|
|
} = await this.prepay();
|
|
if (code === 1) {
|
|
if (!data?.pay_data) {
|
|
that.payResult('success');
|
|
return;
|
|
}
|
|
uni.requestPayment({
|
|
provider: 'wxpay',
|
|
orderInfo: data.pay_data, //微信订单数据(官方说是string。实测为object)
|
|
success: (res) => {
|
|
that.payResult('success');
|
|
},
|
|
fail: (err) => {
|
|
err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail');
|
|
},
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.log('微信App预支付未完成:', e);
|
|
}
|
|
}
|
|
|
|
// 支付结果跳转,success:成功,fail:失败
|
|
payResult(resultType) {
|
|
sheep.$router.redirect('/pages/pay/result', {
|
|
orderSN: this.orderSN,
|
|
payment: this.payment, //重新支付的时候使用
|
|
payState: resultType,
|
|
orderType: this.orderType,
|
|
});
|
|
}
|
|
} |