mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-22 07:04:56 +01:00
Add check payment endpoint
This commit is contained in:
@@ -8,17 +8,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CheckPaymentController : CheckPaymentController struct
|
// CheckPaymentController : CheckPaymentController struct
|
||||||
type CheckPaymentController struct{}
|
type CheckPaymentController struct {
|
||||||
|
svc *service.LndhubService
|
||||||
|
}
|
||||||
|
|
||||||
func NewCheckPaymentController(svc *service.LndhubService) *CheckPaymentController {
|
func NewCheckPaymentController(svc *service.LndhubService) *CheckPaymentController {
|
||||||
return &CheckPaymentController{}
|
return &CheckPaymentController{svc: svc}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckPayment : Check Payment Controller
|
// CheckPayment : Check Payment Controller
|
||||||
func (CheckPaymentController) CheckPayment(c echo.Context) error {
|
func (controller *CheckPaymentController) CheckPayment(c echo.Context) error {
|
||||||
_ = c.Param("payment_hash")
|
userId := c.Get("UserID").(int64)
|
||||||
|
rHash := c.Param("payment_hash")
|
||||||
|
|
||||||
|
invoice, err := controller.svc.FindInvoiceByPaymentHash(userId, rHash)
|
||||||
|
|
||||||
|
// Probably we did not find the invoice
|
||||||
|
if err != nil {
|
||||||
|
c.Logger().Errorf("Invalid checkpayment request payment_hash=%s", rHash)
|
||||||
return c.JSON(http.StatusBadRequest, echo.Map{
|
return c.JSON(http.StatusBadRequest, echo.Map{
|
||||||
"paid": true,
|
"error": true,
|
||||||
|
"code": 8,
|
||||||
|
"message": "Bad arguments",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var responseBody struct {
|
||||||
|
IsPaid bool `json:"paid"`
|
||||||
|
}
|
||||||
|
responseBody.IsPaid = !invoice.SettledAt.IsZero()
|
||||||
|
return c.JSON(http.StatusOK, &responseBody)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,16 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (svc *LndhubService) FindInvoiceByPaymentHash(userId int64, rHash string) (*models.Invoice, error) {
|
||||||
|
var invoice models.Invoice
|
||||||
|
|
||||||
|
err := svc.DB.NewSelect().Model(&invoice).Where("invoice.user_id = ? AND invoice.r_hash = ?", userId, rHash).Limit(1).Scan(context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
return &invoice, err
|
||||||
|
}
|
||||||
|
return &invoice, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (svc *LndhubService) Payinvoice(userId int64, invoice string) error {
|
func (svc *LndhubService) Payinvoice(userId int64, invoice string) error {
|
||||||
debitAccount, err := svc.AccountFor(context.TODO(), "current", userId)
|
debitAccount, err := svc.AccountFor(context.TODO(), "current", userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user