237 lines
7.7 KiB
Go
237 lines
7.7 KiB
Go
package v1
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/dto"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/middleware"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/service"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/app"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/errcode"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/wechat"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// OrderHandler 订单与退款相关接口。
|
|
type OrderHandler struct {
|
|
svc *service.OrderService
|
|
}
|
|
|
|
// NewOrderHandler 创建订单 handler。
|
|
func NewOrderHandler() *OrderHandler {
|
|
return &OrderHandler{svc: service.NewOrderService()}
|
|
}
|
|
|
|
// Create 创建订单并发起微信下单。
|
|
func (h *OrderHandler) Create(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.OrderCreateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.Create(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// VirtualCreate 创建虚拟支付订单(小程序虚拟支付,覆盖 iOS/Android 全终端)。
|
|
func (h *OrderHandler) VirtualCreate(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.VirtualOrderCreateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.VirtualOrderCreate(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// List 分页查询我的订单。
|
|
func (h *OrderHandler) List(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.OrderListRequest
|
|
if err := c.ShouldBindQuery(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.List(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// Cancel 取消订单(orderNo 来自路径参数)。
|
|
func (h *OrderHandler) Cancel(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
request := dto.OrderCancelRequest{OrderNo: c.Param("orderNo")}
|
|
if request.OrderNo == "" {
|
|
response.ToErrorResponse(errcode.InvalidParams)
|
|
return
|
|
}
|
|
out, e := h.svc.Cancel(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// RefundApply 申请退款。
|
|
func (h *OrderHandler) RefundApply(c *gin.Context) {
|
|
response := app.NewResponse(c)
|
|
userID, ok := middleware.GetUserID(c.Request.Context())
|
|
if !ok {
|
|
response.ToErrorResponse(errcode.Unauthorized)
|
|
return
|
|
}
|
|
var request dto.RefundApplyRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
|
|
return
|
|
}
|
|
out, e := h.svc.RefundApply(c.Request.Context(), userID, &request)
|
|
if e != nil {
|
|
response.ToErrorResponse(e)
|
|
return
|
|
}
|
|
response.ToResponse(out)
|
|
}
|
|
|
|
// PayNotify 微信支付结果回调(公开,按微信规范返回受理结果)。
|
|
func (h *OrderHandler) PayNotify(c *gin.Context) {
|
|
if e := h.svc.PayNotify(c.Request.Context(), c.Request); e != nil {
|
|
wechatCallbackFail(c, e.Msg())
|
|
return
|
|
}
|
|
wechatCallbackSuccess(c)
|
|
}
|
|
|
|
// RefundNotify 微信退款结果回调(公开,按微信规范返回受理结果)。
|
|
func (h *OrderHandler) RefundNotify(c *gin.Context) {
|
|
if e := h.svc.RefundNotify(c.Request.Context(), c.Request); e != nil {
|
|
wechatCallbackFail(c, e.Msg())
|
|
return
|
|
}
|
|
wechatCallbackSuccess(c)
|
|
}
|
|
|
|
// XPayVerify 虚拟支付消息推送 URL 配置验证(GET):校验签名并原样回显 echostr。
|
|
// 兼容两种模式:安全模式带 msg_signature 且 echostr 为密文(需解密后回显明文);
|
|
// 明文模式带 signature,直接回显 echostr。
|
|
func (h *OrderHandler) XPayVerify(c *gin.Context) {
|
|
timestamp := c.Query("timestamp")
|
|
nonce := c.Query("nonce")
|
|
echostr := c.Query("echostr")
|
|
|
|
if msgSignature := c.Query("msg_signature"); msgSignature != "" {
|
|
plain, err := wechat.DecodeEchoStr(echostr, timestamp, nonce, msgSignature)
|
|
if err != nil {
|
|
// 兼容模式:echostr 可能仍是明文(msg_signature 对明文计算),原样回显。
|
|
slog.WarnContext(c.Request.Context(), "xpay verify: decode echo str failed, fallback to plain echo",
|
|
"remote_addr", c.Request.RemoteAddr, "error", err)
|
|
c.String(http.StatusOK, echostr)
|
|
return
|
|
}
|
|
c.String(http.StatusOK, plain)
|
|
return
|
|
}
|
|
|
|
if wechat.VerifyNotifyURL(timestamp, nonce, c.Query("signature")) {
|
|
c.String(http.StatusOK, echostr)
|
|
return
|
|
}
|
|
slog.WarnContext(c.Request.Context(), "xpay verify: invalid signature", "remote_addr", c.Request.RemoteAddr)
|
|
c.String(http.StatusUnauthorized, "invalid signature")
|
|
}
|
|
|
|
// XPayNotify 虚拟支付消息推送回调(POST,公开,按虚拟支付规范返回 ErrCode 应答)。
|
|
// 安全模式下包体为密文信封:先验签解密再交给业务层,回包同样加密;明文模式直接透传。
|
|
func (h *OrderHandler) XPayNotify(c *gin.Context) {
|
|
raw, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
slog.ErrorContext(c.Request.Context(), "xpay notify: read request body failed",
|
|
"remote_addr", c.Request.RemoteAddr, "error", err)
|
|
writeXPayNotifyReply(c, false, false)
|
|
return
|
|
}
|
|
|
|
encrypted := wechat.IsEncryptedNotify(c.Query("msg_signature"))
|
|
body := raw
|
|
if encrypted {
|
|
plain, derr := wechat.DecodeEncryptedNotify(raw, c.Query("timestamp"), c.Query("nonce"), c.Query("msg_signature"))
|
|
if derr != nil {
|
|
// 验签/解密失败:回失败应答触发微信重试(fail-closed)。
|
|
slog.ErrorContext(c.Request.Context(), "xpay notify: verify or decrypt failed",
|
|
"remote_addr", c.Request.RemoteAddr, "error", derr)
|
|
writeXPayNotifyReply(c, true, false)
|
|
return
|
|
}
|
|
body = plain
|
|
}
|
|
|
|
resp := h.svc.XPayNotify(c.Request.Context(), body)
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", encryptIfNeeded(c, resp, encrypted))
|
|
}
|
|
|
|
// writeXPayNotifyReply 按模式构造应答:加密模式下加密,否则回明文。
|
|
func writeXPayNotifyReply(c *gin.Context, encrypted, success bool) {
|
|
resp := wechat.BuildNotifyResponse(success)
|
|
c.Data(http.StatusOK, "application/json; charset=utf-8", encryptIfNeeded(c, resp, encrypted))
|
|
}
|
|
|
|
// encryptIfNeeded 安全模式下把明文应答加密为信封;加密不可用时退回明文(仍返回 200)。
|
|
func encryptIfNeeded(c *gin.Context, plain []byte, encrypted bool) []byte {
|
|
if !encrypted {
|
|
return plain
|
|
}
|
|
out, err := wechat.EncodeEncryptedNotify(plain, c.Query("nonce"))
|
|
if err != nil {
|
|
slog.WarnContext(c.Request.Context(), "xpay notify: encrypt reply failed, fallback to plain",
|
|
"remote_addr", c.Request.RemoteAddr, "error", err)
|
|
return plain
|
|
}
|
|
return out
|
|
}
|
|
|
|
// wechatCallbackSuccess 按微信规范返回受理成功,避免微信重复回调。
|
|
func wechatCallbackSuccess(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "成功"})
|
|
}
|
|
|
|
// wechatCallbackFail 按微信规范返回受理失败,微信将按策略重试。
|
|
func wechatCallbackFail(c *gin.Context, message string) {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": "FAIL", "message": message})
|
|
}
|