|
@@ -24,30 +24,42 @@ class PayService extends CrudService {
|
|
|
* @param body.order_id 订单id
|
|
|
* @param body.type 支付方式
|
|
|
*/
|
|
|
- async toPayOrder({ order_id, type }) {
|
|
|
+ async toPayOrder({ order_id, type = '0' }) {
|
|
|
const payWay = await this.dictDataModel.findOne({ value: type, status: '0' });
|
|
|
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;
|
|
|
// 检查是否有支付信息如果有的话,支付方式是否一致
|
|
|
- if (Object.keys(pay).length > 0) {
|
|
|
+ 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是不是一个人.是一个人=>2;不是一个人=>3
|
|
|
+ // 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);
|
|
|
}
|
|
|
// 没有支付信息(要是有支付信息,上面直接return了.漏下来的都是没有的处理方案)
|
|
|
const str = this.ctx.service.util.trade.createNonceStr();
|
|
@@ -55,6 +67,9 @@ class PayService extends CrudService {
|
|
|
// 订单中pay的信息
|
|
|
const payObject = { pay_type: type, pay_no: `${_.last(arr)}-${str}` };
|
|
|
const totalMoney = this.getOrderNeedPay(order);
|
|
|
+ payObject.pay_money = totalMoney;
|
|
|
+ let payData;
|
|
|
+ let res;
|
|
|
// 找到当前用户的openid:这里涉及问题是: 如果自己下单,自己付款.那没有问题;
|
|
|
// 如果是自己下单.如果使用账号密码登录再付款.还用下单的人找到的openid就不能在这个微信号上进行支付了;
|
|
|
// 所以此处是需要用当前用户的openid进行支付,如果之前生成单子.还需要检查当前用户的openid和之前的openid是否一致
|
|
@@ -62,8 +77,11 @@ class PayService extends CrudService {
|
|
|
|
|
|
// 请求微信支付接口的数据
|
|
|
if (type === '0') {
|
|
|
- const toPayData = { config: this.appConfig, money: totalMoney, openid: '', order_no: payObject.pay_no, desc: '购物', notice_url: this.payOrderReturnUrl('order_id') };
|
|
|
+ payData = this.getWxPayData(order_id, totalMoney, payObject.pay_no);
|
|
|
+ res = await this.create(payData);
|
|
|
+ res = this.preparToUniAppWxPay(res);
|
|
|
}
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -71,8 +89,48 @@ class PayService extends CrudService {
|
|
|
* @param {Object} param 请求地址参数
|
|
|
* @param param.order 订单id
|
|
|
*/
|
|
|
- async callBackPayOrder({ order }) {}
|
|
|
+ async callBackPayOrder({ order }) {
|
|
|
+ console.log(order);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 关闭订单
|
|
|
+ * @param {Object} pay 支付信息
|
|
|
+ */
|
|
|
+ async closeOrder(pay) {
|
|
|
+ const { pay_type } = pay;
|
|
|
+ if (pay_type === '0') {
|
|
|
+ // 微信支付方式
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 微信支付:整理出uniapp需要的数据
|
|
|
+ * @param {Object} data 微信接口的数据
|
|
|
+ */
|
|
|
+ preparToUniAppWxPay(data) {
|
|
|
+ const obj = {
|
|
|
+ // appid: _.get(data, 'appid'),
|
|
|
+ // prepayid: _.get(data, 'prepay_id'),
|
|
|
+ nonceStr: _.get(data, 'nonceStr'),
|
|
|
+ package: `prepay_id=${_.get(data, 'prepay_id')}`,
|
|
|
+ signType: _.get(data, 'signType'),
|
|
|
+ timeStamp: _.get(data, 'timestamp'),
|
|
|
+ paySign: _.get(data, 'paySign'),
|
|
|
+ };
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组织微信支付数据
|
|
|
+ * @param {String} order_id 订单号
|
|
|
+ * @param {Number} money 支付金额
|
|
|
+ * @param {String} no 支付订单号
|
|
|
+ */
|
|
|
+ getWxPayData(order_id, money, no) {
|
|
|
+ const openid = _.get(this.ctx, 'user.openid');
|
|
|
+ const data = { config: this.appConfig, money, openid, order_no: no, desc: '购物', notice_url: this.payOrderReturnUrl(order_id) };
|
|
|
+ return data;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 计算订单需支付的金额
|
|
@@ -82,7 +140,7 @@ class PayService extends CrudService {
|
|
|
let total = 0;
|
|
|
const { total_detail = {} } = order;
|
|
|
for (const key in total_detail) {
|
|
|
- total = _.floor(total_detail[key] + total);
|
|
|
+ total = _.floor(total_detail[key] + total, 2);
|
|
|
}
|
|
|
return total;
|
|
|
}
|