public async Task <BaseResult <PaymentPostResult> > ProcessPaymentAsync(PaymentPostRequest paymentPostRequest) { var result = new BaseResult <PaymentPostResult>(); var(errors, currency) = await this.ValidatePaymentPostRequest(paymentPostRequest); if (errors.Any()) { result.Errors = errors; return(result); } var bankClientResult = await this.acquiringBankClient.ProcessTransactionAsync(paymentPostRequest.ToBankClientRequest()); if (bankClientResult == null) { result.Errors.Add("bankClient", "Error while processing bank transaction."); return(result); } try { var paymentModel = paymentPostRequest.ToPaymentModel(currency, bankClientResult); var paymentModelResult = await this.paymentRepository.InsertAsync(paymentModel); result.Result = paymentModelResult.ToPostResult(); } catch (Exception ex) { this.logger.LogError("Error while processing payment.", new { Class = nameof(PaymentService), Method = nameof(ProcessPaymentAsync), ex.Message }); result.Errors.Add("paymentProcess", ex.Message); } return(result); }
public void ToPaymentModel_HasNoCurrencyAndHasNoBankClientResult_ReturnsModel() { // Arrange var paymentPostRequest = new PaymentPostRequest { Amount = 100, CardNumber = "1234567890123456", Currency = "USD", ExpiryMonth = 12, ExpiryYear = 2020, MerchantId = Guid.NewGuid(), SecurityCode = 123 }; // Act var result = paymentPostRequest.ToPaymentModel(); // Assert Assert.NotNull(result); Assert.Null(result.TransactionId); Assert.IsType <Payment>(result); }
public async Task ProcessPaymentAsync_WhenCurrencyIsNotValid_ReturnsError() { // Arrange var paymentRequest = new PaymentPostRequest { MerchantId = Guid.NewGuid(), Currency = "XXX" }; this.merchantRepositoryMock .Setup(s => s.GetByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(new Merchant()); // Act var result = await this.target.ProcessPaymentAsync(paymentRequest); // Assert Assert.False(result.Success); Assert.True(result.Errors.ContainsKey("currencyNotValid")); this.merchantRepositoryMock .Verify(v => v.GetByIdAsync(It.IsAny <Guid>()), Times.Once); }
private async Task <(IDictionary <string, string>, Currency)> ValidatePaymentPostRequest(PaymentPostRequest paymentPostRequest) { var errors = new Dictionary <string, string>(); var merchant = await this.merchantRepository.GetByIdAsync(paymentPostRequest.MerchantId); if (merchant == null) { errors.Add("merchantNotFound", $"MerchantId '{paymentPostRequest.MerchantId}' not found."); } if (!Enum.TryParse <Currency>(paymentPostRequest.Currency, true, out var currency)) { errors.Add("currencyNotValid", $"Currency '{paymentPostRequest.Currency}' is not valid."); } return(errors, currency); }