diff --git a/pkg/services/qr_service.go b/pkg/services/qr_service.go index 24f0c5f..a9e8d26 100755 --- a/pkg/services/qr_service.go +++ b/pkg/services/qr_service.go @@ -149,3 +149,19 @@ func GenerateVCardQR(data ContactData) ([]byte, error) { return buf.Bytes(), nil } + +func GenerateURlQR(url string) ([]byte, error) { + qr, err := qrcode.New(url, qrcode.Medium) + if err != nil { + return nil, err + } + + img := qr.Image(300) + + var buf bytes.Buffer + if err := png.Encode(&buf, img); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} diff --git a/rest/controllers/api/vcard_qr.go b/rest/controllers/api/vcard_qr.go index c032faa..96b6e56 100755 --- a/rest/controllers/api/vcard_qr.go +++ b/rest/controllers/api/vcard_qr.go @@ -169,3 +169,39 @@ func CreateQrTmp(c *fiber.Ctx) error { c.Type("webp") return c.Send(webpBuffer.Bytes()) } + +func CreateUrlQr(c *fiber.Ctx) error { + var data struct { + URL string `json:"url"` + } + if err := json.Unmarshal(c.Body(), &data); err != nil { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "error": "Datos inválidos", + }) + } + + qrBytes, err := services.GenerateURlQR(data.URL) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": "No se pudo generar el QR", + }) + } + + img, err := png.Decode(bytes.NewReader(qrBytes)) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": "No se pudo procesar la imagen", + }) + } + + // Codificar a WebP + var webpBuffer bytes.Buffer + if err := webp.Encode(&webpBuffer, img, &webp.Options{Lossless: true}); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": "No se pudo convertir a WebP", + }) + } + + c.Type("webp") + return c.Send(webpBuffer.Bytes()) +} \ No newline at end of file diff --git a/rest/routes/api.go b/rest/routes/api.go index ba996d7..e643d0e 100755 --- a/rest/routes/api.go +++ b/rest/routes/api.go @@ -22,6 +22,7 @@ func v1AuthRoutes(api fiber.Router) { api.Get("/dlocal/subscription/:subscriptionId/execution/:invoiceId", apiControllers.SeeSubscription) api.Post("/dlocal/payment/crear-pago", apiControllers.CreatePago) api.Post("/rapyd/wallet/create", apiControllers.MakeWallet) + api.Post("/generate-url-qr", apiControllers.CreateUrlQr) } func v1Routes(api fiber.Router) {