|
@@ -0,0 +1,217 @@
|
|
|
+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 { OrderTypeEnum, StatusTypeEnum } from '../../entity/open_payment_order';
|
|
|
+import * as md5 from 'md5';
|
|
|
+
|
|
|
+/**
|
|
|
+ * 开户管理
|
|
|
+ */
|
|
|
+@Provide()
|
|
|
+export class applicationsService extends BaseService {
|
|
|
+ @InjectEntityModel(PayeeEntity)
|
|
|
+ payeeEntity: Repository<PayeeEntity>;
|
|
|
+ @InjectEntityModel(CustomerEntity)
|
|
|
+ customerEntity: Repository<CustomerEntity>;
|
|
|
+ @Inject()
|
|
|
+ paymentService: PaymentService;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ sunPayAdapter: SunPayAdapter;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ ctx;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ logger: ILogger;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ utils: Utils;
|
|
|
+
|
|
|
+ @InjectEntityModel(OpenPaymentAccountEntity)
|
|
|
+ openPaymentAccountEntity: Repository<OpenPaymentAccountEntity>;
|
|
|
+
|
|
|
+ @InjectEntityModel(OpenAccountEntity)
|
|
|
+ openAccountEntity: Repository<OpenAccountEntity>;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ easyPayAdapter: EasyPayAdapter;
|
|
|
+
|
|
|
+ @InjectEntityModel(OpenApplicationsEntity)
|
|
|
+ openApplicationsEntity: Repository<OpenApplicationsEntity>;
|
|
|
+
|
|
|
+ async addApplications(params, { fail, ok }) {
|
|
|
+ const mchInFo = await this.openAccountEntity.findOne({
|
|
|
+ where: {
|
|
|
+ mch_id: params.mchId,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ console.log(58, mchInFo);
|
|
|
+
|
|
|
+ const applicationsParams = {
|
|
|
+ request_id: `fusionget_${new Date().getTime()}`,
|
|
|
+ ...params,
|
|
|
+ account_id: mchInFo.account_id,
|
|
|
+ };
|
|
|
+
|
|
|
+ const res = await this.easyPayAdapter.request(
|
|
|
+ 'POST',
|
|
|
+ '/v3/applications',
|
|
|
+ applicationsParams
|
|
|
+ );
|
|
|
+ console.log(71, res);
|
|
|
+ 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
|
|
|
+ );
|
|
|
+ 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 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
|
|
|
+ }
|
|
|
+}
|