dlocal services
This commit is contained in:
@@ -412,3 +412,53 @@ func SeePlanes(cfg models.DlocalApi) ([]byte, error) {
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func SeeSubscription(cfg models.DlocalApi, subscriptionId string, invoiceId string) ([]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/%s/execution/%s", baseURL, subscriptionId, invoiceId)
|
||||
|
||||
// Crear la solicitud HTTP
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error al crear la solicitud: %w", err)
|
||||
}
|
||||
|
||||
// Establecer encabezados
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// En DLocal se usa generalmente Basic Auth codificado en base64, pero si realmente usan Bearer personalizado:
|
||||
authToken := fmt.Sprintf("Bearer %s:%s", accessKeyID, accessKeySecret)
|
||||
req.Header.Set("Authorization", authToken)
|
||||
|
||||
// Cliente HTTP con timeout
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
|
||||
// Ejecutar solicitud
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error al enviar la solicitud: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Leer respuesta
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error al leer la respuesta: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("respuesta no exitosa (%d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
@@ -159,3 +159,29 @@ func SeePlanes(c *fiber.Ctx) error {
|
||||
"response": string(response),
|
||||
})
|
||||
}
|
||||
func SeeSubscription(c *fiber.Ctx) error {
|
||||
subscriptionId := c.Params("subscriptionId")
|
||||
invoiceId := c.Params("invoiceId")
|
||||
|
||||
// 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 suscripción
|
||||
response, err := services.SeeSubscription(*dlocalConfig, subscriptionId, invoiceId)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": fmt.Sprintf("Error al obtener la suscripción: %v", err),
|
||||
})
|
||||
}
|
||||
|
||||
// Devolver la respuesta exitosa
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"message": "Suscripción obtenida exitosamente.",
|
||||
"response": string(response),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ func v1AuthRoutes(api fiber.Router) {
|
||||
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)
|
||||
api.Get("/dlocal/subscription/:subscriptionId/execution/:invoiceId", apiControllers.SeeSubscription)
|
||||
}
|
||||
|
||||
func v1Routes(api fiber.Router) {
|
||||
|
||||
Reference in New Issue
Block a user