|
@@ -17,6 +17,8 @@ import { EasyPayAdapter } from '../../payment/adapter/easypay.adapter';
|
|
import * as md5 from 'md5';
|
|
import * as md5 from 'md5';
|
|
import { OpenUserEntity } from '../entity/open_user';
|
|
import { OpenUserEntity } from '../entity/open_user';
|
|
import { applicationsService } from './admin/applications';
|
|
import { applicationsService } from './admin/applications';
|
|
|
|
+import { PaymentSuccessService } from './webhook_utils/payment_success';
|
|
|
|
+import { WebHookCommonService } from './webhook_utils/common';
|
|
|
|
|
|
/**
|
|
/**
|
|
* 描述
|
|
* 描述
|
|
@@ -47,12 +49,18 @@ export class OpenApiWebhookService extends BaseService {
|
|
@Inject()
|
|
@Inject()
|
|
applicationsService: applicationsService;
|
|
applicationsService: applicationsService;
|
|
|
|
|
|
|
|
+ @Inject()
|
|
|
|
+ webHookCommonService: WebHookCommonService;
|
|
|
|
+
|
|
@Inject()
|
|
@Inject()
|
|
sunPayAdapter: SunPayAdapter;
|
|
sunPayAdapter: SunPayAdapter;
|
|
|
|
|
|
@Inject()
|
|
@Inject()
|
|
easyPayAdapter: EasyPayAdapter;
|
|
easyPayAdapter: EasyPayAdapter;
|
|
|
|
|
|
|
|
+ @Inject()
|
|
|
|
+ paymentSuccessService: PaymentSuccessService;
|
|
|
|
+
|
|
@Inject()
|
|
@Inject()
|
|
ctx;
|
|
ctx;
|
|
|
|
|
|
@@ -125,6 +133,7 @@ export class OpenApiWebhookService extends BaseService {
|
|
return this.exchange_success(params);
|
|
return this.exchange_success(params);
|
|
case 'payment_success':
|
|
case 'payment_success':
|
|
// 付款成功 payment_success
|
|
// 付款成功 payment_success
|
|
|
|
+ return this.paymentSuccessService.run(params);
|
|
// 如果付款成功,则收取手续费
|
|
// 如果付款成功,则收取手续费
|
|
/*
|
|
/*
|
|
params
|
|
params
|
|
@@ -160,7 +169,7 @@ export class OpenApiWebhookService extends BaseService {
|
|
break;
|
|
break;
|
|
case 'transfer_success':
|
|
case 'transfer_success':
|
|
// 转账延迟2.2秒处理
|
|
// 转账延迟2.2秒处理
|
|
- await this.waitByTime(2200);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(2200);
|
|
// 转账成功 transfer_success
|
|
// 转账成功 transfer_success
|
|
// 如果付款成功,则收取手续费
|
|
// 如果付款成功,则收取手续费
|
|
/*
|
|
/*
|
|
@@ -193,21 +202,12 @@ export class OpenApiWebhookService extends BaseService {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
// 转账延迟4秒处理
|
|
// 转账延迟4秒处理
|
|
- await this.waitByTime(4000);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(4000);
|
|
this.ctx.status = 400;
|
|
this.ctx.status = 400;
|
|
this.ctx.body = {};
|
|
this.ctx.body = {};
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- // 定时器
|
|
|
|
- waitByTime(settime = 100) {
|
|
|
|
- return new Promise((resolve: any) => {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- resolve();
|
|
|
|
- }, settime);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// 入账成功 的手续费处理
|
|
// 入账成功 的手续费处理
|
|
async deposit_success(params) {
|
|
async deposit_success(params) {
|
|
// 获取回调用户的详情
|
|
// 获取回调用户的详情
|
|
@@ -245,33 +245,37 @@ export class OpenApiWebhookService extends BaseService {
|
|
}
|
|
}
|
|
*/
|
|
*/
|
|
// 获取回调用户的详情
|
|
// 获取回调用户的详情
|
|
- accountInfo = await this.getAccountInfo(params.data.account_id);
|
|
|
|
|
|
+ accountInfo = await this.webHookCommonService.getAccountInfo(
|
|
|
|
+ params.data.account_id
|
|
|
|
+ );
|
|
if (!accountInfo) {
|
|
if (!accountInfo) {
|
|
// TODO 如果不存在的话,则为白标用户
|
|
// TODO 如果不存在的话,则为白标用户
|
|
// 转账延迟4秒处理
|
|
// 转账延迟4秒处理
|
|
- await this.waitByTime(4000);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(4000);
|
|
this.ctx.status = 400;
|
|
this.ctx.status = 400;
|
|
this.ctx.body = {};
|
|
this.ctx.body = {};
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取费率信息
|
|
// 获取费率信息
|
|
- withdrawChannelFee = await this.getWithdrawChannelFee({
|
|
|
|
- account_id: params.data.account_id,
|
|
|
|
- currency: params.data.currency,
|
|
|
|
- order_type: 'DEPOSIT',
|
|
|
|
- channel: 'EASYPAY',
|
|
|
|
- amount: params.data.amount,
|
|
|
|
- mch_id: accountInfo.mch_id,
|
|
|
|
- });
|
|
|
|
- // 查询用户的资金
|
|
|
|
|
|
+ withdrawChannelFee =
|
|
|
|
+ await this.webHookCommonService.getWithdrawChannelFee({
|
|
|
|
+ account_id: params.data.account_id,
|
|
|
|
+ currency: params.data.currency,
|
|
|
|
+ order_type: OrderType.DEPOSIT,
|
|
|
|
+ channel: 'EASYPAY',
|
|
|
|
+ amount: params.data.amount,
|
|
|
|
+ mch_id: accountInfo.mch_id,
|
|
|
|
+ });
|
|
|
|
|
|
// 获取余额
|
|
// 获取余额
|
|
- const { before_balance, balance } = await this.getBalanceDiffByCurrency({
|
|
|
|
- account_id: params.data.account_id,
|
|
|
|
- currency: params.data.currency,
|
|
|
|
- amount: params.data.amount,
|
|
|
|
- });
|
|
|
|
|
|
+ const { before_balance, balance } =
|
|
|
|
+ await this.webHookCommonService.getBalanceDiffByCurrency({
|
|
|
|
+ account_id: params.data.account_id,
|
|
|
|
+ currency: params.data.currency,
|
|
|
|
+ amount: params.data.amount,
|
|
|
|
+ order_type: OrderType.DEPOSIT,
|
|
|
|
+ });
|
|
// 记录入账流水
|
|
// 记录入账流水
|
|
const openPaymentOrderParams = {
|
|
const openPaymentOrderParams = {
|
|
mch_id: accountInfo.mch_id,
|
|
mch_id: accountInfo.mch_id,
|
|
@@ -294,11 +298,9 @@ export class OpenApiWebhookService extends BaseService {
|
|
this.logger.info(
|
|
this.logger.info(
|
|
`记录入账流水, ${JSON.stringify(openPaymentOrderParams)}`
|
|
`记录入账流水, ${JSON.stringify(openPaymentOrderParams)}`
|
|
);
|
|
);
|
|
- this.ctx.status = 200;
|
|
|
|
- this.ctx.body = {};
|
|
|
|
// 通知上游之后,间隔10秒执行利润截取操作
|
|
// 通知上游之后,间隔10秒执行利润截取操作
|
|
Promise.resolve().then(async () => {
|
|
Promise.resolve().then(async () => {
|
|
- await this.waitByTime(10000);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(10000);
|
|
let accountInfo = null;
|
|
let accountInfo = null;
|
|
// 获取回调用户的详情
|
|
// 获取回调用户的详情
|
|
let withdrawChannelFee = null;
|
|
let withdrawChannelFee = null;
|
|
@@ -342,6 +344,8 @@ export class OpenApiWebhookService extends BaseService {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+ this.ctx.status = 200;
|
|
|
|
+ this.ctx.body = {};
|
|
return;
|
|
return;
|
|
} catch (error) {
|
|
} catch (error) {
|
|
this.logger.error(`记录入账流水失败, ${JSON.stringify(params)}`);
|
|
this.logger.error(`记录入账流水失败, ${JSON.stringify(params)}`);
|
|
@@ -381,32 +385,37 @@ export class OpenApiWebhookService extends BaseService {
|
|
*/
|
|
*/
|
|
|
|
|
|
// 获取回调用户的详情
|
|
// 获取回调用户的详情
|
|
- const accountInfo = await this.getAccountInfo(params.data.account_id);
|
|
|
|
|
|
+ const accountInfo = await this.webHookCommonService.getAccountInfo(
|
|
|
|
+ params.data.account_id
|
|
|
|
+ );
|
|
if (!accountInfo) {
|
|
if (!accountInfo) {
|
|
// TODO 如果不存在的话,暂时不处理
|
|
// TODO 如果不存在的话,暂时不处理
|
|
// 转账延迟4秒处理
|
|
// 转账延迟4秒处理
|
|
- await this.waitByTime(4000);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(4000);
|
|
this.ctx.status = 400;
|
|
this.ctx.status = 400;
|
|
this.ctx.body = {};
|
|
this.ctx.body = {};
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取费率信息
|
|
// 获取费率信息
|
|
- const withdrawChannelFee = await this.getWithdrawChannelFee({
|
|
|
|
- account_id: params.data.account_id,
|
|
|
|
- currency: params.data.buy_currency,
|
|
|
|
- order_type: 'EXCHANGE',
|
|
|
|
- channel: 'EASYPAY',
|
|
|
|
- amount: params.data.buy_amount,
|
|
|
|
- mch_id: accountInfo.mch_id,
|
|
|
|
- });
|
|
|
|
|
|
+ const withdrawChannelFee =
|
|
|
|
+ await this.webHookCommonService.getWithdrawChannelFee({
|
|
|
|
+ account_id: params.data.account_id,
|
|
|
|
+ currency: params.data.buy_currency,
|
|
|
|
+ order_type: OrderType.EXCHANGE,
|
|
|
|
+ channel: 'EASYPAY',
|
|
|
|
+ amount: params.data.buy_amount,
|
|
|
|
+ mch_id: accountInfo.mch_id,
|
|
|
|
+ });
|
|
|
|
|
|
// 获取余额
|
|
// 获取余额
|
|
- const { before_balance, balance } = await this.getBalanceDiffByCurrency({
|
|
|
|
- account_id: params.data.account_id,
|
|
|
|
- currency: params.data.buy_currency,
|
|
|
|
- amount: params.data.buy_amount,
|
|
|
|
- });
|
|
|
|
|
|
+ const { before_balance, balance } =
|
|
|
|
+ await this.webHookCommonService.getBalanceDiffByCurrency({
|
|
|
|
+ account_id: params.data.account_id,
|
|
|
|
+ currency: params.data.buy_currency,
|
|
|
|
+ amount: params.data.buy_amount,
|
|
|
|
+ order_type: OrderType.EXCHANGE,
|
|
|
|
+ });
|
|
|
|
|
|
// 记录入账流水
|
|
// 记录入账流水
|
|
const openPaymentOrderParams = {
|
|
const openPaymentOrderParams = {
|
|
@@ -434,7 +443,7 @@ export class OpenApiWebhookService extends BaseService {
|
|
);
|
|
);
|
|
// 通知上游之后,间隔10秒执行利润截取操作
|
|
// 通知上游之后,间隔10秒执行利润截取操作
|
|
Promise.resolve().then(async () => {
|
|
Promise.resolve().then(async () => {
|
|
- await this.waitByTime(10000);
|
|
|
|
|
|
+ await this.webHookCommonService.waitByTime(10000);
|
|
// 收取手续费: 换汇
|
|
// 收取手续费: 换汇
|
|
// /v1/transfers
|
|
// /v1/transfers
|
|
const transfers_params = {
|
|
const transfers_params = {
|
|
@@ -472,7 +481,7 @@ export class OpenApiWebhookService extends BaseService {
|
|
fee: 0,
|
|
fee: 0,
|
|
additional_info: {},
|
|
additional_info: {},
|
|
});
|
|
});
|
|
- })
|
|
|
|
|
|
+ });
|
|
this.ctx.status = 200;
|
|
this.ctx.status = 200;
|
|
this.ctx.body = {};
|
|
this.ctx.body = {};
|
|
return;
|
|
return;
|
|
@@ -481,65 +490,4 @@ export class OpenApiWebhookService extends BaseService {
|
|
this.logger.error(error);
|
|
this.logger.error(error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 如果查询都是空的延迟处理
|
|
|
|
- async getAccountInfo(account_id) {
|
|
|
|
- const openAccount = await this.openAccountEntity.findOne({
|
|
|
|
- where: {
|
|
|
|
- account_id,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
- if (openAccount) {
|
|
|
|
- return openAccount;
|
|
|
|
- }
|
|
|
|
- const openUser = await this.openUserEntity.findOne({
|
|
|
|
- where: {
|
|
|
|
- account_id,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
- if (openUser) {
|
|
|
|
- return openUser;
|
|
|
|
- }
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- async getWithdrawChannelFee({
|
|
|
|
- account_id,
|
|
|
|
- currency,
|
|
|
|
- order_type,
|
|
|
|
- channel,
|
|
|
|
- amount,
|
|
|
|
- mch_id,
|
|
|
|
- }) {
|
|
|
|
- const withdrawChannel = await this.withdrawChannelEntity.findOne({
|
|
|
|
- where: {
|
|
|
|
- // account_id: account_id,
|
|
|
|
- channel,
|
|
|
|
- mch_id: mch_id,
|
|
|
|
- order_type,
|
|
|
|
- currency: currency,
|
|
|
|
- status: 1,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
- // 费率
|
|
|
|
- const fee = (Number.parseInt(withdrawChannel.rate) / 100) * (amount / 100); // 费率费用
|
|
|
|
- // 单笔固定费用
|
|
|
|
- const basicFee = withdrawChannel.basicFee;
|
|
|
|
- // 单笔最低费用
|
|
|
|
- const feeMin = withdrawChannel.feeMin;
|
|
|
|
- return Math.max(fee, basicFee, feeMin); // 取最大值
|
|
|
|
- }
|
|
|
|
- // 获取流水记录前后的余额差异
|
|
|
|
-
|
|
|
|
- async getBalanceDiffByCurrency({ account_id, currency, amount }) {
|
|
|
|
- const userBalance =
|
|
|
|
- await this.applicationsService.getAccountsBalancesByCurrency(
|
|
|
|
- account_id,
|
|
|
|
- currency
|
|
|
|
- );
|
|
|
|
- return {
|
|
|
|
- before_balance: userBalance,
|
|
|
|
- balance: userBalance + amount,
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
}
|
|
}
|