This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
paymentcheck.go
70 lines (62 loc) · 2.21 KB
/
paymentcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"gorm.io/gorm"
"github.com/lnbits/infinity/events"
"github.com/lnbits/infinity/lightning"
"github.com/lnbits/infinity/models"
"github.com/lnbits/infinity/storage"
"github.com/lnbits/relampago"
)
func initialPaymentCheck() {
log.Info().Msg("performing startup check for pending payments and invoice")
var payments []models.Payment
result := storage.DB.Where("pending").Find(&payments)
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
log.Fatal().Err(result.Error).Msg("failed to query pending invoices or payments")
return
}
log.Info().Msgf("will check %d payments", len(payments))
for _, payment := range payments {
log := log.With().Str("id", payment.CheckingID).Logger()
log.Info().Int64("amount", payment.Amount).Msg("checking")
if payment.Amount > 0 {
status, err := lightning.LN.GetInvoiceStatus(payment.CheckingID)
if err != nil {
log.Warn().Err(err).Msg("failed to get invoice status")
continue
}
if status.Paid {
log.Info().Msg("invoice paid, updating")
result = storage.DB.Where("checkingID = ?", payment.CheckingID).Update("pending", false)
} else {
continue
}
if result.Error != nil {
log.Error().Err(result.Error).Interface("status", status).Msg("failed to update invoice status")
} else {
events.NotifyInvoicePaid(status)
}
} else {
status, err := lightning.LN.GetPaymentStatus(payment.CheckingID)
if err != nil {
log.Warn().Err(err).Msg("failed to get payment status")
continue
}
if status.Status == relampago.Complete {
log.Info().Str("preimage", status.Preimage).Msg("payment complete, updating")
result = storage.DB.Where("checkingID = ?", payment.CheckingID).Update("pending", false)
} else if status.Status == relampago.Failed {
log.Info().Msg("payment failed, deleting")
result = storage.DB.Where("checkingID = ?", payment.CheckingID).Delete(&models.Payment{})
} else {
log.Info().Interface("status", status.Status).Msg("payment not complete or failed")
continue
}
if result.Error != nil {
log.Error().Err(result.Error).Interface("status", status).Msg("failed to update payment status")
} else {
events.NotifyPaymentSentStatus(status)
}
}
}
}