Skip to content

fix(payment): localize checkout errors - #4841

Open
neow021 wants to merge 3 commits into
QuantumNous:mainfrom
neow021:fix/payment-checkout-i18n
Open

fix(payment): localize checkout errors#4841
neow021 wants to merge 3 commits into
QuantumNous:mainfrom
neow021:fix/payment-checkout-i18n

Conversation

@neow021

@neow021 neow021 commented May 14, 2026

Copy link
Copy Markdown
Contributor

变更描述 / Description

将 payment / topup / subscription checkout 链路中普通用户会直接看到的中文错误文案接入现有 i18n,包括参数错误、支付方式不存在、套餐未启用、金额过低、未配置 webhook/price/product、创建订单失败、拉起支付失败等。

这个 PR 只替换用户可见响应和补充对应 locale key,不改变支付提供方选择、订单创建、回调地址、支付链接生成或其他支付流程逻辑。

变更类型 / Type of change

  • Bug 修复 (Bug fix)
  • 新功能 (New feature)
  • 性能优化 / 重构 (Refactor)
  • 文档更新 (Documentation)

关联任务 / Related Issue

提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 此 PR 修复英文用户在 payment/topup/subscription checkout 流程中仍可能收到中文文案的问题。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

运行证明 / Proof of Work

docker run --rm -v "$PWD":/src -w /src golang:1.26.1-alpine gofmt -w controller/subscription_payment_creem.go controller/subscription_payment_epay.go controller/subscription_payment_stripe.go controller/topup.go controller/topup_creem.go controller/topup_stripe.go controller/topup_waffo.go controller/topup_waffo_pancake.go i18n/keys.go

git diff --cached --check

git diff --cached -U0 -- controller/subscription_payment_creem.go controller/subscription_payment_epay.go controller/subscription_payment_stripe.go controller/topup.go controller/topup_creem.go controller/topup_stripe.go controller/topup_waffo.go controller/topup_waffo_pancake.go i18n/keys.go | rg "^\+.*[\p{Han}]" || true

ruby -e 'require "yaml"; ARGV.each { |f| YAML.load_file(f); puts "#{f} ok" }' i18n/locales/en.yaml i18n/locales/zh-CN.yaml i18n/locales/zh-TW.yaml\n\nfor f in i18n/locales/en.yaml i18n/locales/zh-CN.yaml i18n/locales/zh-TW.yaml; do\n  rg "^(payment|topup|subscription)\\." "$f" | sed "s/:.*//" | sort > "/tmp/$(basename $f).payment.keys"\ndone\ndiff -u /tmp/en.yaml.payment.keys /tmp/zh-CN.yaml.payment.keys\ndiff -u /tmp/en.yaml.payment.keys /tmp/zh-TW.yaml.payment.keys\n\ndocker run --rm -v "$PWD":/src -w /src -e GOCACHE=/tmp/go-cache -e GOMODCACHE=/tmp/go-mod-cache golang:1.26.1-alpine go test ./controller ./i18n\n```\n\n结果:\n```text\ni18n/locales/en.yaml ok\ni18n/locales/zh-CN.yaml ok\ni18n/locales/zh-TW.yaml ok\nok  github.com/QuantumNous/new-api/controller\n?   github.com/QuantumNous/new-api/i18n [no test files]\n```

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **New Features**
  * Payment and top‑up error messages are now fully localized (EN, ZH‑CN, ZH‑TW), with new message keys for disabled/configuration/product selections and min/max top‑up constraints.
* **Bug Fixes**
  * Standardized and centralized error responses across payment providers and flows so users receive consistent, translated error text for validation and payment failures.

<!-- review_stack_entry_start -->

[![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/QuantumNous/new-api/pull/4841?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

- Replace user-facing payment and top-up error strings with existing i18n keys.\n- Add missing payment and top-up message keys and locale entries.\n- Preserve payment flow, callback, and order creation behavior.
@coderabbitai

coderabbitai Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a2cd0f28-4c56-4f1f-964c-eeb8529043e9

📥 Commits

Reviewing files that changed from the base of the PR and between 2b98dc9 and e5845aa.

📒 Files selected for processing (2)
  • controller/topup.go
  • controller/topup_waffo_pancake.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • controller/topup_waffo_pancake.go
  • controller/topup.go

Walkthrough

Replaces hardcoded Chinese payment/top-up error messages with i18n-based localized responses, adds new i18n keys/translations, introduces centralized top-up i18n helpers, and updates subscription and top-up payment handlers (Creem, Epay, Stripe, Waffo, Waffo Pancake) to use the new keys.

Changes

Payment and Top-up I18n Conversion

Layer / File(s) Summary
I18n message keys and translations
i18n/keys.go, i18n/locales/en.yaml, i18n/locales/zh-CN.yaml, i18n/locales/zh-TW.yaml
New i18n message key constants (MsgPaymentDisabled, MsgPaymentConfigIncomplete, MsgPaymentProductRequired, etc.) and MsgTopupAmountMin/Max/GroupFailed are added, with localized strings for English, Simplified Chinese, and Traditional Chinese.
Core top-up handler and i18n helpers
controller/topup.go
Two helper functions paymentDataErrorI18n and paymentMessageErrorI18n centralize i18n error response generation. Validation, amount, and failure paths in RequestEpay, RequestAmount, and AdminCompleteTopUp now use these helpers and i18n keys.
Subscription payment handlers
controller/subscription_payment_creem.go, controller/subscription_payment_epay.go, controller/subscription_payment_stripe.go
Subscription handlers convert error responses for invalid parameters, disabled plans, missing configuration, missing user, purchase limit, and payment operation failures from hardcoded messages to i18n-based ApiErrorI18n/helpers with message keys.
Top-up payment handlers
controller/topup_creem.go, controller/topup_stripe.go, controller/topup_waffo.go, controller/topup_waffo_pancake.go
Top-up handlers now use paymentDataErrorI18n / paymentMessageErrorI18n with i18n keys for parameter validation, amount checks, payment method selection, configuration verification, order creation, and payment start failures.
Creem gen link and request body handling
controller/topup_creem.go, controller/subscription_payment_creem.go
Preserves raw request body for Creem subscription flow and switches Creem JSON serialization/parsing in genCreemLink from encoding/json to common.Marshal/common.Unmarshal.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • seefs001
  • Calcium-Ion

Poem

🐰 I found the errors all askew,
I swapped hard strings for keys anew,
Now Creem, Stripe, Waffo sing,
Errors speak in every tongue—hooray for spring! 🎋

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.76% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(payment): localize checkout errors' directly aligns with the primary objective of replacing hardcoded Chinese error messages with i18n localized messages across payment and topup checkout flows.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
controller/topup_creem.go (1)

9-9: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Replace direct encoding/json with common wrappers.

This file violates the coding guideline requiring all JSON operations to use common.Marshal() and common.Unmarshal(). The direct encoding/json import and four call sites (lines 80, 406, 447, and the import on line 9) must be updated.

Proposed fix
 import (
 	"bytes"
 	"context"
 	"crypto/hmac"
 	"crypto/sha256"
 	"encoding/hex"
-	"encoding/json"
 	"errors"
 	"fmt"
 	"github.com/QuantumNous/new-api/common"
 	"github.com/QuantumNous/new-api/i18n"
@@
-	err := json.Unmarshal([]byte(setting.CreemProducts), &products)
+	err := common.Unmarshal([]byte(setting.CreemProducts), &products)
@@
-	jsonData, err := json.Marshal(requestData)
+	jsonData, err := common.Marshal(requestData)
@@
-	err = json.Unmarshal(body, &checkoutResp)
+	err = common.Unmarshal(body, &checkoutResp)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@controller/topup_creem.go` at line 9, Replace the direct import of
"encoding/json" in topup_creem.go with the project common package and update all
JSON calls to use the wrappers: change json.Marshal(...) to common.Marshal(...)
and json.Unmarshal(...) to common.Unmarshal(...). Specifically update the three
call sites that currently use json (the marshal/unmarshal usages referenced in
the review) and remove the unused "encoding/json" import; ensure error handling
and variable types remain the same after swapping to
common.Marshal/common.Unmarshal.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@controller/topup_creem.go`:
- Line 9: Replace the direct import of "encoding/json" in topup_creem.go with
the project common package and update all JSON calls to use the wrappers: change
json.Marshal(...) to common.Marshal(...) and json.Unmarshal(...) to
common.Unmarshal(...). Specifically update the three call sites that currently
use json (the marshal/unmarshal usages referenced in the review) and remove the
unused "encoding/json" import; ensure error handling and variable types remain
the same after swapping to common.Marshal/common.Unmarshal.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1234dfd4-c8a5-4bde-b276-69703d6cbaeb

📥 Commits

Reviewing files that changed from the base of the PR and between 18282e6 and d92f1bd.

📒 Files selected for processing (12)
  • controller/subscription_payment_creem.go
  • controller/subscription_payment_epay.go
  • controller/subscription_payment_stripe.go
  • controller/topup.go
  • controller/topup_creem.go
  • controller/topup_stripe.go
  • controller/topup_waffo.go
  • controller/topup_waffo_pancake.go
  • i18n/keys.go
  • i18n/locales/en.yaml
  • i18n/locales/zh-CN.yaml
  • i18n/locales/zh-TW.yaml

neow021 added 2 commits May 17, 2026 13:11
- Replace direct encoding/json calls in Creem top-up flow.

- Keep behavior unchanged while following common JSON helpers.
- Merge QuantumNous/new-api main into the payment checkout i18n PR branch.

- Resolve Waffo Pancake checkout conflict while preserving localized errors.
@Calcium-Ion
Calcium-Ion force-pushed the main branch 2 times, most recently from 51fdfc5 to 2b6f1df Compare August 30, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant