deposit_success.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { BaseService } from '@cool-midway/core';
  2. import { ILogger, Inject, Provide } from '@midwayjs/core';
  3. import { WebHookCommonService } from './common';
  4. import {
  5. OpenPaymentOrderEntity,
  6. OrderType,
  7. } from '../../entity/open_payment_order';
  8. import { InjectEntityModel } from '@midwayjs/typeorm';
  9. import { Repository } from 'typeorm';
  10. import { EasyPayAdapter } from '../../../payment/adapter/easypay.adapter';
  11. import * as md5 from 'md5';
  12. /**
  13. * 入账成功
  14. */
  15. @Provide()
  16. export class DepositSuccessService extends BaseService {
  17. @Inject()
  18. webHookCommonService: WebHookCommonService;
  19. @InjectEntityModel(OpenPaymentOrderEntity)
  20. openPaymentOrderEntity: Repository<OpenPaymentOrderEntity>;
  21. @Inject()
  22. ctx;
  23. @Inject()
  24. logger: ILogger;
  25. @Inject()
  26. easyPayAdapter: EasyPayAdapter;
  27. async run(params) {
  28. // 获取回调用户的详情
  29. let accountInfo = null;
  30. // 获取回调用户的详情
  31. let withdrawChannelFee = null;
  32. try {
  33. /*
  34. 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"}}
  35. {
  36. "type": "deposit_success",
  37. "data": {
  38. "id": "29091e03092c42a681f0921118925e84",
  39. "bank_account_id": "262874c798b1452682acd96fb0317bae",
  40. "order_no": "20250310155503589841",
  41. "account_id": "2bd64372841a54bf8b41f879ff07884b",
  42. "bic_number": null,
  43. "account_number": "79765000155",
  44. "inward_amount": 1000,
  45. "fee": 0,
  46. "amount": 1000,
  47. "currency": "GBP",
  48. "payer": "付款信息 格式:{sender.name};{sender.address};{sender.country};{sender.account_number} or {sender.iban};{sender.bic};{sender.routing_code};附言",
  49. "comment": "附言",
  50. "payment_type": "SWIFT",
  51. "order_type": "DEPOSIT",
  52. "payment_id": null,
  53. "clearing_system": null,
  54. "status": "SUCCESS",
  55. "create_time": "2025-03-10T15:55:04+08:00",
  56. "update_time": "2025-03-10T15:55:04+08:00",
  57. "completed_time": "2025-03-10T15:55:03+08:00"
  58. }
  59. }
  60. */
  61. // 获取回调用户的详情
  62. accountInfo = await this.webHookCommonService.getAccountInfo(
  63. params.data.account_id
  64. );
  65. if (!accountInfo) {
  66. // TODO 如果不存在的话,则为白标用户
  67. // 转账延迟4秒处理
  68. await this.webHookCommonService.waitByTime(4000);
  69. this.ctx.status = 400;
  70. this.ctx.body = {};
  71. return;
  72. }
  73. // 获取费率信息
  74. withdrawChannelFee =
  75. await this.webHookCommonService.getWithdrawChannelFee({
  76. account_id: params.data.account_id,
  77. currency: params.data.currency,
  78. order_type: OrderType.DEPOSIT,
  79. channel: 'EASYPAY',
  80. amount: params.data.amount,
  81. mch_id: accountInfo.mch_id,
  82. });
  83. // 获取余额
  84. const { before_balance, balance } =
  85. await this.webHookCommonService.getBalanceDiffByCurrency({
  86. account_id: params.data.account_id,
  87. currency: params.data.currency,
  88. amount: params.data.amount,
  89. order_type: OrderType.DEPOSIT,
  90. });
  91. // 记录入账流水
  92. const openPaymentOrderParams = {
  93. mch_id: accountInfo.mch_id,
  94. amount: params.data.amount / 100,
  95. account_id: params.data.account_id,
  96. from_account_id: params.data.account_id,
  97. to_account_id: params.data.account_id,
  98. event_id: params.data.id,
  99. currency: params.data.currency,
  100. status: params.data.status,
  101. order_type: OrderType.DEPOSIT,
  102. payment_type: params.data.payment_type,
  103. order_id: params.data.order_no,
  104. fee: withdrawChannelFee,
  105. additional_info: {},
  106. before_balance: before_balance / 100,
  107. balance: balance / 100,
  108. };
  109. await this.openPaymentOrderEntity.insert(openPaymentOrderParams);
  110. this.logger.info(
  111. `记录入账流水, ${JSON.stringify(openPaymentOrderParams)}`
  112. );
  113. // 通知上游之后,间隔10秒执行利润截取操作
  114. Promise.resolve().then(async () => {
  115. await this.webHookCommonService.waitByTime(10000);
  116. // 收取手续费: 转账
  117. // /v1/transfers
  118. const transfers_params = {
  119. // ...openOrderObj,
  120. request_id: md5(`${accountInfo.mch_id}_${params.data.id}`),
  121. from_account_id: params.data.account_id,
  122. to_account_id: this.easyPayAdapter.baseInfo.account_id,
  123. currency: params.data.currency,
  124. amount: withdrawChannelFee * 100,
  125. purpose: '收取手续费用',
  126. };
  127. this.logger.info(
  128. `记录入账流水的费率, ${JSON.stringify(transfers_params)}`
  129. );
  130. // 截取流水的转账
  131. const res = await this.easyPayAdapter.request(
  132. 'POST',
  133. '/v1/transfers',
  134. transfers_params
  135. );
  136. // 记录入账手续费的流水
  137. await this.openPaymentOrderEntity.insert({
  138. request_id: transfers_params.request_id,
  139. mch_id: accountInfo.mch_id,
  140. amount: withdrawChannelFee,
  141. account_id: params.data.account_id,
  142. from_account_id: params.data.account_id,
  143. to_account_id: this.easyPayAdapter.baseInfo.account_id,
  144. event_id: params.data.id,
  145. currency: params.data.currency,
  146. status: params.data.status,
  147. order_type: OrderType.TRANSACTION_FEE_ORDER,
  148. payment_type: params.data.payment_type,
  149. order_id: res.data.order_no, // 这里是转账的订单编号
  150. fee: 0,
  151. additional_info: {},
  152. });
  153. });
  154. this.ctx.status = 200;
  155. this.ctx.body = {};
  156. return;
  157. } catch (error) {
  158. this.logger.error(`记录入账流水失败, ${JSON.stringify(params)}`);
  159. this.logger.error(error);
  160. }
  161. }
  162. }