lnbits stuff

This commit is contained in:
callebtc 2022-08-19 23:36:11 +02:00
parent 4bcbd2fefe
commit 0c29fec19f
3 changed files with 72 additions and 9 deletions

View file

@ -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) {

View file

@ -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)

View file

@ -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"`