|
@@ -14,12 +14,16 @@ import { WalletService } from './wallet';
|
|
|
import { MchWithdrawChannelService } from './mchWithdrawChannel';
|
|
|
import { RateService } from './rate';
|
|
|
import { WithdrawStateService } from './withdrawState';
|
|
|
+import { PayService } from './pay';
|
|
|
|
|
|
@Provide()
|
|
|
export class RepayService extends BaseService {
|
|
|
@Inject()
|
|
|
httpService: HttpService;
|
|
|
|
|
|
+ @Inject()
|
|
|
+ payService: PayService;
|
|
|
+
|
|
|
@Inject()
|
|
|
withdrawStateService: WithdrawStateService;
|
|
|
|
|
@@ -50,14 +54,47 @@ export class RepayService extends BaseService {
|
|
|
@Inject()
|
|
|
utils: Utils;
|
|
|
|
|
|
+ async getBanks(payload) {
|
|
|
+ this.logger.info('代付支持银行查询', JSON.stringify(payload));
|
|
|
+ await this.validGetBanksParam(payload);
|
|
|
+ const merchant = await this.payService.validMerchant(payload.mchId);
|
|
|
+ await this.payService.validSign(payload, merchant);
|
|
|
+ const channels = await this.withdrawChannelService.queryByCurrency(payload.currency);
|
|
|
+ const result = await Promise.all(channels.map(item => {
|
|
|
+ return this.dispatchService.getBanks(item, { currency: payload.currency });
|
|
|
+ }))
|
|
|
+ let list = [];
|
|
|
+ result.forEach(item => {
|
|
|
+ list = list.concat(item)
|
|
|
+ })
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ async validGetBanksParam(payload) {
|
|
|
+ if (_.isEmpty(payload.mchId)) {
|
|
|
+ throw new Error('商户号【mchId】不能为空');
|
|
|
+ }
|
|
|
+ if (_.isEmpty(payload.currency)) {
|
|
|
+ throw new Error('货币单位【currency】不能为空');
|
|
|
+ }
|
|
|
+ if (_.isEmpty(payload.sign)) {
|
|
|
+ throw new Error('签名【sign】不能为空');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async queryBalance(payload) {
|
|
|
this.logger.info('余额查询', JSON.stringify(payload));
|
|
|
await this.validQueryBalanceParam(payload);
|
|
|
- const merchant = await this.validMerchant(payload.mchId);
|
|
|
- await this.validSign(payload, merchant);
|
|
|
+ const merchant = await this.payService.validMerchant(payload.mchId);
|
|
|
+ await this.payService.validSign(payload, merchant);
|
|
|
let wallet;
|
|
|
if (payload.currency) {
|
|
|
- wallet = [await this.walletService.queryByMchIdAndCurrency(payload.mchId, payload.currency)];
|
|
|
+ const data = await this.walletService.queryByMchIdAndCurrency(payload.mchId, payload.currency);
|
|
|
+ if (data) {
|
|
|
+ wallet = [data];
|
|
|
+ } else {
|
|
|
+ wallet = [];
|
|
|
+ }
|
|
|
} else {
|
|
|
wallet = await this.walletService.queryByMchId(payload.mchId);
|
|
|
}
|
|
@@ -84,8 +121,8 @@ export class RepayService extends BaseService {
|
|
|
try {
|
|
|
this.logger.info('代付查询', JSON.stringify(payload));
|
|
|
await this.validQueryParam(payload);
|
|
|
- const merchant = await this.validMerchant(payload.mchId);
|
|
|
- await this.validSign(payload, merchant);
|
|
|
+ const merchant = await this.payService.validMerchant(payload.mchId);
|
|
|
+ await this.payService.validSign(payload, merchant);
|
|
|
const order = await this.withdrawService.findByOutOrderNo(
|
|
|
payload.outOrderNo
|
|
|
);
|
|
@@ -124,9 +161,9 @@ export class RepayService extends BaseService {
|
|
|
try {
|
|
|
this.logger.info('代付下单', JSON.stringify(payload));
|
|
|
await this.validParam(payload);
|
|
|
- const merchant = await this.validMerchant(payload.mchId);
|
|
|
+ const merchant = await this.payService.validMerchant(payload.mchId);
|
|
|
const rate = await this.validRate(payload.mchId, payload.currency);
|
|
|
- await this.validSign(payload, merchant);
|
|
|
+ await this.payService.validSign(payload, merchant);
|
|
|
await this.validOutOrderNo(payload.outOrderNo);
|
|
|
const channel = await this.getChannel(merchant.mchId, payload.currency, payload.amount);
|
|
|
let channelCharge = (+payload.amount * +channel.rate) / 100 + +channel.basicFee;
|
|
@@ -146,8 +183,10 @@ export class RepayService extends BaseService {
|
|
|
mchId: merchant.mchId,
|
|
|
amount: payload.amount,
|
|
|
currency: payload.currency,
|
|
|
+ personIdType: payload.personIdType,
|
|
|
+ personId: payload.personId,
|
|
|
+ personName: payload.personName,
|
|
|
accountNo: payload.accountNo,
|
|
|
- accountName: payload.accountName,
|
|
|
bankCode: payload.bankCode,
|
|
|
bankName: payload.bankName,
|
|
|
channelCharge,
|
|
@@ -202,12 +241,18 @@ export class RepayService extends BaseService {
|
|
|
if (_.isEmpty(payload.currency)) {
|
|
|
throw new Error('货币单位【currency】不能为空');
|
|
|
}
|
|
|
+ if (_.isEmpty(payload.personIdType)) {
|
|
|
+ throw new Error('用户证件信息【personIdType】不能为空');
|
|
|
+ }
|
|
|
+ if (_.isEmpty(payload.personId)) {
|
|
|
+ throw new Error('用户证件号【personId】不能为空');
|
|
|
+ }
|
|
|
+ if (_.isEmpty(payload.personName)) {
|
|
|
+ throw new Error('用户姓名【personName】不能为空');
|
|
|
+ }
|
|
|
if (_.isEmpty(payload.accountNo)) {
|
|
|
throw new Error('银行账号【accountNo】不能为空');
|
|
|
}
|
|
|
- if (_.isEmpty(payload.accountName)) {
|
|
|
- throw new Error('收款人姓名【accountName】不能为空');
|
|
|
- }
|
|
|
if (_.isEmpty(payload.bankName)) {
|
|
|
throw new Error('银行名称【bankName】不能为空');
|
|
|
}
|
|
@@ -246,17 +291,6 @@ export class RepayService extends BaseService {
|
|
|
return channel;
|
|
|
}
|
|
|
|
|
|
- async validMerchant(mchId) {
|
|
|
- const merchant = await this.merchantService.getByMchId(mchId);
|
|
|
- if (!merchant) {
|
|
|
- throw new Error('商户不存在');
|
|
|
- }
|
|
|
- if (+merchant.status !== 1) {
|
|
|
- throw new Error('商户状态异常');
|
|
|
- }
|
|
|
- return merchant;
|
|
|
- }
|
|
|
-
|
|
|
async validRate(mchId, currency) {
|
|
|
const rate = await this.rateService.getOne({
|
|
|
mchId,
|
|
@@ -269,26 +303,6 @@ export class RepayService extends BaseService {
|
|
|
return rate;
|
|
|
}
|
|
|
|
|
|
- async validSign(payload, merchant) {
|
|
|
- const data = { ...payload };
|
|
|
- const sign = data.sign;
|
|
|
- delete data.sign;
|
|
|
- const signStr = this.utils.signSort(data);
|
|
|
- const verifySign = md5(signStr + `&key=${merchant.key}`);
|
|
|
- this.logger.info(
|
|
|
- '代付下单加密结果',
|
|
|
- sign,
|
|
|
- signStr,
|
|
|
- verifySign,
|
|
|
- sign === verifySign
|
|
|
- );
|
|
|
- if (sign !== verifySign) {
|
|
|
- throw new Error(
|
|
|
- '验签失败,请确认签名是否正确,正确待签名字符串为:' + signStr
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
async validOutOrderNo(outOrderNo) {
|
|
|
const order = await this.withdrawService.findByOutOrderNo(outOrderNo);
|
|
|
if (order) {
|
|
@@ -299,8 +313,9 @@ export class RepayService extends BaseService {
|
|
|
async validBalance(merchant, currency, amount) {
|
|
|
const wallet = await this.walletService.queryByMchIdAndCurrency(merchant.mchId, currency);
|
|
|
const { balance = 0, freeze = 0 } = wallet;
|
|
|
- if (+amount > +balance - Math.abs(+freeze)) {
|
|
|
- throw new Error('当前商户余额不足,请确认!当前商户可提现余额:' + (+balance - +freeze));
|
|
|
+ const withdrawAmount = (+balance - Math.abs(+freeze)).toFixed(2)
|
|
|
+ if (+amount > +withdrawAmount) {
|
|
|
+ throw new Error('当前商户余额不足,请确认!当前商户可提现余额:' + +withdrawAmount);
|
|
|
}
|
|
|
}
|
|
|
}
|