Files
quyun-v2/backend/app/http/v1/transaction.go

100 lines
2.5 KiB
Go

package v1
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/services"
"quyun/v2/database/models"
"github.com/gofiber/fiber/v3"
)
// @provider
type Transaction struct{}
// Create Order
//
// @Router /t/:tenantCode/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 user local key(__ctx_user)
// @Bind form body
func (t *Transaction) Create(
ctx fiber.Ctx,
user *models.User,
form *dto.OrderCreateForm,
) (*dto.OrderCreateResponse, error) {
tenantID := getTenantID(ctx)
return services.Order.Create(ctx, tenantID, user.ID, form)
}
// Pay for order
//
// @Router /t/:tenantCode/v1/orders/:id<int>/pay [post]
// @Summary Pay for order
// @Description Pay for order
// @Tags Transaction
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Param form body dto.OrderPayForm true "Pay form"
// @Success 200 {object} dto.OrderPayResponse
// @Bind user local key(__ctx_user)
// @Bind id path
// @Bind form body
func (t *Transaction) Pay(
ctx fiber.Ctx,
user *models.User,
id int64,
form *dto.OrderPayForm,
) (*dto.OrderPayResponse, error) {
tenantID := getTenantID(ctx)
return services.Order.Pay(ctx, tenantID, user.ID, id, form)
}
// Check order payment status
//
// @Router /t/:tenantCode/v1/orders/:id<int>/status [get]
// @Summary Check order status
// @Description Check order payment status
// @Tags Transaction
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Success 200 {object} dto.OrderStatusResponse
// @Bind user local key(__ctx_user)
// @Bind id path
func (t *Transaction) Status(ctx fiber.Ctx, user *models.User, id int64) (*dto.OrderStatusResponse, error) {
tenantID := getTenantID(ctx)
return services.Order.Status(ctx, tenantID, user.ID, id)
}
type WebhookForm struct {
OrderID int64 `json:"order_id"`
ExternalID string `json:"external_id"`
}
// Payment Webhook
//
// @Router /t/:tenantCode/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) {
tenantID := getTenantID(ctx)
err := services.Order.ProcessExternalPayment(ctx, tenantID, form.OrderID, form.ExternalID)
if err != nil {
return "fail", err
}
return "success", nil
}