|
@@ -31,43 +31,29 @@ class PayService extends CrudService {
|
|
|
if (!payWay) throw new BusinessError(ErrorCode.DATA_INVALID, '该支付方式暂时无法使用');
|
|
|
const order = await this.orderModel.findById(order_id);
|
|
|
if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单数据');
|
|
|
- const { no, pay } = order;
|
|
|
+ const { no, pay, stauts } = order;
|
|
|
+ let rePayTimes = 0;
|
|
|
+ if (stauts === '1') throw new BusinessError(ErrorCode.DATA_EXISTED, '订单已支付,无需重复支付');
|
|
|
// 检查是否有支付信息如果有的话,支付方式是否一致
|
|
|
if (_.isObject(pay) && Object.keys(pay).length > 0) {
|
|
|
- const pay_type = _.get(pay, 'pay_type');
|
|
|
- // 支付方式不同,则将支付内容刷掉,重新进行数据生成 => 关闭之前的订单
|
|
|
- if (pay_type === type) {
|
|
|
- // 支付方式相同,则再找有没有支付订单
|
|
|
- const pay_no = _.get(pay, 'pay_no');
|
|
|
- if (pay_no) {
|
|
|
- // 有支付订单.则先查支付订单是否可以继续支付
|
|
|
- // TODO:
|
|
|
- // 1.当前用户和之前下单支付的用户的openid是不是一个人.是一个人=>3;不是一个人=>2
|
|
|
- // 2.需要关闭之前的支付订单并且重新进行支付: 关闭订单.再放到下面
|
|
|
- // 3.查询订单是否过期 没过期=>4; 过期=>5
|
|
|
- // 4.继续支付:取出数据,return;
|
|
|
- // 5.放到下面.重新支付
|
|
|
- const openid = _.get(this.ctx, 'user.openid');
|
|
|
- const pay_openid = _.get(pay, 'openid');
|
|
|
- if (pay_openid === openid) {
|
|
|
- // 检查订单是否可以支付
|
|
|
- const canPay = await this.search(pay.pay_no);
|
|
|
- console.log(canPay);
|
|
|
- // if (canPay) {
|
|
|
- // // 继续支付
|
|
|
- // return;
|
|
|
- // }
|
|
|
- await this.closeOrder(pay);
|
|
|
- } else await this.closeOrder(pay);
|
|
|
- }
|
|
|
- // 没有的话,就说明数据有问题.按照没有支付信息处理即可
|
|
|
- } else await this.closeOrder(pay);
|
|
|
+ const pay_no = _.get(pay, 'pay_no');
|
|
|
+ if (pay_no) {
|
|
|
+ // 有支付订单.则先查支付订单是否可以继续支付
|
|
|
+ // TODO:
|
|
|
+ // 1.如果有支付信息及支付单号,直接关闭.重开
|
|
|
+ const arr = pay_no.split('-');
|
|
|
+ // 获取重支付的次数,重支付次数+1
|
|
|
+ let last = _.last(arr);
|
|
|
+ last = parseInt(last);
|
|
|
+ rePayTimes = (last || 0) + 1;
|
|
|
+ }
|
|
|
+ await this.closeOrder(pay);
|
|
|
}
|
|
|
// 没有支付信息(要是有支付信息,上面直接return了.漏下来的都是没有的处理方案)
|
|
|
const str = this.ctx.service.util.trade.createNonceStr();
|
|
|
const arr = no.split('-');
|
|
|
// 订单中pay的信息
|
|
|
- const payObject = { pay_type: type, pay_no: `${_.last(arr)}-${str}` };
|
|
|
+ const payObject = { pay_type: type, pay_no: `${_.last(arr)}-${str}-${rePayTimes}` };
|
|
|
const totalMoney = this.getOrderNeedPay(order);
|
|
|
payObject.pay_money = totalMoney;
|
|
|
let payData;
|
|
@@ -129,11 +115,17 @@ class PayService extends CrudService {
|
|
|
* @param {Object} pay 支付信息
|
|
|
*/
|
|
|
async closeOrder(pay) {
|
|
|
- const { pay_type } = pay;
|
|
|
+ const { pay_type, pay_no } = pay;
|
|
|
if (pay_type === '0') {
|
|
|
// 微信支付方式
|
|
|
+ const params = { config: this.appConfig, order_no: pay_no };
|
|
|
+ const url = `${this.wxDomain}/pay/closeOrder`;
|
|
|
+ const res = await this.httpUtil.cpost(url, params);
|
|
|
+ return res || 'ok';
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 微信支付:整理出uniapp需要的数据
|
|
|
* @param {Object} data 微信接口的数据
|
|
@@ -225,6 +217,7 @@ class PayService extends CrudService {
|
|
|
const wxRefundReq = await this.httpUtil.cpost(url, params);
|
|
|
return wxRefundReq || 'ok';
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
module.exports = PayService;
|