From 81af8269d6fc813a189b1ee549ade0569a2b7898 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 8 May 2025 20:14:58 -0500 Subject: [PATCH] update --- pkg/models/dlocal_api.go | 25 +++++----- pkg/services/dlocal_service.go | 56 +++++++++++++++++++++++ rest/controllers/api/dlocal_controller.go | 27 ++++++++++- rest/routes/api.go | 2 +- 4 files changed, 95 insertions(+), 15 deletions(-) diff --git a/pkg/models/dlocal_api.go b/pkg/models/dlocal_api.go index 65e4f8a..7d0d6aa 100644 --- a/pkg/models/dlocal_api.go +++ b/pkg/models/dlocal_api.go @@ -19,18 +19,19 @@ type DlocalApi struct { Modo string `gorm:"default:'prod'" json:"modo"` } type PlanRequest struct { - Name string `json:"name"` - Description string `json:"description"` - Country string `json:"country"` - Currency string `json:"currency"` - Amount float64 `json:"amount"` - FrequencyType string `json:"frequency_type"` - FrequencyValue int `json:"frequency_value"` - DayOfMonth int `json:"day_of_month"` - NotificationURL string `json:"notification_url"` - SuccessURL string `json:"success_url"` - BackURL string `json:"back_url"` - ErrorURL string `json:"error_url"` + Name string `json:"name" validate:"required"` + Description string `json:"description" validate:"required"` + Currency string `json:"currency" validate:"required"` + Amount float64 `json:"amount" validate:"required"` + FrequencyType string `json:"frequency_type" validate:"required"` + + Country string `json:"country,omitempty"` + FrequencyValue int `json:"frequency_value,omitempty"` + DayOfMonth int `json:"day_of_month,omitempty"` + NotificationURL string `json:"notification_url,omitempty"` + SuccessURL string `json:"success_url,omitempty"` + BackURL string `json:"back_url,omitempty"` + ErrorURL string `json:"error_url,omitempty"` } // TableName asegura que GORM use la tabla 'qrvcard' diff --git a/pkg/services/dlocal_service.go b/pkg/services/dlocal_service.go index 8a68e4d..b4b16c1 100644 --- a/pkg/services/dlocal_service.go +++ b/pkg/services/dlocal_service.go @@ -355,3 +355,59 @@ func DeactivatePlan(cfg models.DlocalApi, planID string, subscriptionId string) // Devolver la respuesta exitosa return responseBody, nil } + +func SeePlanes(cfg models.DlocalApi) ([]byte, error) { + // Determinar la URL base según el modo + baseURL := cfg.UrlDev + accessKeyID := cfg.AccessKeyIDdev + accessKeySecret := cfg.AccessKeySecretdev + + if cfg.Modo == "prod" { + baseURL = cfg.UrlProd + accessKeyID = cfg.AccessKeyID + accessKeySecret = cfg.AccessKeySecret + } + + + // Construir la URL completa para el endpoint + url := fmt.Sprintf("%s/v1/subscription/planes", baseURL) + + // Construir el token de autorización + authToken := fmt.Sprintf("Bearer %s:%s", accessKeyID, accessKeySecret) + + // Crear la solicitud HTTP + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("error al crear la solicitud: %v", err) + } + + // Agregar los encabezados a la solicitud + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", authToken) + + // Configurar el cliente HTTP + client := &http.Client{ + Timeout: 30 * time.Second, + } + + // Ejecutar la solicitud + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("error al enviar la solicitud: %v", err) + } + defer resp.Body.Close() + + // Leer la respuesta + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("error al leer la respuesta: %v", err) + } + + // Si la respuesta no es exitosa, devolver un error + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("error en la respuesta de DLocal (%d): %s", resp.StatusCode, string(responseBody)) + } + + // Devolver la respuesta exitosa + return responseBody, nil +} diff --git a/rest/controllers/api/dlocal_controller.go b/rest/controllers/api/dlocal_controller.go index 601599d..16e0f38 100644 --- a/rest/controllers/api/dlocal_controller.go +++ b/rest/controllers/api/dlocal_controller.go @@ -112,8 +112,6 @@ func DeactivatePlan(c *fiber.Ctx) error { planID := c.Params("planID") subscriptionId := c.Params("subscriptionId") - - // Obtener la configuración activa de DlocalApi dlocalConfig, err := models.GetLastActiveDlocalApi() if err != nil { @@ -136,3 +134,28 @@ func DeactivatePlan(c *fiber.Ctx) error { "response": string(response), }) } + +func SeePlanes(c *fiber.Ctx) error { + + // Obtener la configuración activa de DlocalApi + dlocalConfig, err := models.GetLastActiveDlocalApi() + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": "Error al obtener la configuración activa de DlocalApi.", + }) + } + + // Llamar al servicio para ver el plan + response, err := services.SeePlanes(*dlocalConfig) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprintf("Error al obtener los planes", err), + }) + } + + // Devolver la respuesta exitosa + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "message": "Planes obtenido exitosamente.", + "response": string(response), + }) +} diff --git a/rest/routes/api.go b/rest/routes/api.go index 97c83d2..7a5498c 100755 --- a/rest/routes/api.go +++ b/rest/routes/api.go @@ -18,7 +18,7 @@ func v1AuthRoutes(api fiber.Router) { api.Get("/dlocal/subscription/ver-plan/:planID", apiControllers.SeePlan) api.Patch("/dlocal/subscription/actualizar-plan/:planID", apiControllers.UpdatedPlan) api.Patch("/dlocal/subscription/plan/:planId/subscription/:subscriptionId/deactivate", apiControllers.DeactivatePlan) - + api.Get("/dlocal/subscription/plan/all", apiControllers.SeePlanes) } func v1Routes(api fiber.Router) {