123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { BaseService } from '@cool-midway/core';
- import { ILogger, Inject, Provide } from '@midwayjs/core';
- import { WebHookCommonService } from './common';
- import {
- OpenPaymentOrderEntity,
- OrderType,
- } from '../../entity/open_payment_order';
- import { InjectEntityModel } from '@midwayjs/typeorm';
- import { Repository } from 'typeorm';
- import { EasyPayAdapter } from '../../../payment/adapter/easypay.adapter';
- import * as md5 from 'md5';
- /**
- * 入账成功
- */
- @Provide()
- export class DepositSuccessService extends BaseService {
- @Inject()
- webHookCommonService: WebHookCommonService;
- @InjectEntityModel(OpenPaymentOrderEntity)
- openPaymentOrderEntity: Repository<OpenPaymentOrderEntity>;
- @Inject()
- ctx;
- @Inject()
- logger: ILogger;
- @Inject()
- easyPayAdapter: EasyPayAdapter;
- async run(params) {
- // 获取回调用户的详情
- let accountInfo = null;
- // 获取回调用户的详情
- let withdrawChannelFee = null;
- try {
- /*
- 2025-03-11 16:58:09.069 INFO 113849 [-/::ffff:127.0.0.1/-/1ms POST /api/open/easypay-webhook/notification] easypay的webhook_notification: params{"type":"deposit_success","data":{"id":"85a2e73b90d94354b14b925c8e23b876","bank_account_id":"7021a5e645574004b5678213f94df8a5","order_no":"20250311165807046795","account_id":"590f080eb299590385c7aa628274e73c","bic_number":null,"account_number":"79765000168","inward_amount":90000,"fee":0,"amount":90000,"currency":"EUR","payer":"付款信息 格式:{sender.name};{sender.address};{sender.country};{sender.account_number} or {sender.iban};{sender.bic};{sender.routing_code};附言","comment":"附言","payment_type":"SWIFT","order_type":"DEPOSIT","payment_id":null,"clearing_system":null,"status":"SUCCESS","create_time":"2025-03-11T16:58:08+08:00","update_time":"2025-03-11T16:58:08+08:00","completed_time":"2025-03-11T16:58:08+08:00"}}
-
- {
- "type": "deposit_success",
- "data": {
- "id": "29091e03092c42a681f0921118925e84",
- "bank_account_id": "262874c798b1452682acd96fb0317bae",
- "order_no": "20250310155503589841",
- "account_id": "2bd64372841a54bf8b41f879ff07884b",
- "bic_number": null,
- "account_number": "79765000155",
- "inward_amount": 1000,
- "fee": 0,
- "amount": 1000,
- "currency": "GBP",
- "payer": "付款信息 格式:{sender.name};{sender.address};{sender.country};{sender.account_number} or {sender.iban};{sender.bic};{sender.routing_code};附言",
- "comment": "附言",
- "payment_type": "SWIFT",
- "order_type": "DEPOSIT",
- "payment_id": null,
- "clearing_system": null,
- "status": "SUCCESS",
- "create_time": "2025-03-10T15:55:04+08:00",
- "update_time": "2025-03-10T15:55:04+08:00",
- "completed_time": "2025-03-10T15:55:03+08:00"
- }
- }
- */
- // 获取回调用户的详情
- accountInfo = await this.webHookCommonService.getAccountInfo(
- params.data.account_id
- );
- if (!accountInfo) {
- // TODO 如果不存在的话,则为白标用户
- // 转账延迟4秒处理
- await this.webHookCommonService.waitByTime(4000);
- this.ctx.status = 400;
- this.ctx.body = {};
- return;
- }
- // 获取费率信息
- 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.webHookCommonService.getBalanceDiffByCurrency({
- account_id: params.data.account_id,
- currency: params.data.currency,
- amount: params.data.amount,
- order_type: OrderType.DEPOSIT,
- });
- // 记录入账流水
- const openPaymentOrderParams = {
- mch_id: accountInfo.mch_id,
- amount: params.data.amount / 100,
- account_id: params.data.account_id,
- from_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: {},
- before_balance: before_balance / 100,
- balance: balance / 100,
- };
- await this.openPaymentOrderEntity.insert(openPaymentOrderParams);
- this.logger.info(
- `记录入账流水, ${JSON.stringify(openPaymentOrderParams)}`
- );
- // 通知上游之后,间隔10秒执行利润截取操作
- Promise.resolve().then(async () => {
- await this.webHookCommonService.waitByTime(10000);
- // 收取手续费: 转账
- // /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: res.data.order_no, // 这里是转账的订单编号
- fee: 0,
- additional_info: {},
- });
- });
- this.ctx.status = 200;
- this.ctx.body = {};
- return;
- } catch (error) {
- this.logger.error(`记录入账流水失败, ${JSON.stringify(params)}`);
- this.logger.error(error);
- }
- }
- }
|