payee_address.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Init, Inject, Provide } from '@midwayjs/decorator';
  2. import { BaseService, CoolCommException } from '@cool-midway/core';
  3. import { InjectEntityModel } from '@midwayjs/typeorm';
  4. import { Repository } from 'typeorm';
  5. import { PayeeAddressEntity } from '../entity/payee_address';
  6. import { CustomerEntity } from '../entity/customer';
  7. import { PaymentService } from './payment';
  8. /**
  9. * 描述
  10. */
  11. @Provide()
  12. export class PayeeAddressService extends BaseService {
  13. @InjectEntityModel(PayeeAddressEntity)
  14. payeeAddressEntity: Repository<PayeeAddressEntity>;
  15. @InjectEntityModel(CustomerEntity)
  16. customerEntity: Repository<CustomerEntity>;
  17. @Inject()
  18. paymentService: PaymentService;
  19. @Inject()
  20. ctx;
  21. private _isOpenApi: boolean = false
  22. setIsOpenApi(payload = false) {
  23. this._isOpenApi = payload
  24. }
  25. /**
  26. * 添加收款人
  27. * @param params
  28. * @returns
  29. */
  30. async add(params: any) {
  31. const { merchant } = this.ctx.admin;
  32. const merchantId = merchant.mchId;
  33. if (!['TRC20', 'ERC20'].includes(params.currency)) {
  34. const res = await this.paymentService
  35. .setChannel(params.channel)
  36. .createBeneficiaryAddress(params);
  37. params.beneficiary_address_id = res.data.id;
  38. }
  39. params.merchantId = merchantId;
  40. const payee = await super.add(params);
  41. return payee;
  42. }
  43. async update(params: any) {
  44. const payee = await this.payeeAddressEntity.findOneBy({
  45. id: params.id,
  46. });
  47. if (payee.beneficiary_address_id) {
  48. await this.paymentService
  49. .setChannel(payee.channel)
  50. .updateBeneficiaryAddress(params);
  51. }
  52. await super.update(params);
  53. return params;
  54. }
  55. async delete(params: any) {
  56. const payee = await this.payeeAddressEntity.findOneBy({
  57. id: params.id,
  58. });
  59. if (payee.beneficiary_address_id) {
  60. await this.paymentService
  61. .setChannel(payee.channel)
  62. .deleteBeneficiaryAddress({
  63. id: payee.beneficiary_address_id,
  64. });
  65. }
  66. await super.delete(params);
  67. }
  68. async list(params: any, option: any, connectionName?: any) {
  69. const { channel, currency } = params;
  70. const { merchant } = this.ctx.admin;
  71. const merchantId = merchant.mchId;
  72. const res = await this.payeeAddressEntity.find({
  73. where: {
  74. merchantId: merchantId,
  75. channel,
  76. currency,
  77. },
  78. select: ['id', 'address', 'currency'],
  79. });
  80. return res;
  81. }
  82. async page(params: any) {
  83. const { merchant } = this.ctx.admin;
  84. const merchantId = merchant.mchId;
  85. const query = `SELECT * FROM payee WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`;
  86. return this.sqlRenderPage(query, params, false);
  87. }
  88. }