diff --git a/controllers/invoice.ctrl.go b/controllers/invoice.ctrl.go new file mode 100644 index 0000000..cd06d96 --- /dev/null +++ b/controllers/invoice.ctrl.go @@ -0,0 +1,59 @@ +package controllers + +import ( + "net/http" + + "github.com/getAlby/lndhub.go/lib/responses" + "github.com/getAlby/lndhub.go/lib/service" + "github.com/getsentry/sentry-go" + "github.com/labstack/echo/v4" +) + +// InvoiceController : Add invoice controller struct +type InvoiceController struct { + svc *service.LndhubService +} + +func NewInvoiceController(svc *service.LndhubService) *InvoiceController { + return &InvoiceController{svc: svc} +} + +// Invoice : Invoice Controller +func (controller *InvoiceController) Invoice(c echo.Context) error { + user, err := controller.svc.FindUserByLogin(c.Request().Context(), c.Param("user_login")) + if err != nil { + c.Logger().Errorf("Failed to find user by login: login %v error %v", c.Param("user_login"), err) + return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) + } + + var body AddInvoiceRequestBody + + if err := c.Bind(&body); err != nil { + c.Logger().Errorf("Failed to load invoice request body: %v", err) + return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) + } + + if err := c.Validate(&body); err != nil { + c.Logger().Errorf("Invalid invoice request body: %v", err) + return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) + } + + amount, err := controller.svc.ParseInt(body.Amount) + if err != nil { + return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) + } + c.Logger().Infof("Adding invoice: user_id=%v memo=%s value=%v description_hash=%s", user.ID, body.Memo, amount, body.DescriptionHash) + + invoice, err := controller.svc.AddIncomingInvoice(c.Request().Context(), user.ID, amount, body.Memo, body.DescriptionHash) + if err != nil { + c.Logger().Errorf("Error creating invoice: %v", err) + sentry.CaptureException(err) + return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) + } + responseBody := AddInvoiceResponseBody{} + responseBody.RHash = invoice.RHash + responseBody.PaymentRequest = invoice.PaymentRequest + responseBody.PayReq = invoice.PaymentRequest + + return c.JSON(http.StatusOK, &responseBody) +} diff --git a/main.go b/main.go index a54f6c0..f7bc90a 100644 --- a/main.go +++ b/main.go @@ -128,6 +128,7 @@ func main() { // Public endpoints for account creation and authentication e.POST("/auth", controllers.NewAuthController(svc).Auth) e.POST("/create", controllers.NewCreateUserController(svc).CreateUser, strictRateLimitMiddleware) + e.POST("/invoice/:user_login", controllers.NewInvoiceController(svc).Invoice, middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(c.DefaultRateLimit)))) // Secured endpoints which require a Authorization token (JWT) secured := e.Group("", tokens.Middleware(c.JWTSecret), middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(c.DefaultRateLimit))))