Преглед изворни кода

feat(): 客户接口调用成功的回调处理

max пре 7 месеци
родитељ
комит
0f7ad3ca85
1 измењених фајлова са 41 додато и 2 уклоњено
  1. 41 2
      src/modules/payment/service/customer.ts

+ 41 - 2
src/modules/payment/service/customer.ts

@@ -5,8 +5,9 @@ import { BaseService } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
 import { CustomerEntity } from '../entity/customer';
+import axios from "axios";
 /**
- * 描述
+ * 客户管理
  */
 @Provide()
 export class CustomerService extends BaseService {
@@ -41,19 +42,30 @@ export class CustomerService extends BaseService {
       },
     });
     if (!customer) {
+      // 获取商户的回调
+      await this.request(customer.webhook_url, {
+        biz_status: 'FAIL',
+        biz_type: 'CREATECUSTOMER',
+        data: data
+      })
       return {
         is_success: false,
         message: '客户不存在',
       };
     }
     if (customer.status == 'SUCCESS') {
+      // 获取商户的回调
+      await this.request(customer.webhook_url, {
+        biz_status: 'SUCCESS',
+        biz_type: 'CREATECUSTOMER',
+        data: data
+      })
       return {
         is_success: true,
         message: '成功',
       };
     }
     const status = biz_status === 'FAIL' ? -1 : 1;
-
     customer.status = biz_status;
     const business = await this.businessEntity.findOne({
       where: {
@@ -89,9 +101,36 @@ export class CustomerService extends BaseService {
       }
     }
     await this.customerEntity.save(customer);
+    // 获取商户的回调
+    await this.request(customer.webhook_url, {
+      biz_status: 'SUCCESS',
+      biz_type: 'CREATECUSTOMER',
+      data: data
+    })
     return {
       is_success: true,
       message: '成功',
     };
   }
+
+  /**
+   * 发送请求到 商户 API
+   */
+  private async request( url, data?: any) {
+    try {
+      if (url) {
+        await axios({
+          method: 'post',
+          url,
+          data
+        });
+      }
+    } catch (error) {
+      console.log(error);
+      // if (axios.isAxiosError(error) && error.response) {
+      //   throw new Error(`FUSIONPay API Error2: ${error.response.data.msg}`);
+      // }
+      // throw error;
+    }
+  }
 }