Skip to content

Commit

Permalink
revert fee entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Jun 30, 2023
1 parent 5bf12e4 commit 9e18911
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions lib/service/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,13 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode
func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice *models.Invoice, parentEntry models.TransactionEntry) error {
invoice.State = common.InvoiceStateSettled
invoice.SettledAt = schema.NullTime{Time: time.Now()}

_, err := svc.DB.NewUpdate().Model(invoice).WherePK().Exec(ctx)
tx, err := svc.DB.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not open tx entry for updating successful payment:r_hash:%s %v", invoice.RHash, err)
return err
}
_, err = tx.NewUpdate().Model(invoice).WherePK().Exec(ctx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not update sucessful payment invoice user_id:%v invoice_id:%v, error %s", invoice.UserID, invoice.ID, err.Error())
Expand All @@ -318,12 +323,40 @@ func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice *
Amount: int64(invoice.Fee),
ParentID: parentEntry.ID,
}
_, err = svc.DB.NewInsert().Model(&entry).Exec(ctx)
_, err = tx.NewInsert().Model(&entry).Exec(ctx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not insert fee transaction entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error())
return err
}
//revert the provisional feeLimit amount
feeLimit := svc.CalcFeeLimit(invoice.DestinationPubkeyHex, invoice.Amount)
provisionalFeeRevertEntry := models.TransactionEntry{
UserID: invoice.UserID,
InvoiceID: invoice.ID,
CreditAccountID: parentEntry.DebitAccountID,
DebitAccountID: parentEntry.CreditAccountID,
Amount: feeLimit,
ParentID: parentEntry.ID,
}
_, err = tx.NewInsert().Model(&provisionalFeeRevertEntry).Exec(ctx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not insert revert feeLimit transaction entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error())
return err
}
_, err = tx.NewInsert().Model(&entry).Exec(ctx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not insert fee transaction entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error())
return err
}
err = tx.Commit()
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Failed to commit DB transaction user_id:%v invoice_id:%v %v", invoice.UserID, invoice.ID, err)
return err
}

userBalance, err := svc.CurrentUserBalance(ctx, entry.UserID)
if err != nil {
Expand Down

0 comments on commit 9e18911

Please sign in to comment.