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

138 lines
5.3 KiB
Go
Raw Normal View History

package yandex
2023-06-13 13:22:51 +00:00
import (
"context"
"fmt"
"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 StatusService interface {
SetStatusCanceled(context.Context, string) (*models.Payment, errors.Error)
SetStatusSuccess(context.Context, string) (*models.Payment, errors.Error)
SetStatusWaiting(context.Context, string) (*models.Payment, errors.Error)
SetStatusRefund(context.Context, string) (*models.Payment, errors.Error)
}
type CallbackService interface {
OnSuccess(context.Context, *models.Event) errors.Error
OnFailure(context.Context, *models.Event) errors.Error
}
type YandexStatusControllerDeps struct {
Logger *zap.Logger
StatusService StatusService
CallbackService CallbackService
}
type YandexStatusController struct {
logger *zap.Logger
statusService StatusService
callbackService CallbackService
}
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
}