Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions controller/topup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func GetTopUpInfo(c *gin.Context) {
data := gin.H{
"enable_online_topup": operation_setting.PayAddress != "" && operation_setting.EpayId != "" && operation_setting.EpayKey != "",
"enable_stripe_topup": setting.StripeApiSecret != "" && setting.StripeWebhookSecret != "" && setting.StripePriceId != "",
"enable_creem_topup": setting.CreemApiKey != "" && setting.CreemProducts != "[]",
"creem_products": setting.CreemProducts,
"pay_methods": payMethods,
Comment on lines +54 to 56
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

Compute enable_creem_topup from parsed products; return parsed products, not a JSON string.

Current check setting.CreemProducts != "[]" can be true for invalid JSON; returning a raw JSON string forces clients to re‑parse. Parse once, validate, and return typed JSON.

- "enable_creem_topup":  setting.CreemApiKey != "" && setting.CreemProducts != "[]",
- "creem_products":      setting.CreemProducts,
+ "enable_creem_topup":  setting.CreemApiKey != "" && len(creemProducts) > 0,
+ "creem_products":      creemProducts,

Add before building data:

// at top of GetTopUpInfo
var creemProducts []CreemProduct
if err := json.Unmarshal([]byte(setting.CreemProducts), &creemProducts); err != nil {
    creemProducts = nil
}

Add import:

import "encoding/json"

Please confirm the frontend expects an array, not a string.


🏁 Script executed:

#!/bin/bash
# Grep UI for 'creem_products' usage to confirm expected type
rg -nP -C2 '\bcreem_products\b'

Length of output: 963


Return creem_products as an array and update frontend parsing

  • In controller/topup.go, unmarshal setting.CreemProducts before building data and switch to:

     // parse once at top of GetTopUpInfo
     var creemProducts []CreemProduct
     if err := json.Unmarshal([]byte(setting.CreemProducts), &creemProducts); err != nil {
         creemProducts = nil
     }
    
     // when constructing response
    - "enable_creem_topup": setting.CreemApiKey != "" && setting.CreemProducts != "[]",
    - "creem_products":      setting.CreemProducts,
    + "enable_creem_topup": setting.CreemApiKey != "" && len(creemProducts) > 0,
    + "creem_products":      creemProducts,

    and add import "encoding/json" at the top.

  • In web/src/components/topup/index.jsx (around line 383), remove the JSON.parse and consume the array directly:

    - const products = JSON.parse(data.creem_products || '[]');
    + const products = data.creem_products || [];
      setCreemProducts(products);

This ensures the backend supplies a validated array and the UI no longer re-parses a JSON string.

🤖 Prompt for AI Agents
In controller/topup.go around lines 53 to 55, the code currently returns
setting.CreemProducts as a raw JSON string; unmarshal setting.CreemProducts into
a Go slice (e.g., []string) before assembling the response, add import
"encoding/json" at the top of the file, handle unmarshal errors (return or log
and fall back to empty slice) and set "creem_products" in the response map to
that slice (not the original string); also update
web/src/components/topup/index.jsx (around line ~383) to remove JSON.parse usage
and consume the array directly from the backend.

"min_topup": operation_setting.MinTopUp,
"stripe_min_topup": setting.StripeMinTopUp,
Expand Down
Loading