Browse Source

feat(): 客户接口优化

max 7 months ago
parent
commit
97834abbaf
2 changed files with 61 additions and 51 deletions
  1. 31 26
      src/modules/payment/service/business.ts
  2. 30 25
      src/modules/payment/service/individual.ts

+ 31 - 26
src/modules/payment/service/business.ts

@@ -20,27 +20,20 @@ export class BusinessService extends BaseService {
     await super.init();
     await super.init();
     this.setEntity(this.businessEntity);
     this.setEntity(this.businessEntity);
   }
   }
+
+  private _isOpenApi: boolean = false;
+
   /**
   /**
    * 添加或者更新当前商户的 business 客户信息
    * 添加或者更新当前商户的 business 客户信息
    * @param param
    * @param param
    * @returns
    * @returns
    */
    */
   async add(param) {
   async add(param) {
-    let merchantId;
-    const isOpenApi = param.hasOwnProperty('isOpenApi')
-      ? param.isOpenApi
-      : false;
-    if (isOpenApi) {
-      merchantId = param.out_user_id;
-    } else {
-      const { merchant } = this.ctx.admin;
-      merchantId = merchant.id;
-    }
-
+    let merchantId = this.getMerchantId(param);
     const custom = {
     const custom = {
       ...param,
       ...param,
-      out_user_id: merchantId, // TODO 应该是我们平台的ID
-      webhook_url: this.config.callback.sunpay,
+      out_user_id: merchantId,
+      webhook_url: this.getWebhook_url(param),
       customer_type: 'COMPANY',
       customer_type: 'COMPANY',
     };
     };
     let res = await this.paymentService
     let res = await this.paymentService
@@ -56,21 +49,11 @@ export class BusinessService extends BaseService {
     return res;
     return res;
   }
   }
   async update(param) {
   async update(param) {
-    let merchantId;
-    const isOpenApi = param.hasOwnProperty('isOpenApi')
-      ? param.isOpenApi
-      : false;
-    if (isOpenApi) {
-      merchantId = param.out_user_id;
-    } else {
-      const { merchant } = this.ctx.admin;
-      merchantId = merchant.id;
-    }
-
+    let merchantId = this.getMerchantId(param);
     const custom = {
     const custom = {
       ...param,
       ...param,
-      out_user_id: merchantId, // TODO 应该是我们平台的ID
-      webhook_url: this.config.callback.sunpay,
+      out_user_id: merchantId,
+      webhook_url: this.getWebhook_url(param),
       customer_type: 'COMPANY',
       customer_type: 'COMPANY',
     };
     };
     const res = await this.paymentService
     const res = await this.paymentService
@@ -82,4 +65,26 @@ export class BusinessService extends BaseService {
     });
     });
     return res;
     return res;
   }
   }
+
+  setIsOpenApi(payload = false) {
+    this._isOpenApi = payload
+  }
+
+  getMerchantId(params) {
+    let merchantId;
+    if (this._isOpenApi) {
+      merchantId = params.out_user_id;
+    } else {
+      const { merchant } = this.ctx.admin;
+      merchantId = merchant.id;
+    }
+    return merchantId
+  }
+
+  getWebhook_url(params) {
+    if (this._isOpenApi) {
+      return params.webhook_url
+    }
+    return this.config.callback.sunpay
+  }
 }
 }

+ 30 - 25
src/modules/payment/service/individual.ts

@@ -24,33 +24,26 @@ export class IndividualService extends BaseService {
     await super.init();
     await super.init();
     this.setEntity(this.individualEntity);
     this.setEntity(this.individualEntity);
   }
   }
+  private _isOpenApi: boolean = false
+
   /**
   /**
    * 添加或者更新当前商户的 individual 客户信息
    * 添加或者更新当前商户的 individual 客户信息
    * @param param
    * @param param
    * @returns
    * @returns
    */
    */
   async add(param) {
   async add(param) {
-    let merchantId;
-    const isOpenApi = param.hasOwnProperty('isOpenApi')
-      ? param.isOpenApi
-      : false;
-    if (isOpenApi) {
-      merchantId = param.out_user_id;
-    } else {
-      const { merchant } = this.ctx.admin;
-      merchantId = merchant.id;
-    }
+    let merchantId = this.getMerchantId(param);
     const custom = {
     const custom = {
       ...param,
       ...param,
-      out_user_id: merchantId, // TODO 应该是我们平台的ID
-      webhook_url: this.config.callback.sunpay,
+      out_user_id: merchantId,
+      webhook_url: this.getWebhook_url(param),
       customer_type: 'INDIVIDUAL',
       customer_type: 'INDIVIDUAL',
     };
     };
     let res = await this.paymentService
     let res = await this.paymentService
       .setChannel('SUNPAY')
       .setChannel('SUNPAY')
       .setCustomerInfo(custom);
       .setCustomerInfo(custom);
     param.merchant = {
     param.merchant = {
-      id: merchantId,  // TODO 应该是我们平台的ID
+      id: merchantId,
     };
     };
     await super.add({
     await super.add({
       ...param,
       ...param,
@@ -59,20 +52,11 @@ export class IndividualService extends BaseService {
     return res;
     return res;
   }
   }
   async update(param) {
   async update(param) {
-    let merchantId;
-    const isOpenApi = param.hasOwnProperty('isOpenApi')
-      ? param.isOpenApi
-      : false;
-    if (isOpenApi) {
-      merchantId = param.out_user_id;
-    } else {
-      const { merchant } = this.ctx.admin;
-      merchantId = merchant.id;
-    }
+    let merchantId = this.getMerchantId(param);
     const custom = {
     const custom = {
       ...param,
       ...param,
-      out_user_id: merchantId,  // TODO 应该是我们平台的ID
-      webhook_url: this.config.callback.sunpay,
+      out_user_id: merchantId,
+      webhook_url: this.getWebhook_url(param),
       customer_type: 'INDIVIDUAL',
       customer_type: 'INDIVIDUAL',
     };
     };
     let res = await this.paymentService
     let res = await this.paymentService
@@ -81,4 +65,25 @@ export class IndividualService extends BaseService {
     await super.update(param);
     await super.update(param);
     return res;
     return res;
   }
   }
+  setIsOpenApi(payload = false) {
+    this._isOpenApi = payload
+  }
+
+  getMerchantId(params) {
+    let merchantId;
+    if (this._isOpenApi) {
+      merchantId = params.out_user_id;
+    } else {
+      const { merchant } = this.ctx.admin;
+      merchantId = merchant.id;
+    }
+    return merchantId
+  }
+
+  getWebhook_url(params) {
+    if (this._isOpenApi) {
+      return params.webhook_url
+    }
+    return this.config.callback.sunpay
+  }
 }
 }