add user login to webhook payload

This commit is contained in:
kiwiidb
2022-05-19 11:24:16 +02:00
parent 402a970140
commit d668f2cebf

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/db/models"
@@ -16,8 +17,14 @@ func (svc *LndhubService) StartWebhookSubscribtion(ctx context.Context, url stri
svc.Logger.Infof("Starting webhook subscription with webhook url %s", svc.Config.WebhookUrl)
incomingInvoices := make(chan models.Invoice)
outgoingInvoices := make(chan models.Invoice)
svc.InvoicePubSub.Subscribe(common.InvoiceTypeIncoming, incomingInvoices)
svc.InvoicePubSub.Subscribe(common.InvoiceTypeOutgoing, outgoingInvoices)
_, err := svc.InvoicePubSub.Subscribe(common.InvoiceTypeIncoming, incomingInvoices)
if err != nil {
svc.Logger.Error(err.Error())
}
_, err = svc.InvoicePubSub.Subscribe(common.InvoiceTypeOutgoing, outgoingInvoices)
if err != nil {
svc.Logger.Error(err.Error())
}
for {
select {
case <-ctx.Done():
@@ -31,8 +38,35 @@ func (svc *LndhubService) StartWebhookSubscribtion(ctx context.Context, url stri
}
func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) {
//Look up the user's login to add it to the invoice
user, err := svc.FindUser(context.Background(), invoice.UserID)
if err != nil {
svc.Logger.Error(err)
return
}
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(invoice)
err = json.NewEncoder(payload).Encode(WebhookInvoicePayload{
ID: invoice.ID,
Type: invoice.Type,
UserLogin: user.Login,
Amount: invoice.Amount,
Fee: invoice.Fee,
Memo: invoice.Memo,
DescriptionHash: invoice.DescriptionHash,
PaymentRequest: invoice.PaymentRequest,
DestinationPubkeyHex: invoice.DestinationPubkeyHex,
DestinationCustomRecords: invoice.DestinationCustomRecords,
RHash: invoice.RHash,
Preimage: invoice.Preimage,
Keysend: invoice.Keysend,
State: invoice.State,
ErrorMessage: invoice.ErrorMessage,
CreatedAt: invoice.CreatedAt,
ExpiresAt: invoice.ExpiresAt.Time,
UpdatedAt: invoice.UpdatedAt.Time,
SettledAt: invoice.SettledAt.Time,
})
if err != nil {
svc.Logger.Error(err)
return
@@ -51,3 +85,25 @@ func (svc *LndhubService) postToWebhook(invoice models.Invoice, url string) {
svc.Logger.Errorf("Webhook status code was %d, body: %s", resp.StatusCode, msg)
}
}
type WebhookInvoicePayload struct {
ID int64 `json:"id"`
Type string `json:"type"`
UserLogin string `json:"user_login"`
Amount int64 `json:"amount"`
Fee int64 `json:"fee"`
Memo string `json:"memo"`
DescriptionHash string `json:"description_hash,omitempty"`
PaymentRequest string `json:"payment_request"`
DestinationPubkeyHex string `json:"destination_pubkey_hex"`
DestinationCustomRecords map[uint64][]byte `json:"custom_records,omitempty"`
RHash string `json:"r_hash"`
Preimage string `json:"preimage"`
Keysend bool `json:"keysend"`
State string `json:"state"`
ErrorMessage string `json:"error_message,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UpdatedAt time.Time `json:"updated_at"`
SettledAt time.Time `json:"settled_at"`
}