Exemplo n.º 1
0
        public ActionResult Authorize() {
            HttpContext ctx = System.Web.HttpContext.Current;
            Customer customer = new Customer();
            Settings settings = ViewBag.settings;
            // Retrieve Customer from Sessions/Cookie
            customer.GetFromStorage(ctx);
            if (!customer.Cart.Validate()) {
                return RedirectToAction("Index", "Cart");
            }

            if (customer.Cart.GetPaymentID() > 0) {
                UDF.ExpireCart(ctx, customer.ID);
                return RedirectToAction("Index", "Cart");
            }

            customer.BindAddresses();

            decimal amount = customer.Cart.getTotal();
            string cardnum = Request.Form["cardnumber"];
            string month = Request.Form["expiremonth"];
            string year = Request.Form["expireyear"];
            string cvv = Request.Form["cvv"];
            string first = Request.Form["first"];
            string last = Request.Form["last"];

            //step 1 - create the request
            IGatewayRequest request = new AuthorizationRequest(cardnum, month + year, amount, "Transaction");

            //These are optional calls to the API
            request.AddCardCode(cvv);

            //Customer info - this is used for Fraud Detection
            request.AddCustomer(customer.ID.ToString(), first, last, customer.Cart.Billing.street1 + ((customer.Cart.Billing.street2 != "") ? " " + customer.Cart.Billing.street2 : ""), customer.Cart.Billing.State1.abbr, customer.Cart.Billing.postal_code);

            //order number
            //request.AddInvoice("invoiceNumber");

            //Custom values that will be returned with the response
            //request.AddMerchantValue("merchantValue", "value");

            //Shipping Address
            request.AddShipping(customer.ID.ToString(), customer.Cart.Shipping.first, customer.Cart.Shipping.last, customer.Cart.Shipping.street1 + ((customer.Cart.Shipping.street2 != "") ? " " + customer.Cart.Shipping.street2 : ""), customer.Cart.Shipping.State1.abbr, customer.Cart.Shipping.postal_code);


            //step 2 - create the gateway, sending in your credentials and setting the Mode to Test (boolean flag)
            //which is true by default
            //this login and key are the shared dev account - you should get your own if you 
            //want to do more testing
            bool testmode = false;
            if (settings.Get("AuthorizeNetTestMode").Trim() == "true") {
                testmode = true;
            }

            Gateway gate = new Gateway(settings.Get("AuthorizeNetLoginKey"), settings.Get("AuthorizeNetTransactionKey"), testmode);
            customer.Cart.SetStatus((int)OrderStatuses.PaymentPending);

            //step 3 - make some money
            IGatewayResponse response = gate.Send(request);
            if (response.Approved) {
                customer.Cart.AddPayment("credit card", response.AuthorizationCode, "Complete");
                customer.Cart.SetStatus((int)OrderStatuses.PaymentComplete);
                customer.Cart.SendConfirmation(ctx);
                customer.Cart.SendInternalOrderEmail(ctx);
                int cartid = customer.Cart.ID;
                
                Cart new_cart = new Cart().Save();
                new_cart.UpdateCart(ctx, customer.ID);
                DateTime cookexp = Request.Cookies["hdcart"].Expires;
                HttpCookie cook = new HttpCookie("hdcart", new_cart.ID.ToString());
                cook.Expires = cookexp;
                Response.Cookies.Add(cook);

                customer.Cart = new_cart;
                customer.Cart.BindAddresses();

                return RedirectToAction("Complete", new { id = cartid });
            } else {
                customer.Cart.SetStatus((int)OrderStatuses.PaymentDeclined);
                return RedirectToAction("Index", new { message = response.Message });
            }
        }
        public ActionResult Authorize(int id = 0)
        {
            Customer c = new Customer { ID = id };
            c.Get();
            Cart currentCart = c.Carts.Where(x => x.payment_id == 0).First<Cart>();
            Settings settings = ViewBag.settings;

            decimal amount = currentCart.getTotal();
            string cardnum = Request.Form["cardnumber"];
            string month = Request.Form["expiremonth"];
            string year = Request.Form["expireyear"];
            string cvv = Request.Form["cvv"];
            string first = Request.Form["first"];
            string last = Request.Form["last"];

            //step 1 - create the request
            IGatewayRequest request = new AuthorizationRequest(cardnum, month + year, amount, "Transaction");

            //These are optional calls to the API
            request.AddCardCode(cvv);

            //Customer info - this is used for Fraud Detection
            request.AddCustomer(c.ID.ToString(), first, last, currentCart.Billing.street1 + ((currentCart.Billing.street2 != "") ? " " + currentCart.Billing.street2 : ""), currentCart.Billing.State1.abbr, currentCart.Billing.postal_code);

            //order number
            //request.AddInvoice("invoiceNumber");

            //Custom values that will be returned with the response
            //request.AddMerchantValue("merchantValue", "value");

            //Shipping Address
            request.AddShipping(c.ID.ToString(), currentCart.Shipping.first, currentCart.Shipping.last, currentCart.Shipping.street1 + ((currentCart.Shipping.street2 != "") ? " " + currentCart.Shipping.street2 : ""), currentCart.Shipping.State1.abbr, currentCart.Shipping.postal_code);

            //step 2 - create the gateway, sending in your credentials and setting the Mode to Test (boolean flag)
            //which is true by default
            //this login and key are the shared dev account - you should get your own if you
            //want to do more testing
            bool testmode = false;
            if (settings.Get("AuthorizeNetTestMode").Trim() == "true") {
                testmode = true;
            }

            Gateway gate = new Gateway(settings.Get("AuthorizeNetLoginKey"), settings.Get("AuthorizeNetTransactionKey"), testmode);

            //step 3 - make some money
            IGatewayResponse response = gate.Send(request);
            if (response.Approved) {
                currentCart.AddPayment("credit card", response.AuthorizationCode,"Complete");
                currentCart.SendConfirmation();
                int cartid = currentCart.ID;
                return RedirectToAction("Step6", new { id = cartid });
            } else {
                TempData["message"] = response.Message;
                return RedirectToAction("Step5", new { id = c.ID });
            }
        }