diff --git a/internal/api/lightning.go b/internal/api/lightning.go index 05f2498..2c92f9a 100644 --- a/internal/api/lightning.go +++ b/internal/api/lightning.go @@ -1,8 +1,12 @@ package api import ( - "github.com/LightningTipBot/LightningTipBot/internal/telegram" + "encoding/json" "net/http" + + "github.com/LightningTipBot/LightningTipBot/internal" + "github.com/LightningTipBot/LightningTipBot/internal/lnbits" + "github.com/LightningTipBot/LightningTipBot/internal/telegram" ) type Service struct { @@ -10,11 +14,40 @@ type Service struct { } func (s Service) Balance(w http.ResponseWriter, r *http.Request) { - + user := &lnbits.User{} + balance, err := s.bot.GetUserBalance(user) + if err != nil { + // return ctx, errors.Create("balance check failed") + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(balance) } func (s Service) CreateInvoice(w http.ResponseWriter, r *http.Request) { + var createInvoiceRequest CreateInvoiceRequest + err := json.NewDecoder(r.Body).Decode(&createInvoiceRequest) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + user := &lnbits.User{} + invoice, err := user.Wallet.Invoice( + lnbits.InvoiceParams{ + Amount: createInvoiceRequest.Amount, + Out: false, + DescriptionHash: createInvoiceRequest.DescriptionHash, + UnhashedDescription: createInvoiceRequest.UnhashedDescription, + Webhook: internal.Configuration.Lnbits.WebhookServer}, + s.bot.Client) + if err != nil { + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(invoice) } func (s Service) PayInvoice(w http.ResponseWriter, r *http.Request) { diff --git a/internal/lnbits/lnbits.go b/internal/lnbits/lnbits.go index c446b9a..64e0a7e 100644 --- a/internal/lnbits/lnbits.go +++ b/internal/lnbits/lnbits.go @@ -1,6 +1,7 @@ package lnbits import ( + "fmt" "time" "github.com/imroc/req" @@ -131,7 +132,7 @@ func (c Client) Info(w Wallet) (wtx Wallet, err error) { return } -// Info returns wallet payments +// Payments returns wallet payments func (c Client) Payments(w Wallet) (wtx Payments, err error) { // custom header with invoice key invoiceHeader := req.Header{ @@ -155,6 +156,30 @@ func (c Client) Payments(w Wallet) (wtx Payments, err error) { return } +// Payment state of a payment +func (c Client) Payment(w Wallet, payment_hash string) (payment Payment, err error) { + // custom header with invoice key + invoiceHeader := req.Header{ + "Content-Type": "application/json", + "Accept": "application/json", + "X-Api-Key": w.Inkey, + } + resp, err := req.Get(c.url+fmt.Sprintf("/api/v1/payments/%s", payment_hash), invoiceHeader, nil) + if err != nil { + return + } + + if resp.Response().StatusCode >= 300 { + var reqErr Error + resp.ToJSON(&reqErr) + err = reqErr + return + } + + err = resp.ToJSON(&payment) + return +} + // Wallets returns all wallets belonging to an user func (c Client) Wallets(w User) (wtx []Wallet, err error) { resp, err := req.Get(c.url+"/usermanager/api/v1/wallets/"+w.ID, c.header, nil) diff --git a/internal/lnbits/types.go b/internal/lnbits/types.go index c15a3a0..37824fc 100644 --- a/internal/lnbits/types.go +++ b/internal/lnbits/types.go @@ -75,11 +75,12 @@ func (u *User) ResetState() { } type InvoiceParams struct { - Out bool `json:"out"` // must be True if invoice is payed, False if invoice is received - Amount int64 `json:"amount"` // amount in Satoshi - Memo string `json:"memo,omitempty"` // the invoice memo. - Webhook string `json:"webhook,omitempty"` // the webhook to fire back to when payment is received. - DescriptionHash string `json:"description_hash,omitempty"` // the invoice description hash. + Out bool `json:"out"` // must be True if invoice is payed, False if invoice is received + Amount int64 `json:"amount"` // amount in Satoshi + Memo string `json:"memo,omitempty"` // the invoice memo. + Webhook string `json:"webhook,omitempty"` // the webhook to fire back to when payment is received. + DescriptionHash string `json:"description_hash,omitempty"` // the invoice description hash. + UnhashedDescription string `json:"unhashed_description,omitempty"` // the unhashed invoice description. } type PaymentParams struct { @@ -117,7 +118,7 @@ type Wallet struct { User string `json:"user"` } -type Payments []struct { +type Payment struct { CheckingID string `json:"checking_id"` Pending bool `json:"pending"` Amount int64 `json:"amount"` @@ -133,6 +134,10 @@ type Payments []struct { WebhookStatus interface{} `json:"webhook_status"` } +type Payments []struct { + Payment +} + type BitInvoice struct { PaymentHash string `json:"payment_hash"` PaymentRequest string `json:"payment_request"`