瀏覽代碼

feat(): 客户管理api对接

max 7 月之前
父節點
當前提交
ffe8c2790b

+ 41 - 66
src/modules/api/controller/customer.ts

@@ -1,18 +1,16 @@
-import {CreateCustomerDTO} from "../dto/customer";
 import { CoolController, BaseController } from '@cool-midway/core';
 import {
   ALL,
   Body,
   Get,
-  Inject, Param,
+  Inject,
   Post,
   Provide,
   Put,
 } from '@midwayjs/decorator';
-import { BusinessEntity } from '../../payment/entity/business';
-import {Validate} from "@midwayjs/validate";
-import {BusinessService} from "../../payment/service/business";
-import {PaymentService} from "../../payment/service/payment";
+import { IndividualEntity } from '../../payment/entity/individual';
+import { CustomerService } from '../service/customer';
+import {SunPayAdapter} from "../../payment/adapter/sunpay.adapter";
 
 /**
  * 客户管理
@@ -20,77 +18,54 @@ import {PaymentService} from "../../payment/service/payment";
 @Provide()
 @CoolController('/api/v1/Fiat')
 export class CustomerController extends BaseController {
+  @Inject()
+  customerService: CustomerService;
 
   @Inject()
-  businessService: BusinessService;
+  sunPayAdapter: SunPayAdapter;
   /**
    * 创建客户
    */
   @Post('/CreateCustomer', { summary: '创建客户' })
-  @Validate({
-    errorStatus: 422,
-  })
-  async createCustomer(@Body() params: CreateCustomerDTO) {
-    return this.ok(params);
-    // return this.ok(await this.businessService.add(params));
-  }
-  /**
-   * 获取创建客户必填字段
-   */
-  @Get('/CustomerRequiredFields', { summary: '获取创建客户必填字段' })
-  async getCustomerRequiredFields(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
-  }
-  /**
-   * 验证客户必填字段
-   * /api/v3/Fiat/Customer/Validate
-   */
-  @Post('/Customer/Validate', { summary: '验证客户必填字段' })
-  async param(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async createCustomer(@Body() params: IndividualEntity) {
+    return this.ok(await this.customerService.createCustomer(params));
   }
+  // /**
+  //  * 获取创建客户必填字段
+  //  */
+  // @Get('/CustomerRequiredFields', { summary: '获取创建客户必填字段' })
+  // async getCustomerRequiredFields(@Body(ALL) params: CreateUserParams) {
+  //   return this.ok(await this.sunPayAdapter.validateCustomerInfo(params));
+  // }
+  // /**
+  //  * 验证客户必填字段
+  //  * /api/v3/Fiat/Customer/Validate
+  //  */
+  // @Post('/Customer/Validate', { summary: '验证客户必填字段' })
+  // async param(@Body(ALL) business: CreateUserParams) {
+  //   return this.ok(await this.sunPayAdapter.validateCustomerInfo(params));
+  // }
   /**
    * 修改客户
    * /api/v3/Fiat/Customer
    */
   @Put('/updateCustomer', { summary: '修改客户' })
-  async updateCustomer(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
-  }
-  /**
-   * TODO 创建客户回调通知
-   *
-   */
-  @Post('/callback', { summary: '创建客户回调通知' })
-  async createCustomerCallback(@Body(ALL) business: BusinessEntity) {
-    // if (!this.allowKeys.includes(key)) {
-    //   return this.fail('非法操作');
-    // }
-    // 关键参数校验
-    // 数据落库
-    // 回调
-    console.log(business);
-    return this.ok('hello, cool-admin!!!');
+  async updateCustomer(@Body() params: IndividualEntity) {
+    return await this.customerService.updateCustomer(params);
   }
+  // /**
+  //  * TODO 创建客户回调通知
+  //  *
+  //  */
+  // @Post('/callback', { summary: '创建客户回调通知' })
+  // async createCustomerCallback(@Body(ALL) business: BusinessEntity) {
+  //   // if (!this.allowKeys.includes(key)) {
+  //   //   return this.fail('非法操作');
+  //   // }
+  //   // 关键参数校验
+  //   // 数据落库
+  //   // 回调
+  //   console.log(business);
+  //   return this.ok('hello, cool-admin!!!');
+  // }
 }

+ 29 - 21
src/modules/api/dto/customer.ts

@@ -25,25 +25,29 @@ export class CompanyDTO {
       'SOLE_TRADER',
       'LIMITED_LIABILITY',
     ],
-    description:
-      '类型,可选值如下:\n' +
-      '\n' +
-      'OTHER:其他\n' +
-      '\n' +
-      'PUBLIC_LIMITED_COMPANY:公共有限公司\n' +
-      '\n' +
-      'PARTNERSHIP:合伙企业\n' +
-      '\n' +
-      'CHARITY:慈善机构\n' +
-      '\n' +
-      'PRIVATE_LIMITED_COMPANY:私营有限公司\n' +
-      '\n' +
-      'JOINT_STOCK_COMPANY:股份公司\n' +
-      '\n' +
-      'SOLE_TRADER:独资经营企业\n' +
-      '\n' +
-      'LIMITED_LIABILITY:有限责任公司',
+    description: `类型,可选值如下:
+
+OTHER:其他
+
+PUBLIC_LIMITED_COMPANY:公共有限公司
+
+PARTNERSHIP:合伙企业
+
+CHARITY:慈善机构
+
+PRIVATE_LIMITED_COMPANY:私营有限公司
+
+JOINT_STOCK_COMPANY:股份公司
+
+SOLE_TRADER:独资经营企业
+
+LIMITED_LIABILITY:有限责任公司`,
   })
+  @Rule(
+    RuleType.string()
+      .valid(...Object.values(BusinessType))
+      .required()
+  )
   type: BusinessType;
 
   @ApiProperty({ description: '国家编码' })
@@ -98,7 +102,11 @@ export class CompanyDTO {
       '\n' +
       'PASSPORT:护照',
   })
-  @Rule(RuleType.string().required())
+  @Rule(
+    RuleType.string()
+      .valid(...Object.values(CertificateType))
+      .required()
+  )
   partners_document_type: CertificateType;
 
   @ApiProperty({ description: '合伙人' })
@@ -250,11 +258,11 @@ export class CreateCustomerDTO {
     description: '公司',
     type: CompanyDTO,
   })
-  company: CompanyDTO;
+  company?: CompanyDTO;
 
   @ApiProperty({
     description: '个人',
     type: IndividualDTO,
   })
-  individual: IndividualDTO;
+  individual?: IndividualDTO;
 }

+ 40 - 2
src/modules/api/service/customer.ts

@@ -1,8 +1,46 @@
-import { BaseService } from '@cool-midway/core';
+import { BaseService, CoolCommException } from '@cool-midway/core';
 import { Provide } from '@midwayjs/core';
+import { Inject } from '@midwayjs/decorator';
+import { BusinessService } from '../../payment/service/business';
+import { SunPayAdapter } from '../../payment/adapter/sunpay.adapter';
+import { IndividualService } from '../../payment/service/individual';
 
 /**
  * 描述
  */
 @Provide()
-export class CustomerService extends BaseService {}
+export class CustomerService extends BaseService {
+  @Inject()
+  sunPayAdapter: SunPayAdapter;
+  @Inject()
+  businessService: BusinessService; // 公司
+  @Inject()
+  individualService: IndividualService; // 个人
+
+  async createCustomer(params) {
+    const isIndividual = params.customer_type === 'INDIVIDUAL';
+    // const isCompany = params.customer_type === 'COMPANY';
+    if (!params?.individual && !params?.company) {
+      throw new CoolCommException('company或individual必须传一个');
+    }
+    params.isOpenApi = true;
+    if (isIndividual) {
+      return await this.individualService.add(params);
+    }
+    return await this.businessService.add(params);
+    // TODO 过滤sunpay返回
+  }
+  async updateCustomer(params) {
+    const isIndividual = params.customer_type === 'INDIVIDUAL';
+    // const isCompany = params.customer_type === 'COMPANY';
+    if (!params?.individual && !params?.company) {
+      throw new CoolCommException('company或individual必须传一个');
+    }
+    params.isOpenApi = true;
+    if (isIndividual) {
+      return await this.individualService.update(params);
+    }
+    return await this.businessService.update(params);
+    // TODO 过滤sunpay返回
+  }
+}

+ 39 - 8
src/modules/payment/service/business.ts

@@ -1,5 +1,5 @@
 import { BusinessEntity } from './../entity/business';
-import { ALL, Config, Inject, Provide } from '@midwayjs/decorator';
+import { ALL, Config, Init, Inject, Provide } from '@midwayjs/decorator';
 import { BaseService } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
@@ -15,16 +15,31 @@ export class BusinessService extends BaseService {
   ctx;
   @Config(ALL)
   config;
+  @Init()
+  async init() {
+    await super.init();
+    this.setEntity(this.businessEntity);
+  }
   /**
    * 添加或者更新当前商户的 business 客户信息
    * @param param
    * @returns
    */
   async add(param) {
-    const { merchant } = this.ctx.admin;
+    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;
+    }
+
     const custom = {
       ...param,
-      out_user_id: merchant.id,
+      out_user_id: merchantId, // TODO 应该是我们平台的ID
       webhook_url: this.config.callback.sunpay,
       customer_type: 'COMPANY',
     };
@@ -32,23 +47,39 @@ export class BusinessService extends BaseService {
       .setChannel('SUNPAY')
       .setCustomerInfo(custom);
     param.merchant = {
-      id: merchant.id,
+      id: merchantId,
     };
-    await super.add(param);
+    await super.add({
+      ...param,
+      ...param.company,
+    });
     return res;
   }
   async update(param) {
-    const { merchant } = this.ctx.admin;
+    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;
+    }
+
     const custom = {
       ...param,
-      out_user_id: merchant.id,
+      out_user_id: merchantId, // TODO 应该是我们平台的ID
       webhook_url: this.config.callback.sunpay,
       customer_type: 'COMPANY',
     };
     const res = await this.paymentService
       .setChannel('SUNPAY')
       .setCustomerInfo(custom);
-    await super.update(param);
+    await super.update({
+      ...param,
+      ...param.company,
+    });
     return res;
   }
 }

+ 33 - 7
src/modules/payment/service/individual.ts

@@ -1,7 +1,7 @@
 import { CustomerEntity } from './../entity/customer';
 import { SunPayAdapter } from './../adapter/sunpay.adapter';
 import { IndividualEntity } from './../entity/individual';
-import { ALL, Config, Inject, Provide } from '@midwayjs/decorator';
+import { ALL, Config, Init, Inject, Provide } from '@midwayjs/decorator';
 import { BaseService } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
@@ -19,16 +19,30 @@ export class IndividualService extends BaseService {
   customerEntity: Repository<CustomerEntity>;
   @Config(ALL)
   config;
+  @Init()
+  async init() {
+    await super.init();
+    this.setEntity(this.individualEntity);
+  }
   /**
    * 添加或者更新当前商户的 individual 客户信息
    * @param param
    * @returns
    */
   async add(param) {
-    const { merchant } = this.ctx.admin;
+    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;
+    }
     const custom = {
       ...param,
-      out_user_id: merchant.id,
+      out_user_id: merchantId, // TODO 应该是我们平台的ID
       webhook_url: this.config.callback.sunpay,
       customer_type: 'INDIVIDUAL',
     };
@@ -36,16 +50,28 @@ export class IndividualService extends BaseService {
       .setChannel('SUNPAY')
       .setCustomerInfo(custom);
     param.merchant = {
-      id: merchant.id,
+      id: merchantId,  // TODO 应该是我们平台的ID
     };
-    await super.add(param);
+    await super.add({
+      ...param,
+      ...param.company,
+    });
     return res;
   }
   async update(param) {
-    const { merchant } = this.ctx.admin;
+    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;
+    }
     const custom = {
       ...param,
-      out_user_id: merchant.id,
+      out_user_id: merchantId,  // TODO 应该是我们平台的ID
       webhook_url: this.config.callback.sunpay,
       customer_type: 'INDIVIDUAL',
     };