Browse Source

fix: unable to save admin billing settings

Cat 1 year ago
parent
commit
7ebc2159ad

+ 6 - 6
composer.lock

@@ -568,16 +568,16 @@
         },
         {
             "name": "aws/aws-crt-php",
-            "version": "v1.2.5",
+            "version": "v1.2.6",
             "source": {
                 "type": "git",
                 "url": "https://github.com/awslabs/aws-crt-php.git",
-                "reference": "0ea1f04ec5aa9f049f97e012d1ed63b76834a31b"
+                "reference": "a63485b65b6b3367039306496d49737cf1995408"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/awslabs/aws-crt-php/zipball/0ea1f04ec5aa9f049f97e012d1ed63b76834a31b",
-                "reference": "0ea1f04ec5aa9f049f97e012d1ed63b76834a31b",
+                "url": "https://api.github.com/repos/awslabs/aws-crt-php/zipball/a63485b65b6b3367039306496d49737cf1995408",
+                "reference": "a63485b65b6b3367039306496d49737cf1995408",
                 "shasum": ""
             },
             "require": {
@@ -616,9 +616,9 @@
             ],
             "support": {
                 "issues": "https://github.com/awslabs/aws-crt-php/issues",
-                "source": "https://github.com/awslabs/aws-crt-php/tree/v1.2.5"
+                "source": "https://github.com/awslabs/aws-crt-php/tree/v1.2.6"
             },
-            "time": "2024-04-19T21:30:56+00:00"
+            "time": "2024-06-13T17:21:28+00:00"
         },
         {
             "name": "aws/aws-sdk-php",

+ 2 - 2
resources/views/tabler/admin/setting/billing.tpl

@@ -329,10 +329,10 @@
                     dataType: "json",
                     data: {
                         {foreach $update_field as $key}
-                        {$key}: $('#{$key}').val(),
+                            {$key}: $('#{$key}').val(),
                         {/foreach}
                         {foreach $payment_gateways as $key => $value}
-                        {$key}: $("#{$key}_enable").is(":checked"),
+                            {$value}: $("#{$value}_enable").is(":checked"),
                         {/foreach}
                     },
                     success: function (data) {

+ 11 - 18
src/Controllers/Admin/Setting/BillingController.php

@@ -16,8 +16,6 @@ use Stripe\Exception\ApiErrorException;
 use Stripe\Stripe;
 use Stripe\WebhookEndpoint;
 use Throwable;
-use function json_decode;
-use function json_encode;
 
 final class BillingController extends BaseController
 {
@@ -36,8 +34,6 @@ final class BillingController extends BaseController
      */
     public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
-        $settings = Config::getClass('billing');
-
         return $response->write(
             $this->view()
                 ->assign('update_field', $this->update_field)
@@ -50,20 +46,15 @@ final class BillingController extends BaseController
 
     public function save(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
-        $gateway_in_use = [];
-
-        foreach ($this->returnGatewaysList() as $value) {
-            $payment_enable = $request->getParam($value);
+        $active_gateway = [];
 
-            if ($payment_enable === 'true') {
-                $gateway_in_use[] = $value;
+        foreach ($this->returnGatewaysList() as $key => $value) {
+            if ($request->getParam($value) === 'true') {
+                $active_gateway[] = $value;
             }
         }
 
-        $gateway = (new Config())->where('item', 'payment_gateway')->first();
-        $gateway->value = json_encode($gateway_in_use);
-
-        if (! $gateway->save()) {
+        if (! Config::set('payment_gateway', $active_gateway)) {
             return $response->withJson([
                 'ret' => 0,
                 'msg' => '保存支付网关时出错',
@@ -71,6 +62,10 @@ final class BillingController extends BaseController
         }
 
         foreach ($this->update_field as $item) {
+            if ($item === 'payment_gateway') {
+                continue;
+            }
+
             if (! Config::set($item, $request->getParam($item))) {
                 return $response->withJson([
                     'ret' => 0,
@@ -150,7 +145,7 @@ final class BillingController extends BaseController
         $result = [];
 
         foreach (Payment::getAllPaymentMap() as $payment) {
-            $result[$payment::_name()] = $payment::_name();
+            $result[$payment::_readableName()] = $payment::_name();
         }
 
         return $result;
@@ -158,8 +153,6 @@ final class BillingController extends BaseController
 
     public function returnActiveGateways(): ?array
     {
-        $payment_gateways = (new Config())->where('item', 'payment_gateway')->first();
-
-        return json_decode($payment_gateways->value);
+        return Config::obtain('payment_gateway');
     }
 }

+ 26 - 22
src/Models/Config.php

@@ -5,6 +5,10 @@ declare(strict_types=1);
 namespace App\Models;
 
 use Illuminate\Database\Query\Builder;
+use Illuminate\Database\QueryException;
+use function is_array;
+use function json_decode;
+use function json_encode;
 
 /**
  * @property int    $id         配置ID
@@ -12,6 +16,7 @@ use Illuminate\Database\Query\Builder;
  * @property string $value      配置值
  * @property string $class      配置类别
  * @property string $is_public  是否为公共参数
+ * @property string $type       配置值类型
  * @property string $default    默认值
  * @property string $mark       备注
  *
@@ -22,13 +27,14 @@ final class Config extends Model
     protected $connection = 'default';
     protected $table = 'config';
 
-    public static function obtain($item): bool|int|string
+    public static function obtain($item): bool|int|array|string
     {
         $config = (new Config())->where('item', $item)->first();
 
         return match ($config->type) {
             'bool' => (bool) $config->value,
             'int' => (int) $config->value,
+            'array' => json_decode($config->value),
             default => (string) $config->value,
         };
     }
@@ -39,13 +45,12 @@ final class Config extends Model
         $all_configs = (new Config())->where('class', $class)->get();
 
         foreach ($all_configs as $config) {
-            if ($config->type === 'bool') {
-                $configs[$config->item] = (bool) $config->value;
-            } elseif ($config->type === 'int') {
-                $configs[$config->item] = (int) $config->value;
-            } else {
-                $configs[$config->item] = (string) $config->value;
-            }
+            $configs[$config->item] = match ($config->type) {
+                'bool' => (bool) $config->value,
+                'int' => (int) $config->value,
+                'array' => json_decode($config->value),
+                default => (string) $config->value,
+            };
         }
 
         return $configs;
@@ -69,28 +74,27 @@ final class Config extends Model
         $all_configs = (new Config())->where('is_public', '1')->get();
 
         foreach ($all_configs as $config) {
-            if ($config->type === 'bool') {
-                $configs[$config->item] = (bool) $config->value;
-            } elseif ($config->type === 'int') {
-                $configs[$config->item] = (int) $config->value;
-            } else {
-                $configs[$config->item] = (string) $config->value;
-            }
+            $configs[$config->item] = match ($config->type) {
+                'bool' => (bool) $config->value,
+                'int' => (int) $config->value,
+                'array' => json_decode($config->value),
+                default => (string) $config->value,
+            };
         }
 
         return $configs;
     }
 
-    public static function set($item, $value): bool
+    public static function set(string $item, mixed $value): bool
     {
-        $config = (new Config())->where('item', $item)->first();
+        $value = is_array($value) ? json_encode($value) : $value;
 
-        if ($config->tpye === 'array') {
-            $config->value = json_encode($value);
-        } else {
-            $config->value = $value;
+        try {
+            (new Config())->where('item', $item)->update(['value' => $value]);
+        } catch (QueryException $e) {
+            return false;
         }
 
-        return $config->save();
+        return true;
     }
 }