Parcourir la source

feat: 入账流水 3-11

max il y a 4 mois
Parent
commit
909049c604
2 fichiers modifiés avec 123 ajouts et 36 suppressions
  1. 4 1
      src/modules/api/entity/open_payment_order.ts
  2. 119 35
      src/modules/api/service/webhook.ts

+ 4 - 1
src/modules/api/entity/open_payment_order.ts

@@ -44,10 +44,13 @@ export class OpenPaymentOrderEntity extends BaseEntity {
 
   @Column({ comment: '历史余额', type: 'decimal', precision: 20, scale: 6, default: 0 })
   before_balance?: number;
-  
+
   @Column({ comment: '余额', type: 'decimal', precision: 20, scale: 6, default: 0 })
   balance?: number;
 
+  @Column({ comment: '手续费', type: 'decimal', precision: 20, scale: 6, default: 0 })
+  fee?: number;
+
   @Column({ length: 100, comment: 'credit or debit', default: '' })
   type?: string;
 

+ 119 - 35
src/modules/api/service/webhook.ts

@@ -1,19 +1,17 @@
-import { BaseService } from '@cool-midway/core';
-import { ILogger, Provide } from '@midwayjs/core';
-import { Inject } from '@midwayjs/decorator';
-import { InjectEntityModel } from '@midwayjs/typeorm';
-import { PayeeAddressEntity } from '../../payment/entity/payee_address';
-import { Repository } from 'typeorm';
-import { CustomerEntity } from '../../payment/entity/customer';
-import { PaymentService } from '../../payment/service/payment';
-import { PayeeEntity } from '../../payment/entity/payee';
-import { SunPayAdapter } from '../../payment/adapter/sunpay.adapter';
-import {
-  OpenPaymentOrderEntity,
-  OrderType,
-} from '../entity/open_payment_order';
-import { WithdrawChannelEntity } from '../entity/withdrawChannel';
-import { OpenAccountEntity } from '../entity/open_account';
+import {BaseService} from '@cool-midway/core';
+import {ILogger, Provide} from '@midwayjs/core';
+import {Inject} from '@midwayjs/decorator';
+import {InjectEntityModel} from '@midwayjs/typeorm';
+import {Repository} from 'typeorm';
+import {CustomerEntity} from '../../payment/entity/customer';
+import {PaymentService} from '../../payment/service/payment';
+import {PayeeEntity} from '../../payment/entity/payee';
+import {SunPayAdapter} from '../../payment/adapter/sunpay.adapter';
+import {OpenPaymentOrderEntity, OrderType,} from '../entity/open_payment_order';
+import {WithdrawChannelEntity} from '../entity/withdrawChannel';
+import {OpenAccountEntity} from '../entity/open_account';
+import {EasyPayAdapter} from '../../payment/adapter/easypay.adapter';
+import * as md5 from 'md5';
 
 /**
  * 描述
@@ -41,6 +39,9 @@ export class OpenApiWebhookService extends BaseService {
   @Inject()
   sunPayAdapter: SunPayAdapter;
 
+  @Inject()
+  easyPayAdapter: EasyPayAdapter;
+
   @Inject()
   ctx;
 
@@ -195,26 +196,102 @@ export class OpenApiWebhookService extends BaseService {
       }, settime);
     });
   }
+
   //
   async deposit_success(params) {
     // 如果入账成功,则收取手续费
     console.log(193, params);
-    
+
     // 获取回调用户的详情
     const accountInfo = await this.getAccountInfo(params.data.account_id);
-    
-    // TODO 如果不存在的话,则为白标用户
+    if (!accountInfo) {
+      // TODO 如果不存在的话,则为白标用户
+      this.ctx.status = 400;
+      this.ctx.body = {};
+      return;
+    }
+
     // 获取费率信息
-    const withdrawChannelFee = this.getWithdrawChannelFee({
+    const 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,
+    });
+    // 查询用户的资金
+
+    // 记录入账流水
+    await this.openPaymentOrderEntity.insert({
+      mch_id: accountInfo.mch_id,
+      amount: params.data.amount/100,
+      account_id: params.data.account_id,
+      to_account_id: params.data.account_id,
+      event_id: params.data.id,
+      currency: params.data.currency,
+      status: params.data.status,
+      order_type: OrderType.DEPOSIT,
+      payment_type: params.data.payment_type,
+      order_id: params.data.order_no,
+      fee: withdrawChannelFee,
+      additional_info: {}
+    });
+    this.logger.info(
+      `记录入账流水, ${JSON.stringify({
+        mch_id: accountInfo.mch_id,
+        amount: params.data.amount/100,
         account_id: params.data.account_id,
-        currency:  params.data.currency,
-        order_type: "DEPOSIT",
-        channel: "EASYPAY", 
-        amount:params.data.amount
-    })
-    // 记录流水
-    
-    // 收取手续费
+        to_account_id: params.data.account_id,
+        event_id: params.data.id,
+        currency: params.data.currency,
+        status: params.data.status,
+        order_type: OrderType.DEPOSIT,
+        payment_type: params.data.payment_type,
+        order_id: params.data.order_no,
+        fee: withdrawChannelFee,
+      })}`
+    );
+    // 收取手续费: 转账
+    // /v1/transfers
+    const transfers_params = {
+      // ...openOrderObj,
+      request_id: md5(`${accountInfo.mch_id}_${params.data.id}`),
+      from_account_id: params.data.account_id,
+      to_account_id: this.easyPayAdapter.baseInfo.account_id,
+      currency: params.data.currency,
+      amount: withdrawChannelFee*100,
+      purpose: '收取手续费用',
+    };
+    this.logger.info(`记录入账流水的费率, ${JSON.stringify(transfers_params)}`);
+    // 截取流水的转账
+    const res = await this.easyPayAdapter.request(
+      'POST',
+      '/v1/transfers',
+      transfers_params
+    );
+    // 记录入账手续费
+    await this.openPaymentOrderEntity.insert({
+      request_id: transfers_params.request_id,
+      mch_id: accountInfo.mch_id,
+      amount: withdrawChannelFee,
+      account_id: params.data.account_id,
+      from_account_id: params.data.account_id,
+      to_account_id: this.easyPayAdapter.baseInfo.account_id,
+      event_id: params.data.id,
+      currency: params.data.currency,
+      status: params.data.status,
+      order_type: OrderType.TRANSACTION_FEE_ORDER,
+      payment_type: params.data.payment_type,
+      order_id: params.data.order_no,
+      fee: 0,
+      additional_info: {}
+    });
+    this.ctx.status = 200;
+    this.ctx.body = {};
+    return;
+
+    // 转账的流水
 
     // withdrawChannelFee
     // withdrawChannel
@@ -267,25 +344,32 @@ export class OpenApiWebhookService extends BaseService {
       },
     });
   }
+
   async getWithdrawChannelFee({
-    account_id, currency,order_type, channel, amount
+    account_id,
+    currency,
+    order_type,
+    channel,
+    amount,
+    mch_id,
   }) {
     const withdrawChannel = await this.withdrawChannelEntity.findOne({
       where: {
-        account_id: account_id,
+        // account_id: account_id,
         channel,
-        // mch_id: accountInfo.mch_id,
+        mch_id: mch_id,
         order_type,
         currency: currency,
         status: 1,
       },
     });
+    console.log(360, withdrawChannel);
     // 费率
-    const fee = Number.parseInt(withdrawChannel.rate) / 100 * amount // 费率费用
+    const fee = (Number.parseInt(withdrawChannel.rate) / 100) * (amount/100); // 费率费用
     // 单笔固定费用
-    const basicFee = withdrawChannel.basicFee
+    const basicFee = withdrawChannel.basicFee;
     // 单笔最低费用
-    const feeMin = withdrawChannel.feeMin
-    return Math.max(fee, basicFee, feeMin) // 取最大值
+    const feeMin = withdrawChannel.feeMin;
+    return Math.max(fee, basicFee, feeMin); // 取最大值
   }
 }