public async Task <ResponseAPIModel> ProcessPayment([FromBody] ProcessPaymentAPIModel processPaymentModel)
 {
     using (PaymentEngine paymentEngine = new PaymentEngine())
     {
         return(await paymentEngine.ProcessPayment(processPaymentModel));
     }
 }
        public ResponseAPIModel ProcessPayment(ProcessPaymentAPIModel paymentModel)
        {
            var client  = new RestClient("http://localhost:52843/api/PaymentGateway/GetTransactions");
            var request = new RestRequest(Method.POST);

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("paymentModel", JsonConvert.SerializeObject(paymentModel), ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            return(JsonConvert.DeserializeObject <ResponseAPIModel>(response.Content));
        }
        public bool ValidatePaymentModel(ProcessPaymentAPIModel paymentModel, Logger logger)
        {
            //Simple model validation
            //TO-DO: Add more robust one

            if (paymentModel.MerchantId == null)
            {
                logger.Error("Merchant Id is null");
                return(false);
            }

            if (paymentModel.CardNumber == null)
            {
                logger.Error("Card number is null");
                return(false);
            }
            if (!IsValidCardNumber(paymentModel.CardNumber))
            {
                logger.Error("Card number is invalid");
                return(false);
            }

            if (paymentModel.ExpiryDate < DateTime.Now)
            {
                logger.Error("Expiry date cannot be less than today's date");
                return(false);
            }

            if (!isValidExpiryDate(paymentModel.ExpiryDate.ToString()))
            {
                logger.Error("Expiry date is invalid");
                return(false);
            }

            if (paymentModel.ExpiryDate == null)
            {
                logger.Error("Expiry date is null");
                return(false);
            }

            if (paymentModel.Amount < 1)
            {
                logger.Error("Amount cannot be negative");
                return(false);
            }

            if (!ContainsNumbers(paymentModel.CVV))
            {
                logger.Error("CVV cannot contain alphabets");
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
 private static ProcessPaymentModel BuildPaymentModel(ProcessPaymentAPIModel paymentModel)
 {
     logger.Info("Building payment process model");
     return(new ProcessPaymentModel
     {
         MerchantId = paymentModel.MerchantId,
         Amount = paymentModel.Amount,
         CardNumber = EncryDecryMechanism.EncryptData(paymentModel.CardNumber, secretKey),
         CVV = paymentModel.CVV,
         Currency = paymentModel.Currency,
         ExpiryDate = paymentModel.ExpiryDate
     });
 }
Esempio n. 5
0
        public async Task <ResponseAPIModel> ProcessPayment(ProcessPaymentAPIModel paymentModel)
        {
            //Serilog implementation
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .WriteTo.Seq("http://localhost:5341")
                         .CreateLogger();

            logger.Info("Starting Process Payment...");
            Log.Information("Starting Process Payment...");
            logger.Info("Validating process payment model");
            Log.Information("Validating process payment model");

            if (paymentModel != null && validator.ValidatePaymentModel(paymentModel, logger))
            {
                logger.Info("Valid payment model");
                Log.Information("Valid payment model");

                ProcessPaymentModel processModel = BuildPaymentModel(paymentModel);

                //contact and expose data to external bank api
                ExternalBankExpo contactExternalBank = new ExternalBankExpo();
                logger.Info("Contacting external bank endpoints");
                Log.Information("Contacting external bank endpoints");

                try
                {
                    var response = contactExternalBank.ProcessPayment(processModel);

                    if (response != null && response.ResponseStatus == States.Success.ToString())
                    {
                        logger.Info("Success in contacting external bank endpoints");
                        Log.Information("Success in contacting external bank endpoints");


                        //build transaction model and save data to db
                        Transaction transaction = BuildTransactionModel(processModel, response);

                        logger.Info("Attempting to save transaction");
                        Log.Information("Attempting to save transaction");

                        try
                        {
                            using (CheckOutPaymentGatewayEntities db = new CheckOutPaymentGatewayEntities())
                            {
                                db.Transactions.Add(transaction);
                                await db.SaveChangesAsync();
                            }

                            if (transaction.TransactionStatus == States.Success.ToString())
                            {
                                logger.Info("Transaction saved successfully");
                            }
                            else
                            {
                                logger.Error("Transaction failed");
                            }

                            return(transaction.TransactionStatus == States.Success.ToString() ? new ResponseAPIModel(States.Success)
                                : new ResponseAPIModel(States.Error, "Cannot execute transaction"));
                        }
                        catch (Exception ee)
                        {
                            logger.Error("Failed to contact perform transaction - " + ee.ToString());
                            Log.Information("Failed to contact perform transaction - " + ee.ToString());
                        }
                    }
                    else
                    {
                        logger.Error("Failed to contact external bank endpoints");
                        Log.Information("Failed to contact external bank endpoints");

                        return(new ResponseAPIModel(States.Error, "Cannot contact and process payment to external bank"));
                    }
                }
                catch (Exception e)
                {
                    logger.Error("Failed to contact external bank endpoints - " + e.ToString());
                    Log.Information("Failed to contact external bank endpoints - " + e.ToString());

                    return(new ResponseAPIModel(States.Error, "Cannot contact and process payment to external bank"));
                }
            }
            logger.Error("Invalid payment model");
            Log.CloseAndFlush();
            return(new ResponseAPIModel(States.Error, "Invalid payment api model"));
        }
        private void btnProcessPayment_Click(object sender, EventArgs e)
        {
            //DIRTY CODES AHEAD :D

            if (tabControl1.SelectedTab == tabPage1)
            {
                ProcessPaymentAPIModel model   = new ProcessPaymentAPIModel();
                NumberStyles           style   = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
                CultureInfo            culture = CultureInfo.CreateSpecificCulture("en-GB");
                decimal amount;

                if (MerchantId.Text.Trim() == string.Empty || CardNumber.Text.Trim() == string.Empty ||
                    ExpiryDate.Text.Trim() == string.Empty || Amount.Text.Trim() == string.Empty ||
                    Currency.Text.Trim() == string.Empty || CVV.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("All fields are required for process the payment!");
                    return;
                }

                if (IsGuid(MerchantId.Text))
                {
                    model.MerchantId = Guid.Parse(MerchantId.Text);
                }
                else
                {
                    MessageBox.Show("Invalid Merchant Id!");
                    return;
                }

                if (IsValidCardNumber(CardNumber.Text))
                {
                    model.CardNumber = CardNumber.Text;
                }
                else
                {
                    MessageBox.Show("Invalid Card Number!");
                    return;
                }


                if (isValidExpiryDate(ExpiryDate.Text))
                {
                    model.ExpiryDate = Convert.ToDateTime(ExpiryDate.Text);
                }
                else
                {
                    MessageBox.Show("Invalid Expiry Date");
                    return;
                }

                if (Decimal.TryParse(Amount.Text, style, culture, out amount))
                {
                    model.Amount = amount;
                }
                else
                {
                    MessageBox.Show("Invalid Amount");
                    return;
                }


                //can add more validations...
                model.Currency = Currency.Text;
                model.CVV      = CVV.Text;

                var response = expo.ProcessPayment(model);
                lblStatus.Text = !string.IsNullOrEmpty(response.ResponseMessage) ?
                                 response.ResponseStatus.ToString() + " : " + response.ResponseMessage.ToString() : response.ResponseStatus.ToString();
            }
        }