Files
jjx-wap/uniCloud-tcb/cloudfunctions/router/service/client/order/kh/payRefuse.js
T

206 lines
7.8 KiB
JavaScript

'use strict';
const dayjs = require("dayjs");
const OpenSdk = require('../../../../util/base').default;
const notaryPay = require('./util/notaryPay');
const sdk = new OpenSdk({
appId: '8a81c1db8fc5238d01910b9e312d0692',
appKey: 'a15ceac60f5b464eb758c4fe7800ee9f'
});
const REFUND_MID = '898450100002303';
const REFUND_TID = 'G5SBN2XJ';
const REFUND_INST_MID = 'WGDEFAULT';
const REFUND_SUB_MID = '898450100004218';
const REFUND_SUB_MID_MAIN = '898450100004711';
const PLATFORM_RATE_DICT_ID = 'd84694f2696818f20c28f55f36764783';
module.exports = {
/**
* XXXnameXXX
* @url client/order/kh/payRefuse 前端调用的url参数地址
* data 请求参数
* @param {String} params1 参数1
*/
main: async (event) => {
let {data = {}, userInfo, util, filterResponse, originalParam} = event;
let {customUtil, uniID, config, pubFun, vk, db, _} = util;
let {uid, refuseReason} = data;
let res = {code: 0, msg: ""};
// 业务逻辑开始-----------------------------------------------------------
const payRecord = await vk.baseDao.updateAndReturn({
dbName: "jjx-order-pay-list",
whereJson: {
_id: data.id,
},
dataJson: {
buyerRefuse: true,
refuseReason,
transferStatus: 2,
transferRefuseAt: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
}
})
const order = await vk.baseDao.findById({
dbName: 'jjx-order',
id: payRecord.orderId
})
const isNotaryPay = !!order && Number(order.type) > 3 && Number(order.signMethod) === 0;
if (isNotaryPay && payRecord.isNotaryApply) {
res.refund = {
skipped: true,
reason: '公证付款申请驳回不触发退款'
};
await notaryPay.addMessage(vk, {
uid: payRecord.sellerUid,
title: '付款单被驳回',
content: `订单${order && order.orderNo ? order.orderNo : payRecord.orderId}的${payRecord.type || ''}付款单已被买家驳回,原因:${refuseReason || '未填写'}`,
type: 'order-pay-refuse',
pk: payRecord.orderId
});
return res;
}
if (['预付款', '货到付款'].includes(payRecord.type) && !isNotaryPay) {
await vk.baseDao.updateById({
dbName: "jjx-order",
id: payRecord.orderId,
dataJson: {
payStatus: 0
}
})
}
const hasPay = await vk.baseDao.findByWhereJson({
dbName: "jjx-order-pay-list",
whereJson: {
orderId: payRecord.orderId,
buyerRefuse: false,
sellerConfirm: true
}
})
const hasExpress = await vk.baseDao.findByWhereJson({
dbName: "jjx-order-pay-list",
whereJson: {
orderId: payRecord.orderId,
isExpress: true,
hasPaid: true,
}
})
// orderStatus 0待支付 1已支付待发货 2已发货待收货 3已收货待完成 4已完成
// 发货单已支付的不能再次发货
let orderStatus = 0
// 未发货且没有支付单
if (!hasExpress && !hasPay) orderStatus = 0
// 未发货且有支付单
if (!hasExpress && !!hasPay) orderStatus = 1
// 已发货且已确认支付
if (!!hasExpress) orderStatus = 2
res.hasExpress = hasExpress
res.hasPay = hasPay
await vk.baseDao.updateById({
dbName: "jjx-order",
id: payRecord.orderId,
dataJson: {
orderStatus
}
});
const hasManualVoucher = Array.isArray(payRecord.paidVoucher) ? payRecord.paidVoucher.length > 0 : !!payRecord.paidVoucher;
const refundAmount = Math.round(Number(payRecord.amount || 0) * 100);
const shouldCallRefund = !!payRecord.hasPaid && !hasManualVoucher && !!payRecord.orderNo && refundAmount > 0 && !isNotaryPay;
if (shouldCallRefund) {
const refundOrderId = vk.pubfn.createOrderNo("39XU", 24);
const refundData = {
queryUrl: 'https://api-mop.chinaums.com/v1/netpay/refund',
timestamp: dayjs().format('YYYYMMDDHHmmss'),
nonce: `refund_${Date.now()}`,
content: {
requestTimestamp: dayjs().format('YYYY-MM-DD HH:mm:ss'),
merOrderId: payRecord.orderNo,
instMid: REFUND_INST_MID,
mid: REFUND_MID,
tid: REFUND_TID,
refundAmount,
refundOrderId,
refundDesc: refuseReason || '买家驳回支付单退款'
}
};
try {
const platformRateData = await vk.baseDao.findById({
dbName: 'jjx-dict',
id: PLATFORM_RATE_DICT_ID
});
const platformRate = Number(platformRateData && platformRateData.content);
if (!Number.isNaN(platformRate) && platformRate >= 0 && platformRate <= 100) {
const subAmount = parseInt(refundAmount * ((100 - platformRate) / 100), 10);
const subAmountMain = refundAmount - subAmount;
refundData.content.platformAmount = 0;
refundData.content.subOrders = [
{
mid: REFUND_SUB_MID,
totalAmount: String(subAmount),
},
{
mid: REFUND_SUB_MID_MAIN,
totalAmount: String(subAmountMain),
}
];
}
const refundRawResult = await sdk.exec(refundData);
const refundResult = typeof refundRawResult === "string" ? JSON.parse(refundRawResult) : refundRawResult;
const refundSuccess = refundResult && refundResult.errCode === 'SUCCESS';
await vk.baseDao.updateById({
dbName: "jjx-order-pay-list",
id: payRecord._id,
dataJson: {
refundApplyAt: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss"),
refundApplyPayload: refundData.content,
refundApplyResult: refundResult,
refundApplySuccess: refundSuccess
}
});
res.refund = {
success: refundSuccess,
result: refundResult
};
} catch (err) {
const errMsg = err && err.message ? err.message : JSON.stringify(err);
await vk.baseDao.updateById({
dbName: "jjx-order-pay-list",
id: payRecord._id,
dataJson: {
refundApplyAt: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss"),
refundApplyPayload: refundData.content,
refundApplySuccess: false,
refundApplyError: errMsg
}
});
res.refund = {
success: false,
error: errMsg
};
}
} else {
res.refund = {
skipped: true,
reason: '当前支付单未命中线上退款条件'
};
}
// 业务逻辑结束-----------------------------------------------------------
return res;
}
}