UNPKG

rapid-ztx

Version:

Rapid ZTX module

151 lines (135 loc) 4.69 kB
/** * Copyright (c) 2017 Lucky Byte, Inc. */ const debug = require('debug')('ztx'); const uuid = require('uuid'); const moment = require('moment'); const rapid_db = require('rapid-db'); const auth = require('../lib/auth'); const utils = require('../lib/utils'); /** * 绑定贷记卡 * * 参数: * info: 绑卡参数,包含下面的数据: * * sumer: 用户 uuid * * card_no: 卡号 * * cvn: CVN * * expiry: 卡有效期 * * 返回: * 失败抛出异常,成功返回贷记卡完整信息 */ const bind = async (info) => { const keys = [ 'sumer', 'card_no', 'cvn', 'expiry' ]; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!info[key]) { throw new Error(`绑定贷记卡错:缺少参数[${key}]`); } } if (!/^[0-9]{3}$/.test(info.cvn)) { throw new Error('绑定贷记卡错:CVN 格式错'); } if (!/^[0-9]{4}$/.test(info.expiry)) { throw new Error('绑定贷记卡错:卡有效期格式错'); } // 检查卡有效期,有效期格式 MMYY const month = info.expiry.substr(0,2); if (parseInt(month) > 12) { throw new Error('绑定贷记卡错:卡有效期无效(月份错)'); } const year = info.expiry.substr(2); if (parseInt(year) < moment().year() % 100) { throw new Error('绑定贷记卡错:卡已过期'); } // 查询用户信息 const sumer = await unique_db.oneOrNone(` select * from rapid_ctod_sumers where uuid = $1 `, [ info.sumer ]); if (!sumer) { throw new Error('绑定贷记卡错:绑卡用户不存在'); } // 用户未绑定借记卡的情况下,不能绑定贷记卡 if (!sumer.debit_card) { throw new Error('绑定贷记卡错:用户未绑定借记卡'); } // 如果已经绑定过贷记卡,则检查是否重复绑定 if (sumer.credit_cards) { for (let i = 0; i < sumer.credit_cards.length; i++) { if (info.card_no == sumer.credit_cards[i].card_no) { throw new Error('绑定贷记卡错:此卡重复绑定'); } } } // 查询卡 BIN 信息 let bin_info = await rapid_db.cardbin.query(info.card_no); if (!bin_info) { throw new Error('绑定贷记卡错:查询卡 BIN 信息错,请检查卡号'); } // 检查是否为贷记卡,卡 BIN 信息中有此标识 if (bin_info.debit_credit_flag != 2 && bin_info.debit_credit_flag != 3) { throw new Error('绑定贷记卡错:此卡非贷记卡或准贷记卡'); } // 不对测试卡号进行实名认证 if (info.card_no != '6221558812340000') { if (!sumer.debit_card.name || !sumer.debit_card.idno) { throw new Error('绑定贷记卡错:借记卡缺少姓名和证件号码'); } // 银行卡实名认证 await auth.card_auth(sumer, Object.assign(info, { name: sumer.debit_card.name, idno: sumer.debit_card.idno, card_type: bin_info.debit_credit_flag, })); } // 组织贷记卡信息 const credit_card = { name: sumer.debit_card.name, idno: sumer.debit_card.idno, card_no: info.card_no, cvn: info.cvn, expiry: info.expiry, card_type: bin_info.debit_credit_flag, issuer_code: bin_info.issuer_code, issuer_name: bin_info.issuer_name, bank_name: bin_info.issuer_name, card_name: bin_info.card_name, bin_num: bin_info.bin_num, bin_len: bin_info.bin_len, } // 添加到贷记卡列表 let credit_cards = sumer.credit_cards || []; credit_cards.push(credit_card); await unique_db.none(` update rapid_ctod_sumers set credit_cards = $1 where uuid = $2 and debit_card is not null `, [ JSON.stringify(credit_cards), sumer.uuid ]); // 记录绑卡历史 await unique_db.none(` insert into rapid_ctod_bhist ( uuid, sumer, action, card, notes ) values ( $1, $2, $3, $4, $5 ) `, [ uuid.v4(), sumer.uuid, 3, credit_card, '绑定贷记卡' ]); // 给代理商联系人发送邮件通知 try { const html = ` <p>用户[${sumer.name}][${sumer.mobile}]已绑定贷记卡!</p> `; await utils.mail_to_agent(sumer.agent, '用户绑卡通知', html); } catch (err) { await rapid_db.notify.warn(`发送绑卡通知邮件错: ${err.message}`); } return credit_card; } module.exports = bind;