Exemplo n.º 1
0
        public ActionResult CreditCard()
        {
            //there should be an order in TempData...
            OrderData data = new OrderData();

            data.CurrentOrder = GetTempOrder();

            //we should have a shipping address by now
            //calc the tax
            data.CurrentOrder.TaxAmount = _taxService.CalculateTaxAmount(data.CurrentOrder);

            string sAddressID = Request.Form["addressid"];
            int    addressID  = 0;

            if (!String.IsNullOrEmpty(sAddressID))
            {
                int.TryParse(sAddressID, out addressID);
                data.CurrentOrder.BillingAddress = _orderService.GetAddress(addressID);
                //save it
                _orderService.SaveOrder(data.CurrentOrder);
                return(View(data));
            }


            //if this is a post from the CreditCard action
            //check the number
            string ccNumber = Request.Form["accountnumber"];

            if (!string.IsNullOrEmpty(ccNumber))
            {
                //set the Billing and CC
                if (data.CurrentOrder.BillingAddress == null)
                {
                    data.CurrentOrder.BillingAddress          = new Address();
                    data.CurrentOrder.BillingAddress.UserName = this.GetUserName();
                }

                UpdateModel(data.CurrentOrder.BillingAddress, new[] {
                    "FirstName",
                    "LastName",
                    "Email",
                    "Street1",
                    "Street2",
                    "City",
                    "StateOrProvince",
                    "Zip",
                    "Country"
                });



                CreditCard cc = new CreditCard();

                UpdateModel(cc, new[] {
                    "AccountNumber",
                    "CardType",
                    "ExpirationMonth",
                    "ExpirationYear",
                    "VerificationCode"
                });

                data.CurrentOrder.PaymentMethod = cc;

                PutTempOrder(data.CurrentOrder);

                //make sure the card is valid
                if (!cc.IsValid())
                {
                    this.SetErrorMessage("This credit card is not valid. Please check the number and expiration date");
                    return(View(data));
                }
                else
                {
                    return(RedirectToAction("Finalize"));
                }
            }
            else
            {
                if (data.CurrentOrder.ShippingAddress == null)
                {
                    return(RedirectToAction("Shipping"));
                }
                else
                {
                    if (data.CurrentOrder.BillingAddress == null)
                    {
                        data.CurrentOrder.BillingAddress = data.CurrentOrder.ShippingAddress;
                    }
                    PutTempOrder(data.CurrentOrder);
                    return(View(data));
                }
            }
        }