gatsby преди 3 седмици
родител
ревизия
a273c6ea05
променени са 2 файла, в които са добавени 44 реда и са изтрити 15 реда
  1. 32 12
      backend/app/plugin/module_payment/openapi/service.py
  2. 12 3
      backend/tests/test_apikey_sign.py

+ 32 - 12
backend/app/plugin/module_payment/openapi/service.py

@@ -52,38 +52,58 @@ class OpenTransferService:
         cls,
         auth: AuthSchema,
         order_no: str,
-    ):
+    ) -> bool:
+        """
+        发送转账回调通知
+        
+        参数:
+        - auth: 认证信息
+        - order_no: 订单号
+        
+        返回:
+        - bool: 是否成功发送通知
+        """
         try:
-            crud = TransferCRUD(auth)
-            transfer = await crud.get_by_order_no(order_no)
+            transfer_crud = TransferCRUD(auth)
+            transfer = await transfer_crud.get_by_order_no(order_no)
 
             if not transfer or not transfer.out_biz_no:
-                return
+                log.info("回调通知: 订单不存在或缺少 out_biz_no, order_no={}", order_no)
+                return False
 
-            crud = OpenTransferCRUD(auth)
-            open_data = await crud.get(out_biz_no=transfer.out_biz_no)
+            open_transfer_crud = OpenTransferCRUD(auth)
+            open_data = await open_transfer_crud.get(out_biz_no=transfer.out_biz_no)
 
             if not open_data:
-                return
+                log.info("回调通知: 开放转账记录不存在, out_biz_no={}", transfer.out_biz_no)
+                return False
 
             conf = await OpenConfService.get_conf_service(auth)
             if not conf:
-                return
+                log.info("回调通知: 开放转账配置不存在, tenant_id={}", auth.tenant_id)
+                return False
+
+            if not conf.return_url:
+                log.info("回调通知: 未配置回调地址, tenant_id={}", auth.tenant_id)
+                return False
 
             result = TransferOutSchema.model_validate(transfer).model_dump(exclude_none=True)
             result["third_biz_no"] = open_data.third_biz_no
+            
             notify_id = f"n{get_snowflake_id()}"
             timestamp = int(time.time() * 1000)
             content = json.dumps(result)
 
-            async with aiohttp.ClientSession() as session:
-                log.info(f"调用回调接口: {conf.return_url}")
+            timeout = aiohttp.ClientTimeout(total=30)
+            async with aiohttp.ClientSession(timeout=timeout) as session:
+                log.info("回调通知: order_no={}, url={}, notify_id={}", order_no, conf.return_url, notify_id)
                 await fetch_manual_retry(
                     session, conf.return_url, notify_id, conf.app_id, timestamp, content
                 )
 
-        except: #ignore
-            pass
+        except Exception as e:
+            log.error("回调通知异常: order_no={}, error={}", order_no, e, exc_info=True)
+            return False
 
 
     @classmethod

+ 12 - 3
backend/tests/test_apikey_sign.py

@@ -3,19 +3,28 @@ from app.plugin.module_payment.apikey.service import TenantApiKeyService
 
 class TestApiKeySign(unittest.TestCase):
 
+    def test_qq(self):
+        data = {
+            "third_biz_no": "123424202604270001"
+        }
+        sign = TenantApiKeyService.generate_signature(
+            "c0764f3e3a0af4bec292ceaf8b38d054d64e41bff36333a7f24d7cafada7cba98b8ac4dcac49367907e51b0b4e1566dec473198ad729ad1cbd5734b70322d7cd",
+            request_data=data)
+        print(sign)
+
     def test_tt(self):
         data = {
             "account_book_id": "2088480770941200",
             "amount": 1.00,
-            "order_title": "Apikey转账",
-            "third_biz_no": "123424202604270001",
+            "order_title": "Apikey转账2",
+            "third_biz_no": "123424202604270022",
             "payee_info": {
                 "identity_type": "ALIPAY_ACCOUNT",
                 "name": "钱红武",
                 "identity": "15399795365"
             }
         }
-        sign = TenantApiKeyService.generate_signature("b18d87cee2b7aec9dfb62d5e1eebf6ff22c51019587fcb4adc8d863deef4439900c7dda64adc364a44e95a76a531064adbd5fa20b21de8f1db73518b0b9ef150", request_data=data)
+        sign = TenantApiKeyService.generate_signature("c0764f3e3a0af4bec292ceaf8b38d054d64e41bff36333a7f24d7cafada7cba98b8ac4dcac49367907e51b0b4e1566dec473198ad729ad1cbd5734b70322d7cd", request_data=data)
         print(sign)
 
     def test_sign(self) -> None: