Skip to content

Commit

Permalink
fix: check for duplicate payment
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Aug 20, 2024
1 parent 33e7be6 commit 9f4edf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions transactions/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/lnclient"
"github.com/getAlby/hub/tests"
"github.com/stretchr/testify/assert"
Expand All @@ -28,6 +29,28 @@ func TestSendPaymentSync_NoApp(t *testing.T) {
assert.Equal(t, "123preimage", *transaction.Preimage)
}

func TestSendPaymentSync_Duplicate(t *testing.T) {
ctx := context.TODO()

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_SETTLED,
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
AmountMsat: 123000,
})

transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
transaction, err := transactionsService.SendPaymentSync(ctx, tests.MockLNClientTransaction.Invoice, svc.LNClient, nil, nil)

assert.Error(t, err)
assert.Equal(t, "this invoice has already been paid", err.Error())
assert.Nil(t, transaction)
}

func TestSendPaymentSync_FailedRemovesFeeReserve(t *testing.T) {
ctx := context.TODO()

Expand Down
10 changes: 10 additions & 0 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri
var dbTransaction db.Transaction

err = svc.db.Transaction(func(tx *gorm.DB) error {
var existingSettledTransaction db.Transaction
if tx.Limit(1).Find(&existingSettledTransaction, &db.Transaction{
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: paymentRequest.PaymentHash,
State: constants.TRANSACTION_STATE_SETTLED,
}).RowsAffected > 0 {
logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Info("this invoice has already been paid")
return errors.New("this invoice has already been paid")
}

err := svc.validateCanPay(tx, appId, uint64(paymentRequest.MSatoshi))
if err != nil {
return err
Expand Down

0 comments on commit 9f4edf3

Please sign in to comment.