public async Task <ResponseMessage> CapturePayment(Guid orderId) { var transactions = await _paymentRepository.GetTransactionByOrderId(orderId); var authorizedTransaction = transactions?.FirstOrDefault(t => t.Status == TransactionStatus.Authorized); var validationResult = new ValidationResult(); if (authorizedTransaction == null) { throw new DomainException($"Transaction not found for the order {orderId}"); } var transaction = await _paymentFacade.CapturePayment(authorizedTransaction); if (transaction.Status != TransactionStatus.Paid) { validationResult.Errors.Add(new ValidationFailure("Payment", $"An error occurred to process the payment for the order {orderId}")); return(new ResponseMessage(validationResult)); } transaction.PaymentId = authorizedTransaction.PaymentId; _paymentRepository.AddTransaction(transaction); if (!await _paymentRepository.UnitOfWork.Commit()) { validationResult.Errors.Add(new ValidationFailure("Payment", $"An error occurred to process the payment for the order {orderId}")); } return(new ResponseMessage(validationResult)); }
public async Task <ResponseMessage> CapturePayment(Guid orderId) { var transactions = await _paymentRepository.GetTransactionsByOrderId(orderId); var transactionAuthorized = transactions?.FirstOrDefault(t => t.Status == StatusTransaction.Authorized); var validationResult = new ValidationResult(); if (transactionAuthorized == null) { throw new DomainException($"transaction not found - order {orderId}"); } var transaction = await _paymentFacade.CapturePayment(transactionAuthorized); if (transaction.Status != StatusTransaction.Paid) { validationResult.Errors.Add(new ValidationFailure("Payment", $"Order payment could not be captured {orderId}")); return(new ResponseMessage(validationResult)); } transaction.PaymentId = transactionAuthorized.PaymentId; _paymentRepository.AddTransaction(transaction); if (!await _paymentRepository.UnitOfWork.Commit()) { validationResult.Errors.Add(new ValidationFailure("Payment", $"Order payment capture could not be persisted {orderId}")); return(new ResponseMessage(validationResult)); } return(new ResponseMessage(validationResult)); }