From 1549d5b25dd53c8b88d1465bc6033bf732e13347 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 2 Nov 2026 22:33:18 +0800 Subject: [PATCH] fix(payment): honor schema defaults for balance recharge and fix withdrawal warning Fixes two issues: 1. **Balance recharge disabled on fresh install**: The `pay_balance_s` setting has `default: true` in SchemaPayment but was treated as disabled when the key was absent from the database. This meant users had to toggle it off and back on to activate the feature. Fixed by using `!isset() || !empty()` pattern to honor the schema default when the key is missing. 2. **PHP Warning in withdrawal settings**: `get_withdrawal_settings()` passed `$s['affiliate_withdrawal_reset_time']` directly to `preg_match()` without extracting it first, causing "Undefined array key" warnings when the option was unset. Fixed by extracting to `$reset_time_raw` before the regex check. Changed: - `PaymentRuntime::get_payment_recharge_settings()`: enabled defaults to true - `PaymentRuntime::get_payment_gateway_settings()`: balance_enabled defaults to true - `PaymentRuntime::is_gateway_enabled('balance')`: defaults to true - `PaymentRuntime::get_withdrawal_settings()`: no longer warns on missing key Added test script `tools/test-payment-runtime-defaults.php` to verify the fix works with empty settings (fresh install). Co-Authored-By: Claude Fable 5 --- src/Settings/PaymentRuntime.php | 11 +-- tools/test-payment-runtime-defaults.php | 100 ++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 tools/test-payment-runtime-defaults.php diff --git a/src/Settings/PaymentRuntime.php b/src/Settings/PaymentRuntime.php index b7395ca9..753cd191 100644 --- a/src/Settings/PaymentRuntime.php +++ b/src/Settings/PaymentRuntime.php @@ -88,7 +88,7 @@ class PaymentRuntime { $desc = sanitize_textarea_field((string) ($settings['pay_balance_desc'] ?? '')); return [ - 'enabled' => !empty($settings['pay_balance_s']), + 'enabled' => !isset($settings['pay_balance_s']) || !empty($settings['pay_balance_s']), 'withdraw_enabled' => !empty($settings['pay_balance_withdraw_s']), 'desc' => $desc, 'packages_raw' => $packages, @@ -377,7 +377,7 @@ class PaymentRuntime { } $paypal_enabled = !empty($settings['pay_paypal_sdk_s']); $stripe_enabled = !empty($settings['pay_stripe_sdk_s']); - $balance_enabled = !empty($settings['pay_balance_s']); + $balance_enabled = !isset($settings['pay_balance_s']) || !empty($settings['pay_balance_s']); // 卡密收款接口随三个兑换开关任一生效(余额 / 积分 / 会员)。 $card_pass_enabled = !empty($settings['pay_balance_pass_charge_s']) || !empty($settings['points_pass_exchange_s']) @@ -512,13 +512,14 @@ class PaymentRuntime { $s = Settings::get_all(); $channels = array_values(array_intersect(['alipay', 'wechat'], array_map('sanitize_key', (array) ($s['affiliate_payout_channels'] ?? ['alipay', 'wechat'])))); if (!$channels) { $channels = ['alipay', 'wechat']; } + $reset_time_raw = (string) ($s['affiliate_withdrawal_reset_time'] ?? '00:00'); return [ 'min_amount' => self::money_decimal($s['affiliate_min_withdrawal'] ?? '50.00', '50.00', 99999900), 'service_charge_rate' => max(0, min(100, (int) ($s['affiliate_withdrawal_fee'] ?? 5))), 'daily_count' => max(0, min(99999, (int) ($s['affiliate_daily_withdrawal_count'] ?? 3))), 'daily_limit' => self::money_decimal($s['affiliate_daily_withdrawal_limit'] ?? '500.00', '500.00', 99999900), - 'reset_time' => preg_match('/^(?:[01]\\d|2[0-3]):[0-5]\\d$/', (string) ($s['affiliate_withdrawal_reset_time'] ?? '00:00')) - ? (string) $s['affiliate_withdrawal_reset_time'] + 'reset_time' => preg_match('/^(?:[01]\\d|2[0-3]):[0-5]\\d$/', $reset_time_raw) + ? $reset_time_raw : '00:00', 'payout_channels' => $channels, ]; @@ -553,7 +554,7 @@ class PaymentRuntime { case 'stripe': return !empty($settings['pay_stripe_sdk_s']); case 'balance': - return !empty($settings['pay_balance_s']); + return !isset($settings['pay_balance_s']) || !empty($settings['pay_balance_s']); case 'card_pass': // 收款接口随三个兑换开关任一生效(余额 / 积分 / 会员)。 return !empty($settings['pay_balance_pass_charge_s']) diff --git a/tools/test-payment-runtime-defaults.php b/tools/test-payment-runtime-defaults.php new file mode 100644 index 00000000..0558c41f --- /dev/null +++ b/tools/test-payment-runtime-defaults.php @@ -0,0 +1,100 @@ +getMessage() . "\n"; +} +echo "\n"; + +echo "All tests completed.\n";