fix: 参照company项目重写BepUsdt支付:amount用%.2f格式化,请求体直接序列化map
This commit is contained in:
@@ -25,16 +25,6 @@ func NewPaymentHandler() *PaymentHandler {
|
||||
return &PaymentHandler{}
|
||||
}
|
||||
|
||||
type BepUsdtCreateRequest struct {
|
||||
TradeType string `json:"trade_type,omitempty"`
|
||||
OrderID string `json:"order_id"`
|
||||
Amount float64 `json:"amount"`
|
||||
NotifyURL string `json:"notify_url"`
|
||||
RedirectURL string `json:"redirect_url,omitempty"`
|
||||
Signature string `json:"signature"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
userID := c.GetUint("user_id")
|
||||
orderID, _ := strconv.Atoi(c.Param("id"))
|
||||
@@ -76,39 +66,35 @@ 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()
|
||||
|
||||
amountStr := fmt.Sprint(order.TotalAmount)
|
||||
|
||||
params := map[string]string{
|
||||
"order_id": strconv.Itoa(int(order.ID)),
|
||||
"amount": amountStr,
|
||||
"amount": fmt.Sprintf("%.2f", order.TotalAmount),
|
||||
"notify_url": notifyURL,
|
||||
"redirect_url": redirectURL,
|
||||
"timestamp": strconv.FormatInt(timestamp, 10),
|
||||
}
|
||||
if tradeType != "" {
|
||||
params["trade_type"] = tradeType
|
||||
}
|
||||
|
||||
signature := signBepUsdt(params, apiToken)
|
||||
params["signature"] = signature
|
||||
|
||||
log.Printf("[BepUsdt] signature params: %v", params)
|
||||
log.Printf("[BepUsdt] signature: %s, token: %s", signature, apiToken)
|
||||
|
||||
reqBody := BepUsdtCreateRequest{
|
||||
TradeType: tradeType,
|
||||
OrderID: strconv.Itoa(int(order.ID)),
|
||||
Amount: order.TotalAmount,
|
||||
NotifyURL: notifyURL,
|
||||
RedirectURL: redirectURL,
|
||||
Signature: signature,
|
||||
Timestamp: timestamp,
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(reqBody)
|
||||
body, _ := json.Marshal(params)
|
||||
log.Printf("[BepUsdt] request body: %s", string(body))
|
||||
resp, err := http.Post(apiURL+"/api/v1/order/create-transaction", "application/json", strings.NewReader(string(body)))
|
||||
|
||||
req, err := http.NewRequest("POST", apiURL+"/api/v1/order/create-transaction", strings.NewReader(string(body)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"})
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to connect payment gateway"})
|
||||
return
|
||||
@@ -116,7 +102,6 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
|
||||
log.Printf("[BepUsdt] response: %s", string(respBody))
|
||||
|
||||
var result map[string]interface{}
|
||||
@@ -252,7 +237,7 @@ func (h *PaymentHandler) CheckPaymentStatus(c *gin.Context) {
|
||||
func signBepUsdt(params map[string]string, token string) string {
|
||||
keys := make([]string, 0, len(params))
|
||||
for k := range params {
|
||||
if params[k] != "" {
|
||||
if params[k] != "" && k != "signature" && k != "sign_type" {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user