fix: 修复BepUsdt签名错误和待支付订单付款入口
- payment.go: 添加timestamp字段到签名和请求体(参照官方epusdt实现) - payment.go: 修复amount格式化,使用strconv.FormatFloat替代Sprintf - payment.go: 修复expiration_time处理(毫秒转秒,计算剩余秒数) - payment.go: 修复apiURL尾部斜杠处理 - OrderDetail.vue: 添加去支付按钮(pending_payment状态) - Orders.vue: 添加去支付按钮(bepusdt跳转收银台)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sale/internal/models"
|
||||
"sale/internal/utils"
|
||||
@@ -24,27 +25,13 @@ func NewPaymentHandler() *PaymentHandler {
|
||||
}
|
||||
|
||||
type BepUsdtCreateRequest struct {
|
||||
Address string `json:"address"`
|
||||
TradeType string `json:"trade_type"`
|
||||
TradeType string `json:"trade_type,omitempty"`
|
||||
OrderID string `json:"order_id"`
|
||||
Amount float64 `json:"amount"`
|
||||
Signature string `json:"signature"`
|
||||
NotifyURL string `json:"notify_url"`
|
||||
RedirectURL string `json:"redirect_url"`
|
||||
Timeout int `json:"timeout"`
|
||||
Rate string `json:"rate"`
|
||||
}
|
||||
|
||||
type BepUsdtCreateResponse struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
Message string `json:"message"`
|
||||
TradeID string `json:"trade_id"`
|
||||
OrderID string `json:"order_id"`
|
||||
Amount string `json:"amount"`
|
||||
ActualAmount string `json:"actual_amount"`
|
||||
Token string `json:"token"`
|
||||
Expiration int `json:"expiration_time"`
|
||||
PaymentURL string `json:"payment_url"`
|
||||
RedirectURL string `json:"redirect_url,omitempty"`
|
||||
Signature string `json:"signature"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
@@ -73,17 +60,12 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
var config map[string]string
|
||||
json.Unmarshal([]byte(channel.Config), &config)
|
||||
|
||||
apiURL := config["bepusdt_api_url"]
|
||||
apiURL := strings.TrimSuffix(config["bepusdt_api_url"], "/")
|
||||
apiToken := config["bepusdt_api_token"]
|
||||
tradeType := config["bepusdt_trade_type"]
|
||||
if tradeType == "" {
|
||||
tradeType = "usdt.trc20"
|
||||
}
|
||||
timeout, _ := strconv.Atoi(config["bepusdt_timeout"])
|
||||
if timeout < 60 {
|
||||
timeout = 600
|
||||
}
|
||||
rate := config["bepusdt_rate"]
|
||||
|
||||
scheme := "http"
|
||||
if c.Request.TLS != nil || c.GetHeader("X-Forwarded-Proto") == "https" {
|
||||
@@ -94,19 +76,17 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
|
||||
notifyURL := fmt.Sprintf("%s/api/payment/bepusdt/notify", baseURL)
|
||||
redirectURL := fmt.Sprintf("%s/orders/%d", baseURL, order.ID)
|
||||
timestamp := time.Now().Unix()
|
||||
|
||||
params := map[string]string{
|
||||
"order_id": strconv.Itoa(int(order.ID)),
|
||||
"amount": fmt.Sprintf("%.2f", order.TotalAmount),
|
||||
"amount": strconv.FormatFloat(order.TotalAmount, 'f', -1, 64),
|
||||
"notify_url": notifyURL,
|
||||
"redirect_url": redirectURL,
|
||||
"trade_type": tradeType,
|
||||
"timestamp": strconv.FormatInt(timestamp, 10),
|
||||
}
|
||||
if timeout > 0 {
|
||||
params["timeout"] = strconv.Itoa(timeout)
|
||||
}
|
||||
if rate != "" {
|
||||
params["rate"] = rate
|
||||
if tradeType != "" {
|
||||
params["trade_type"] = tradeType
|
||||
}
|
||||
|
||||
signature := signBepUsdt(params, apiToken)
|
||||
@@ -115,11 +95,10 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
TradeType: tradeType,
|
||||
OrderID: strconv.Itoa(int(order.ID)),
|
||||
Amount: order.TotalAmount,
|
||||
Signature: signature,
|
||||
NotifyURL: notifyURL,
|
||||
RedirectURL: redirectURL,
|
||||
Timeout: timeout,
|
||||
Rate: rate,
|
||||
Signature: signature,
|
||||
Timestamp: timestamp,
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(reqBody)
|
||||
@@ -144,11 +123,35 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
data := result["data"].(map[string]interface{})
|
||||
data, ok := result["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid response from payment gateway"})
|
||||
return
|
||||
}
|
||||
|
||||
tradeID, _ := data["trade_id"].(string)
|
||||
actualAmount, _ := data["actual_amount"].(string)
|
||||
token, _ := data["token"].(string)
|
||||
expiration, _ := data["expiration_time"].(float64)
|
||||
|
||||
var actualAmount float64
|
||||
if v, ok := data["actual_amount"].(float64); ok {
|
||||
actualAmount = v
|
||||
}
|
||||
|
||||
var expiration int64
|
||||
if v, ok := data["expiration_time"].(float64); ok {
|
||||
expiration = int64(v)
|
||||
}
|
||||
if expiration > 1e12 {
|
||||
expiration = expiration / 1000
|
||||
}
|
||||
|
||||
var remainingSeconds int64
|
||||
if expiration > 0 {
|
||||
remainingSeconds = expiration - time.Now().Unix()
|
||||
if remainingSeconds < 0 {
|
||||
remainingSeconds = 0
|
||||
}
|
||||
}
|
||||
|
||||
utils.DB.Model(&order).Updates(map[string]interface{}{
|
||||
"payment_method": "bepusdt",
|
||||
@@ -160,7 +163,7 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
"amount": data["amount"],
|
||||
"actual_amount": actualAmount,
|
||||
"token": token,
|
||||
"expiration_time": int(expiration),
|
||||
"expiration_time": remainingSeconds,
|
||||
"trade_type": tradeType,
|
||||
"channel_id": channel.ID,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user