public async Task <ResponseMessage> CancelPayment(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.CancelPayment(authorizedTransaction); if (transaction.Status != TransactionStatus.Cancelled) { validationResult.Errors.Add(new ValidationFailure("Payment", $"An error occurred to cancel 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 cancel the payment for the order {orderId}")); } return(new ResponseMessage(validationResult)); }
public async Task <ResponseMessage> CancelPayment(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.CancelPayment(transactionAuthorized); if (transaction.Status != StatusTransaction.Canceled) { validationResult.Errors.Add(new ValidationFailure("Payment", $"Order payment could not be canceled {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 cancel could not be persisted {orderId}")); return(new ResponseMessage(validationResult)); } return(new ResponseMessage(validationResult)); }