Merge pull request #227 from getAlby/fix/0-amount-internal-invoices

Fix/0 amount internal invoices
This commit is contained in:
kiwiidb
2022-09-26 10:07:02 +02:00
committed by GitHub
2 changed files with 23 additions and 1 deletions

View File

@@ -131,6 +131,27 @@ func (suite *PaymentTestSuite) TestInternalPayment() {
assert.Equal(suite.T(), 1, len(transactionEntriesBob))
assert.Equal(suite.T(), int64(bobSatRequested), transactionEntriesBob[0].Amount)
assert.Equal(suite.T(), int64(bobSatRequested), bobBalance)
//generate 0 amount invoice
zeroAmt := suite.createAddInvoiceReq(0, "integration test internal payment bob 0 amount", suite.bobToken)
toPayForZeroAmt := 10
rec := httptest.NewRecorder()
var buf bytes.Buffer
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedPayInvoiceRequestBody{
Invoice: zeroAmt.PayReq,
Amount: toPayForZeroAmt,
}))
req := httptest.NewRequest(http.MethodPost, "/payinvoice", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
payInvoiceResponse := &ExpectedPayInvoiceResponseBody{}
assert.Equal(suite.T(), http.StatusOK, rec.Code)
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(payInvoiceResponse))
//assert bob was credited the correct amount
bobBalance, _ = suite.service.CurrentUserBalance(context.Background(), bobId)
assert.Equal(suite.T(), int64(bobSatRequested+toPayForZeroAmt), bobBalance)
}
func (suite *PaymentTestSuite) TestInternalPaymentFail() {

View File

@@ -80,7 +80,7 @@ func (svc *LndhubService) SendInternalPayment(ctx context.Context, invoice *mode
InvoiceID: incomingInvoice.ID,
CreditAccountID: recipientCreditAccount.ID,
DebitAccountID: recipientDebitAccount.ID,
Amount: incomingInvoice.Amount,
Amount: invoice.Amount,
}
_, err = svc.DB.NewInsert().Model(&recipientEntry).Exec(ctx)
if err != nil {
@@ -101,6 +101,7 @@ func (svc *LndhubService) SendInternalPayment(ctx context.Context, invoice *mode
incomingInvoice.Internal = true // mark incoming invoice as internal, just for documentation/debugging
incomingInvoice.State = common.InvoiceStateSettled
incomingInvoice.SettledAt = schema.NullTime{Time: time.Now()}
incomingInvoice.Amount = invoice.Amount // set just in case of 0 amount invoice
_, err = svc.DB.NewUpdate().Model(&incomingInvoice).WherePK().Exec(ctx)
if err != nil {
// could not save the invoice of the recipient