public async Task <IActionResult> Post(PaymentRequestDto paymentRequest)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var paymentState = await _paymentRequestService.Pay(paymentRequest);

                    var paymentResponse = new PaymentResponseDto()
                    {
                        IsProcessed = paymentState.PaymentState == PaymentStateEnum.Processed
                        ,
                        PaymentState = paymentState
                    };

                    if (!paymentResponse.IsProcessed)
                    {
                        return(StatusCode(500, new { error = "Payment could not be processed" }));
                    }
                    return(Ok(paymentResponse));
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(StatusCode(500));
            }
        }
Example #2
0
        public async Task <IActionResult> ProcessPayment(PaymentRequestDto paymentRequest)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var paymentState = await _paymentRequestService.Pay(paymentRequest);

                    if (!(paymentState.PaymentState == PaymentStateEnum.Processed))
                    {
                        return(StatusCode(400, "400 bad request"));
                    }
                    return(StatusCode(200, "200 Ok"));
                }
                else
                {
                    return(StatusCode(400, "400 bad request"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(StatusCode(500, "500 internal server error"));
            }
        }
Example #3
0
        public async Task Test_PaymentRequestService_Pay(PaymentRequestDto paymentRequestDto, PaymentStateDto cheapGatewayResponseDto, int timesCheapGatewayCalled, PaymentStateDto expensiveGatewayResponseDto, int timesExpensiveGatewayCalled, PaymentStateEnum expectedPaymentStateEnum)
        {
            //arrange

            _cheapPaymentGateway.Setup(s => s.ProcessPayment(paymentRequestDto)).Returns(cheapGatewayResponseDto);
            _expensivePaymentGateway.Setup(s => s.ProcessPayment(paymentRequestDto)).Returns(expensiveGatewayResponseDto);


            //act
            var paymentStateDto = await _paymentRequestService.Pay(paymentRequestDto);

            //assert
            Assert.IsNotNull(paymentStateDto);
            Assert.AreEqual(paymentStateDto.PaymentState, expectedPaymentStateEnum);
            _cheapPaymentGateway.Verify(s => s.ProcessPayment(paymentRequestDto), Times.Exactly(timesCheapGatewayCalled));
            _expensivePaymentGateway.Verify(s => s.ProcessPayment(paymentRequestDto), Times.Exactly(timesExpensiveGatewayCalled));
        }