public async Task CreatePaymentAsync_Should_Throw_Exception_If_Payment_Is_Null() { // Arrange // Act async Task CreatePayment() => await _paymentsService.CreatePaymentAsync(null); // Assert await Assert.ThrowsAsync <ArgumentNullException>(CreatePayment); }
public async Task <ActionResult <BankResponse> > Post([FromBody] PaymentRequest paymentRequest, CancellationToken cancellationToken) { const string apiRoute = Constants.ApiRoutes.Payments; const string httpVerb = Constants.HttpVerbs.Post; using (RequestProcessingDurationSummary.WithLabels(apiRoute, httpVerb).NewTimer()) { _logger.LogInformation("Received a request on route {ApiRoute} with http verb {HttpVerb}.", apiRoute, httpVerb); RequestsReceivedCounter.WithLabels(apiRoute, httpVerb).Inc(); if (paymentRequest == null) { throw new ArgumentNullException(nameof(paymentRequest)); } paymentRequest.CreditCardNumber = NormalizeCreditCardNumber(paymentRequest.CreditCardNumber); var bankResponse = await _bankService.ProcessTransactionAsync(paymentRequest, cancellationToken); // This is NOT production-ready. From the minimal research I've done on the topic, we're not supposed to store credit card info // anywhere, not even encrypted. Due to time constraints and since the database used in this assignment is in-memory only, I've // decided to implement it this way to be able to fulfill the assignment's requirements. I've never had to handle credit cards // in my professional career so far and if a project requires it in the future, I would do extensive research on the best way // to handle them and hopefully ask someone more experienced in the subject for pointers. var payment = new Payment { Amount = paymentRequest.Amount, CreditCardNumber = paymentRequest.CreditCardNumber, CreditCardCvv = paymentRequest.CreditCardCvv, CreditCardExpiryMonth = paymentRequest.CreditCardExpiryMonth, CreditCardExpiryYear = paymentRequest.CreditCardExpiryYear, Currency = paymentRequest.Currency, TransactionId = bankResponse.TransactionId, TransactionStatusCode = bankResponse.TransactionStatusCode }; await _paymentsService.CreatePaymentAsync(payment, cancellationToken); _logger.LogInformation("Successfully processed a request on route {ApiRoute} with http verb {HttpVerb}.", apiRoute, httpVerb); RequestsProcessedCounter.WithLabels(apiRoute, httpVerb).Inc(); return(bankResponse); } }