fix(currency): initialize exchange rate to 0 and improve error logging in purchase checkout

This commit is contained in:
Chang lue Tsen 2026-01-07 09:35:52 -05:00
parent 58caa497c3
commit 7d84cf858b
3 changed files with 12 additions and 1 deletions

View File

@ -24,7 +24,7 @@ func Currency(ctx *svc.ServiceContext) {
AccessKey string AccessKey string
}{} }{}
tool.SystemConfigSliceReflectToStruct(currency, &configs) tool.SystemConfigSliceReflectToStruct(currency, &configs)
ctx.ExchangeRate = 0 // Default exchange rate to 0
ctx.Config.Currency = config.Currency{ ctx.Config.Currency = config.Currency{
Unit: configs.CurrencyUnit, Unit: configs.CurrencyUnit,
Symbol: configs.CurrencySymbol, Symbol: configs.CurrencySymbol,

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"reflect" "reflect"
"github.com/perfect-panel/server/initialize"
"github.com/perfect-panel/server/internal/config" "github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/internal/model/system" "github.com/perfect-panel/server/internal/model/system"
"github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/pkg/tool"
@ -54,6 +55,7 @@ func (l *UpdateCurrencyConfigLogic) UpdateCurrencyConfig(req *types.CurrencyConf
// clear cache // clear cache
return l.svcCtx.Redis.Del(l.ctx, config.CurrencyConfigKey, config.GlobalConfigKey).Err() return l.svcCtx.Redis.Del(l.ctx, config.CurrencyConfigKey, config.GlobalConfigKey).Err()
}) })
initialize.Currency(l.svcCtx)
if err != nil { if err != nil {
l.Errorw("[UpdateCurrencyConfig] update currency config error", logger.Field("error", err.Error())) l.Errorw("[UpdateCurrencyConfig] update currency config error", logger.Field("error", err.Error()))
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update invite config error: %v", err) return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update invite config error: %v", err)

View File

@ -51,6 +51,7 @@ func NewPurchaseCheckoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
// PurchaseCheckout processes the checkout for an order using the specified payment method // PurchaseCheckout processes the checkout for an order using the specified payment method
// It validates the order, retrieves payment configuration, and routes to the appropriate payment handler // It validates the order, retrieves payment configuration, and routes to the appropriate payment handler
func (l *PurchaseCheckoutLogic) PurchaseCheckout(req *types.CheckoutOrderRequest) (resp *types.CheckoutOrderResponse, err error) { func (l *PurchaseCheckoutLogic) PurchaseCheckout(req *types.CheckoutOrderRequest) (resp *types.CheckoutOrderResponse, err error) {
// Validate and retrieve order information // Validate and retrieve order information
orderInfo, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo) orderInfo, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
if err != nil { if err != nil {
@ -76,6 +77,7 @@ func (l *PurchaseCheckoutLogic) PurchaseCheckout(req *types.CheckoutOrderRequest
// Process EPay payment - generates payment URL for redirect // Process EPay payment - generates payment URL for redirect
url, err := l.epayPayment(paymentConfig, orderInfo, req.ReturnUrl) url, err := l.epayPayment(paymentConfig, orderInfo, req.ReturnUrl)
if err != nil { if err != nil {
l.Logger.Error("[PurchaseCheckout] epay error", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "epayPayment error: %v", err.Error()) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "epayPayment error: %v", err.Error())
} }
resp = &types.CheckoutOrderResponse{ resp = &types.CheckoutOrderResponse{
@ -274,6 +276,7 @@ func (l *PurchaseCheckoutLogic) epayPayment(config *payment.Payment, info *order
// Convert order amount to CNY using current exchange rate // Convert order amount to CNY using current exchange rate
amount, err = l.queryExchangeRate("CNY", info.Amount) amount, err = l.queryExchangeRate("CNY", info.Amount)
if err != nil { if err != nil {
l.Logger.Error("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
return "", err return "", err
} }
} else { } else {
@ -381,6 +384,11 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
// Convert cents to decimal amount // Convert cents to decimal amount
amount = float64(src) / float64(100) amount = float64(src) / float64(100)
// No conversion needed if target currency matches system currency
if to == l.svcCtx.Config.Currency.Unit {
return amount, nil
}
if l.svcCtx.ExchangeRate != 0 && to == "CNY" { if l.svcCtx.ExchangeRate != 0 && to == "CNY" {
amount = amount * l.svcCtx.ExchangeRate amount = amount * l.svcCtx.ExchangeRate
return amount, nil return amount, nil
@ -394,6 +402,7 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
// Convert currency if system currency differs from target currency // Convert currency if system currency differs from target currency
result, err := exchangeRate.GetExchangeRete(l.svcCtx.Config.Currency.Unit, to, l.svcCtx.Config.Currency.AccessKey, 1) result, err := exchangeRate.GetExchangeRete(l.svcCtx.Config.Currency.Unit, to, l.svcCtx.Config.Currency.AccessKey, 1)
if err != nil { if err != nil {
l.Logger.Error("[PurchaseCheckout] QueryExchangeRate error", logger.Field("error", err.Error()))
return 0, err return 0, err
} }
l.svcCtx.ExchangeRate = result l.svcCtx.ExchangeRate = result