Esempio n. 1
0
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                Response.Redirect("Main");
            }
            string   cardNumber, cardName;
            int      cvc, count = 0;
            DateTime expiryDate;
            double   totalPriceCost = 0, totalCheckout;

            //Get user input for payment details
            cardNumber = order.CardNumber;
            cardName   = order.CardName;
            cvc        = Convert.ToInt32(order.CardCVC);
            expiryDate = Convert.ToDateTime(order.CardExpiryDate + "/1");


            //Get total of products in checkout
            foreach (Product p in cart)
            {
                totalPriceCost += p.Quantity * p.Price;
            }

            //Total of checkout including postage
            totalCheckout = totalPriceCost + order.PostOption.Price;


            INFT3050.PaymentSystem.IPaymentSystem paymentSystem = INFT3050.PaymentSystem.INFT3050PaymentFactory.Create();

            INFT3050.PaymentSystem.PaymentRequest payment = new INFT3050.PaymentSystem.PaymentRequest();

            //User input being added to payment process
            payment.CardName    = cardName;
            payment.CardNumber  = cardNumber;
            payment.CVC         = cvc;
            payment.Expiry      = expiryDate;
            payment.Amount      = Convert.ToDecimal(totalCheckout);
            payment.Description = "test";

            var task = paymentSystem.MakePayment(payment);

            //Keep repeating until the task has completed
            while (count == 0)
            {
                if (task.IsCompleted)
                {
                    //Hide card invalid message
                    lblCardMessage.Visible = false;

                    //Transaction was approved
                    if (Convert.ToInt32(task.Result.TransactionResult) == 0)
                    {
                        order       = (Order)Session["order"];
                        order.Total = Convert.ToInt32(total);
                        QueryClass.AddOrder(order);
                        //List<Product> temp = new List<Product>();
                        //foreach (Product p in order.Products)
                        //{

                        //    foreach (Product p2 in GlobalData.productList)
                        //    {
                        //        //Reduce stock based on quanity purchase
                        //        if (p2.Name.Equals(p.Name))
                        //        {
                        //            p2.Stock -= p.Quantity;
                        //        }
                        //    }
                        //}
                        //foreach (Product p in GlobalData.productList)
                        //{
                        //    if (p.Stock > 0)
                        //    {
                        //        temp.Add(GlobalData.productList[GlobalData.productList.IndexOf(p)]);
                        //    }
                        //}
                        //GlobalData.productList = temp;
                        //Accessing gmail account to send email
                        SmtpClient client = new SmtpClient();
                        client.DeliveryMethod = SmtpDeliveryMethod.Network;
                        client.EnableSsl      = true;
                        client.Host           = "smtp.gmail.com";
                        client.Port           = 587;
                        System.Net.NetworkCredential credentials =
                            new System.Net.NetworkCredential("*****@*****.**", "bhpassword");
                        client.UseDefaultCredentials = false;
                        client.Credentials           = credentials;

                        //Adding message
                        MailMessage msg = new MailMessage();
                        msg.From = new MailAddress("*****@*****.**");
                        msg.To.Add(new MailAddress(order.UserEmail));
                        msg.Subject = "Bobblehead - Invoice";
                        //If html does not exist return non-html email
                        msg.Body = invoiceMessage(false, order.FirstName, order.LastName, order.Postcode, order.StreetAddress, order.Suburb, order.PostOption.ID, order.Date, order.Products);

                        //create an alternate HTML view that includes images and formatting
                        string        html = invoiceMessage(true, order.FirstName, order.LastName, order.Postcode, order.StreetAddress, order.Suburb, order.PostOption.ID, order.Date, order.Products);
                        AlternateView view = AlternateView
                                             .CreateAlternateViewFromString(
                            html, null, "text/html");

                        //Adding an image to the email
                        string         imgPath = Server.MapPath("../img/bobblebuttlogo.png");
                        LinkedResource img     = new LinkedResource(imgPath);
                        img.ContentId = "logoImage";
                        view.LinkedResources.Add(img);

                        //add the HTML view to the message and send
                        msg.AlternateViews.Add(view);
                        try
                        {
                            client.Send(msg);
                        }
                        catch
                        {
                            lblCardMessage.Text = "Error Sending Email";
                        }

                        //Remove items from cart and redirect to success purchase home/main page
                        Session.Remove("order");
                        Session.Remove("cart");
                        Response.Redirect("Main?orderEmail=" + order.UserEmail);
                    }
                    //Transaction was denied
                    else if (Convert.ToInt32(task.Result.TransactionResult) == 1)
                    {
                        lblCardMessage.Text    = "The card was denied";
                        lblCardMessage.Visible = true;
                    }
                    //Transaction was invalid
                    else if (Convert.ToInt32(task.Result.TransactionResult) == 2)
                    {
                        lblCardMessage.Text    = "The card details were invalid";
                        lblCardMessage.Visible = true;
                    }
                    //Transaction card expiry date was invalid
                    else if (Convert.ToInt32(task.Result.TransactionResult) == 3)
                    {
                        lblCardMessage.Text    = "The card Expiry Date was invalid";
                        lblCardMessage.Visible = true;
                    }
                    //Transaction timed out
                    else if (Convert.ToInt32(task.Result.TransactionResult) == 4)
                    {
                        lblCardMessage.Text    = "The card details could not be processed due to Connection Failure";
                        lblCardMessage.Visible = true;
                    }
                    //Will stop the while loop
                    count = 1;
                }
                //Keep the count at 0 so the while loop continues
                else
                {
                    count = 0;
                }
            }
        }