public async Task <PaymentDto> CreatePaymentAsync(CreatePaymentDto create)
        {
            Logger.LogInformation("About to call External Provider");

            string status = "failed";

            if (create.Amount < 20)
            {
                status = CheapPaymentGateway.MakePayment(create);
            }
            else if (create.Amount > 21 && create.Amount < 500)
            {
                status = ExpensivePaymentGateway.MakePayment(create);
                if (status == "failed")
                {
                    status = CheapPaymentGateway.MakePayment(create);
                }
            }
            else
            {
                for (var i = 0; i < 3; i++)
                {
                    status = PremiumPaymentService.MakePayment(create);
                    if (status != "failed")
                    {
                        break;
                    }
                }
            }

            if (status == "failed")
            {
                return(null);
            }

            Logger.LogInformation("About to Insert new payment entry");
            var paymentInfo = await PaymentRepo.InsertAsync(new Payment()
            {
                CreditCardNumber = create.CreditCardNumber,
                CardHolder       = create.CardHolder,
                ExpirationDate   = create.ExpirationDate,
                SecurityCode     = create.SecurityCode,
                Amount           = create.Amount
            });

            var commitResult = await UnitOfWork.CommitAsync();

            var paymentStatus = await PaymentStatusService.CreatePaymentStatusAsync(new CreatePaymentStatusDto()
            {
                PaymentId = paymentInfo.Entity.Id,
                Status    = status
            });

            return(commitResult == 1 ?  new PaymentDto()
            {
                Id = paymentInfo.Entity.Id,
                Amount = paymentInfo.Entity.Amount,
                Status = paymentStatus != null ? paymentStatus.Status : null
            } : null);
        }
        private async Task <PaymentStatusType> ExpensivePayment(PayDetails payDetails)
        {
            var payStatus = await _expensivePaymentGateway.MakePayment(payDetails);

            if (payStatus == PaymentStatusType.Failed)
            {
                payStatus = await _cheapPaymentGateway.MakePayment(payDetails);
            }

            return(payStatus);
        }
Exemple #3
0
 public bool Execute()
 {
     // In real implementation make request to expensivePaymentGateway
     if (_expensiveGateWay.IsGatewayAvaibale())
     {
         return(_expensiveGateWay.MakePayment(_amount));
     }
     if (_cheapGateWay.IsGatewayAvaibale())
     {
         return(_cheapGateWay.MakePayment(_amount));
     }
     return(false);
 }
Exemple #4
0
        public async Task <ProcessPaymentResponse> ProcessNewPayment(PaymentDTO payment)
        {
            try
            {
                //validate request
                // 1. validate card number by calling the ValidateCardNumberClass
                if (ValidateCardNumber.IsCardNumberValid(ValidateCardNumber.RemoveSpaceAndAlphaInCardNumber(payment.CreditCardNumber)))
                {
                    return new ProcessPaymentResponse {
                               Status = false, Message = "Invalid card number", ResponseType = ResponseType.badRequest
                    }
                }
                ;
                if (string.IsNullOrEmpty(payment.CardHolder))
                {
                    return new ProcessPaymentResponse {
                               Status = false, Message = "Provide card holder name", ResponseType = ResponseType.badRequest
                    }
                }
                ;
                if (payment.Amount <= 0)
                {
                    return new ProcessPaymentResponse {
                               Status = false, Message = "Provide a positive amount", ResponseType = ResponseType.badRequest
                    }
                }
                ;
                if (ValidateCardNumber.RemoveSpaceAndAlphaInCardNumber(payment.SecurityCode).Length != 3)
                {
                    return new ProcessPaymentResponse {
                               Status = false, Message = "Provide a valid security code", ResponseType = ResponseType.badRequest
                    }
                }
                ;
                if (DateTime.Now.Date > payment.ExpirationDate.Date)
                {
                    return new ProcessPaymentResponse {
                               Status = false, Message = "Card has expired", ResponseType = ResponseType.badRequest
                    }
                }
                ;

                var submitPaymentInfo = await _paymentService.AddPayment(_mapper.Map <Payment>(payment));

                if (submitPaymentInfo != null)
                {
                    var response = false;

                    // Call the  -ICheapPaymentGateway(Amount <20)

                    if (payment.Amount < 20)
                    {
                        response = await _cheapPayment.MakePayment(_config.GetSection("CheapServiceURL").Value, payment);
                    }
                    //Call the - IExpensivePaymentGateway(Amount btw 21-500)
                    else if (payment.Amount > 20 && payment.Amount < 500)
                    {
                        response = await _expensivePayment.MakePayment(_config.GetSection("ExpensiveServiceURL").Value, payment);

                        //if failed retry nce with ICheapPaymentGateway
                        if (!response)
                        {
                            response = await _cheapPayment.MakePayment(_config.GetSection("CheapServiceURL").Value, payment);
                        }
                    }
                    else
                    {
                        //Call the  PremiumPaymentService (Amount >500 ) and retry 3 times if failed
                        var retryCount = 0;
                        while (!response && retryCount < 3)
                        {
                            response = await _cheapPayment.MakePayment(_config.GetSection("PremiumServiceURL").Value, payment);
                        }
                    }

                    if (response)
                    {
                        await _paymentStatusService.UpdatePaymentStatus(submitPaymentInfo.PaymentId, "processed");

                        return(new ProcessPaymentResponse {
                            Status = response, Message = "processed", ResponseType = ResponseType.badRequest
                        });
                    }
                    else
                    {
                        await _paymentStatusService.UpdatePaymentStatus(submitPaymentInfo.PaymentId, "processed");

                        return(new ProcessPaymentResponse {
                            Status = response, Message = "failed", ResponseType = ResponseType.badRequest
                        });
                    }
                }

                else
                {
                    return(new ProcessPaymentResponse {
                        Message = "Internal server error", Status = false, ResponseType = ResponseType.internalServerError
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ProcessPaymentResponse {
                    Message = "Internal server error", Status = false, ResponseType = ResponseType.internalServerError
                });
            }
        }