Beispiel #1
0
        public async Task <IActionResult> Post([FromBody] Payment payment)
        {
            try
            {
                _logger.LogInformation($"Post Payment Request - Merchant ID: {payment.MerchantId} + Payment Ref: {payment.MerchantTransactionReference}");

                Merchant merchant = await _merchantRepository.GetMerchantAsync(payment.MerchantId);

                if (merchant == null)
                {
                    _logger.LogWarning($"Merchant ({payment.MerchantId}) not found");
                    return(new BadRequestObjectResult("No Such Merchant Found"));
                }

                payment.MerchantId = merchant.Id;

                _logger.LogInformation("Attempting to connect to bank");

                var bankResponse = await _bankProcessor.ProcessPaymentAsync(payment);

                _logger.LogInformation("Bank process complete");

                Payment storedPayment = await _paymentRepository.AddPaymentAsync(bankResponse);

                _logger.LogInformation("Payment Processed");

                return(new OkObjectResult(payment));
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());

                return(new StatusCodeResult(500));
            }
        }
        public async Task ShouldFailProcessPayment()
        {
            _bankProcessor = new MockBankProcessor();

            var payment = new Payment
            {
                MerchantId = "bd2347d5-98b3-4d4e-8773-4eb4b33ca0d6",
                MerchantTransactionReference = "TestReference",
                PaymentMethod  = 0,
                Amount         = 22.99M,
                Currency       = Currency.GBP,
                PaymentDetails = new PaymentDetails
                {
                    CardNumber  = "1234567812345678",
                    Name        = "Test Name",
                    ExpiryMonth = "12",
                    ExpiryYear  = "2030",
                    CVV         = "1234",
                    Scheme      = CardScheme.Mastercard,
                    Type        = CardType.Credit,
                    Address     = new Address
                    {
                        Number   = 1,
                        Street   = "Test Road",
                        City     = "Test City",
                        County   = "Test County",
                        PostCode = "TE5 7PC"
                    }
                }
            };


            var result = await _bankProcessor.ProcessPaymentAsync(payment);

            Assert.IsTrue(result.Status == Status.Unpaid);
        }