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:
2026-05-26 20:55:13 +08:00
parent 766eb34000
commit 5d3e045e12
3 changed files with 63 additions and 39 deletions
+41 -38
View File
@@ -10,6 +10,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
"sale/internal/models" "sale/internal/models"
"sale/internal/utils" "sale/internal/utils"
@@ -24,27 +25,13 @@ func NewPaymentHandler() *PaymentHandler {
} }
type BepUsdtCreateRequest struct { type BepUsdtCreateRequest struct {
Address string `json:"address"` TradeType string `json:"trade_type,omitempty"`
TradeType string `json:"trade_type"`
OrderID string `json:"order_id"` OrderID string `json:"order_id"`
Amount float64 `json:"amount"` Amount float64 `json:"amount"`
Signature string `json:"signature"`
NotifyURL string `json:"notify_url"` NotifyURL string `json:"notify_url"`
RedirectURL string `json:"redirect_url"` RedirectURL string `json:"redirect_url,omitempty"`
Timeout int `json:"timeout"` Signature string `json:"signature"`
Rate string `json:"rate"` Timestamp int64 `json:"timestamp"`
}
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"`
} }
func (h *PaymentHandler) CreatePayment(c *gin.Context) { func (h *PaymentHandler) CreatePayment(c *gin.Context) {
@@ -73,17 +60,12 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
var config map[string]string var config map[string]string
json.Unmarshal([]byte(channel.Config), &config) json.Unmarshal([]byte(channel.Config), &config)
apiURL := config["bepusdt_api_url"] apiURL := strings.TrimSuffix(config["bepusdt_api_url"], "/")
apiToken := config["bepusdt_api_token"] apiToken := config["bepusdt_api_token"]
tradeType := config["bepusdt_trade_type"] tradeType := config["bepusdt_trade_type"]
if tradeType == "" { if tradeType == "" {
tradeType = "usdt.trc20" tradeType = "usdt.trc20"
} }
timeout, _ := strconv.Atoi(config["bepusdt_timeout"])
if timeout < 60 {
timeout = 600
}
rate := config["bepusdt_rate"]
scheme := "http" scheme := "http"
if c.Request.TLS != nil || c.GetHeader("X-Forwarded-Proto") == "https" { 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) notifyURL := fmt.Sprintf("%s/api/payment/bepusdt/notify", baseURL)
redirectURL := fmt.Sprintf("%s/orders/%d", baseURL, order.ID) redirectURL := fmt.Sprintf("%s/orders/%d", baseURL, order.ID)
timestamp := time.Now().Unix()
params := map[string]string{ params := map[string]string{
"order_id": strconv.Itoa(int(order.ID)), "order_id": strconv.Itoa(int(order.ID)),
"amount": fmt.Sprintf("%.2f", order.TotalAmount), "amount": strconv.FormatFloat(order.TotalAmount, 'f', -1, 64),
"notify_url": notifyURL, "notify_url": notifyURL,
"redirect_url": redirectURL, "redirect_url": redirectURL,
"trade_type": tradeType, "timestamp": strconv.FormatInt(timestamp, 10),
} }
if timeout > 0 { if tradeType != "" {
params["timeout"] = strconv.Itoa(timeout) params["trade_type"] = tradeType
}
if rate != "" {
params["rate"] = rate
} }
signature := signBepUsdt(params, apiToken) signature := signBepUsdt(params, apiToken)
@@ -115,11 +95,10 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
TradeType: tradeType, TradeType: tradeType,
OrderID: strconv.Itoa(int(order.ID)), OrderID: strconv.Itoa(int(order.ID)),
Amount: order.TotalAmount, Amount: order.TotalAmount,
Signature: signature,
NotifyURL: notifyURL, NotifyURL: notifyURL,
RedirectURL: redirectURL, RedirectURL: redirectURL,
Timeout: timeout, Signature: signature,
Rate: rate, Timestamp: timestamp,
} }
body, _ := json.Marshal(reqBody) body, _ := json.Marshal(reqBody)
@@ -144,11 +123,35 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
return 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) tradeID, _ := data["trade_id"].(string)
actualAmount, _ := data["actual_amount"].(string)
token, _ := data["token"].(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{}{ utils.DB.Model(&order).Updates(map[string]interface{}{
"payment_method": "bepusdt", "payment_method": "bepusdt",
@@ -160,7 +163,7 @@ func (h *PaymentHandler) CreatePayment(c *gin.Context) {
"amount": data["amount"], "amount": data["amount"],
"actual_amount": actualAmount, "actual_amount": actualAmount,
"token": token, "token": token,
"expiration_time": int(expiration), "expiration_time": remainingSeconds,
"trade_type": tradeType, "trade_type": tradeType,
"channel_id": channel.ID, "channel_id": channel.ID,
} }
+11 -1
View File
@@ -6,6 +6,7 @@
<div class="status-section"> <div class="status-section">
<el-tag :type="statusType(order.status)" size="large">{{ statusText(order.status) }}</el-tag> <el-tag :type="statusType(order.status)" size="large">{{ statusText(order.status) }}</el-tag>
<span v-if="order.payment_method" class="payment-method">支付方式: {{ paymentMethodText(order.payment_method) }}</span> <span v-if="order.payment_method" class="payment-method">支付方式: {{ paymentMethodText(order.payment_method) }}</span>
<el-button v-if="order.status === 'pending_payment'" type="primary" @click="goPay">去支付</el-button>
</div> </div>
<div class="section-title">订单信息</div> <div class="section-title">订单信息</div>
@@ -57,12 +58,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { orderApi } from '../../api' import { orderApi } from '../../api'
import BackNav from '../../components/BackNav.vue' import BackNav from '../../components/BackNav.vue'
const route = useRoute() const route = useRoute()
const router = useRouter()
const order = ref<any>(null) const order = ref<any>(null)
const showRefund = ref(false) const showRefund = ref(false)
const refundReason = ref('') const refundReason = ref('')
@@ -108,6 +110,14 @@ onMounted(async () => {
order.value = res.data order.value = res.data
}) })
function goPay() {
if (order.value?.payment_method === 'bepusdt') {
router.push(`/checkout/${order.value.id}`)
} else {
ElMessage.info('请联系客服完成支付')
}
}
async function applyRefund() { async function applyRefund() {
if (!refundReason.value.trim()) { if (!refundReason.value.trim()) {
ElMessage.warning('请填写退款原因') ElMessage.warning('请填写退款原因')
+11
View File
@@ -18,6 +18,7 @@
<el-table-column label="操作" width="200"> <el-table-column label="操作" width="200">
<template #default="{ row }"> <template #default="{ row }">
<el-button link type="primary" size="small" @click="$router.push(`/orders/${row.id}`)">查看</el-button> <el-button link type="primary" size="small" @click="$router.push(`/orders/${row.id}`)">查看</el-button>
<el-button v-if="row.status === 'pending_payment'" link type="success" size="small" @click="goPay(row)">去支付</el-button>
<el-button v-if="row.status === 'pending_payment' || row.status === 'pending_confirm'" link type="warning" size="small" @click="cancelOrder(row.id)">取消</el-button> <el-button v-if="row.status === 'pending_payment' || row.status === 'pending_confirm'" link type="warning" size="small" @click="cancelOrder(row.id)">取消</el-button>
<el-button v-if="row.status === 'shipped'" link type="success" size="small" @click="confirmReceipt(row.id)">确认收货</el-button> <el-button v-if="row.status === 'shipped'" link type="success" size="small" @click="confirmReceipt(row.id)">确认收货</el-button>
</template> </template>
@@ -29,10 +30,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted } from 'vue' import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus' import { ElMessage, ElMessageBox } from 'element-plus'
import { orderApi } from '../../api' import { orderApi } from '../../api'
import BackNav from '../../components/BackNav.vue' import BackNav from '../../components/BackNav.vue'
const router = useRouter()
const orders = ref<any[]>([]) const orders = ref<any[]>([])
const statusMap: Record<string, string> = { const statusMap: Record<string, string> = {
@@ -66,6 +69,14 @@ async function fetchOrders() {
} }
} }
function goPay(row: any) {
if (row.payment_method === 'bepusdt') {
router.push(`/checkout/${row.id}`)
} else {
ElMessage.info('请联系客服完成支付')
}
}
async function cancelOrder(id: number) { async function cancelOrder(id: number) {
try { try {
await ElMessageBox.confirm('确定要取消该订单吗?', '提示', { type: 'warning' }) await ElMessageBox.confirm('确定要取消该订单吗?', '提示', { type: 'warning' })