public async Task <bool> ProcessPayment(ProcessPaymentModel model)
        {
            try
            {
                //Adding Payment
                var payment = new Payment
                {
                    CreditCardNumber = model.CreditCardNumber,
                    CardHolder       = model.CardHolder,
                    ExpirationDate   = model.ExpirationDate,
                    SecurityCode     = model.SecurityCode,
                    Amount           = model.Amount
                };
                _uow.Add(payment);
                await _uow.CommitAsync();

                //Adding Payment Status as Initial - Pending
                var paymentStatus = new PaymentStatus
                {
                    PaymentId = payment.Id,
                    StatusId  = (int)TransactionStatus.Pending
                };
                _uow.Add(paymentStatus);

                paymentStatus = new PaymentStatus()
                {
                    PaymentId = payment.Id
                };

                var             factory        = new PaymentGatewayFactory();
                IPaymentGateway paymentGateway = factory.GetPaymentGateway(model.Amount);

                if (await paymentGateway.ProcessPayment())
                {
                    paymentStatus.StatusId = (int)TransactionStatus.Processed;
                }
                else
                {
                    paymentStatus.StatusId = (int)TransactionStatus.Failed;
                }

                _uow.Add(paymentStatus);
                await _uow.CommitAsync();

                return(true);
            }
            catch (Exception ex)
            {
                throw new BadRequestException(ex.Message);
            }
        }
        public void GetPaymentGateway_VerifyMethodCallOfCheapGateway(decimal amount, string status)
        {
            PaymentStatus expected;

            Enum.TryParse <PaymentStatus>(status, out expected);

            mockCheapGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);
            mockExpensiveGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);
            mockPremiumGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);

            sut = new PaymentGatewayFactory(mockCheapGateway.Object, mockExpensiveGateway.Object, mockPremiumGateway.Object);

            sut.GetPaymentGateway(amount);

            mockCheapGateway.Verify(x => x.ProceessAmount(amount), Times.Once);
        }
        public void GetPaymentGateway_WithStatus(decimal amount, string status)
        {
            PaymentStatus expected;

            Enum.TryParse <PaymentStatus>(status, out expected);

            mockCheapGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);
            mockExpensiveGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);
            mockPremiumGateway.Setup(x => x.ProceessAmount(amount)).Returns(expected);

            sut = new PaymentGatewayFactory(mockCheapGateway.Object, mockExpensiveGateway.Object, mockPremiumGateway.Object);

            var result = sut.GetPaymentGateway(amount);

            Assert.Equal(expected, result);
        }
Beispiel #4
0
        public async Task <String> MakePayment(PaymentDetails paymentDetails)
        {
            String response = String.Empty;

            try
            {
                //Using Custom Credit Card Validation Instead of CreditCardAttribute
                if (!String.IsNullOrEmpty(paymentDetails?.CreditCardNumber) && Validation.IsCardNumberValid(paymentDetails.CreditCardNumber))
                {
                    if (paymentDetails.Amount > 0 && Validation.IsExpirationDateValid(paymentDetails.ExpirationDate))
                    {
                        //String encryptedCardNumber = _Encryption.EncryptAndSave(paymentDetails.CreditCardNumber);
                        String encryptedCardNumber = paymentDetails.CreditCardNumber;

                        //Checking for existing user.
                        var existing_user = _PaymentContext.People.Include(x => x.CardInformation)
                                            .Where(x => x.FullName == paymentDetails.CardHolderName)
                                            .FirstOrDefault();
                        if (existing_user is null)
                        {
                            // if the user doesn't exist , insert into the database
                            existing_user = AddNewUser(paymentDetails, encryptedCardNumber);
                        }
                        else
                        {
                            // checking whether the card exists or not
                            var card = existing_user.CardInformation.Where(x => x.CreditCardNumber == encryptedCardNumber).FirstOrDefault();
                            if (card is null)
                            {
                                CardInformation cardInformation = new CardInformation()
                                {
                                    CreditCardNumber = encryptedCardNumber,
                                    SecurityCode     = paymentDetails.SecurityCode
                                };
                                existing_user.CardInformation.Add(cardInformation);
                                existing_user = _PeopleRepo.UpdateAndGet(existing_user);
                            }
                        }

                        //Creating a new transaction
                        Transactions NewTransaction = new Transactions();
                        _TransactionRepo.Insert(NewTransaction);
                        _TransactionRepo.Save();
                        existing_user.CardInformation.Where(x => x.CreditCardNumber == encryptedCardNumber).FirstOrDefault().Transactions.Add(NewTransaction);
                        existing_user = _PeopleRepo.UpdateAndGet(existing_user);

                        //Payment Gateway Call
                        _PaymentGateway = _PaymentGatewayFactory.GetPaymentGateway(paymentDetails.Amount);
                        response        = await _PaymentGateway.GetPaymentStatus(paymentDetails);

                        //Updating the response
                        NewTransaction.PaymentState = response;
                        _TransactionRepo.Update(NewTransaction);
                        _TransactionRepo.Save();
                    }
                }
                else
                {
                    response = "Invalid Card Number";
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }

            return(response);
        }