treasurer/internal/controller/http_controllers/yandex/yandex.go

127 lines
4.9 KiB
Go
Raw Permalink Normal View History

package yandex
2023-06-13 13:22:51 +00:00
import (
"fmt"
2025-06-12 08:59:43 +00:00
"gitea.pena/PenaSide/treasurer/internal/service/callback"
"gitea.pena/PenaSide/treasurer/internal/service/status"
"github.com/gofiber/fiber/v2"
2023-06-13 13:22:51 +00:00
"net/http"
2024-12-16 13:47:40 +00:00
"gitea.pena/PenaSide/treasurer/internal/errors"
"gitea.pena/PenaSide/treasurer/internal/models"
"gitea.pena/PenaSide/treasurer/internal/models/yandex"
"go.uber.org/zap"
2023-06-13 13:22:51 +00:00
)
type YandexStatusControllerDeps struct {
Logger *zap.Logger
2025-06-12 08:59:43 +00:00
StatusService *status.Service
CallbackService *callback.Service
2023-06-13 13:22:51 +00:00
}
type YandexStatusController struct {
logger *zap.Logger
2025-06-12 08:59:43 +00:00
statusService *status.Service
callbackService *callback.Service
2023-06-13 13:22:51 +00:00
}
func NewYandexStatusController(deps YandexStatusControllerDeps) (*YandexStatusController, errors.Error) {
if deps.Logger == nil {
return nil, errors.NewWithMessage("logger is nil on <NewYandexStatusController>", errors.ErrInvalidArgs)
}
if deps.StatusService == nil {
return nil, errors.NewWithMessage("StatusService is nil on <NewYandexStatusController>", errors.ErrInvalidArgs)
}
if deps.CallbackService == nil {
return nil, errors.NewWithMessage("CallbackService is nil on <NewYandexStatusController>", errors.ErrInvalidArgs)
}
return &YandexStatusController{
2023-06-19 17:29:15 +00:00
logger: deps.Logger,
statusService: deps.StatusService,
callbackService: deps.CallbackService,
2023-06-13 13:22:51 +00:00
}, nil
}
2025-06-11 15:39:39 +00:00
func (r *YandexStatusController) SetPaymentStatusCanceled(ctx *fiber.Ctx) error {
var request yandex.WebhookNotification[yandex.Payment]
if err := ctx.BodyParser(&request); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to parse body on <SetPaymentStatusCanceled> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, errors.NewWithError(fmt.Errorf("failed to parse input body: %w", err), errors.ErrInternalError))
}
2025-06-11 15:39:39 +00:00
payment, setStatusErr := r.statusService.SetStatusCanceled(ctx.Context(), request.Object.ID)
2023-06-13 13:22:51 +00:00
if setStatusErr != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to set canceled payment status on <SetPaymentStatusCanceled> of <YandexStatusController>", zap.Error(setStatusErr))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, setStatusErr)
}
2025-06-11 15:39:39 +00:00
if err := r.callbackService.OnFailure(ctx.Context(), &models.Event{
2023-06-13 13:22:51 +00:00
Key: string(request.Event),
Message: "yandex send event: payment canceled",
Payment: payment,
}); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed send success callback on <SetPaymentStatusSucceeded> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, err)
}
return ctx.SendStatus(http.StatusOK)
2023-06-13 13:22:51 +00:00
}
2025-06-11 15:39:39 +00:00
func (r *YandexStatusController) SetPaymentStatusSucceeded(ctx *fiber.Ctx) error {
var request yandex.WebhookNotification[yandex.Payment]
if err := ctx.BodyParser(&request); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to parse body on <SetPaymentStatusSucceeded> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, errors.NewWithError(fmt.Errorf("failed to parse input body: %w", err), errors.ErrInternalError))
}
2025-06-11 15:39:39 +00:00
payment, setStatusErr := r.statusService.SetStatusSuccess(ctx.Context(), request.Object.ID)
2023-06-13 13:22:51 +00:00
if setStatusErr != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to set success payment status on <SetPaymentStatusSucceeded> of <YandexStatusController>", zap.Error(setStatusErr))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, setStatusErr)
}
2025-06-11 15:39:39 +00:00
if err := r.callbackService.OnSuccess(ctx.Context(), &models.Event{
2023-06-13 13:22:51 +00:00
Key: string(request.Event),
Message: "yandex send event: payment succeeded",
Payment: payment,
}); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed send success callback on <SetPaymentStatusSucceeded> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, err)
}
return ctx.SendStatus(http.StatusOK)
2023-06-13 13:22:51 +00:00
}
2025-06-11 15:39:39 +00:00
func (r *YandexStatusController) SetPaymentStatusWaiting(ctx *fiber.Ctx) error {
var request yandex.WebhookNotification[yandex.Payment]
if err := ctx.BodyParser(&request); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to parse body on <SetPaymentStatusWaiting> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, errors.NewWithError(fmt.Errorf("failed to parse input body: %w", err), errors.ErrInternalError))
}
2025-06-11 15:39:39 +00:00
if _, err := r.statusService.SetStatusWaiting(ctx.Context(), request.Object.ID); err != nil {
r.logger.Error("failed to set waiting payment status on <SetPaymentStatusWaiting> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, err)
}
return ctx.SendStatus(http.StatusOK)
2023-06-13 13:22:51 +00:00
}
2025-06-11 15:39:39 +00:00
func (r *YandexStatusController) SetRefundStatusSucceeded(ctx *fiber.Ctx) error {
var request yandex.WebhookNotification[yandex.Payment]
if err := ctx.BodyParser(&request); err != nil {
2025-06-11 15:39:39 +00:00
r.logger.Error("failed to parse body on <SetRefundStatusSucceeded> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, errors.NewWithError(fmt.Errorf("failed to parse input body: %w", err), errors.ErrInternalError))
}
2025-06-11 15:39:39 +00:00
if _, err := r.statusService.SetStatusRefund(ctx.Context(), request.Object.ID); err != nil {
r.logger.Error("failed to set payment status refund on <SetRefundStatusSucceeded> of <YandexStatusController>", zap.Error(err))
2023-06-13 13:22:51 +00:00
return errors.HTTP(ctx, err)
}
return ctx.SendStatus(http.StatusOK)
2023-06-13 13:22:51 +00:00
}