Pārlūkot izejas kodu

refactor(WebhookDispatchService): 添加签名验证开关

- 在 WebhookDispatchService 的 dispatch 方法中添加 $noSign 参数
- 根据 $noSign 参数决定是否进行 Web
notfff 6 mēneši atpakaļ
vecāks
revīzija
715c49123c

+ 7 - 5
app/Module/ThirdParty/Services/WebhookDispatchService.php

@@ -31,7 +31,7 @@ class WebhookDispatchService
      * @return array
      * @throws \Exception
      */
-    public function dispatch(string $packageName, string $handlerRoute, Request $request): array
+    public function dispatch(string $packageName, string $handlerRoute, Request $request,$noSign = false): array
     {
         // 只支持POST请求
         if ($request->method() !== 'POST') {
@@ -40,12 +40,14 @@ class WebhookDispatchService
 
         // 获取服务配置
         $service = $this->getServiceByPackage($packageName);
-
-        // 验证Webhook签名(在实例化之前)
-        if (!$this->validateWebhookSignature($request, $service)) {
-            throw new \Exception('Webhook签名验证失败');
+        if(!$noSign){
+            // 验证Webhook签名(在实例化之前)
+            if (!$this->validateWebhookSignature($request, $service)) {
+                throw new \Exception('Webhook签名验证失败');
+            }
         }
 
+
         // 获取处理器类
         $handlerClass = $this->getPackageHandler($packageName, $handlerRoute);
 

+ 0 - 184
app/Module/ThirdParty/Tests/BaseArchitectureTest.php

@@ -1,184 +0,0 @@
-<?php
-
-namespace App\Module\ThirdParty\Tests;
-
-use App\Module\ThirdParty\Services\BaseRequest;
-use App\Module\ThirdParty\Services\BaseWebhook;
-use App\Module\ThirdParty\Services\WebhookDispatchService;
-use Illuminate\Http\Request;
-
-/**
- * ThirdParty基础架构测试
- * 
- * 测试请求基类、Webhook基类和分发服务的基本功能
- */
-class BaseArchitectureTest
-{
-    /**
-     * 测试请求基类
-     */
-    public function testBaseRequest()
-    {
-        echo "=== 测试BaseRequest基类 ===\n";
-        
-        try {
-            // 创建测试请求类
-            $testRequest = new class extends BaseRequest {
-                public function __construct()
-                {
-                    // 不调用parent::__construct,避免数据库依赖
-                    $this->serviceCode = 'test';
-                    $this->requestId = uniqid('test_', true);
-                    $this->startTime = microtime(true);
-                }
-                
-                protected function handler(array $params): array
-                {
-                    return [
-                        'success' => true,
-                        'message' => '测试请求处理成功',
-                        'params' => $params,
-                        'timestamp' => time(),
-                    ];
-                }
-                
-                // 重写方法以避免数据库依赖
-                protected function checkQuota(): bool { return true; }
-                protected function updateQuota(): void {}
-                protected function logRequest(array $params, array $result, bool $success): void {}
-                protected function getConfig(?string $key = null) { 
-                    return $key ? 'test_value' : ['test_key' => 'test_value']; 
-                }
-            };
-            
-            echo "✅ BaseRequest类实例化成功\n";
-            echo "✅ 请求ID: " . $testRequest->getRequestId() . "\n";
-            
-        } catch (\Exception $e) {
-            echo "❌ BaseRequest测试失败: " . $e->getMessage() . "\n";
-        }
-    }
-    
-    /**
-     * 测试Webhook基类
-     */
-    public function testBaseWebhook()
-    {
-        echo "\n=== 测试BaseWebhook基类 ===\n";
-        
-        try {
-            // 创建模拟请求
-            $request = Request::create('/test', 'POST', [
-                'action' => 'test',
-                'data' => 'test_data'
-            ]);
-            
-            // 创建测试Webhook类
-            $testWebhook = new class($request) extends BaseWebhook {
-                public function __construct(Request $request)
-                {
-                    // 不调用parent::__construct,避免数据库依赖
-                    $this->serviceCode = 'test';
-                    $this->request = $request;
-                    $this->requestId = uniqid('webhook_test_', true);
-                    $this->startTime = microtime(true);
-                }
-                
-                protected function handler(string $action, Request $request): array
-                {
-                    return [
-                        'success' => true,
-                        'message' => 'Webhook处理成功',
-                        'action' => $action,
-                        'data' => $request->all(),
-                        'timestamp' => time(),
-                    ];
-                }
-                
-                // 重写方法以避免数据库依赖
-                protected function validateSignature(): bool { return true; }
-                protected function logWebhook(string $action, array $requestData, array $result, bool $success): void {}
-                protected function getConfig(?string $key = null) { 
-                    return $key ? 'test_value' : ['test_key' => 'test_value']; 
-                }
-            };
-            
-            echo "✅ BaseWebhook类实例化成功\n";
-            echo "✅ 请求ID: " . $testWebhook->getRequestId() . "\n";
-            
-        } catch (\Exception $e) {
-            echo "❌ BaseWebhook测试失败: " . $e->getMessage() . "\n";
-        }
-    }
-    
-    /**
-     * 测试Webhook分发服务
-     */
-    public function testWebhookDispatchService()
-    {
-        echo "\n=== 测试WebhookDispatchService ===\n";
-        
-        try {
-            $service = new WebhookDispatchService();
-            
-            // 测试注册包处理器
-            WebhookDispatchService::registerPackageHandler('test_package', 'test_action', 'TestHandler');
-            echo "✅ 包处理器注册成功\n";
-            
-            // 测试检查包是否注册
-            $isRegistered = $service->isPackageRegistered('test_package');
-            echo "✅ 包注册检查: " . ($isRegistered ? '已注册' : '未注册') . "\n";
-            
-            // 测试检查处理器是否注册
-            $isHandlerRegistered = $service->isHandlerRegistered('test_package', 'test_action');
-            echo "✅ 处理器注册检查: " . ($isHandlerRegistered ? '已注册' : '未注册') . "\n";
-            
-            // 测试获取已注册包列表
-            $packages = $service->getRegisteredPackages();
-            echo "✅ 已注册包数量: " . count($packages) . "\n";
-            
-            // 测试批量注册
-            WebhookDispatchService::registerPackageHandlers('test_package2', [
-                'action1' => 'Handler1',
-                'action2' => 'Handler2',
-            ]);
-            echo "✅ 批量注册处理器成功\n";
-            
-            // 测试注销处理器
-            WebhookDispatchService::unregisterPackageHandler('test_package', 'test_action');
-            echo "✅ 处理器注销成功\n";
-            
-            // 测试注销整个包
-            WebhookDispatchService::unregisterPackageHandler('test_package2');
-            echo "✅ 包注销成功\n";
-            
-        } catch (\Exception $e) {
-            echo "❌ WebhookDispatchService测试失败: " . $e->getMessage() . "\n";
-        }
-    }
-    
-    /**
-     * 运行所有测试
-     */
-    public function runAllTests()
-    {
-        echo "开始ThirdParty基础架构测试...\n\n";
-        
-        $this->testBaseRequest();
-        $this->testBaseWebhook();
-        $this->testWebhookDispatchService();
-        
-        echo "\n=== 测试完成 ===\n";
-        echo "✅ 所有基础架构组件测试通过\n";
-        echo "✅ 请求基类功能正常\n";
-        echo "✅ Webhook基类功能正常\n";
-        echo "✅ 分发服务功能正常\n";
-        echo "\n基础架构已准备就绪,可以开始创建具体的第三方包!\n";
-    }
-}
-
-// 如果直接运行此文件,执行测试
-if (php_sapi_name() === 'cli' && isset($argv[0]) && basename($argv[0]) === 'BaseArchitectureTest.php') {
-    $test = new BaseArchitectureTest();
-    $test->runAllTests();
-}

+ 33 - 0
app/Module/ThirdParty/Tests/TestTixian.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Module\ThirdParty\Tests;
+
+use App\Module\ThirdParty\Services\WebhookDispatchService;
+use Illuminate\Support\Facades\Request;
+use Tests\TestCase;
+
+class TestTixian extends TestCase
+{
+
+    public function testA()
+    {
+        $user_id                = 39113;
+        $ac                     = 2;
+        $WebhookDispatchService = new WebhookDispatchService();
+        $request                = new \Illuminate\Http\Request();
+        $request->header('CONTENT_TYPE', 'json');
+        $in = new  \Symfony\Component\HttpFoundation\InputBag();
+        $in->add([
+                     "order_id" => "140",
+                     "user_id"  => "10002",
+                     "amount"   => "1"
+                 ]);
+        $request->setJson($in);
+        $request->setMethod('POST');
+        $res = $WebhookDispatchService->dispatch('urs', 'withdraw', $request, true);
+
+        dump($res);
+
+    }
+
+}