import { BaseService } from '@cool-midway/core'; import { ILogger, Inject, Provide } from '@midwayjs/core'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { PayeeEntity } from '../../../payment/entity/payee'; import { Repository } from 'typeorm'; import { CustomerEntity } from '../../../payment/entity/customer'; import { PaymentService } from '../../../payment/service/payment'; import { SunPayAdapter } from '../../../payment/adapter/sunpay.adapter'; import { OpenPaymentAccountEntity } from '../../entity/open_payment_account'; import { OpenAccountEntity } from '../../entity/open_account'; import { EasyPayAdapter } from '../../../payment/adapter/easypay.adapter'; import { OpenApplicationsEntity } from '../../entity/open_applications'; import { Utils } from '../../../../comm/utils'; import { OrderType, OrderTypeEnum, StatusTypeEnum, } from '../../entity/open_payment_order'; import * as md5 from 'md5'; import { PayeeAddressEntity } from '../../../payment/entity/payee_address'; import { EasyOpenService } from '../open'; /** * 开户管理 */ @Provide() export class applicationsService extends BaseService { @InjectEntityModel(PayeeEntity) payeeEntity: Repository; @InjectEntityModel(CustomerEntity) customerEntity: Repository; @Inject() paymentService: PaymentService; @InjectEntityModel(PayeeAddressEntity) payeeAddressEntity: Repository; @Inject() sunPayAdapter: SunPayAdapter; @Inject() ctx; @Inject() logger: ILogger; @Inject() utils: Utils; @InjectEntityModel(OpenPaymentAccountEntity) openPaymentAccountEntity: Repository; @InjectEntityModel(OpenAccountEntity) openAccountEntity: Repository; @Inject() easyPayAdapter: EasyPayAdapter; @Inject() easyOpenService: EasyOpenService; @InjectEntityModel(OpenApplicationsEntity) openApplicationsEntity: Repository; async addApplications(params, { fail, ok }) { const mchInFo = await this.openAccountEntity.findOne({ where: { mch_id: params.mch_id, }, }); const applicationsParams = { request_id: `fusionget_${new Date().getTime()}`, ...params, account_id: mchInFo.account_id, }; const res = await this.easyPayAdapter.request( 'POST', '/v3/applications', applicationsParams ); if (res.hasOwnProperty('errors') && res.errors.length > 0) { return fail(res.errors); } // 存入数据库 await this.openApplicationsEntity.insert({ mch_id: params.mch_id, //商户编号 account_id: mchInFo.account_id, request_id: applicationsParams.request_id, purpose: params.purpose, // 开户目的 currency: params.currency, // 币种 payment_type: params.payment_type, // 支付方式 status: res.data.status, // 申请状态 source: 'EASYPAY', // 来源 application_id: res.data.id, }); return ok('添加成功'); // 创建商户 } async getApplicationsList(params) { const res = await this.easyPayAdapter.request( 'GET', '/v3/applications', params ); console.log(106, params); return res; } /* 查询账户余额信息 */ 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; } async getTransactionsListByMchId(params) { const merchantInfo = await this.getMerchantInfo(); const res = await this.easyPayAdapter.request( 'GET', `/v3/accounts/${merchantInfo.account_id}/transactions`, params ); res.data = res.data.map(elm => { return { ...elm, create_time: this.utils.formatNewTime(elm.create_time), order_type: OrderTypeEnum[elm.order_type] || elm.order_type, status: StatusTypeEnum[elm.status] || elm.status, }; }); return res; } // 获取收款账户 async getBankAccountsBy(params) { const merchantInfo = await this.getMerchantInfo(); const res = await this.easyPayAdapter.request('GET', `/v1/bank_accounts`, { page: 1, size: 10, account_id: merchantInfo.account_id, }); const bankAccountsIndex = res.data.findIndex( elm => elm.currency === params.currency ); return res.data[bankAccountsIndex]; } // 查询汇率 async getExchangeCurrency(params) { const res = await this.easyPayAdapter.request( 'GET', `/v1/exchange_rates`, params ); return res; } // 换汇 async exchanges(params) { const merchantInfo = await this.getMerchantInfo(); const res = await this.easyPayAdapter.request('POST', `/v1/exchanges`, { account_id: merchantInfo.account_id, ...params, buy_amount: params.buy_amount * 100, }); // 费率拦截 return res; } // 转账 async transfer(params) { console.log(186, params); const to_merchantInfo = await this.getMerchantInfo(params.to_mch_id); const merchantInfo = await this.getMerchantInfo(); const resParams = { request_id: md5( `transfer_${merchantInfo.account_id}_${new Date().getTime()}` ), from_account_id: merchantInfo.account_id, to_account_id: to_merchantInfo.account_id, currency: params.currency, amount: params.amount * 100, purpose: params.purpose, }; const res = await this.easyPayAdapter.request( 'POST', `/v1/transfers`, resParams ); console.log(202, resParams); console.log(203, res); return res; } // 付款 async payments(params) { // 余额 判断是否足以支持交易手续费 // 获取指定币种的余额 /* amount : 12312 beneficiary_id : 10 comment : "123" currency : "AUD" purpose : "123123" */ const merchantInfo = await this.getMerchantInfo(); // 获取收款地址 const mchPayeeAddress = await this.payeeAddressEntity.findOne({ where: { id: params.beneficiary_id, }, }); const paymentsParams = { request_id: md5( `payments_${merchantInfo.account_id}_${new Date().getTime()}` ), account_id: merchantInfo.account_id, currency: params.currency, amount: params.amount * 100, purpose: params.purpose, comment: params.comment, beneficiary: { legal_entity_type: 'COMPANY', payment_type: mchPayeeAddress.payment_type, // 支付方式 region: mchPayeeAddress.region, // 地区 currency: params.currency, bank_name: mchPayeeAddress.bank_name, account_holder_name: mchPayeeAddress.account_holder_name, // 银行账户持有人 account_number: mchPayeeAddress.account_number, // 银行账号/IBAN bic_number: mchPayeeAddress.bank_routing_code_key, // 路由号/SWIFT address: mchPayeeAddress.address, // 地址 }, }; console.log(241, paymentsParams); const res = await this.easyPayAdapter.request( 'POST', `/v1/payments`, paymentsParams ); // 付款拦截利润 改为订单交易成功之后收取 // this.easyOpenService.save_user_order(res, params, OrderType.PAYMENT); console.log(243, res); return res; } // 获取商户信息 async getMerchantInfo(mch_id = 'ep001@fusion.com') { const merchantInfo = await this.openAccountEntity.findOne({ where: { mch_id: this.ctx.admin.merchant.mchId, // mch_id: 'easypay@qq.com', // mch_id: 'ep001@fusion.com', // mch_id: 'easypay003@fusion.com', // mch_id }, }); return merchantInfo; } // amount 单位为分 async getAccountsBalancesByCurrency(account_id, currency) { const balances = await this.easyPayAdapter.request( 'GET', `/v3/accounts/${account_id}/balances`, {} ); let amount = 0; balances.data.forEach(elm => { if(elm.currency === currency && !amount ) { amount = elm.amount } }) return amount } }