feat(handler): 支持代购订单预检和充值订单支付回调

- OrderHandler 新增 PurchaseCheck 接口用于代购订单预检
- PaymentCallback 支持充值订单支付回调处理
- 根据订单号前缀区分订单类型(代购订单 vs 充值订单)
- 充值订单回调自动更新订单状态和钱包余额
This commit is contained in:
2026-01-31 12:15:03 +08:00
parent 760b3db1df
commit 902ddb3687
2 changed files with 49 additions and 7 deletions

View File

@@ -107,3 +107,21 @@ func (h *OrderHandler) Cancel(c *fiber.Ctx) error {
return response.Success(c, nil) return response.Success(c, nil)
} }
// PurchaseCheck 代购订单预检接口
// 路由: POST /api/admin/orders/purchase-check
// 参数: order_type(订单类型), resource_id(资源ID), package_ids(套餐ID列表)
// 响应: 套餐总价、强充要求、实际支付金额等预检结果
func (h *OrderHandler) PurchaseCheck(c *fiber.Ctx) error {
var req dto.PurchaseCheckRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
result, err := h.service.GetPurchaseCheck(c.UserContext(), &req)
if err != nil {
return err
}
return response.Success(c, result)
}

View File

@@ -3,12 +3,15 @@ package callback
import ( import (
"context" "context"
"net/http" "net/http"
"strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp/fasthttpadaptor" "github.com/valyala/fasthttp/fasthttpadaptor"
"github.com/break/junhong_cmp_fiber/internal/model" "github.com/break/junhong_cmp_fiber/internal/model"
orderService "github.com/break/junhong_cmp_fiber/internal/service/order" orderService "github.com/break/junhong_cmp_fiber/internal/service/order"
rechargeService "github.com/break/junhong_cmp_fiber/internal/service/recharge"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors" "github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/response" "github.com/break/junhong_cmp_fiber/pkg/response"
"github.com/break/junhong_cmp_fiber/pkg/wechat" "github.com/break/junhong_cmp_fiber/pkg/wechat"
@@ -16,12 +19,14 @@ import (
type PaymentHandler struct { type PaymentHandler struct {
orderService *orderService.Service orderService *orderService.Service
rechargeService *rechargeService.Service
wechatPayment wechat.PaymentServiceInterface wechatPayment wechat.PaymentServiceInterface
} }
func NewPaymentHandler(orderService *orderService.Service, wechatPayment wechat.PaymentServiceInterface) *PaymentHandler { func NewPaymentHandler(orderService *orderService.Service, rechargeService *rechargeService.Service, wechatPayment wechat.PaymentServiceInterface) *PaymentHandler {
return &PaymentHandler{ return &PaymentHandler{
orderService: orderService, orderService: orderService,
rechargeService: rechargeService,
wechatPayment: wechatPayment, wechatPayment: wechatPayment,
} }
} }
@@ -41,6 +46,14 @@ func (h *PaymentHandler) WechatPayCallback(c *fiber.Ctx) error {
if result.TradeState != "SUCCESS" { if result.TradeState != "SUCCESS" {
return nil return nil
} }
// 根据订单号前缀判断订单类型
if strings.HasPrefix(result.OutTradeNo, constants.RechargeOrderPrefix) {
// 充值订单回调
return h.rechargeService.HandlePaymentCallback(ctx, result.OutTradeNo, model.PaymentMethodWechat, result.TransactionID)
}
// 套餐订单回调
return h.orderService.HandlePaymentCallback(ctx, result.OutTradeNo, model.PaymentMethodWechat) return h.orderService.HandlePaymentCallback(ctx, result.OutTradeNo, model.PaymentMethodWechat)
}) })
@@ -65,9 +78,20 @@ func (h *PaymentHandler) AlipayCallback(c *fiber.Ctx) error {
return errors.New(errors.CodeInvalidParam, "订单号不能为空") return errors.New(errors.CodeInvalidParam, "订单号不能为空")
} }
if err := h.orderService.HandlePaymentCallback(c.UserContext(), req.OrderNo, model.PaymentMethodAlipay); err != nil { ctx := c.UserContext()
// 根据订单号前缀判断订单类型
if strings.HasPrefix(req.OrderNo, constants.RechargeOrderPrefix) {
// 充值订单回调
if err := h.rechargeService.HandlePaymentCallback(ctx, req.OrderNo, model.PaymentMethodAlipay, ""); err != nil {
return err return err
} }
} else {
// 套餐订单回调
if err := h.orderService.HandlePaymentCallback(ctx, req.OrderNo, model.PaymentMethodAlipay); err != nil {
return err
}
}
return c.SendString("success") return c.SendString("success")
} }