rapid-ztx
Version:
Rapid ZTX module
147 lines (132 loc) • 4.47 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('ztx');
const uuid = require('uuid');
const rapid_db = require('rapid-db');
const ysepay = require('rapid-ysepay');
/**
* 银行卡实名认证
*/
const card_auth = async (sumer, auth_info) => {
// 检查当日绑卡次数上限
const bhist = await unique_db.one(`
select count(*) as count from rapid_ctod_bhist
where sumer = $1 and
ctime::date = CURRENT_DATE and action in (1,3)
`, [
sumer.uuid
]);
if (bhist.count >= 20) {
await rapid_db.notify.warn(`用户[${sumer.mobile}]绑卡次数达到上限`);
throw new Error('当天绑卡次数已达上限');
}
// 检查当日实名认证次数上限
const auth_list = await unique_db.one(`
select count(*) as count from rapid_auth_list
where mobile = $1 and ctime::date = CURRENT_DATE
`, [
sumer.mobile
]);
if (auth_list.count >= 20) {
await rapid_db.notify.warn(`用户[${sumer.mobile}]实名认证次数达到上限`);
throw new Error('当天实名认证次数已达上限');
}
// 查询实名认证平台商户信息
const merch = await unique_db.one(`
select * from rapid_merchs where uuid = (
select auth_merch from rapid_ctod_params limit 1
)
`);
if (merch.disabled) {
throw new Error('实名认证商户已被禁用');
}
return await card_auth_ysepay(sumer, merch, auth_info);
}
/**
* 通过银盛支付进行实名认证
*/
const card_auth_ysepay = async (sumer, merch, auth_info) => {
const tunnel = await unique_db.one(`
select * from rapid_tunnel limit 1
`);
// 查询通道配置
if (!tunnel.ysepay_url) {
throw new Error('未配置银盛支付服务地址');
}
// 查询银盛商户信息
const ysepay_merch = await unique_db.one(`
select * from rapid_ysepay where uuid = $1
`, [
merch.ysepay_merch
]);
if (ysepay_merch.disabled) {
throw new Error('银盛实名认证商户已被禁用');
}
if (!ysepay_merch.merno || !ysepay_merch.sign_pem ||
!ysepay_merch.verify_cer) {
throw new Error('银盛实名认证商户配置不完整,请检查');
}
const order_id = await rapid_db.new_order_id();
// 发起实名认证交易
const resp = await ysepay.auth.exchange(tunnel.ysepay_url, {
merno: ysepay_merch.merno,
sign_pem: ysepay_merch.sign_pem,
verify_cer: ysepay_merch.verify_cer,
order_id: order_id,
name: auth_info.name,
idno: auth_info.idno,
card_no: auth_info.card_no,
});
const result = card_auth_ysepay_result(resp);
// 记录实名认证流水
await unique_db.none(`
insert into rapid_auth_list (
uuid, order_id, req_pkt, res_pkt,
name, mobile, idno, card_no, card_type, status, notes
) values (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)
`, [
uuid.v4(), order_id, resp.reqt, resp.resp,
auth_info.name, sumer.mobile, auth_info.idno, auth_info.card_no,
auth_info.card_type, result.status, result.notes
]);
if (result.status != 2) {
throw new Error(`实名认证失败:${result.notes}`);
}
}
/**
* 判断银盛实名认证结果
*/
const card_auth_ysepay_result = (resp) => {
let status = 2;
let notes = '认证成功';
const resp_json = resp.resp.ysepay_authenticate_four_key_element_response;
if (!resp_json) {
throw new Error('银盛实名认证响应缺少[ysepay_authenticate_four_key_element_response]');
}
if (resp_json.order_status) {
if (resp_json.order_status.toLowerCase() != 'success') {
if (resp_json.order_status_description) {
notes = resp_json.order_status_description;
} else {
notes = `${resp_json.msg || ''}-${resp_json.sub_msg || ''}`;
}
status = 1;
}
} else if (resp_json.code) {
if (resp_json.code != '10000') {
status = 1;
notes = `${resp_json.msg || ''}-${resp_json.sub_msg || ''}`;
}
} else {
throw new Error('银盛实名认证响应报文缺少判断成功或失败的字段');
}
return {
status: status, notes: notes
}
}
module.exports = {
card_auth: card_auth,
}