individual.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { CustomerEntity } from './../entity/customer';
  2. import { SunPayAdapter } from './../adapter/sunpay.adapter';
  3. import { IndividualEntity } from './../entity/individual';
  4. import { ALL, Config, Init, Inject, Provide } from '@midwayjs/decorator';
  5. import { BaseService } from '@cool-midway/core';
  6. import { InjectEntityModel } from '@midwayjs/typeorm';
  7. import { Repository } from 'typeorm';
  8. import { PaymentService } from './payment';
  9. import {MerchantEntity} from "../entity/merchant";
  10. @Provide()
  11. export class IndividualService extends BaseService {
  12. @InjectEntityModel(IndividualEntity)
  13. individualEntity: Repository<IndividualEntity>;
  14. @Inject()
  15. paymentService: PaymentService;
  16. @Inject()
  17. ctx;
  18. @InjectEntityModel(CustomerEntity)
  19. customerEntity: Repository<CustomerEntity>;
  20. @InjectEntityModel(MerchantEntity)
  21. merchantEntity: Repository<MerchantEntity>;
  22. @Config(ALL)
  23. config;
  24. @Init()
  25. async init() {
  26. await super.init();
  27. this.setEntity(this.individualEntity);
  28. }
  29. private _isOpenApi: boolean = false
  30. /**
  31. * 添加或者更新当前商户的 individual 客户信息
  32. * @param param
  33. * @returns
  34. */
  35. async add(param) {
  36. let merchantId = await this.getMerchantId(param);
  37. const custom = {
  38. ...param,
  39. out_user_id: merchantId,
  40. webhook_url: this.getWebhook_url(param),
  41. customer_type: 'INDIVIDUAL',
  42. };
  43. let res = await this.paymentService
  44. .setChannel('SUNPAY')
  45. .setCustomerInfo(custom);
  46. param.merchantId = merchantId
  47. await super.add({
  48. ...param,
  49. ...param.company,
  50. customer_id: res.data.customer_id,
  51. });
  52. return res;
  53. }
  54. async update(param) {
  55. let merchantId = this.getMerchantId(param);
  56. const custom = {
  57. ...param,
  58. out_user_id: merchantId,
  59. webhook_url: this.getWebhook_url(param),
  60. customer_type: 'INDIVIDUAL',
  61. };
  62. let res = await this.paymentService
  63. .setChannel('SUNPAY')
  64. .setCustomerInfo(custom);
  65. await super.update(param);
  66. return res;
  67. }
  68. setIsOpenApi(payload = false) {
  69. this._isOpenApi = payload
  70. }
  71. async page(params: any) {
  72. const merchantId = await this.getMerchantId(params);
  73. const query = `SELECT * FROM individual WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`;
  74. return this.sqlRenderPage(query, params, false);
  75. }
  76. async getMerchantId(params) {
  77. let merchantId;
  78. if (this._isOpenApi) {
  79. merchantId = params.out_user_id;
  80. } else if (params?.mch_id || params?.mchId) {
  81. // const merchantInfo = await this.merchantEntity.findOneBy({
  82. // mchId: params?.mch_id
  83. // })
  84. merchantId = params?.mch_id || params?.mchId
  85. } else {
  86. const { merchant } = this.ctx.admin;
  87. merchantId = merchant.mchId;
  88. }
  89. return merchantId
  90. }
  91. getWebhook_url(params) {
  92. if (this._isOpenApi) {
  93. return params.webhook_url
  94. }
  95. return this.config.callback.sunpay
  96. }
  97. }