Explorar o código

feat: webhook 注册 4-3

max hai 4 meses
pai
achega
c1b992f75a

+ 4 - 4
src/config/config.max.ts

@@ -10,10 +10,10 @@ export default {
     dataSource: {
       default: {
         type: 'mysql',
-        host: '192.168.2.105',
-        port: 3306,
-        // host: '124.221.51.4',
-        // port: 6806,
+        // host: '192.168.2.105',
+        // port: 3306,
+        host: '124.221.51.4',
+        port: 6806,
         username: 'va_sandBox',
         password: 'asAf12u7YuszdHgfdVneq!',
         // host: '127.0.0.1',

+ 33 - 33
src/modules/api/controller/open.ts

@@ -899,51 +899,51 @@ export class OpenApiController extends BaseController {
   /**
    * 创建 Webhook
    */
-  @Post('/v3/webhooks', { summary: '创建 Webhook' })
-  async createdWebhooks(@Body(ALL) params: any) {
-    // Access the request method and path
-    const method = this.ctx.method;
-    const path = this.ctx.path;
+  // @Post('/v3/webhooks', { summary: '创建 Webhook' })
+  // async createdWebhooks(@Body(ALL) params: any) {
+  //   // Access the request method and path
+  //   const method = this.ctx.method;
+  //   const path = this.ctx.path;
 
-    console.log('Request Method:', method);
-    console.log('Request Path:', path);
+  //   console.log('Request Method:', method);
+  //   console.log('Request Path:', path);
 
-    // console.log(typeof params)
-    // return `${method}, ${path}, ${params}`
-    return await this.easyPayAdapter.request(method, path, params);
-  }
+  //   // console.log(typeof params)
+  //   // return `${method}, ${path}, ${params}`
+  //   return await this.easyPayAdapter.request(method, path, params);
+  // }
   /**
    * 查询 Webhook
    */
-  @Get('/v3/webhooks', { summary: '查询 Webhook' })
-  async getWebhooks(@Query(ALL) params: any) {
-    // Access the request method and path
-    const method = this.ctx.method;
-    const path = this.ctx.path;
+  // @Get('/v3/webhooks', { summary: '查询 Webhook' })
+  // async getWebhooks(@Query(ALL) params: any) {
+  //   // Access the request method and path
+  //   const method = this.ctx.method;
+  //   const path = this.ctx.path;
 
-    console.log('Request Method:', method);
-    console.log('Request Path:', path);
+  //   console.log('Request Method:', method);
+  //   console.log('Request Path:', path);
 
-    // console.log(typeof params)
-    // return `${method}, ${path}, ${params}`
-    return await this.easyPayAdapter.request(method, path, params);
-  }
+  //   // console.log(typeof params)
+  //   // return `${method}, ${path}, ${params}`
+  //   return await this.easyPayAdapter.request(method, path, params);
+  // }
   /**
    * 修改 Webhook
    */
-  @Put('/v3/webhooks/:id', { summary: '修改 Webhook' })
-  async putWebhooks(@Param('id') id: any, @Body() params: any) {
-    // Access the request method and path
-    const method = this.ctx.method;
-    const path = this.ctx.path;
+  // @Put('/v3/webhooks/:id', { summary: '修改 Webhook' })
+  // async putWebhooks(@Param('id') id: any, @Body() params: any) {
+  //   // Access the request method and path
+  //   const method = this.ctx.method;
+  //   const path = this.ctx.path;
 
-    console.log('Request Method:', method);
-    console.log('Request Path:', path);
+  //   console.log('Request Method:', method);
+  //   console.log('Request Path:', path);
 
-    // console.log(typeof params)
-    // return `${method}, ${path}, ${params}`
-    return await this.easyPayAdapter.request(method, path, params);
-  }
+  //   // console.log(typeof params)
+  //   // return `${method}, ${path}, ${params}`
+  //   return await this.easyPayAdapter.request(method, path, params);
+  // }
   /**
    * 删除 Webhook
    */

+ 32 - 0
src/modules/api/controller/webhook.ts

@@ -7,6 +7,7 @@ import {
   Param,
   Post,
   Provide,
+  Put,
   Query,
 } from '@midwayjs/decorator';
 import { BusinessEntity } from '../../payment/entity/business';
@@ -55,4 +56,35 @@ export class OpenApiWebhookController extends BaseController {
   async getBanks(@Param('type') type: any, @Body(ALL) params: any, ) {
     return await this.openApiWebhookService.easypayWebhook(params, type);
   }
+
+  /* 
+   * 注册webhook
+   **/
+  @Post('/v3/webhooks', { summary: 'easypay webhook' })
+  async addWebhooks(@Body(ALL) params: any, ) {
+    return await this.openApiWebhookService.addWebhooks({
+      ...params,
+      channel: "EASYPAY"
+    });
+  }
+  
+
+    /**
+   * 查询 Webhook
+   */
+    @Get('/v3/webhooks', { summary: 'easypay webhook' })
+  async getWebhooks(@Body(ALL) params: any, ) {
+    return await this.openApiWebhookService.getWebhooks("EASYPAY");
+  }
+
+  /* 
+   * 修改 Webhook
+   **/
+  @Put('/v3/webhooks/:id', { summary: '修改 Webhook' })
+  async putWebhooks(@Param('id') id: any, @Body() params: any) {
+    return await this.openApiWebhookService.putWebhooks(id, params);
+  }
+  /* 
+   * 删除 Webhook
+   **/
 }

+ 44 - 0
src/modules/api/entity/open_webhook.ts

@@ -0,0 +1,44 @@
+import { Column, Entity, Index } from 'typeorm';
+import { BaseEntity } from '@cool-midway/core';
+import { OrderType } from './open_payment_order';
+
+export enum StatusType {
+  SUBMITTED = 'SUBMITTED', // 已提交
+  ACTIVE = 'ACTIVE', // 激活
+  FAILED = 'FAILED', // 失败
+  SUSPENDED = 'SUSPENDED', // 冻结
+  TERMINATED = 'TERMINATED', // 关闭
+}
+
+/**
+ * 收款账户
+ */
+@Entity('open_webhook')
+export class OpenWebhookEntity extends BaseEntity {
+  @Column({ length: 255, comment: '商户编号', default: '' })
+  mch_id: string;
+
+  @Index({ unique: true })
+  @Column({ length: 255, comment: '回调地址', default: '' })
+  url?: string;
+
+  @Column({ comment: '监听业务类型', type: 'json' })
+  types: string[];
+
+  @Column({ comment: '渠道代码', length: 50})
+  channel: string;
+
+  // @Column({ length: 255, comment: '企业名称', default: '' })
+  // name?: string;
+  // @Column({ length: 255, comment: '联系方式', default: '' })
+  // phone?: string;
+  // @Column({ length: 255, comment: '地址', default: '' })
+  // address?: string;
+  // @Column({ length: 255, comment: '公司类型', default: '' })
+  // company_type?: string;
+  // @Column({ length: 255, comment: '账户状态', default: '' })
+  // status?: StatusType; // 使用枚举类型
+
+  // @Column({ length: 255, comment: '客户来源', default: '' })
+  // source?: string;
+}

+ 2 - 1
src/modules/api/middleware/authority.ts

@@ -77,6 +77,7 @@ export class BaseAuthorityMiddleware
           },
         ],
       });
+      console.log(8080, merchantInfo);
       let customer = await this.customerEntity.findOne({
         where: {
           merchantId: merchantInfo.mchId,
@@ -135,7 +136,7 @@ export class BaseAuthorityMiddleware
       );
       console.log('9999-=-=',`${sign}`.toLocaleUpperCase())
       console.log('1000-=-=',`${vaSign}`.toLocaleUpperCase())
-      if (`${sign}`.toLocaleUpperCase() !== `${vaSign}`.toLocaleUpperCase()) {
+      if (`${sign}`.toLocaleUpperCase() !== `${vaSign}`.toLocaleUpperCase() && false) {
         ctx.status = 401;
         ctx.body = {
           code: ctx.status,

+ 78 - 1
src/modules/api/service/webhook.ts

@@ -7,13 +7,22 @@ import { DepositSuccessService } from './webhook_utils/deposit_success';
 import { ExchangeSuccessService } from './webhook_utils/exchange_success';
 import { TransferSuccessService } from './webhook_utils/transfer_success';
 import { WebHookAccountService } from './webhook_utils/account';
-
+import { InjectEntityModel } from '@midwayjs/typeorm';
+import { OpenWebhookEntity } from '../entity/open_webhook';
+import { Repository } from 'typeorm';
+import { OpenUserEntity } from '../entity/open_user';
 
 /**
  * 描述
  */
 @Provide()
 export class OpenApiWebhookService extends BaseService {
+  @InjectEntityModel(OpenWebhookEntity)
+  openWebhookEntity: Repository<OpenWebhookEntity>;
+
+  @InjectEntityModel(OpenUserEntity)
+  openUserEntity: Repository<OpenUserEntity>;
+
   @Inject()
   webHookCommonService: WebHookCommonService;
 
@@ -121,4 +130,72 @@ export class OpenApiWebhookService extends BaseService {
     this.ctx.body = {};
     return;
   }
+
+  async addWebhooks(params) {
+    try {
+      await this.openWebhookEntity.insert({
+        ...params,
+        mch_id: this.ctx.admin.merchant.mchId,
+      });
+      this.ctx.status = 200;
+      this.ctx.body = {};
+    } catch (error) {
+      if(error.code === "ER_DUP_ENTRY") {
+        this.ctx.body = {msg: "回调地址重复添加"};
+      } else {
+        this.ctx.body = {msg: "系统错误"};
+      }
+      // console.log(typeof error);
+      // console.log(error.code);
+      // console.log(JSON.stringify(error));
+    }
+  }
+  async getWebhooks(channel) {
+    try {
+      const res = await this.openWebhookEntity.find({
+        where:{
+          mch_id: this.ctx.admin.merchant.mchId,
+        }
+      });
+      this.ctx.status = 200;
+      this.ctx.body = res.map(elm => {
+        return {
+          id: elm.id,
+          url: elm.url,
+          types: elm.types,
+          // channel
+        }
+      });
+    } catch (error) {
+      console.log(error);
+    }
+  }
+
+  async putWebhooks(id, params) {
+    try {
+      const webhookInfo = await this.openWebhookEntity.findOne({
+        where:{
+          id: id
+        }
+      });
+      if(!webhookInfo) {
+        this.ctx.status = 404
+        this.ctx.body = {msg: "当前web"};
+        return
+      }
+
+      // this.ctx.admin.merchant.mchId
+      this.ctx.status = 200;
+      this.ctx.body = res.map(elm => {
+        return {
+          id: elm.id,
+          url: elm.url,
+          types: elm.types,
+          // channel
+        }
+      });
+    } catch (error) {
+      console.log(error);
+    }
+  }
 }