max 7 сар өмнө
parent
commit
4b8ecb9146

+ 2 - 0
src/modules/payment/entity/wallet.ts

@@ -15,6 +15,8 @@ export class WalletEntity extends BaseEntity {
   channel: string;
   @Column({ comment: '客户ID', length: 100 })
   customerId: string;
+  @Column({ comment: '回调地址'})
+  webhook_url!: string;
   @Column({ comment: '货币类型', length: 20 })
   currency: string;
   @Column({

+ 87 - 19
src/modules/payment/service/wallet.ts

@@ -1,11 +1,12 @@
 import { CustomerEntity } from './../entity/customer';
-import { Init, Inject, Provide } from '@midwayjs/decorator';
+import {ALL, Config, Init, Inject, Provide} from '@midwayjs/decorator';
 import { BaseService, CoolCommException } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
 import { WalletEntity } from '../entity/wallet';
 import { PaymentService } from './payment';
 import { MerchantEntity } from '../entity/merchant';
+import axios from "axios";
 /**
  * 描述
  */
@@ -21,24 +22,14 @@ export class WalletService extends BaseService {
   paymentService: PaymentService;
   @Inject()
   ctx;
+  @Config(ALL)
+  config;
   /**
    * 描述
    */
   async add(param: any) {
-    const { userId } = this.ctx.admin;
-    const merchant = await this.merchantEntity.findOne({
-      where: {
-        userId,
-      },
-    });
-    if (!merchant) {
-      throw new CoolCommException('商户不存在');
-    }
-
-    const customer = await this.customerEntity.findOne({
-      where: {
-        merchantId: merchant.id.toString(),
-      },
+    const customer = await this.customerEntity.findOneBy({
+      customer_id: param.customerId
     });
     if (!customer) {
       throw new CoolCommException('客户不存在,请先创建认证');
@@ -51,12 +42,13 @@ export class WalletService extends BaseService {
         .createWallet({
           ...param,
           customerId: customer.customer_id,
+          webhook_url: this.getWebhook_url(param)
         });
       status = res.status;
     }
-
-    param.merchantId = merchant.id;
+    param.merchantId = customer.merchantId;
     param.customerId = customer.customer_id;
+    param.webhook_url = customer.webhook_url;
     param.status = status;
     param.config = {};
     return super.add(param);
@@ -80,12 +72,24 @@ export class WalletService extends BaseService {
       },
     });
     if (!wallet) {
+      // 获取商户的回调
+      await this.request(wallet.webhook_url, {
+        biz_status: 'FAIL',
+        biz_type: 'CREATECUSTOMER',
+        data: data
+      })
       return {
         is_success: false,
         message: '钱包不存在',
       };
     }
     if (wallet.status == 'SUCCESS') {
+      // 获取商户的回调
+      await this.request(wallet.webhook_url, {
+        biz_status: 'SUCCESS',
+        biz_type: 'CREATECUSTOMER',
+        data: data
+      })
       return {
         is_success: true,
         message: '成功',
@@ -97,6 +101,12 @@ export class WalletService extends BaseService {
     });
     wallet.config = res.data;
     await this.walletEntity.save(wallet);
+    // 获取商户的回调
+    await this.request(wallet.webhook_url, {
+      biz_status: 'SUCCESS',
+      biz_type: 'CREATECUSTOMER',
+      data: data
+    })
     return {
       is_success: true,
       message: '成功',
@@ -126,10 +136,12 @@ export class WalletService extends BaseService {
   }
 
   async list(params: any, option: any, connectionName?: any) {
-    const { merchant } = this.ctx.admin;
+    // const { merchant } = this.ctx.admin;
+    const merchantId = await this.getMerchantId(params)
+    console.log(params)
     const res = await this.walletEntity.find({
       where: {
-        merchantId: merchant.id.toString(),
+        merchantId: merchantId,
         channel: params.channel,
         status: 'ACTIVE',
       },
@@ -137,4 +149,60 @@ export class WalletService extends BaseService {
     });
     return res;
   }
+
+  async page(params: any) {
+    const merchantId = await this.getMerchantId(params);
+    console.log(params, merchantId);
+
+    // Manually quote the merchantId (only if absolutely necessary)
+    const query = `SELECT * FROM wallet WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`;
+
+    return this.sqlRenderPage(query, params, false);
+  }
+
+  private _isOpenApi = false
+
+  setIsOpenApi(payload = false) {
+    this._isOpenApi = payload
+  }
+
+  getWebhook_url(params) {
+    if (this._isOpenApi) {
+      return params.webhook_url
+    }
+    return this.config.callback.sunpay
+  }
+
+  /**
+   * 发送请求到 商户 API
+   */
+  private async request( url, data?: any) {
+    try {
+      if (url) {
+        await axios({
+          method: 'post',
+          url,
+          data
+        });
+      }
+    } catch (error) {
+      console.log(error);
+    }
+  }
+
+  async getMerchantId(params) {
+    let merchantId;
+    if (this._isOpenApi) {
+      merchantId = params.out_user_id;
+    } else if (params?.mch_id || params?.mchId) {
+      // const  merchantInfo = await this.merchantEntity.findOneBy({
+      //    mchId: params?.mch_id
+      //  })
+      merchantId = params?.mch_id || params?.mchId
+    } else {
+      const { merchant } = this.ctx.admin;
+      merchantId = merchant.mchId;
+    }
+    return merchantId
+  }
 }