From 5b8f29ae789dff0f38d32313c8a998e8600732e1 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 22 Nov 2026 19:45:37 +0800 Subject: [PATCH] fix(payment): honor only provider checkout expiration --- .../Controllers/Admin/PaytaroController.php | 2 -- web/app/Controllers/Admin/YipayController.php | 2 -- web/app/Services/Payment/PaytaroConfig.php | 2 -- .../Payment/PaytaroPaymentService.php | 3 +-- web/app/Services/Payment/YipayConfig.php | 2 -- .../Services/Payment/YipayPaymentService.php | 3 +-- web/docs/payment-currency-rollout.md | 8 +++---- web/docs/paytaro-payment.md | 4 ++++ web/docs/yipay-payment.md | 4 ++++ web/public/cloud/invoice-detail-runtime.js | 21 ++++--------------- .../views/venix/admin/paytaro/index.tpl | 2 +- .../views/venix/admin/yipay/index.tpl | 5 +++-- web/tests/CloudInvoiceDetailContractTest.php | 13 ++++++------ web/tests/PaymentCheckoutQuoteTest.php | 12 ++++++++++- web/tests/PaytaroPaymentServiceTest.php | 8 +++++-- web/tests/YipayPaymentServiceTest.php | 6 ++++++ 16 files changed, 51 insertions(+), 46 deletions(-) diff --git a/web/app/Controllers/Admin/PaytaroController.php b/web/app/Controllers/Admin/PaytaroController.php index 5ac1444f..f983c290 100644 --- a/web/app/Controllers/Admin/PaytaroController.php +++ b/web/app/Controllers/Admin/PaytaroController.php @@ -155,8 +155,6 @@ class PaytaroController extends AdminController 'paytaro_method_uuid' => strtolower($methodUuid), 'paytaro_min_amount' => $minimum, 'paytaro_max_amount' => $maximum, - 'paytaro_quote_minutes' => (string) max(5, min(1440, (int) ($payload['paytaro_quote_minutes'] - ?? $current['paytaro_quote_minutes']))), 'paytaro_request_timeout_seconds' => (string) max(3, min(60, (int) ($payload['paytaro_request_timeout_seconds'] ?? 15))), ]; } diff --git a/web/app/Controllers/Admin/YipayController.php b/web/app/Controllers/Admin/YipayController.php index 6d605cf4..a0f8d50e 100644 --- a/web/app/Controllers/Admin/YipayController.php +++ b/web/app/Controllers/Admin/YipayController.php @@ -227,8 +227,6 @@ class YipayController extends AdminController 'yipay_min_amount' => $minimum, 'yipay_max_amount' => $maximum, 'yipay_cny_confirmed' => YipayConfig::truthy($payload['yipay_cny_confirmed'] ?? false) ? 'true' : 'false', - 'yipay_quote_minutes' => (string) max(5, min(1440, (int) ($payload['yipay_quote_minutes'] - ?? $current['yipay_quote_minutes']))), 'yipay_request_timeout_seconds' => (string) max(3, min(60, (int) ($payload['yipay_request_timeout_seconds'] ?? 15))), ]; } diff --git a/web/app/Services/Payment/PaytaroConfig.php b/web/app/Services/Payment/PaytaroConfig.php index 84f34fdb..89df6ccb 100644 --- a/web/app/Services/Payment/PaytaroConfig.php +++ b/web/app/Services/Payment/PaytaroConfig.php @@ -19,7 +19,6 @@ class PaytaroConfig 'paytaro_method_uuid' => '', 'paytaro_min_amount' => '1.00', 'paytaro_max_amount' => '10000.00', - 'paytaro_quote_minutes' => '30', 'paytaro_request_timeout_seconds' => '15', ]; @@ -51,7 +50,6 @@ class PaytaroConfig $values['paytaro_test_mode'] = self::truthy($values['paytaro_test_mode']); $values['paytaro_min_amount'] = PaymentAmount::normalize($values['paytaro_min_amount']); $values['paytaro_max_amount'] = PaymentAmount::normalize($values['paytaro_max_amount']); - $values['paytaro_quote_minutes'] = max(5, min(1440, (int) $values['paytaro_quote_minutes'])); $values['paytaro_request_timeout_seconds'] = max(3, min(60, (int) $values['paytaro_request_timeout_seconds'])); return $values; } diff --git a/web/app/Services/Payment/PaytaroPaymentService.php b/web/app/Services/Payment/PaytaroPaymentService.php index 3c697250..16adf4cb 100644 --- a/web/app/Services/Payment/PaytaroPaymentService.php +++ b/web/app/Services/Payment/PaytaroPaymentService.php @@ -21,8 +21,7 @@ class PaytaroPaymentService $amount = $this->validateAmount($rawAmount, $settings); $orderNo = $this->orderNo(); $now = time(); - $quote = PaymentCheckoutQuotes::prepare('paytaro', $amount, $now, - $now + (int) $settings['paytaro_quote_minutes'] * 60); + $quote = PaymentCheckoutQuotes::prepare('paytaro', $amount, $now, null); if (PaytaroOrder::where('user_id', (int) $user->id)->where('created_at', '>=', $now - 60)->count() >= 5) { throw new \RuntimeException('Paytaro 下单过于频繁,请稍后重试'); } diff --git a/web/app/Services/Payment/YipayConfig.php b/web/app/Services/Payment/YipayConfig.php index 450a86f6..db09f29e 100644 --- a/web/app/Services/Payment/YipayConfig.php +++ b/web/app/Services/Payment/YipayConfig.php @@ -21,7 +21,6 @@ class YipayConfig 'yipay_min_amount' => '1.00', 'yipay_max_amount' => '10000.00', 'yipay_cny_confirmed' => 'false', - 'yipay_quote_minutes' => '30', 'yipay_request_timeout_seconds' => '15', ]; @@ -53,7 +52,6 @@ class YipayConfig $values['yipay_min_amount'] = PaymentAmount::normalize($values['yipay_min_amount']); $values['yipay_max_amount'] = PaymentAmount::normalize($values['yipay_max_amount']); $values['yipay_cny_confirmed'] = self::truthy($values['yipay_cny_confirmed']); - $values['yipay_quote_minutes'] = max(5, min(1440, (int) $values['yipay_quote_minutes'])); $values['yipay_request_timeout_seconds'] = max(3, min(60, (int) $values['yipay_request_timeout_seconds'])); return $values; } diff --git a/web/app/Services/Payment/YipayPaymentService.php b/web/app/Services/Payment/YipayPaymentService.php index c9149207..8d014183 100644 --- a/web/app/Services/Payment/YipayPaymentService.php +++ b/web/app/Services/Payment/YipayPaymentService.php @@ -22,8 +22,7 @@ class YipayPaymentService $amount = $this->validateAmount($rawAmount, $settings); $orderNo = $this->orderNo(); $now = time(); - $quote = PaymentCheckoutQuotes::prepare('yipay_' . $method, $amount, $now, - $now + (int) $settings['yipay_quote_minutes'] * 60); + $quote = PaymentCheckoutQuotes::prepare('yipay_' . $method, $amount, $now, null); if (YipayOrder::where('user_id', (int) $user->id)->where('created_at', '>=', $now - 60)->count() >= 5) { throw new \RuntimeException('易支付下单过于频繁,请稍后重试'); } diff --git a/web/docs/payment-currency-rollout.md b/web/docs/payment-currency-rollout.md index 9ad4a3a7..b9dbd85a 100644 --- a/web/docs/payment-currency-rollout.md +++ b/web/docs/payment-currency-rollout.md @@ -12,15 +12,15 @@ ## 部署已有面板 1. 备份业务库,从三个受影响渠道关闭新单入口。保留商户配置和 USDT 扫链,记录在途订单及原交易凭证。Stripe 和余额支付可以继续使用。 -2. 在业务库对当前 `go` 旧版结构运行一次 [payment_checkout_quotes.sql](../sql/payment_checkout_quotes.sql)。应用前应已有 `usdt_payment_orders`、`yipay_refunds` 和 `cloud_invoice_payments`。SQL 增加报价账本、退款币种及关单证据字段,保留历史金额和已入账余额。全新面板按 [部署文档](../../docs/DEPLOYMENT.md) 初始化。 +2. 在业务库对当前 `go` 旧版结构运行一次 [payment_checkout_quotes.sql](../sql/payment_checkout_quotes.sql)。应用前应已有 `usdt_payment_orders`、`yipay_refunds` 和 `cloud_invoice_payments`。SQL 增加报价账本并保留历史金额和已入账余额。全新面板按 [部署文档](../../docs/DEPLOYMENT.md) 初始化。 3. 发布 PHP 代码并重新加载 PHP-FPM 和负责在线支付创建的任务 Worker,在原部署用户的定时任务中同步 [crontab](../crontab)。在汇率任务的私有运行环境配置有取价权限的 `WISE_PAYMENT_RATE_TOKEN`。手动执行一次 `php xcat paymentRateJob`,在后台确认两个币对的行情状态及有效时间。Stripe、易支付、Paytaro 和扫描器继续使用各自已有的凭据。 -4. 在易支付设置确认已开通的支付宝和微信商户都用人民币收款,并按商户支持的支付期限填写报价时限。Paytaro 同样配置面板报价时限。按渠道用测试模式验收真实付款、原单查询、余额或账单入账以及易支付原路退款,再逐一打开新单。 +4. 在易支付设置确认已开通的支付宝和微信商户都用人民币收款。面板不会为易支付或 Paytaro 自行设置二维码有效期;只有上游返回并由集成明确支持的期限才会显示为支付截止。按渠道用测试模式验收真实付款、原单查询、余额或账单入账以及易支付原路退款,再逐一打开新单。 ## 核对未决记录 - 在任一渠道设置页进入「支付汇率与对账」,按 CNY、USDT 或 USD 查询与导出 CSV。待核对的回执有独立状态,金额汇总只计入已核实收款。易支付退款明细同时显示渠道实际退回的 CNY 和原 USD 入账的扣回。 -- 历史错价、少付、渠道币种冲突,以及超过本地报价期限且缺少可信付款时间的 CNY 回执保留凭证,交由管理员逐笔核对。已结算历史余额按原流水保留。两笔独立有效款项对应同一账单时,账单只履约一次,另一笔保留在余额。 -- 超过面板报价期限只会关闭面板的付款入口。商户平台确认 CNY 订单已经关闭且未收款后,超级管理员可登记原订单与商户凭据,释放该账单的同渠道重新报价。已有收款证据的订单留在对账中。关单后收到矛盾的签名来款时,系统保留回执并标为待核对。 +- 历史错价、少付和渠道币种冲突的 CNY 回执保留凭证,交由管理员逐笔核对。已结算历史余额按原流水保留。两笔独立有效款项对应同一账单时,账单只履约一次,另一笔保留在余额。 +- 易支付和 Paytaro 每次明确的新支付请求都会生成独立上游订单;面板不以账单截止时间代替上游二维码有效期,也不在本地关闭上游订单。旧订单的有效签名来款仍按其精确订单号结算。 - USDT 按链上确认的转账时间、合约、收款地址、精确尾数与订单自己的迟付宽限验款。没有唯一归属的转账继续出现在链上核对列表。 - 易支付退款渠道已确认时,可从原单继续完成本地记账。退款结果未知时,在商户平台核对原流水,保留当前退款凭证。 diff --git a/web/docs/paytaro-payment.md b/web/docs/paytaro-payment.md index f0ae31c4..92b46226 100644 --- a/web/docs/paytaro-payment.md +++ b/web/docs/paytaro-payment.md @@ -48,6 +48,10 @@ test connectivity before enabling the payment method. MAPI requests use the EPay MD5 signing rules and send the frozen CNY quote. After verification, the panel applies the USD credit. Desktop browsers show the Alipay QR code. Mobile browsers show the quote and an Alipay app link. +The MAPI response currently has no documented expiry field consumed by this +integration. The panel therefore does not invent a local QR deadline or use an +invoice deadline to close a Paytaro order. A later signed callback remains +authoritative for that exact upstream order. Paytaro must be able to reach these public callback routes: diff --git a/web/docs/yipay-payment.md b/web/docs/yipay-payment.md index 0a5de9e3..f2d15644 100644 --- a/web/docs/yipay-payment.md +++ b/web/docs/yipay-payment.md @@ -46,6 +46,10 @@ Open `/admin/yipay`, enter the HTTPS gateway root URL, one public HTTPS panel callback base URL, merchant PID and merchant key. Save and test the connection before enabling the user-facing switch. Confirm that each enabled method charges CNY. The panel presents the frozen CNY quote and accounts in USD. +The supported MAPI response has no documented expiry field consumed by this +integration. The panel does not invent a local QR deadline or use an invoice +deadline to close a Yipay order; a later valid callback remains attributable +to its exact upstream order. The gateway must be able to send a GET request to the fixed `/yipay/callback` URL. This route must bypass login and CDN caching. The browser diff --git a/web/public/cloud/invoice-detail-runtime.js b/web/public/cloud/invoice-detail-runtime.js index 254f5b91..4c5c828d 100644 --- a/web/public/cloud/invoice-detail-runtime.js +++ b/web/public/cloud/invoice-detail-runtime.js @@ -458,17 +458,6 @@ function mergeInvoiceStatus(invoice, update) { return merged; } -function onlinePaymentFallbackExpiresAt(invoice = state.selectedInvoice) { - const now = Date.now(); - const maximum = now + STATUS_POLL_MAX_LIFETIME_MS; - const invoiceDeadline = serviceTimestamp( - String(invoice?.purpose || '').toLowerCase() === 'renewal' - ? invoice?.deletion_at : invoice?.payment_deadline_at, - ); - return invoiceDeadline !== null && invoiceDeadline > now - ? Math.min(invoiceDeadline, maximum) : maximum; -} - function clearOnlinePaymentExpiryTimer() { if (state.onlinePaymentExpiryTimer !== null) { clearTimeout(state.onlinePaymentExpiryTimer); @@ -753,9 +742,7 @@ function renderOnlinePayment(container, payment) { state.onlinePaymentMethodId = firstText(payment.method, state.selectedInvoicePaymentMethod); const paymentExpiresAt = serviceTimestamp(payment.expires_at); - state.onlinePaymentExpiresAt = paymentExpiresAt === null - ? onlinePaymentFallbackExpiresAt(state.selectedInvoice) - : paymentExpiresAt; + state.onlinePaymentExpiresAt = paymentExpiresAt ?? 0; let qrValue = ''; if (payment.payment_kind === 'usdt') { @@ -1263,9 +1250,9 @@ function hydrateInvoiceDetail(payload, expectedInvoiceId = document.body.dataset state.onlinePaymentPending = Boolean(serverPendingPayment); if (state.onlinePaymentPending) { state.onlinePaymentInvoiceId = String(payload.invoice.id); - if (state.onlinePaymentExpiresAt <= 0) { - state.onlinePaymentExpiresAt = onlinePaymentFallbackExpiresAt(payload.invoice); - } + state.onlinePaymentExpiresAt = serviceTimestamp( + state.activePayment?.payment?.expires_at, + ) ?? 0; } if (entersProvisioningAfterPayment(previousInvoice, payload.invoice, payload.presentation, wasAwaitingOnlinePayment)) { diff --git a/web/resources/views/venix/admin/paytaro/index.tpl b/web/resources/views/venix/admin/paytaro/index.tpl index 2ec5bbde..1e9f2378 100644 --- a/web/resources/views/venix/admin/paytaro/index.tpl +++ b/web/resources/views/venix/admin/paytaro/index.tpl @@ -34,7 +34,7 @@
-
+
由 Paytaro 决定面板不会用本地时间提前关闭上游支付链接。
diff --git a/web/resources/views/venix/admin/yipay/index.tpl b/web/resources/views/venix/admin/yipay/index.tpl index 56c9e66d..59107bbe 100644 --- a/web/resources/views/venix/admin/yipay/index.tpl +++ b/web/resources/views/venix/admin/yipay/index.tpl @@ -82,8 +82,9 @@
- - + + 由支付上游决定 + 面板不会用本地时间提前关闭上游支付链接。
diff --git a/web/tests/CloudInvoiceDetailContractTest.php b/web/tests/CloudInvoiceDetailContractTest.php index 5f06f3f6..816cb158 100644 --- a/web/tests/CloudInvoiceDetailContractTest.php +++ b/web/tests/CloudInvoiceDetailContractTest.php @@ -283,7 +283,7 @@ $assert(str_contains($needsPolling, 'state.presentation?.polling_required === tr && !str_contains($invoiceDetailRuntime, 'function provisionFinished('), 'Invoice polling must consume the server projection instead of rebuilding task state.'); $paymentExpiry = $section($invoiceDetailRuntime, - 'function onlinePaymentFallbackExpiresAt(', 'function invoiceNeedsPolling('); + 'function clearOnlinePaymentExpiryTimer(', 'function invoiceNeedsPolling('); $onlinePaymentRender = $section($invoiceDetailRuntime, 'function renderOnlinePayment(', 'async function payInvoiceBalance('); $detailHydration = $section($invoiceDetailRuntime, @@ -295,25 +295,24 @@ $assert(!str_contains($invoiceDetailRuntime, 'function hasPendingInvoicePaymentK && str_contains($detailHydration, 'state.onlinePaymentPending = Boolean(serverPendingPayment);') && str_contains($invoiceDetailRuntime, 'resumeInvoicePolling();') - && str_contains($paymentExpiry, 'STATUS_POLL_MAX_LIFETIME_MS') - && str_contains($paymentExpiry, 'serviceTimestamp(') && str_contains($paymentExpiry, 'function onlinePaymentHasExpired(') && str_contains($paymentExpiry, 'function expireOnlinePaymentInstructions(') && str_contains($paymentExpiry, 'clearOnlinePaymentState(true);') && str_contains($paymentExpiry, "makeElement('strong', '', t('billing.payment.expired.title'))") && str_contains($paymentExpiry, 'void refreshInvoiceDetail(invoiceId);') - && str_contains($onlinePaymentRender, 'paymentExpiresAt === null') && str_contains($onlinePaymentRender, - 'onlinePaymentFallbackExpiresAt(state.selectedInvoice)') + 'state.onlinePaymentExpiresAt = paymentExpiresAt ?? 0;') && str_contains($onlinePaymentRender, 'scheduleOnlinePaymentExpiry(generation);') && str_contains($detailHydration, 'if (onlinePaymentHasExpired()) expireOnlinePaymentInstructions();') - && str_contains($detailHydration, 'state.onlinePaymentExpiresAt <= 0') + && str_contains($detailHydration, + 'state.activePayment?.payment?.expires_at') + && !str_contains($invoiceDetailRuntime, 'onlinePaymentFallbackExpiresAt') && !str_contains($detailHydration, 'state.onlinePaymentExpiresAt <= Date.now()') && str_contains($invoiceDetailRuntime, 'STATUS_POLL_SLOW_INTERVAL_MS') && str_contains($invoiceDetailRuntime, 'Date.now() < state.pollUntil'), - 'Provider payments must recover through a bounded fallback expiry and disable stale instructions.'); + 'Provider payments must expire only from the durable provider deadline, never a local fallback.'); $assert(str_contains($invoiceDetailRuntime, 'function entersProvisioningAfterPayment(') && substr_count($invoiceDetailRuntime, diff --git a/web/tests/PaymentCheckoutQuoteTest.php b/web/tests/PaymentCheckoutQuoteTest.php index 4ab584de..d87ec921 100644 --- a/web/tests/PaymentCheckoutQuoteTest.php +++ b/web/tests/PaymentCheckoutQuoteTest.php @@ -46,7 +46,7 @@ $schema->create('yipay_refunds', static function (Blueprint $table): void { $now = 1_770_000_000; PaymentRateService::override('USD/CNY', '7.20', $now + 3600, 7, 'First acceptance quote', $now); -$draft = PaymentCheckoutQuotes::prepare('paytaro', '72.00', $now, $now + 900); +$draft = PaymentCheckoutQuotes::prepare('paytaro', '72.00', $now, null); $connection->transaction(fn () => PaymentCheckoutQuotes::persist($draft, 'PT-QUOTE-A', 21)); PaymentRateService::override('USD/CNY', '7.30', $now + 3600, 7, 'Second acceptance quote', $now + 1); $later = PaymentCheckoutQuotes::prepare('yipay_wxpay', '72.00', $now + 1, $now + 900); @@ -55,6 +55,16 @@ $connection->transaction(fn () => PaymentCheckoutQuotes::persist($later, 'YP-QUO $old = PaymentCheckoutQuotes::find('paytaro', 'PT-QUOTE-A'); checkCheckoutQuote($old->expected_units === 51840 && $later->expected_units === 52560, 'Changing the rate affected an existing order or failed to price the next one.'); +checkCheckoutQuote($old->provider_expires_at === null + && $old->accept_until === null + && PaymentCheckoutQuotes::checkout('paytaro', 'PT-QUOTE-A', 21, $now + 86400) + === [ + 'checkout_state' => 'payable', + 'amount' => '72.00', + 'charge_amount' => '518.40', + 'charge_currency' => 'CNY', + 'expires_at' => null, + ], 'A CNY checkout without upstream expiry was closed by local time.'); checkCheckoutQuote(PaymentCheckoutQuotes::find('yipay_wxpay', 'PT-QUOTE-A') === null, 'A quote was read using a different provider.'); diff --git a/web/tests/PaytaroPaymentServiceTest.php b/web/tests/PaytaroPaymentServiceTest.php index 0a64cc63..67ebb167 100644 --- a/web/tests/PaytaroPaymentServiceTest.php +++ b/web/tests/PaytaroPaymentServiceTest.php @@ -162,7 +162,6 @@ try { (new PaytaroClient([ 'paytaro_gateway_url' => PaytaroConfig::MAPI_GATEWAY_URL, 'paytaro_request_timeout_seconds' => 15, - 'paytaro_quote_minutes' => 30, PaytaroConfig::SECRET_KEY => 'merchant-secret', ], $httpClient))->createOrder($fixed); fwrite(STDERR, 'Paytaro accepted a non-HTTPS payment URL.' . PHP_EOL); @@ -397,7 +396,13 @@ $createdInvoiceOrder = $invoiceCreateService->createOrder( ); $createdAttempt = Capsule::table('cloud_invoice_payments') ->where('provider_order_no', (string) $createdInvoiceOrder->order_no)->first(); +$createdQuote = PaymentCheckoutQuotes::find( + 'paytaro', (string) $createdInvoiceOrder->order_no +); if ($createdAttempt === null + || $createdQuote === null + || $createdQuote->provider_expires_at !== null + || $createdQuote->accept_until !== null || (string) $createdAttempt->status !== 'creating' || (string) $createdAttempt->provider !== 'paytaro' || ($invoiceCreateGateway->outbound['money'] ?? '') !== '302.40' @@ -706,7 +711,6 @@ $smarty->assign([ 'paytaro_min_amount' => '1.00', 'paytaro_max_amount' => '10000.00', 'paytaro_request_timeout_seconds' => 15, - 'paytaro_quote_minutes' => 30, ], 'paytaro_orders' => null, 'paytaro_summary' => ['total' => 0, 'pending' => 0, 'paid' => 0, 'paid_amount' => '0.00'], diff --git a/web/tests/YipayPaymentServiceTest.php b/web/tests/YipayPaymentServiceTest.php index e5963da2..a1710c55 100644 --- a/web/tests/YipayPaymentServiceTest.php +++ b/web/tests/YipayPaymentServiceTest.php @@ -273,7 +273,13 @@ $createdInvoiceOrder = $invoiceCreateService->createOrder( ); $createdAttempt = Capsule::table('cloud_invoice_payments') ->where('provider_order_no', (string) $createdInvoiceOrder->order_no)->first(); +$createdQuote = PaymentCheckoutQuotes::find( + 'yipay_alipay', (string) $createdInvoiceOrder->order_no +); if ($createdAttempt === null + || $createdQuote === null + || $createdQuote->provider_expires_at !== null + || $createdQuote->accept_until !== null || (string) $createdAttempt->status !== 'creating' || (string) $createdAttempt->provider !== 'yipay_alipay' || ($invoiceCreateGateway->outbound['money'] ?? '') !== '302.40'