|
@@ -19,6 +19,7 @@ import {
|
|
|
import * as md5 from 'md5';
|
|
|
import { PayeeAddressEntity } from '../../../payment/entity/payee_address';
|
|
|
import { EasyOpenService } from '../open';
|
|
|
+import { WebHookCommonService } from '../webhook_utils/common';
|
|
|
|
|
|
/**
|
|
|
* 开户管理
|
|
@@ -62,6 +63,9 @@ export class applicationsService extends BaseService {
|
|
|
@InjectEntityModel(OpenApplicationsEntity)
|
|
|
openApplicationsEntity: Repository<OpenApplicationsEntity>;
|
|
|
|
|
|
+ @Inject()
|
|
|
+ webHookCommonService: WebHookCommonService;
|
|
|
+
|
|
|
async addApplications(params, { fail, ok }) {
|
|
|
const mchInFo = await this.openAccountEntity.findOne({
|
|
|
where: {
|
|
@@ -112,35 +116,39 @@ export class applicationsService extends BaseService {
|
|
|
查询账户余额信息
|
|
|
*/
|
|
|
async getApplicationsListByMchId() {
|
|
|
- const merchantInfo = await this.getMerchantInfo();
|
|
|
- const bank_accounts_res = await this.easyPayAdapter.request(
|
|
|
- 'GET',
|
|
|
- `/v1/bank_accounts`,
|
|
|
- {
|
|
|
- page: 1,
|
|
|
- size: 10,
|
|
|
- account_id: merchantInfo.account_id,
|
|
|
- }
|
|
|
- );
|
|
|
- const balances_res = await this.easyPayAdapter.request(
|
|
|
- 'GET',
|
|
|
- `/v3/accounts/${merchantInfo.account_id}/balances`
|
|
|
- );
|
|
|
- bank_accounts_res.data = bank_accounts_res.data.map(elm => {
|
|
|
- let amount = 0;
|
|
|
- if (balances_res.data.length > 0) {
|
|
|
- balances_res.data.forEach(item => {
|
|
|
- if (!amount && item.currency === elm.currency) {
|
|
|
- amount = item.amount;
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- return {
|
|
|
- ...elm,
|
|
|
- amount,
|
|
|
- };
|
|
|
- });
|
|
|
- return bank_accounts_res;
|
|
|
+ try {
|
|
|
+ const merchantInfo = await this.getMerchantInfo();
|
|
|
+ const bank_accounts_res = await this.easyPayAdapter.request(
|
|
|
+ 'GET',
|
|
|
+ `/v1/bank_accounts`,
|
|
|
+ {
|
|
|
+ page: 1,
|
|
|
+ size: 10,
|
|
|
+ account_id: merchantInfo.account_id,
|
|
|
+ }
|
|
|
+ );
|
|
|
+ const balances_res = await this.easyPayAdapter.request(
|
|
|
+ 'GET',
|
|
|
+ `/v3/accounts/${merchantInfo.account_id}/balances`
|
|
|
+ );
|
|
|
+ bank_accounts_res.data = bank_accounts_res.data.map(elm => {
|
|
|
+ let amount = 0;
|
|
|
+ if (balances_res.data.length > 0) {
|
|
|
+ balances_res.data.forEach(item => {
|
|
|
+ if (!amount && item.currency === elm.currency) {
|
|
|
+ amount = item.amount;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ ...elm,
|
|
|
+ amount,
|
|
|
+ };
|
|
|
+ });
|
|
|
+ return bank_accounts_res;
|
|
|
+ } catch (error) {
|
|
|
+ console.log(147, error);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
async getTransactionsListByMchId(params) {
|
|
@@ -188,6 +196,42 @@ export class applicationsService extends BaseService {
|
|
|
// 换汇
|
|
|
async exchanges(params) {
|
|
|
const merchantInfo = await this.getMerchantInfo();
|
|
|
+ // 获取指定卖出币种余额
|
|
|
+ const sellCurrencyByCurrency = await this.getAccountsBalancesByCurrency(
|
|
|
+ merchantInfo.account_id,
|
|
|
+ params.sell_currency
|
|
|
+ );
|
|
|
+ const buy_amount_exchange_rate = await this.webHookCommonService.exchange_rates_buy_amount(params) // 卖出币种的金额
|
|
|
+ if (buy_amount_exchange_rate > sellCurrencyByCurrency) {
|
|
|
+ this.ctx.status = 400;
|
|
|
+ this.ctx.body = {
|
|
|
+ msg: `${params.sell_currency}不足以兑换${params.buy_currency}${params.buy_amount}`,
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 获取指定卖入币种余额
|
|
|
+ const buyCurrencyByCurrency = await this.getAccountsBalancesByCurrency(
|
|
|
+ merchantInfo.account_id,
|
|
|
+ params.buy_currency
|
|
|
+ );
|
|
|
+ // 获取费率信息
|
|
|
+ const withdrawChannelFee =
|
|
|
+ await this.webHookCommonService.getWithdrawChannelFee({
|
|
|
+ account_id: merchantInfo.account_id,
|
|
|
+ currency: params.buy_currency,
|
|
|
+ order_type: OrderType.EXCHANGE,
|
|
|
+ channel: 'EASYPAY',
|
|
|
+ amount: params.buy_amount,
|
|
|
+ mch_id: merchantInfo.mch_id,
|
|
|
+ });
|
|
|
+ if (buyCurrencyByCurrency / 100 < params.amount + withdrawChannelFee) {
|
|
|
+ this.ctx.status = 400;
|
|
|
+ this.ctx.body = {
|
|
|
+ msg: `换汇之后的${params.buy_currency}余额不足以支付手续费(${withdrawChannelFee}元)`,
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 获取交易之后的汇率金额是否足额
|
|
|
const res = await this.easyPayAdapter.request('POST', `/v1/exchanges`, {
|
|
|
account_id: merchantInfo.account_id,
|
|
|
...params,
|
|
@@ -198,7 +242,6 @@ export class applicationsService extends BaseService {
|
|
|
}
|
|
|
// 转账
|
|
|
async transfer(params) {
|
|
|
- console.log(186, params);
|
|
|
const to_merchantInfo = await this.getMerchantInfo(params.to_mch_id);
|
|
|
const merchantInfo = await this.getMerchantInfo();
|
|
|
|
|
@@ -212,6 +255,30 @@ export class applicationsService extends BaseService {
|
|
|
amount: params.amount * 100,
|
|
|
purpose: params.purpose,
|
|
|
};
|
|
|
+
|
|
|
+ // 获取指定币种余额
|
|
|
+ const balancesByCurrency = await this.getAccountsBalancesByCurrency(
|
|
|
+ merchantInfo.account_id,
|
|
|
+ params.currency
|
|
|
+ );
|
|
|
+ // 获取费率信息
|
|
|
+ const withdrawChannelFee =
|
|
|
+ await this.webHookCommonService.getWithdrawChannelFee({
|
|
|
+ account_id: merchantInfo.account_id,
|
|
|
+ currency: params.currency,
|
|
|
+ order_type: OrderType.TRANSFER,
|
|
|
+ channel: 'EASYPAY',
|
|
|
+ amount: params.amount,
|
|
|
+ mch_id: merchantInfo.mch_id,
|
|
|
+ });
|
|
|
+ if (balancesByCurrency / 100 < params.amount + withdrawChannelFee) {
|
|
|
+ this.ctx.status = 400;
|
|
|
+ this.ctx.body = {
|
|
|
+ msg: '余额不足以支付手续费',
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
const res = await this.easyPayAdapter.request(
|
|
|
'POST',
|
|
|
`/v1/transfers`,
|
|
@@ -239,6 +306,28 @@ export class applicationsService extends BaseService {
|
|
|
id: params.beneficiary_id,
|
|
|
},
|
|
|
});
|
|
|
+ // 获取指定币种余额
|
|
|
+ const balancesByCurrency = await this.getAccountsBalancesByCurrency(
|
|
|
+ merchantInfo.account_id,
|
|
|
+ params.currency
|
|
|
+ );
|
|
|
+ // 获取费率信息
|
|
|
+ const withdrawChannelFee =
|
|
|
+ await this.webHookCommonService.getWithdrawChannelFee({
|
|
|
+ account_id: merchantInfo.account_id,
|
|
|
+ currency: params.currency,
|
|
|
+ order_type: OrderType.PAYMENT,
|
|
|
+ channel: 'EASYPAY',
|
|
|
+ amount: params.amount,
|
|
|
+ mch_id: merchantInfo.mch_id,
|
|
|
+ });
|
|
|
+ if (balancesByCurrency / 100 < params.amount + withdrawChannelFee) {
|
|
|
+ this.ctx.status = 400;
|
|
|
+ this.ctx.body = {
|
|
|
+ msg: '余额不足以支付手续费',
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
const paymentsParams = {
|
|
|
request_id: md5(
|
|
@@ -289,17 +378,18 @@ export class applicationsService extends BaseService {
|
|
|
|
|
|
// amount 单位为分
|
|
|
async getAccountsBalancesByCurrency(account_id, currency) {
|
|
|
- const balances = await this.easyPayAdapter.request(
|
|
|
+ const balances = await this.easyPayAdapter.request(
|
|
|
'GET',
|
|
|
`/v3/accounts/${account_id}/balances`,
|
|
|
{}
|
|
|
);
|
|
|
let amount = 0;
|
|
|
+ // console.log(334, balances, account_id, currency);
|
|
|
balances.data.forEach(elm => {
|
|
|
- if(elm.currency === currency && !amount ) {
|
|
|
- amount = elm.amount
|
|
|
+ if (elm.currency === currency && !amount) {
|
|
|
+ amount = elm.amount;
|
|
|
}
|
|
|
- })
|
|
|
- return amount
|
|
|
+ });
|
|
|
+ return amount;
|
|
|
}
|
|
|
}
|