fix: retire public payment verify and backfill trade no
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -459,29 +460,20 @@ type PublicOrderResult struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// VerifyOrderPublic verifies payment status without requiring authentication.
|
||||
// Returns limited order info (no user details) to prevent information leakage.
|
||||
var errPaymentPublicOrderVerifyRemoved = infraerrors.New(
|
||||
http.StatusGone,
|
||||
"PAYMENT_PUBLIC_ORDER_VERIFY_REMOVED",
|
||||
"public payment order verification by out_trade_no has been removed; use resume_token recovery instead",
|
||||
).WithMetadata(map[string]string{
|
||||
"replacement_endpoint": "/api/v1/payment/public/orders/resolve",
|
||||
"replacement_field": "resume_token",
|
||||
})
|
||||
|
||||
// VerifyOrderPublic is kept as a compatibility shim for the removed anonymous
|
||||
// out_trade_no lookup endpoint and always returns HTTP 410 Gone.
|
||||
// POST /api/v1/payment/public/orders/verify
|
||||
func (h *PaymentHandler) VerifyOrderPublic(c *gin.Context) {
|
||||
var req VerifyOrderRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
order, err := h.paymentService.VerifyOrderPublic(c.Request.Context(), req.OutTradeNo)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, PublicOrderResult{
|
||||
ID: order.ID,
|
||||
OutTradeNo: order.OutTradeNo,
|
||||
Amount: order.Amount,
|
||||
PayAmount: order.PayAmount,
|
||||
PaymentType: order.PaymentType,
|
||||
OrderType: order.OrderType,
|
||||
Status: order.Status,
|
||||
})
|
||||
response.ErrorFrom(c, errPaymentPublicOrderVerifyRemoved)
|
||||
}
|
||||
|
||||
// ResolveOrderPublicByResumeToken resolves a payment order from a signed resume token.
|
||||
|
||||
@@ -3,10 +3,24 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
dbent "github.com/Wei-Shaw/sub2api/ent"
|
||||
"github.com/Wei-Shaw/sub2api/ent/enttest"
|
||||
"github.com/Wei-Shaw/sub2api/internal/payment"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestApplyWeChatPaymentResumeClaims(t *testing.T) {
|
||||
@@ -59,3 +73,42 @@ func TestApplyWeChatPaymentResumeClaimsRejectsPaymentTypeMismatch(t *testing.T)
|
||||
t.Fatal("applyWeChatPaymentResumeClaims should reject mismatched payment types")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyOrderPublicReturnsGone(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
db, err := sql.Open("sqlite", "file:payment_handler_public_verify?mode=memory&cache=shared")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
_, err = db.Exec("PRAGMA foreign_keys = ON")
|
||||
require.NoError(t, err)
|
||||
|
||||
drv := entsql.OpenDB(dialect.SQLite, db)
|
||||
client := enttest.NewClient(t, enttest.WithOptions(dbent.Driver(drv)))
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
|
||||
paymentSvc := service.NewPaymentService(client, payment.NewRegistry(), nil, nil, nil, nil, nil, nil)
|
||||
h := NewPaymentHandler(paymentSvc, nil, nil)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
ctx.Request = httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/payment/public/orders/verify",
|
||||
bytes.NewBufferString(`{"out_trade_no":"legacy-order-no"}`),
|
||||
)
|
||||
ctx.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
h.VerifyOrderPublic(ctx)
|
||||
|
||||
require.Equal(t, http.StatusGone, recorder.Code)
|
||||
|
||||
var resp response.Response
|
||||
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &resp))
|
||||
require.Equal(t, http.StatusGone, resp.Code)
|
||||
require.Equal(t, "PAYMENT_PUBLIC_ORDER_VERIFY_REMOVED", resp.Reason)
|
||||
require.Contains(t, resp.Message, "removed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user