rapid-ztx
Version:
Rapid ZTX module
172 lines (160 loc) • 6.51 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('ztx');
const moment = require('moment');
const rapid_db = require('rapid-db');
/**
* 检查代理商的交易控制
*
* 参数:
* agent_uuid: 商户 uuid
* amount: 交易金额,以分为单位
* card_no: 交易卡号,贷记卡
*
* 失败抛出异常
*/
const check_for_agent = async (agent_uuid, amount, card_no) => {
const agent = await unique_db.one(`
select shortname as name, trctrl
from rapid_ctod_agents where uuid = $1
`, [
agent_uuid
]);
if (!agent.trctrl) {
await rapid_db.notify.error(`代理商[${agent.name}]尚未配置交易控制`);
throw new Error('代理商未配置交易控制,不能进行交易');
}
const trctrl = agent.trctrl;
// 单笔最低/最高金额
if (trctrl.amt_per_min && trctrl.amt_per_min > 0) {
if (amount < trctrl.amt_per_min * 100) {
await rapid_db.notify.warn(
`交易受限:交易额[${amount / 100}]低于代理商[${agent.name}]` +
`配置的单笔最低限额[${trctrl.amt_per_min}]`
);
throw new Error('交易额低于最低限额,不允许交易');
}
}
if (trctrl.amt_per_max && trctrl.amt_per_max > 0) {
if (amount > trctrl.amt_per_max * 100) {
await rapid_db.notify.warn(
`交易受限:交易额[${amount / 100}]高于代理商[${agent.name}]` +
`配置的单笔最高限额[${trctrl.amt_per_max}]`
);
throw new Error('交易额高于最高限额,不允许交易');
}
}
// 交易时间段检查
if (trctrl.time_begin) {
if (moment().isBefore(moment(trctrl.time_begin, 'HH:mm:ss'))) {
await rapid_db.notify.info(
`交易受限:当前时间[${moment().format('HH:mm:ss')}]` +
`不在代理商[${agent.name}]正常交易时间范围内`
);
throw new Error('当前时间非交易时间');
}
}
if (trctrl.time_end) {
if (moment().isAfter(moment(trctrl.time_end, 'HH:mm:ss'))) {
await rapid_db.notify.info(
`交易受限:当前时间[${moment().format('HH:mm:ss')}]` +
`不在代理商[${agent.name}]正常交易时间范围内`
);
throw new Error('当前时间非交易时间');
}
}
// 单卡单日金额上限
if (trctrl.amt_card_max && trctrl.amt_card_max > 0) {
try {
const result = await unique_db.oneOrNone(`
select amount from rapid_card_limit
where date = CURRENT_DATE and card_no = $1
`, [
card_no
]);
if (result) {
const amt_card_max = parseInt(trctrl.amt_card_max) * 100;
if (parseInt(result.amount) + parseInt(amount) > amt_card_max) {
await rapid_db.notify.info(
`交易受限:卡[${card_no}]当日累计交易额[${result.amount / 100}]` +
`加上当前交易额[${amount / 100}]超出代理商[${agent.name}]` +
`单卡单日限额[${amt_card_max / 100}]`
);
throw new Error('单卡单日金额已达上限');
}
}
} catch (err) {
throw new Error(`查询单卡单日交易累计总额错: ${err.message}`);
}
}
// 单卡单日笔数上限
if (trctrl.cnt_card_max && trctrl.cnt_card_max > 0) {
try {
const result = await unique_db.oneOrNone(`
select count from rapid_card_limit
where date = CURRENT_DATE and card_no = $1
`, [
card_no
]);
if (result) {
if (parseInt(result.count) >= trctrl.cnt_card_max) {
await rapid_db.notify.warn(
`交易受限:卡[${card_no}]当日累计交易笔数[${result.count}]超出` +
`代理商[${agent.name}]单卡单日笔数限制[${trctrl.cnt_card_max}]`
);
throw new Error('单卡单日交易笔数已达上限');
}
}
} catch (err) {
throw new Error(`查询单卡单日交易累计总笔数错: ${err.message}`);
}
}
// 当日交易金额上限
if (trctrl.amt_day_max && trctrl.amt_day_max > 0) {
try {
const result = await unique_db.oneOrNone(`
select amount from rapid_agent_limit
where date = CURRENT_DATE and agent = $1
`, [
agent_uuid
]);
if (result) {
const amt_day_max = parseInt(trctrl.amt_day_max) * 100;
if (parseInt(result.amount) + parseInt(amount) > amt_day_max) {
await rapid_db.notify.warn(
`交易受限:代理商[${agent.name}]当日累计交易额` +
`[${result.amount / 100}]加上当前交易额[${amount / 100}]` +
`超出限额[${amt_day_max / 100}]`
);
throw new Error('当日交易金额已达上限');
}
}
} catch (err) {
throw new Error(`查询代理商当日交易累计总额错: ${err.message}`);
}
}
// 当日交易笔数上限
if (trctrl.cnt_day_max && trctrl.cnt_day_max > 0) {
try {
const result = await unique_db.oneOrNone(`
select count from rapid_agent_limit
where date = CURRENT_DATE and agent = $1
`, [
agent_uuid
]);
if (result) {
if (parseInt(result.count) >= trctrl.cnt_day_max) {
await rapid_db.notify.warn(
`交易受限:代理商[${agent.name}]当日累计交易笔数` +
`[${result.count}]超出笔数限制[${trctrl.cnt_day_max}]`
);
throw new Error('当日交易笔数已达上限');
}
}
} catch (err) {
throw new Error(`查询代理商交易当日累计总笔数错: ${err.message}`);
}
}
}
module.exports = check_for_agent;