Forráskód Böngészése

refactor(lakala):重构配置类以支持V3接口

- 修改 Config 类继承关系,移除 Configuration 继承- 新增 getV3Config 方法返回 V3 版本配置对象
- 调整 TransactionService 构造函数使用 V3 配置
- 移除旧版配置方法如 getAppId、getSerialNo 等
- 更新证书路径和调试模式配置逻辑
runphp 4 hónapja
szülő
commit
b048670374
1 módosított fájl, 46 hozzáadás és 8 törlés
  1. 46 8
      src/Config.php

+ 46 - 8
src/Config.php

@@ -3,6 +3,7 @@
 namespace SixShop\Lakala;
 
 use SixShop\Lakala\Extension;
+use SixShop\Lakala\OpenAPISDK\V2\V2Configuration;
 use SixShop\Lakala\OpenAPISDK\V3\Configuration;
 use SixShop\System\Trait\ConfigTrait;
 
@@ -35,18 +36,55 @@ class Config
      * 获取V3配置
      * @return Configuration
      */
-    public function getV3Config():Configuration
+    public function getV3Config(): Configuration
     {
-        return new Configuration([
+        return new Configuration($this->getCommonConfig());
+    }
+
+    /**
+     * 获取V2配置
+     * @return V2Configuration
+     */
+    public function getV2Config(): V2Configuration
+    {
+        return new V2Configuration($this->getCommonConfig());
+    }
+
+    private function getCommonConfig(): array
+    {
+        $signCert = $this->getConfig('sign_cert');
+        $verifyCert = $this->getConfig('verify_cert');
+        $gateway = $this->getConfig('gateway');
+
+        // 校验必要字段
+        if (!is_array($signCert) || !isset($signCert[0])) {
+            throw new \InvalidArgumentException("sign_cert must be an array and contain at least one element.");
+        }
+
+        if (!is_array($verifyCert) || !isset($verifyCert[0])) {
+            throw new \InvalidArgumentException("verify_cert must be an array and contain at least one element.");
+        }
+
+        if (!is_array($gateway) || !isset($gateway['test'], $gateway['product'])) {
+            throw new \InvalidArgumentException("gateway config must contain both 'test' and 'product' keys.");
+        }
+
+        $environment = $this->getConfig('environment');
+
+        return [
             'app_id' => $this->getConfig('appid'),
             'serial_no' => $this->getConfig('serial_no'),
             'sm4_key' => null,
-            'merchant_private_key_path' => public_path().$this->getConfig('sign_cert')[0],
-            'lkl_certificate_path' => public_path().$this->getConfig('verify_cert')[0],
-            'app_debug' => $this->getConfig('environment') === 'test',
-            'host_test' => $this->getConfig('gateway')['test'],
-            'host_pro' => $this->getConfig('gateway')['product'],
-        ]);
+            'merchant_private_key_path' => $this->buildPath($signCert[0]),
+            'lkl_certificate_path' => $this->buildPath($verifyCert[0]),
+            'app_debug' => $environment === 'test',
+            'host_test' => $gateway['test'],
+            'host_pro' => $gateway['product'],
+        ];
+    }
 
+    private function buildPath(string $relativePath): string
+    {
+        return rtrim(public_path(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($relativePath, DIRECTORY_SEPARATOR);
     }
 }