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

108 lines
4.4 KiB
JavaScript

'use strict';
const notaryPay = require('./util/notaryPay');
module.exports = {
/**
* XXXnameXXX
* @url client/order/kh/makePayList 前端调用的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} = data;
let res = {code: 0, msg: ""};
// 业务逻辑开始-----------------------------------------------------------
// orderStatus 0待支付 1已支付待发货 2已发货待收货 3已收货待完成 4已完成
const order = await vk.baseDao.findById({
dbName: "jjx-order",
id: data.payInfo.orderId
})
const isNotaryPay = !!order && Number(order.type) > 3 && Number(order.signMethod) === 0
let fullPayRecord = null
if (isNotaryPay) {
if (Number(order.notaryPayStatus) !== 1) return {code: -1, msg: '买家尚未完成公证全额支付,暂不能申请付款'}
fullPayRecord = await notaryPay.findFullPayRecord(vk, order._id)
if (!fullPayRecord || !fullPayRecord.hasPaid || !fullPayRecord.orderNo) {
return {code: -1, msg: '未找到已支付的公证全额支付单'}
}
const applyList = await vk.baseDao.select({
dbName: "jjx-order-pay-list",
whereJson: {
orderId: order._id,
isNotaryApply: true,
buyerRefuse: false
},
pageIndex: 1,
pageSize: -1
})
const appliedAmount = (applyList.rows || []).reduce((sum, item) => sum + Number(item.amount || 0), 0)
const nextAmount = Number(data.payInfo.amount || 0)
const fullAmount = Number(fullPayRecord.amount || 0)
if (!Number.isFinite(nextAmount) || nextAmount <= 0) return {code: -1, msg: '申请金额不正确'}
if (Number((appliedAmount + nextAmount).toFixed(2)) > fullAmount) {
return {code: -1, msg: '累计申请金额不能超过买家公证支付总额'}
}
}
const payApplyFields = await notaryPay.buildPayApplyFields(vk)
const dataJson = {
...data.payInfo,
...payApplyFields,
isNotaryPay,
isNotaryApply: isNotaryPay,
sourcePayListId: isNotaryPay ? fullPayRecord._id : '',
sourcePayOrderNo: isNotaryPay ? fullPayRecord.orderNo : '',
fullPayListId: isNotaryPay ? fullPayRecord._id : '',
fullPayOrderNo: isNotaryPay ? fullPayRecord.orderNo : '',
fullPayAmount: isNotaryPay ? fullPayRecord.amount : '',
fullPayAt: isNotaryPay ? (fullPayRecord.paidAt || '') : '',
hasPaid: isNotaryPay ? true : data.payInfo.hasPaid,
paidAt: isNotaryPay ? (fullPayRecord.paidAt || '') : data.payInfo.paidAt,
paidVoucher: isNotaryPay ? '' : data.payInfo.paidVoucher,
buyerRemark: isNotaryPay ? '公证付款申请,待买家确认划付' : data.payInfo.buyerRemark
}
dataJson.orderNo = vk.pubfn.createOrderNo("39XU", 24)
const payListId = await vk.baseDao.add({
dbName: "jjx-order-pay-list",
dataJson
});
dataJson._id = payListId
let orderStatus = data.payInfo.isExpress ? 2 : 1;
const hasExpress = await vk.baseDao.findByWhereJson({
dbName: "jjx-order-pay-list",
whereJson:{
orderId: data.payInfo.orderId,
isExpress: true,
buyerRefuse: false
}
});
if (hasExpress) orderStatus = 2
// payStatus(弃用) 0待创建 1待支付预付款 2已支付预付款,支付进度款中 3进度款已支付,尾款支付中 4完成
await vk.baseDao.updateById({
dbName: "jjx-order",
id: data.payInfo.orderId,
dataJson: {
orderStatus,
payStatus: isNotaryPay ? 2 : 1
}
});
if (isNotaryPay) {
await notaryPay.notifyPayApply({
vk,
payRecord: dataJson,
order
})
}
// 业务逻辑结束-----------------------------------------------------------
return res;
}
}