123 lines
5.1 KiB
JavaScript
123 lines
5.1 KiB
JavaScript
import { gminiPayAPI } from "@/api/card/index"
|
||
class Payment{
|
||
// 微信支付类型
|
||
WeChat = 1;
|
||
// 支付宝支付类型
|
||
Alipay = 2;
|
||
// 开始支付
|
||
/**
|
||
* @param {String} order_sn 订单编号 必传
|
||
* @param {String} OrderType 订单类型 必传
|
||
* @param {String} pending 支付时需要做什么 选填
|
||
* @param {String} payType 支付类型 APP中内嵌H5必填-支付宝支付还是微信支付
|
||
* @param {Object} parameters 其余参数 需要携带的其余参数
|
||
*/
|
||
_Payment_(data){
|
||
const {order_sn,OrderType,pending=()=>{},payType = 1,parameters = {}} = data;
|
||
/**
|
||
* 对付款前做什么处理
|
||
* 因为有前车之鉴不同场景的微信支付需要调用不同的支付接口
|
||
* 比如:
|
||
* 场景A需要调用 /chwl.pay/api1 接口, 场景B景需要调用 /chwl.pay/api2 接口, 传入的参数也不同,
|
||
* 所以如果存在以上情况则可以在_beforePayment_函数中对支付前判断做一些处理
|
||
* 若不存在以上情况,则可以直接在_beforePayment_中直接处理即可
|
||
*/
|
||
// this._beforePayment_({order_sn,OrderType,pending,payType,...parameters}).then(res => {
|
||
// 检查是否是微信支付还时内嵌内嵌H5
|
||
if (getApp()['globalData']['parameters']?.isWechat) {
|
||
const miniPayRequest = {
|
||
// timeStamp: encodeURIComponent(res['data']['miniPayRequest']?.timestamp || res['data']['miniPayRequest']?.timeStamp),
|
||
// nonceStr: encodeURIComponent(res['data']['miniPayRequest']['nonceStr']),
|
||
// package: encodeURIComponent(res['data']['miniPayRequest']['package']),
|
||
// signType: encodeURIComponent(res['data']['miniPayRequest']['signType']),
|
||
// paySign: encodeURIComponent(res['data']['miniPayRequest']['paySign']),
|
||
// 订单号
|
||
order_sn,
|
||
OrderType,
|
||
...parameters
|
||
};
|
||
// console.log(res,getApp()['globalData']['parameters']?.isWechat,wx.miniProgram,miniPayRequest,'支付前参数')
|
||
|
||
// 带着支付参数进入微信支付页面
|
||
wx.miniProgram.navigateTo({
|
||
url: `/pages/wxpay/h5pay/newpay?data=${encodeURIComponent(JSON.stringify({ order_sn, OrderType}))}`
|
||
});
|
||
// 延迟n秒后H5内部跳转
|
||
// 延迟两秒后执行一些逻辑,就是在跳转微信调起微信支付的过程中执行什么,
|
||
// 一般都是在调起微信支付的时候先跳转至支付失败或者成功的页面,然后微信中的支付方法会在支付完成或者失败后退页,
|
||
// 或者在调起微信支付的时候先跳转至订单详情页面然后再页面onshow生命周期中执行重新获取订单信息方法来更新支付状态
|
||
setTimeout(()=>{
|
||
pending();
|
||
},2000)
|
||
|
||
} else {
|
||
// 打开微信
|
||
openMiniProgram({
|
||
path: `pages/wxpay/h5pay/newpay?data=${encodeURIComponent(JSON.stringify({ order_sn, OrderType}))}`,
|
||
id: 'gh_faaea9b90759',
|
||
// type: 'release',
|
||
type: 'preview'
|
||
});
|
||
}
|
||
|
||
}
|
||
|
||
|
||
// 付款前需要做什么处理
|
||
_beforePayment_(data){
|
||
console.log('进入_beforePayment_');
|
||
console.log(data['order_sn'],'支付参数order_sn');
|
||
console.log(data['OrderType'],'支付参数OrderType');
|
||
console.log(data['pending'],'支付参数pending');
|
||
console.log(data['payType'],'支付参数payType');
|
||
console.log(data['parameters'],'支付参数parameters');
|
||
console.log(getApp()['globalData']['parameters']?.isWechat,'支付参数isWechat');
|
||
if(getApp()['globalData']['parameters']?.isWechat){
|
||
// return gminiPayAPI({order_sn:data['order_sn'],type:data['OrderType']})
|
||
return Promise.resolve()
|
||
}else{
|
||
// console.log(data['payType'],'支付方法');
|
||
// // APP中的微信支付与微信支付
|
||
// // 微信支付参数
|
||
// const paypara = {
|
||
// order_no:data['order_sn'],
|
||
// type:data['type'],
|
||
// redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${data['order_sn']}&type=${data['type']}&redirect_url=${data['redirect_url']}`
|
||
// };
|
||
// if(data['payType'] == this['WeChat']) return pay_wechat_h5API(paypara);
|
||
// // 支付宝可以直接跳转请求的后台接口,也可以请求后台接口获取表单然后提交提交
|
||
// if(data['payType'] == this['Alipay']) return Promise.resolve({
|
||
// data:{redirect_url:`https://mall.agrimedia.cn/api/pay_alipay_h5?order_no=${data['order_sn']}&redirect_url=${this.pay_statepage(paypara,`${getApp()['globalData']['urlfileName']}/pages/pay/index`)}`}
|
||
// })
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @支付回调统一管理方法
|
||
* @查询订单状态完成之后的回调地址redirect_url
|
||
*/
|
||
pay_statepage(parameters, redirect_url) {
|
||
// console.log(order_sn,'pay_statepage方法参数');
|
||
let url = '';
|
||
url += redirect_url;
|
||
console.log(url, '判断跳转链接');
|
||
/**
|
||
* @拼接参数
|
||
*/
|
||
console.log('拼接参数')
|
||
if (typeof parameters !== 'undefined') {
|
||
for (let key in parameters) {
|
||
if (url.indexOf('?') == -1) {
|
||
url += `?${key}=${parameters[key]}`;
|
||
continue;
|
||
};
|
||
url += `&${key}=${parameters[key]}`
|
||
}
|
||
};
|
||
console.log(url, '地址');
|
||
return url;
|
||
}
|
||
}
|
||
|
||
export default new Payment();
|