Jelajahi Sumber

添加加拿大最大最小金额获取

llp 20 jam lalu
induk
melakukan
7c059d96b6

+ 16 - 2
packages/Longyi/Pay/Afterpay/src/Config/system.php

@@ -79,14 +79,28 @@ return [
                 'default'       => 5,
             ], [
                 'name'          => 'min_order_total',
-                'title'         => 'Minimum Order Total',
+                'title'         => 'Minimum Order Total (US)',
                 'type'          => 'blade',
                 'path'          => 'Afterpay::admin.system.readonly-value',
                 'channel_based' => false,
                 'locale_based'  => false,
             ], [
                 'name'          => 'max_order_total',
-                'title'         => 'Maximum Order Total',
+                'title'         => 'Maximum Order Total (US)',
+                'type'          => 'blade',
+                'path'          => 'Afterpay::admin.system.readonly-value',
+                'channel_based' => false,
+                'locale_based'  => false,
+            ], [
+                'name'          => 'min_order_total_ca',
+                'title'         => 'Minimum Order Total (CA)',
+                'type'          => 'blade',
+                'path'          => 'Afterpay::admin.system.readonly-value',
+                'channel_based' => false,
+                'locale_based'  => false,
+            ], [
+                'name'          => 'max_order_total_ca',
+                'title'         => 'Maximum Order Total (CA)',
                 'type'          => 'blade',
                 'path'          => 'Afterpay::admin.system.readonly-value',
                 'channel_based' => false,

+ 29 - 13
packages/Longyi/Pay/Afterpay/src/Http/Controllers/AfterpayController.php

@@ -44,9 +44,21 @@ class AfterpayController extends Controller
         $code = $request->input('code', 'afterpay');
 
         try {
-            $result = $this->$code->fetchConfiguration($code);
+            // afterpay 支持多国家,需要分别查询各国限额
+            $countries = ($code === 'afterpay')
+                ? ['US' => '', 'CA' => '_ca']   // US 后缀为空保持兼容,CA 后缀 _ca
+                : ['GBP' => ''];                  // clearpay 只查一次
 
-            if (! $result) {
+            $results = [];
+            foreach ($countries as $countryCode => $keySuffix) {
+                $result = $this->$code->fetchConfiguration($code, $countryCode);
+                if (! $result) {
+                    continue;
+                }
+                $results[$keySuffix] = $result;
+            }
+
+            if (empty($results)) {
                 return response()->json([
                     'success' => false,
                     'message' => 'Failed to fetch configuration from Afterpay',
@@ -55,16 +67,18 @@ class AfterpayController extends Controller
 
             // 写入数据库
             $prefix = 'sales.payment_methods.' . $code . '.';
-            $fields = [
-                'min_order_total' => $result['min_order_total'] ?? '',
-                'max_order_total' => $result['max_order_total'] ?? '',
-            ];
+            foreach ($results as $keySuffix => $result) {
+                $fields = [
+                    'min_order_total' . $keySuffix => $result['min_order_total'] ?? '',
+                    'max_order_total' . $keySuffix => $result['max_order_total'] ?? '',
+                ];
 
-            foreach ($fields as $field => $value) {
-                \Webkul\Core\Models\CoreConfig::updateOrCreate(
-                    ['code' => $prefix . $field],
-                    ['value' => $value]
-                );
+                foreach ($fields as $field => $value) {
+                    \Webkul\Core\Models\CoreConfig::updateOrCreate(
+                        ['code' => $prefix . $field],
+                        ['value' => $value]
+                    );
+                }
             }
 
             // 清除配置缓存
@@ -74,8 +88,10 @@ class AfterpayController extends Controller
             return response()->json([
                 'success'         => true,
                 'message'         => 'Limits updated successfully',
-                'min_order_total' => $result['min_order_total'] ?? '',
-                'max_order_total' => $result['max_order_total'] ?? '',
+                'min_order_total' => $results['']['min_order_total'] ?? '',
+                'max_order_total' => $results['']['max_order_total'] ?? '',
+                'min_order_total_ca' => $results['_ca']['min_order_total'] ?? '',
+                'max_order_total_ca' => $results['_ca']['max_order_total'] ?? '',
             ]);
         } catch (\Exception $e) {
             \Log::channel('payment')->error("{$code} updateLimits error: " . $e->getMessage());

+ 8 - 0
packages/Longyi/Pay/Afterpay/src/Payment/Afterpay.php

@@ -43,8 +43,13 @@ class Afterpay extends Payment
     protected function resolveCredentials(?string $countryCode = null): void
     {
         if ($countryCode === 'CA') {
+            // 使用加拿大凭证 (CA)
             $this->clientId = $this->getConfigData('client_id_ca');
             $this->apikey = $this->getConfigData('secret_id_ca');
+        } elseif($countryCode === 'GBP') {
+            // 使用英国凭证 (GBP)
+            $this->clientId = $this->getConfigData('client_id');
+            $this->apikey = $this->getConfigData('secret_id');
         } else {
             // 默认使用美国凭证 (US)
             $this->clientId = $this->getConfigData('client_id_us');
@@ -260,6 +265,9 @@ class Afterpay extends Payment
     public function fetchConfiguration(string $code = 'afterpay', ?string $countryCode = null): ?array
     {
         try {
+            // 根据国家代码解析对应凭证
+            $this->resolveCredentials($countryCode);
+
             $client = new Client();
             $response = $client->request('GET', $this->configurationUlr, [
                 'headers' => [

+ 4 - 0
packages/Longyi/Pay/Afterpay/src/Resources/views/admin/system/update-limits.blade.php

@@ -52,8 +52,12 @@
                 if (data.success) {
                     var minEl = section.querySelector('.afterpay-limit-value[data-field="min_order_total"]');
                     var maxEl = section.querySelector('.afterpay-limit-value[data-field="max_order_total"]');
+                    var minCaEl = section.querySelector('.afterpay-limit-value[data-field="min_order_total_ca"]');
+                    var maxCaEl = section.querySelector('.afterpay-limit-value[data-field="max_order_total_ca"]');
                     if (minEl) minEl.textContent = data.min_order_total || '-';
                     if (maxEl) maxEl.textContent = data.max_order_total || '-';
+                    if (minCaEl) minCaEl.textContent = data.min_order_total_ca || '-';
+                    if (maxCaEl) maxCaEl.textContent = data.max_order_total_ca || '-';
                     msgEl.textContent = data.message;
                     msgEl.className = 'text-sm text-green-600';
                 } else {