fix(server/pay): 订单展示金额兜底 — 老单 amount_minor=0 回退档位展示价(按方式结算币种)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
This commit is contained in:
wangjia
2026-07-12 06:54:18 +08:00
parent ff91be928a
commit b30f472d90
2 changed files with 40 additions and 0 deletions
+8
View File
@@ -165,6 +165,14 @@ func orderViewFromRow(row *PurchaseRow) orderView {
}
if it, ok := CatalogBySKU(row.SKU); ok {
v.Plan, v.Days = it.Plan, it.Days
// 展示金额兜底:老单/webhook 前 amount_minor=0(旧下单不落金额)→ 按档位+
// 支付方式的结算币种回退到展示价,列表/详情不再显 0。webhook 到账后为实际金额,不触发。
if v.AmountMinor == 0 {
v.AmountMinor = DisplayAmountMinor(it, row.Method)
if v.Currency == "" {
v.Currency = SettlementCurrency(row.Method)
}
}
}
if row.PaidAt.Valid {
s := row.PaidAt.Time.UTC().Format(time.RFC3339)
+32
View File
@@ -211,6 +211,38 @@ func TestList_OwnershipAndFields(t *testing.T) {
}
}
// 展示金额兜底:老单 amount_minor=0 → 列表按档位+方式回退到展示价(不显 0)。
func TestList_ZeroAmountFallback(t *testing.T) {
router, st := newHandlerRig(t, nil)
ctx := context.Background()
// 模拟老单:amount=0、currency 空(旧下单不落金额)
if err := st.Insert(ctx, 1, "uuid-1", "pro_month", "old-alipay", "alipay", 0, ""); err != nil {
t.Fatal(err)
}
if err := st.Insert(ctx, 1, "uuid-1", "pro_year", "old-crypto", "crypto", 0, ""); err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
router.ServeHTTP(w, authed(httptest.NewRequest(http.MethodGet, "/v1/pay/orders", nil), 1))
var resp struct {
Orders []orderView `json:"orders"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
got := map[string]orderView{}
for _, o := range resp.Orders {
got[o.OrderNo] = o
}
// alipay → CNY 展示价 2999;crypto → USDT 展示价 34990000
if a := got["old-alipay"]; a.AmountMinor != 2999 || a.Currency != "CNY" {
t.Fatalf("alipay 兜底不符: amount=%d curr=%q, want 2999/CNY", a.AmountMinor, a.Currency)
}
if cr := got["old-crypto"]; cr.AmountMinor != 34990000 || cr.Currency != "USDT" {
t.Fatalf("crypto 兜底不符: amount=%d curr=%q, want 34990000/USDT", cr.AmountMinor, cr.Currency)
}
}
// 订单详情:回带台账富字段 + pay_status/activated。
func TestGetOrder_EnrichedFields(t *testing.T) {
router, st := newHandlerRig(t, func(w http.ResponseWriter, r *http.Request) {