oak-pay-business/lib/utils/pay.js

107 lines
3.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDepositRatio = exports.payNotify = exports.fullPayProjection = void 0;
const tslib_1 = require("tslib");
const payClazz_1 = require("./payClazz");
const assert_1 = tslib_1.__importDefault(require("assert"));
const uuid_1 = require("oak-domain/lib/utils/uuid");
const PayConfig_1 = require("../types/PayConfig");
exports.fullPayProjection = {
id: 1,
applicationId: 1,
price: 1,
meta: 1,
iState: 1,
channel: 1,
paid: 1,
refunded: 1,
timeoutAt: 1,
forbidRefundAt: 1,
orderId: 1,
order: {
id: 1,
title: 1,
desc: 1,
},
accountId: 1,
};
async function payNotify(context, body, payId, headers) {
const [pay] = await context.select('pay', {
data: {
id: 1,
price: 1,
iState: 1,
channel: 1,
meta: 1,
applicationId: 1,
},
filter: {
id: payId,
}
}, {});
if (!pay) {
console.error('接受到无效的payId', payId);
return;
}
const { applicationId, channel, price } = pay;
const payClazz = await (0, payClazz_1.getPayClazz)(applicationId, channel, context);
const { payId: payId2, iState, extra } = await payClazz.decodePayNotification({
headers,
}, body);
(0, assert_1.default)(payId2 === payId);
if (iState !== pay.iState) {
let action = 'close';
const updateData = {};
switch (iState) {
case 'closed': {
// action = 'close';
break;
}
case 'paid': {
action = 'succeedPaying';
updateData.paid = price;
if (extra) {
Object.assign(updateData, extra);
}
break;
}
default: {
(0, assert_1.default)(false);
}
}
await context.operate('pay', {
id: await (0, uuid_1.generateNewIdAsync)(),
action,
data: updateData,
filter: {
id: payId,
}
}, {});
}
return;
}
exports.payNotify = payNotify;
/**
* 计算充值的损耗比例
* @param context
* @param channel
* @param application
*/
function getDepositRatio(channel, application) {
const { payConfig, system } = application;
const { payConfig: systemPayConfig } = system;
const accountConfig = systemPayConfig.find(ele => ele.channel === PayConfig_1.PAY_CHANNEL_ACCOUNT_NAME);
const { depositLoss, depositLossRatio } = accountConfig;
if (!depositLoss) {
return 0;
}
if (depositLossRatio) {
return depositLossRatio;
}
const config = systemPayConfig?.find(ele => ele.channel === channel) || payConfig?.find(ele => ele.channel === channel);
(0, assert_1.default)(config && PayConfig_1.PAY_WECHAT_CHANNELS.includes(channel));
const { lossRatio } = config;
return lossRatio || 0;
}
exports.getDepositRatio = getDepositRatio;