fix(currency): initialize exchange rate to 0 and improve error logging in purchase checkout
This commit is contained in:
parent
58caa497c3
commit
7d84cf858b
@ -24,7 +24,7 @@ func Currency(ctx *svc.ServiceContext) {
|
||||
AccessKey string
|
||||
}{}
|
||||
tool.SystemConfigSliceReflectToStruct(currency, &configs)
|
||||
|
||||
ctx.ExchangeRate = 0 // Default exchange rate to 0
|
||||
ctx.Config.Currency = config.Currency{
|
||||
Unit: configs.CurrencyUnit,
|
||||
Symbol: configs.CurrencySymbol,
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/perfect-panel/server/initialize"
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
"github.com/perfect-panel/server/internal/model/system"
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
@ -54,6 +55,7 @@ func (l *UpdateCurrencyConfigLogic) UpdateCurrencyConfig(req *types.CurrencyConf
|
||||
// clear cache
|
||||
return l.svcCtx.Redis.Del(l.ctx, config.CurrencyConfigKey, config.GlobalConfigKey).Err()
|
||||
})
|
||||
initialize.Currency(l.svcCtx)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
@ -51,6 +51,7 @@ func NewPurchaseCheckoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
// 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
|
||||
func (l *PurchaseCheckoutLogic) PurchaseCheckout(req *types.CheckoutOrderRequest) (resp *types.CheckoutOrderResponse, err error) {
|
||||
|
||||
// Validate and retrieve order information
|
||||
orderInfo, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
@ -76,6 +77,7 @@ func (l *PurchaseCheckoutLogic) PurchaseCheckout(req *types.CheckoutOrderRequest
|
||||
// Process EPay payment - generates payment URL for redirect
|
||||
url, err := l.epayPayment(paymentConfig, orderInfo, req.ReturnUrl)
|
||||
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())
|
||||
}
|
||||
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
|
||||
amount, err = l.queryExchangeRate("CNY", info.Amount)
|
||||
if err != nil {
|
||||
l.Logger.Error("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
@ -381,6 +384,11 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
|
||||
// Convert cents to decimal amount
|
||||
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" {
|
||||
amount = amount * l.svcCtx.ExchangeRate
|
||||
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
|
||||
result, err := exchangeRate.GetExchangeRete(l.svcCtx.Config.Currency.Unit, to, l.svcCtx.Config.Currency.AccessKey, 1)
|
||||
if err != nil {
|
||||
l.Logger.Error("[PurchaseCheckout] QueryExchangeRate error", logger.Field("error", err.Error()))
|
||||
return 0, err
|
||||
}
|
||||
l.svcCtx.ExchangeRate = result
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user