feat(支付): 添加支付金额转换日志并优化代码结构
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m17s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m17s
为支付宝、Stripe、Epay和CryptoSaaS支付逻辑添加金额转换的日志记录 将支付请求参数提取为变量并添加日志记录 优化代码结构,提高可读性和可维护性
This commit is contained in:
parent
6fba1d683a
commit
d1a620f939
@ -185,13 +185,20 @@ func (l *PurchaseCheckoutLogic) alipayF2fPayment(pay *payment.Payment, info *ord
|
|||||||
l.Errorw("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
|
l.Errorw("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
|
||||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "queryExchangeRate error: %s", err.Error())
|
return "", errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "queryExchangeRate error: %s", err.Error())
|
||||||
}
|
}
|
||||||
convertAmount := int64(math.Round(amount * 100)) // Convert to cents for API
|
convertAmount := int64(math.Round(amount * 100))
|
||||||
|
l.Infow("alipay amount",
|
||||||
|
logger.Field("src_cents", info.Amount),
|
||||||
|
logger.Field("decimal", amount),
|
||||||
|
logger.Field("cents", convertAmount),
|
||||||
|
)
|
||||||
|
|
||||||
// Create pre-payment trade and generate QR code
|
// Create pre-payment trade and generate QR code
|
||||||
QRCode, err := client.PreCreateTrade(l.ctx, alipay.Order{
|
o := alipay.Order{
|
||||||
OrderNo: info.OrderNo,
|
OrderNo: info.OrderNo,
|
||||||
Amount: convertAmount,
|
Amount: convertAmount,
|
||||||
})
|
}
|
||||||
|
l.Infow("alipay request", logger.Field("order", o))
|
||||||
|
QRCode, err := client.PreCreateTrade(l.ctx, o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[PurchaseCheckout] PreCreateTrade error", logger.Field("error", err.Error()))
|
l.Errorw("[PurchaseCheckout] PreCreateTrade error", logger.Field("error", err.Error()))
|
||||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "PreCreateTrade error: %s", err.Error())
|
return "", errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "PreCreateTrade error: %s", err.Error())
|
||||||
@ -223,19 +230,24 @@ func (l *PurchaseCheckoutLogic) stripePayment(config string, info *order.Order,
|
|||||||
l.Errorw("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
|
l.Errorw("[PurchaseCheckout] queryExchangeRate error", logger.Field("error", err.Error()))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "queryExchangeRate error: %s", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "queryExchangeRate error: %s", err.Error())
|
||||||
}
|
}
|
||||||
convertAmount := int64(math.Round(amount * 100)) // Convert to cents for Stripe API
|
convertAmount := int64(math.Round(amount * 100))
|
||||||
|
l.Infow("stripe amount",
|
||||||
|
logger.Field("src_cents", info.Amount),
|
||||||
|
logger.Field("decimal", amount),
|
||||||
|
logger.Field("cents", convertAmount),
|
||||||
|
)
|
||||||
|
|
||||||
// Create Stripe payment sheet for client-side processing
|
// Create Stripe payment sheet for client-side processing
|
||||||
result, err := client.CreatePaymentSheet(&stripe.Order{
|
ord := &stripe.Order{
|
||||||
OrderNo: info.OrderNo,
|
OrderNo: info.OrderNo,
|
||||||
Subscribe: strconv.FormatInt(info.SubscribeId, 10),
|
Subscribe: strconv.FormatInt(info.SubscribeId, 10),
|
||||||
Amount: convertAmount,
|
Amount: convertAmount,
|
||||||
Currency: "cny",
|
Currency: "cny",
|
||||||
Payment: stripeConfig.Payment,
|
Payment: stripeConfig.Payment,
|
||||||
},
|
}
|
||||||
&stripe.User{
|
usr := &stripe.User{Email: identifier}
|
||||||
Email: identifier,
|
l.Infow("stripe request", logger.Field("order", ord), logger.Field("user", usr))
|
||||||
})
|
result, err := client.CreatePaymentSheet(ord, usr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[PurchaseCheckout] CreatePaymentSheet error", logger.Field("error", err.Error()))
|
l.Errorw("[PurchaseCheckout] CreatePaymentSheet error", logger.Field("error", err.Error()))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "CreatePaymentSheet error: %s", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "CreatePaymentSheet error: %s", err.Error())
|
||||||
@ -271,11 +283,15 @@ func (l *PurchaseCheckoutLogic) epayPayment(config *payment.Payment, info *order
|
|||||||
client := epay.NewClient(epayConfig.Pid, epayConfig.Url, epayConfig.Key, epayConfig.Type)
|
client := epay.NewClient(epayConfig.Pid, epayConfig.Url, epayConfig.Key, epayConfig.Type)
|
||||||
|
|
||||||
// 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 {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
amount = math.Round(amount*100) / 100
|
amount = math.Round(amount*100) / 100
|
||||||
|
l.Infow("epay amount",
|
||||||
|
logger.Field("src_cents", info.Amount),
|
||||||
|
logger.Field("decimal", amount),
|
||||||
|
)
|
||||||
|
|
||||||
// Build notification URL for payment status callbacks
|
// Build notification URL for payment status callbacks
|
||||||
notifyUrl := ""
|
notifyUrl := ""
|
||||||
@ -290,14 +306,16 @@ func (l *PurchaseCheckoutLogic) epayPayment(config *payment.Payment, info *order
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create payment URL for user redirection
|
// Create payment URL for user redirection
|
||||||
url := client.CreatePayUrl(epay.Order{
|
ord := epay.Order{
|
||||||
Name: l.svcCtx.Config.Site.SiteName,
|
Name: l.svcCtx.Config.Site.SiteName,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
OrderNo: info.OrderNo,
|
OrderNo: info.OrderNo,
|
||||||
SignType: "MD5",
|
SignType: "MD5",
|
||||||
NotifyUrl: notifyUrl,
|
NotifyUrl: notifyUrl,
|
||||||
ReturnUrl: returnUrl,
|
ReturnUrl: returnUrl,
|
||||||
})
|
}
|
||||||
|
l.Infow("epay request", logger.Field("order", ord))
|
||||||
|
url := client.CreatePayUrl(ord)
|
||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,11 +332,15 @@ func (l *PurchaseCheckoutLogic) CryptoSaaSPayment(config *payment.Payment, info
|
|||||||
client := epay.NewClient(epayConfig.AccountID, epayConfig.Endpoint, epayConfig.SecretKey, epayConfig.Type)
|
client := epay.NewClient(epayConfig.AccountID, epayConfig.Endpoint, epayConfig.SecretKey, epayConfig.Type)
|
||||||
|
|
||||||
// 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 {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
amount = math.Round(amount*100) / 100
|
amount = math.Round(amount*100) / 100
|
||||||
|
l.Infow("crypto amount",
|
||||||
|
logger.Field("src_cents", info.Amount),
|
||||||
|
logger.Field("decimal", amount),
|
||||||
|
)
|
||||||
|
|
||||||
// Build notification URL for payment status callbacks
|
// Build notification URL for payment status callbacks
|
||||||
notifyUrl := ""
|
notifyUrl := ""
|
||||||
@ -333,14 +355,16 @@ func (l *PurchaseCheckoutLogic) CryptoSaaSPayment(config *payment.Payment, info
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create payment URL for user redirection
|
// Create payment URL for user redirection
|
||||||
url := client.CreatePayUrl(epay.Order{
|
ord := epay.Order{
|
||||||
Name: l.svcCtx.Config.Site.SiteName,
|
Name: l.svcCtx.Config.Site.SiteName,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
OrderNo: info.OrderNo,
|
OrderNo: info.OrderNo,
|
||||||
SignType: "MD5",
|
SignType: "MD5",
|
||||||
NotifyUrl: notifyUrl,
|
NotifyUrl: notifyUrl,
|
||||||
ReturnUrl: returnUrl,
|
ReturnUrl: returnUrl,
|
||||||
})
|
}
|
||||||
|
l.Infow("crypto request", logger.Field("order", ord))
|
||||||
|
url := client.CreatePayUrl(ord)
|
||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
25
scripts/rounding-demo/main.go
Normal file
25
scripts/rounding-demo/main.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
price := int64(300 * 7)
|
||||||
|
discountPercent := 95.19
|
||||||
|
discount := discountPercent / 100.0
|
||||||
|
amount := int64(math.Round(float64(price) * discount))
|
||||||
|
fmt.Printf("price_cents=%d discount=%.4f amount_cents=%d amount=%.2f\n", price, discount, amount, float64(amount)/100.0)
|
||||||
|
|
||||||
|
couponPercent := 10.0
|
||||||
|
coupon := int64(math.Round(float64(amount) * (couponPercent / 100.0)))
|
||||||
|
amount -= coupon
|
||||||
|
fmt.Printf("coupon_percent=%.2f coupon_cents=%d amount_cents=%d amount=%.2f\n", couponPercent, coupon, amount, float64(amount)/100.0)
|
||||||
|
|
||||||
|
feePercent := 5.0
|
||||||
|
fee := int64(math.Round(float64(amount) * (feePercent / 100.0)))
|
||||||
|
total := amount + fee
|
||||||
|
fmt.Printf("fee_percent=%.2f fee_cents=%d total_cents=%d total=%.2f\n", feePercent, fee, total, float64(total)/100.0)
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user