Merge pull request #134 from getAlby/unauthorized-invoice-endpoint

Unauthorized invoice endpoint
This commit is contained in:
kiwiidb
2022-03-22 07:51:08 +01:00
committed by GitHub
6 changed files with 154 additions and 13 deletions

View File

@@ -33,6 +33,10 @@ type AddInvoiceResponseBody struct {
// AddInvoice : Add invoice Controller
func (controller *AddInvoiceController) AddInvoice(c echo.Context) error {
userID := c.Get("UserID").(int64)
return AddInvoice(c, controller.svc, userID)
}
func AddInvoice(c echo.Context, svc *service.LndhubService, userID int64) error {
var body AddInvoiceRequestBody
if err := c.Bind(&body); err != nil {
@@ -45,13 +49,13 @@ func (controller *AddInvoiceController) AddInvoice(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
amount, err := controller.svc.ParseInt(body.Amount)
amount, err := 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", userID, body.Memo, amount, body.DescriptionHash)
invoice, err := controller.svc.AddIncomingInvoice(c.Request().Context(), userID, amount, body.Memo, body.DescriptionHash)
invoice, err := svc.AddIncomingInvoice(c.Request().Context(), userID, amount, body.Memo, body.DescriptionHash)
if err != nil {
c.Logger().Errorf("Error creating invoice: %v", err)
sentry.CaptureException(err)

View File

@@ -0,0 +1,29 @@
package controllers
import (
"net/http"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"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)
}
return AddInvoice(c, controller.svc, user.ID)
}