Kaynağa Gözat

代付接口优化

增加hambit BRL代付
test 7 ay önce
ebeveyn
işleme
c99a024a78

+ 73 - 15
src/modules/dj/service/channels/hambitBra.ts

@@ -15,6 +15,7 @@ const QUERY_URL = '/api/v3/bra/query/collectingOrder'
 const BALANCE_URL = '/api/v3/bra/query/balance'
 const BANK_URL = '/api/v3/bra/query/bank';
 const WITHDRAW_URL = '/api/v3/bra/createTransferOrder';
+const QUERY_WITHDRAW_URL = '/api/v3/bra/query/transferOrder'
 
 const NOTIFY_HOST = `http://157.175.73.225/api/admin/dj/open/hambit/bra/notifyOrder`;
 const WITHDRAW_NOTIFY_HOST = `http://157.175.73.225/api/admin/dj/open/hambit/bra/notifyWithdraw`;
@@ -228,30 +229,34 @@ export class HambitBraService extends BaseService {
       }
     });
     this.logger.info('代付接口返回', param, JSON.stringify(res.data));
-    const { success, msg, data } = res.data;
+    const { success, msg, desc, data } = res.data;
     if (success && data.orderId) {
       return {
+        status: 2,
         traceNo: data.orderId
       };
     } else {
-      throw new Error(msg);
+      return {
+        status: 3,
+        message: desc || msg
+      }
     }
   }
 
   async handleWithdrawNotify(payload, headers) {
-    // const sign = headers['sign'];
-    // const timestamp = headers['timestamp'];
-    // const nonce = headers['nonce'];
-    // const signStr = this.utils.signSort({
-    //   timestamp,
-    //   nonce,
-    //   access_key: ACCESS_KEY,
-    //   ...payload
-    // });
-    // const validSign = this.utils.signByHmacSha1(signStr, SECRET_KEY);
-    // if (sign !== validSign) {
-    //   throw new Error('sign error');
-    // }
+    const sign = headers['sign'];
+    const timestamp = headers['timestamp'];
+    const nonce = headers['nonce'];
+    const signStr = this.utils.signSort({
+      timestamp,
+      nonce,
+      access_key: ACCESS_KEY,
+      ...payload
+    });
+    const validSign = this.utils.signByHmacSha1(signStr, SECRET_KEY);
+    if (sign !== validSign) {
+      throw new Error('sign error');
+    }
     if (+payload.orderStatusCode !== 8 && +payload.orderStatusCode !== 4 && +payload.orderStatusCode !== 16) {
       throw new Error('order no result');
     }
@@ -268,4 +273,57 @@ export class HambitBraService extends BaseService {
       orderNo: payload.externalOrderId
     };
   }
+
+  async queryWithdraw(payload) {
+    const param = {
+      externalOrderId: payload.orderNo
+    };
+    const timestamp = +moment();
+    const nonce = uuidv4();
+    const signStr = this.utils.signSort({
+      timestamp,
+      nonce,
+      access_key: ACCESS_KEY,
+      ...param
+    });
+    const sign = this.utils.signByHmacSha1(signStr, SECRET_KEY);
+    const res = await this.httpService.post(HOST + QUERY_WITHDRAW_URL, param, {
+      headers: {
+        'access_key': ACCESS_KEY,
+        'timestamp': timestamp,
+        'nonce': nonce,
+        'sign': sign
+      }
+    });
+    this.logger.info('查询代付接口返回', JSON.stringify(res.data));
+    const { success, msg, data = [] } = res.data;
+    const orderData = data.find(item => item.externalOrderId === payload.orderNo)
+    if (success && orderData) {
+      if(+orderData.orderStatus === 4 || +orderData.orderStatus === 16) {
+        return {
+          date: new Date(),
+          status: 3,
+          message: orderData.errorMsg,
+          orderNo: orderData.externalOrderId
+        };
+      } else if(+orderData.orderStatus === 8) {
+        return {
+          date: new Date(),
+          status: 8,
+          message: orderData.errorMsg,
+          orderNo: orderData.externalOrderId
+        };
+      }
+      return {
+        status: 1,
+        traceNo: orderData.orderId,
+        date: new Date()
+      };
+    } else {
+      return {
+        status: 3,
+        message: '渠道订单不存在'
+      };
+    }
+  }
 }

+ 17 - 11
src/modules/dj/service/repay.ts

@@ -123,20 +123,21 @@ export class RepayService extends BaseService {
       await this.validQueryParam(payload);
       const merchant = await this.payService.validMerchant(payload.mchId);
       await this.payService.validSign(payload, merchant);
-      const order = await this.withdrawService.findByOutOrderNo(
+      const withdraw = await this.withdrawService.findByOutOrderNo(
         payload.outOrderNo
       );
-      if (!order || order.mchId !== payload.mchId) {
+      if (!withdraw || withdraw.mchId !== payload.mchId) {
         throw new Error('订单不存在');
       }
       return {
         mchId: payload.mchId,
-        orderNo: order.orderNo,
+        orderNo: withdraw.orderNo,
         outOrderNo: payload.outOrderNo,
-        amount: order.amount,
-        status: order.status,
-        charge: order.charge,
-        remark: order.remark,
+        amount: withdraw.amount,
+        status: withdraw.status,
+        charge: withdraw.charge,
+        message: withdraw.remark,
+        currency: withdraw.currency
       };
     } catch (err) {
       this.logger.error('查询失败', JSON.stringify(payload), err.message);
@@ -204,14 +205,19 @@ export class RepayService extends BaseService {
         currency: withdraw.currency,
         freeze: (+withdraw.amount + +withdraw.charge)
       })
+      let status = withdraw.status;
       if (withdraw.code) {
-        this.withdrawStateService.stateTo(withdraw.status, this.withdrawStateService.STATUS.PROCESSIONG, withdraw);
+        status = await this.withdrawStateService.stateTo(withdraw.status, this.withdrawStateService.STATUS.PROCESSIONG, withdraw);
       }
       return {
+        mchId: payload.mchId,
         orderNo: withdraw.orderNo,
-        outOrderNo: withdraw.outOrderNo,
-        currency: withdraw.currency,
-        status: withdraw.status
+        outOrderNo: payload.outOrderNo,
+        amount: withdraw.amount,
+        status: status,
+        charge: withdraw.charge,
+        message: withdraw.remark,
+        currency: withdraw.currency
       };
     } catch (err) {
       this.logger.error('代付失败', JSON.stringify(payload), err.message);

+ 1 - 1
src/modules/dj/service/withdraw.ts

@@ -195,7 +195,7 @@ export class WithdrawService extends BaseService {
           withdraw.traceNo = data.traceNo
         }
         if (data.message) {
-          withdraw.remark = data.message + '\n' + withdraw.remark;
+          withdraw.remark = data.message;
         }
         await this.withdrawStateService.stateTo(+withdraw.status, +data.status, withdraw);
       }

+ 2 - 1
src/modules/dj/service/withdrawNotify.ts

@@ -75,8 +75,9 @@ export class WithdrawNotifyService extends BaseService {
         outOrderNo: withdrawOrder.outOrderNo,
         amount: withdrawOrder.amount,
         charge: withdrawOrder.charge,
+        currency: withdrawOrder.currency,
         status: withdrawOrder.status,
-        remark: withdrawOrder.remark
+        message: withdrawOrder.remark
       };
       const signStr = this.utils.signSort(params);
       params.sign = md5(signStr + `&key=${merchant.key}`);

+ 9 - 2
src/modules/dj/service/withdrawState.ts

@@ -156,12 +156,19 @@ export class WithdrawStateService extends BaseService {
                 channelCharge = channel.feeMin;
             }
             withdraw.channelCharge = channelCharge;
-            const { traceNo } = await this.dispatchService.withdraw(withdraw);
-            withdraw.traceNo = traceNo;
+            const { status, traceNo, message } = await this.dispatchService.withdraw(withdraw);
+            if (+status === 3) {
+                this.logger.info(withdraw.orderNo, `------------发起代付失败-------------`);
+                withdraw.status = 3;
+                withdraw.remark = message
+            } else {
+                withdraw.traceNo = traceNo;
+            }
         } catch (err) {
             withdraw.remark = err.message;
         }
         await this.withdrawEntity.update(withdraw.id, withdraw);
+        return withdraw.status;
     }
 
     // 重新发起:失败 =》等待