fix:修复钱包功能相关问题
This commit is contained in:
1 parent
9cd7c52f92
commit
1d4d9836da
4 files changed
+28
-9
No files matched your search
@@ -28,13 +28,13 @@ func HandlePaymentCallback(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
// HandlePaymentEpayReturn 处理 Epay 支付完成后的浏览器同步跳转。
|
||||
// 仅查询订单状态决定跳转目标,履约由异步回调完成。
|
||||
func HandlePaymentEpayReturn(r *ghttp.Request) {
|
||||
err := payment.ProcessCallback(r.Context(), r.Request, "epay")
|
||||
if err != nil {
|
||||
r.Response.WriteStatus(302)
|
||||
r.Response.Header().Set("Location", "/console/wallet?pay=fail")
|
||||
orderNo := r.GetQuery("out_trade_no").String()
|
||||
paid := payment.QueryOrderPaid(r.Context(), orderNo)
|
||||
if paid {
|
||||
r.Response.RedirectTo("/console/wallet?pay=success")
|
||||
return
|
||||
}
|
||||
r.Response.WriteStatus(302)
|
||||
r.Response.Header().Set("Location", "/console/wallet?pay=success")
|
||||
r.Response.RedirectTo("/console/wallet?pay=fail")
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package billing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
|
||||
lcommon "github.com/qianfree/team-api/internal/logic/common"
|
||||
)
|
||||
@@ -9,10 +10,13 @@ import (
|
||||
const defaultCNYToUSD = 0.14
|
||||
const defaultUSDToCNY = 7.25
|
||||
|
||||
// ConvertCNYToUSD 将人民币金额转换为美元
|
||||
// usdPrecision 钱包余额精度:小数点后 6 位。
|
||||
const usdPrecision = 1e6
|
||||
|
||||
// ConvertCNYToUSD 将人民币金额转换为美元,向上取整到小数点后 6 位。
|
||||
func ConvertCNYToUSD(ctx context.Context, cnyAmount float64) float64 {
|
||||
rate := GetExchangeRateCNYToUSD(ctx)
|
||||
return cnyAmount * rate
|
||||
return math.Ceil(cnyAmount*rate*usdPrecision) / usdPrecision
|
||||
}
|
||||
|
||||
// ConvertUSDToCNY 将美元金额转换为人民币
|
||||
|
||||
@@ -138,3 +138,17 @@ func ProcessCallback(ctx context.Context, r *http.Request, channelType string) e
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryOrderPaid 查询订单是否已完成支付(paid 或 fulfilled)。
|
||||
// 用于浏览器同步回跳时展示结果,不参与履约处理。
|
||||
func QueryOrderPaid(ctx context.Context, orderNo string) bool {
|
||||
if orderNo == "" {
|
||||
return false
|
||||
}
|
||||
var status string
|
||||
_ = dao.OrdOrders.Ctx(ctx).
|
||||
Where("order_no", orderNo).
|
||||
Fields("status").
|
||||
Scan(&status)
|
||||
return status == "paid" || status == "fulfilled"
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package payment
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -72,7 +73,7 @@ func (p *EpayProvider) HandleCallback(ctx context.Context, r *http.Request, conf
|
||||
|
||||
receivedSign := r.FormValue("sign")
|
||||
expectedSign := epaySign(params, cfg.MerchantKey)
|
||||
if receivedSign != expectedSign {
|
||||
if subtle.ConstantTimeCompare([]byte(receivedSign), []byte(expectedSign)) != 1 {
|
||||
return nil, lcommon.NewBusinessError(422, "易支付签名验证失败")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user