Files
quyun-v2/backend/app/http/v1/transaction.go
Rogee 54de243fa1 feat: Refactor user context handling and service methods
- Updated middleware to fetch user and tenant models by ID and set them in context.
- Refactored common service methods to accept userID as a parameter instead of extracting from context.
- Modified content service methods to include userID as a parameter for better clarity and performance.
- Adjusted coupon, creator, notification, order, tenant, user, and wallet services to utilize userID directly.
- Enhanced context key constants for improved readability and maintainability.
2025-12-30 22:49:26 +08:00

87 lines
2.3 KiB
Go

package v1
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/services"
"quyun/v2/pkg/consts"
"github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
)
// @provider
type Transaction struct{}
// Create Order
//
// @Router /v1/orders [post]
// @Summary Create Order
// @Description Create Order
// @Tags Transaction
// @Accept json
// @Produce json
// @Param form body dto.OrderCreateForm true "Create form"
// @Success 200 {object} dto.OrderCreateResponse
// @Bind form body
func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.OrderCreateResponse, error) {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Order.Create(ctx.Context(), uid, form)
}
// Pay for order
//
// @Router /v1/orders/:id/pay [post]
// @Summary Pay for order
// @Description Pay for order
// @Tags Transaction
// @Accept json
// @Produce json
// @Param id path string true "Order ID"
// @Param form body dto.OrderPayForm true "Pay form"
// @Success 200 {object} dto.OrderPayResponse
// @Bind id path
// @Bind form body
func (t *Transaction) Pay(ctx fiber.Ctx, id string, form *dto.OrderPayForm) (*dto.OrderPayResponse, error) {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Order.Pay(ctx.Context(), uid, id, form)
}
// Check order payment status
//
// @Router /v1/orders/:id/status [get]
// @Summary Check order status
// @Description Check order payment status
// @Tags Transaction
// @Accept json
// @Produce json
// @Param id path string true "Order ID"
// @Success 200 {object} dto.OrderStatusResponse
// @Bind id path
func (t *Transaction) Status(ctx fiber.Ctx, id string) (*dto.OrderStatusResponse, error) {
return services.Order.Status(ctx, id)
}
type WebhookForm struct {
OrderID string `json:"order_id"`
ExternalID string `json:"external_id"`
}
// Payment Webhook
//
// @Router /v1/webhook/payment/notify [post]
// @Summary Payment Webhook
// @Description Payment Webhook
// @Tags Transaction
// @Accept json
// @Produce json
// @Param form body WebhookForm true "Webhook Data"
// @Success 200 {string} string "success"
// @Bind form body
func (t *Transaction) Webhook(ctx fiber.Ctx, form *WebhookForm) (string, error) {
err := services.Order.ProcessExternalPayment(ctx, form.OrderID, form.ExternalID)
if err != nil {
return "fail", err
}
return "success", nil
}