Skip to content

Commit

Permalink
fix: incorrect payment update due to not updating wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Mar 1, 2024
1 parent 878fb40 commit dca4011
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Domain/Payment/Service.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using CSharp_Result;
using Domain.Transaction;
using Domain.Wallet;

namespace Domain.Payment;

public class PaymentService(
IPaymentRepository repo,
IPaymentGateway gateway,
IWalletRepository walletRepo,
ITransactionRepository transactionRepo,
ITransactionGenerator generator,
ITransactionManager transactionManager
Expand Down Expand Up @@ -49,8 +51,16 @@ public Task<Result<IEnumerable<PaymentPrincipal>>> Search(PaymentSearch search)
public Task<Result<Payment>> CompleteById(Guid id, PaymentRecord record)
{
return transactionManager.Start(() =>
repo.UpdateById(id, record)
repo
// update payment
.UpdateById(id, record)
.NullToError(id.ToString())
// update wallet
.DoAwait(DoType.MapErrors, w =>
walletRepo.Deposit(w.Wallet.Id, w.Principal.Record.CapturedAmount)
.NullToError(w.Wallet.Id.ToString())
)
// update transaction
.DoAwait(DoType.MapErrors, x =>
transactionRepo.Create(x.Wallet.Id, generator.Deposit(x.Principal))
.Then(t => repo.Link(t.Id, x.Principal.Reference.Id), Errors.MapAll)
Expand All @@ -61,8 +71,16 @@ public Task<Result<Payment>> CompleteById(Guid id, PaymentRecord record)
public Task<Result<Payment>> CompleteByRef(string reference, PaymentRecord record)
{
return transactionManager.Start(() =>
repo.UpdateByRef(reference, record)
repo
// update payment
.UpdateByRef(reference, record)
.NullToError(reference)
// update wallet
.DoAwait(DoType.MapErrors, w =>
walletRepo.Deposit(w.Wallet.Id, w.Principal.Record.CapturedAmount)
.NullToError(w.Wallet.Id.ToString())
)
// update transaction
.DoAwait(DoType.MapErrors, x =>
transactionRepo.Create(x.Wallet.Id, generator.Deposit(x.Principal))
.Then(t => repo.Link(t.Id, x.Principal.Reference.Id), Errors.MapAll)
Expand Down

0 comments on commit dca4011

Please sign in to comment.