diff --git a/integration_tests/payment_failure_test.go b/integration_tests/payment_failure_test.go index f4c81fb..82e0285 100644 --- a/integration_tests/payment_failure_test.go +++ b/integration_tests/payment_failure_test.go @@ -90,8 +90,8 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() { //test an expired invoice externalInvoice := lnrpc.Invoice{ - Memo: "integration tests: external pay from alice", - Value: int64(externalSatRequested), + Memo: "integration tests: external pay from alice", + Value: int64(externalSatRequested), Expiry: 1, } invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice) @@ -148,10 +148,17 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() { fmt.Printf("Error when getting balance %v\n", err.Error()) } - // check if there are 3 transaction entries, with reversed credit and debit account ids - assert.Equal(suite.T(), 3, len(transactonEntries)) - assert.Equal(suite.T(), transactonEntries[1].CreditAccountID, transactonEntries[2].DebitAccountID) - assert.Equal(suite.T(), transactonEntries[1].DebitAccountID, transactonEntries[2].CreditAccountID) + // check if there are 5 transaction entries: + // - the incoming payment + // - the outgoing payment + // - the fee reserve + the fee reserve reversal + // - the outgoing payment reversal + // with reversed credit and debit account ids for payment 2/5 & payment 3/4 + assert.Equal(suite.T(), 5, len(transactonEntries)) + assert.Equal(suite.T(), transactonEntries[1].CreditAccountID, transactonEntries[4].DebitAccountID) + assert.Equal(suite.T(), transactonEntries[1].DebitAccountID, transactonEntries[4].CreditAccountID) + assert.Equal(suite.T(), transactonEntries[2].CreditAccountID, transactonEntries[3].DebitAccountID) + assert.Equal(suite.T(), transactonEntries[2].DebitAccountID, transactonEntries[3].CreditAccountID) assert.Equal(suite.T(), transactonEntries[1].Amount, int64(externalSatRequested)) assert.Equal(suite.T(), transactonEntries[2].Amount, int64(externalSatRequested)) // assert that balance is the same diff --git a/lib/service/invoices.go b/lib/service/invoices.go index bc822cb..dbb572c 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -247,7 +247,16 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode svc.Logger.Errorf("Could not open tx entry for updating failed payment:r_hash:%s %v", invoice.RHash, err) return err } - // add transaction entry with reverted credit/debit account id + + //revert the fee reserve if necessary + err = svc.RevertFeeReserve(ctx, &entryToRevert, invoice, tx) + if err != nil { + sentry.CaptureException(err) + svc.Logger.Errorf("Could not revert fee reserve entry entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) + return err + } + + //revert the payment if necessary entry := models.TransactionEntry{ UserID: invoice.UserID, InvoiceID: invoice.ID, @@ -264,13 +273,6 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode return err } - err = svc.RevertFeeReserve(ctx, &entryToRevert, tx) - if err != nil { - sentry.CaptureException(err) - svc.Logger.Errorf("Could not revert fee reserve entry entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) - return err - } - invoice.State = common.InvoiceStateError if failedPaymentError != nil { invoice.ErrorMessage = failedPaymentError.Error() @@ -337,12 +339,12 @@ func (svc *LndhubService) InsertTransactionEntry(ctx context.Context, invoice *m return entry, err } -func (svc *LndhubService) RevertFeeReserve(ctx context.Context, entry *models.TransactionEntry, tx bun.Tx) (err error) { +func (svc *LndhubService) RevertFeeReserve(ctx context.Context, entry *models.TransactionEntry, invoice *models.Invoice, tx bun.Tx) (err error) { if entry.FeeReserve != nil { entryToRevert := entry.FeeReserve feeReserveRevert := models.TransactionEntry{ UserID: entryToRevert.UserID, - InvoiceID: entryToRevert.ID, + InvoiceID: invoice.ID, CreditAccountID: entryToRevert.DebitAccountID, DebitAccountID: entryToRevert.CreditAccountID, Amount: entryToRevert.Amount, @@ -394,7 +396,7 @@ func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice * } //revert the fee reserve entry - err = svc.RevertFeeReserve(ctx, &parentEntry, tx) + err = svc.RevertFeeReserve(ctx, &parentEntry, invoice, tx) if err != nil { tx.Rollback() sentry.CaptureException(err)