llp 3 дней назад
Родитель
Сommit
052d8098d5

+ 20 - 4
packages/Longyi/Pay/Afterpay/src/Config/system.php

@@ -38,16 +38,32 @@ return [
                 'channel_based' => false,
                 'locale_based' => true,
             ], [
-                'name' => 'client_id',
-                'title' => 'Merchant ID',
+                'name' => 'client_id_us',
+                'title' => 'Merchant ID (US)',
                 'type' => 'text',
                 'depend' => 'active:1',
                 'validation' => 'required_if:active,1',
                 'channel_based' => false,
                 'locale_based' => true,
             ], [
-                'name' => 'secret_id',
-                'title' => 'Merchant Key',
+                'name' => 'secret_id_us',
+                'title' => 'Merchant Key (US)',
+                'type' => 'password',
+                'depend' => 'active:1',
+                'validation' => 'required_if:active,1',
+                'channel_based' => false,
+                'locale_based' => true,
+            ], [
+                'name' => 'client_id_ca',
+                'title' => 'Merchant ID (CA)',
+                'type' => 'text',
+                'depend' => 'active:1',
+                'validation' => 'required_if:active,1',
+                'channel_based' => false,
+                'locale_based' => true,
+            ], [
+                'name' => 'secret_id_ca',
+                'title' => 'Merchant Key (CA)',
                 'type' => 'password',
                 'depend' => 'active:1',
                 'validation' => 'required_if:active,1',

+ 42 - 3
packages/Longyi/Pay/Afterpay/src/Payment/Afterpay.php

@@ -29,11 +29,36 @@ class Afterpay extends Payment
             $this->capturePaymentUlr = 'https://global-api.afterpay.com/v2/payments/capture';
             $this->configurationUlr = 'https://global-api.afterpay.com/v2/configuration';
         }
-        $this->clientId = $this->getConfigData('client_id');
-        $this->apikey = $this->getConfigData('secret_id');
+        $this->clientId = $this->getConfigData('client_id_us');
+        $this->apikey = $this->getConfigData('secret_id_us');
         $this->secret = $this->getConfigData('webhook_id');
     }
 
+    /**
+     * 根据国家代码动态解析并设置 Afterpay 凭证
+     * 美国用 US 凭证,加拿大用 CA 凭证
+     */
+    protected function resolveCredentials(?string $countryCode = null): void
+    {
+        if ($countryCode === 'CA') {
+            $this->clientId = $this->getConfigData('client_id_ca');
+            $this->apikey = $this->getConfigData('secret_id_ca');
+        } else {
+            // 默认使用美国凭证 (US)
+            $this->clientId = $this->getConfigData('client_id_us');
+            $this->apikey = $this->getConfigData('secret_id_us');
+        }
+    }
+
+    /**
+     * 从当前订单或购物车获取国家代码
+     */
+    protected function getCurrentCountryCode(): ?string
+    {
+        $info = $this->getOrder() ?: Cart::getCart();
+        return $info->billing_address->country ?? null;
+    }
+
     protected $code  = 'afterpay';
 
     public function getRedirectUrl()
@@ -75,6 +100,10 @@ class Afterpay extends Payment
 
     public function createPayment($order, $override, $input = null)
     {
+        // 根据订单 billing 国家解析对应凭证
+        $countryCode = $order->billing_address->country ?? null;
+        $this->resolveCredentials($countryCode);
+
         $shop = $order->shipping_address;
         $bill = $order->billing_address;
         $telephone = $shop->phone ?: $bill->phone;
@@ -187,6 +216,10 @@ class Afterpay extends Payment
         if ($order->status != Order::STATUS_PENDING) {
             return new \Exception('order status is error');
         }
+        // 根据订单 billing 国家解析对应凭证
+        $countryCode = $order->billing_address->country ?? null;
+        $this->resolveCredentials($countryCode);
+
         $additional = $order->payment->additional;
         $token = $additional['gateway_order_id'];
         if ($transactionId != $token) {
@@ -221,9 +254,15 @@ class Afterpay extends Payment
 
 
 
-    public function fetchConfiguration(string $code = 'afterpay'): ?array
+    public function fetchConfiguration(string $code = 'afterpay', ?string $countryCode = null): ?array
     {
         try {
+            // 如果未指定国家,尝试从当前订单/购物车获取
+            if ($countryCode === null) {
+                $countryCode = $this->getCurrentCountryCode();
+            }
+            $this->resolveCredentials($countryCode);
+
             $client = new Client();
             $response = $client->request('GET', $this->configurationUlr, [
                 'headers' => [