private Order CopyExpressCheckoutReqToOrder(ExprPayPalRequest req)
        {
            CartShipPayInfo csp = (CartShipPayInfo)Session["CartShipPayInfo"];

            Order order = new Order
            {
                CspPayerEmail         = req.PayerEmail,
                CspShippingAddress    = req.ShipAddress,
                RequestCurrency       = req.currency,
                RequestFirstName      = req.PayerName,
                RequestLastName       = req.PayerLastName,
                RequestPayerId        = req.PayerId,
                RequestListOfProducts = req.Products,
                PayPalPayerId         = req.PayPalPayerId,
                RequestPaymentDate    = req.TransTime,
                CspContactNumber      = csp.ShippingDetails.Phone,
                CartNumberOfItems     = Convert.ToString(csp.ShoppingCart.ProductLines.Sum(p => p.Quantity)),
                RequestPaymentStatus  = req.PaymentStatus,
                RequestTxnId          = req.TransId,
                RequestTxnType        = req.TransType,
                RequestReceiverId     = req.PayPalMerchantUsername,
                Total        = req.Total,
                ShiptoName   = req.ShiptoName,
                ShippingCost = req.ShippingCost,
                SubTotal     = req.SubTotal
            };

            order.OrderedProducts = GetOrderedProducts(req.ProductsList, order.Id);

            return(order);
        }
        public ViewResult DoExpressCheckoutPayment()
        {
            try
            {
                ExprPayPalRequest req = (ExprPayPalRequest)Session[reqKey];
                req.Method = "DoExpressCheckoutPayment";

                string resp = SendRequest(req.Endpoint, req.ToString());

                string ack = ParseToken(resp, "ACK");
                req.TransId       = ParseToken(resp, "PAYMENTINFO_0_TRANSACTIONID");
                req.TransType     = ParseToken(resp, "PAYMENTINFO_0_TRANSACTIONTYPE");
                req.PaymentStatus = ParseToken(resp, "PAYMENTINFO_0_PAYMENTSTATUS");

                // send mail to customer
                if (ack.Equals("Success") && (req.Token.Equals(ParseToken(resp, "TOKEN"))))
                {
                    Order order = CopyExpressCheckoutReqToOrder(req);
                    repo.SaveAllToDatabase(order);

                    sendCustomerEmail(order);
                    sendAdminEmail(order);

                    CartShipPayInfo csp = (CartShipPayInfo)Session["CartShipPayInfo"];
                    csp.ShoppingCart.ClearCart();
                    TempData["Confirm"] = "Payment Successful";
                    return(View("ConfirmPayment", order));
                }
            }
            catch (Exception)
            {
            }
            TempData["Fail"] = failMessage;
            return(View("_ErrorDisplay"));
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        // following code is used for PayPal's Eexpress Checkout functionality
        // detailed tutorial on express checkout is at https://developer.paypal.com/webapps/developer/docs/classic/express-checkout/ht_ec-singleItemPayment-curl-etc/
        ////////////////////////////////////////////////////////////////////////////////////////////////////



        public RedirectResult SetExpressCheckout()
        {
            CartShipPayInfo   csp = (CartShipPayInfo)Session["CartShipPayInfo"];
            ExprPayPalRequest req = new ExprPayPalRequest()
            {
                // live credentials
                Password = "******",
                PayPalMerchantUsername = "******",
                Signature = "AcQ.7s0YrYjFbaBoVNy6bGaoP-VpAkRQHqL0If7cnR.3iZZFIjV8BG6h",

                Endpoint = "https://api-3t.paypal.com/nvp/", //nvp production
                //req.Endpoint = "https://api-3t.sandbox.paypal.com/nvp/"; // nvp     sandbox
                Method = "SetExpressCheckout",               // METHOD
                // set all costs and products details from cart info
                ProductsList = csp.ShoppingCart.ProductLines,
                Total        = Convert.ToString(csp.ShoppingCart.ComputeCartValue() + csp.ShoppingCart.ShippingCosts),
                SubTotal     = Convert.ToString(csp.ShoppingCart.ComputeCartValue()),//
                ShippingCost = Convert.ToString(csp.ShoppingCart.ShippingCosts),
                currency     = "AUD",
                //  set urls
                //ReturnUrl = "http://localhost:33966/ExpressCheckout/GetExpressCheckoutDetails",
                CancelUrl = "http://localhost:33966/Cart/RedirectAfterCancel",
                ReturnUrl = "https://stesha.com.au/ExpressCheckout/GetExpressCheckoutDetails",
                //CancelUrl = "https://stesha.com.au/Cart/RedirectAfterCancel",
                Version       = 93,
                PaymentAction = "SALE"
            };



            try
            {
                // make a call to PayPal and read answer
                string resp = SendRequest(req.Endpoint, req.ToString());
                req.Token = ParseToken(resp, "TOKEN");
            }
            catch (Exception)
            {
                TempData["Fail"] = failMessage;
                return(Redirect("_ErrorDisplay"));
            }

            // string url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + req.Token;
            string url = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + req.Token;

            Session[reqKey] = req;

            return(Redirect(url));
        }
        public ActionResult GetExpressCheckoutDetails()
        {
            CartShipPayInfo csp = (CartShipPayInfo)Session["CartShipPayInfo"];
            // after the payment is confirmed get the payer's details
            ExprPayPalRequest req = (ExprPayPalRequest)Session[reqKey];

            req.Method = "GetExpressCheckoutDetails";

            try
            {
                //send data to pay pal server  and read response
                string resp = SendRequest(req.Endpoint, req.ToString());
                string ack  = ParseToken(resp, "ACK");

                // verify success and token
                if (ack.Equals("Success") && (req.Token.Equals(ParseToken(resp, "TOKEN"))))
                {
                    req.PayerId = (Session["Profile"] == null) ? csp.ShippingDetails.Email :
                                  Convert.ToString(((ApplicationUser)Session["Profile"]).Id);
                    req.PayPalPayerId = ParseToken(resp, "PAYERID");
                    req.Total         = ParseToken(resp, "PAYMENTREQUEST_0_AMT");
                    req.PayerEmail    = csp.ShippingDetails.Email;
                    req.PayerName     = ParseToken(resp, "FIRSTNAME");
                    req.PayerLastName = ParseToken(resp, "LASTNAME");
                    req.ShiptoName    = csp.ShippingDetails.Name + " " + csp.ShippingDetails.Surname;
                    req.ShipAddress   = csp.ShippingDetails.GetAddress();
                    req.TransTime     = Convert.ToString(DateTime.Parse(ParseToken(resp, "TIMESTAMP")));
                    Session[reqKey]   = req;

                    return(new RedirectResult("/ExpressCheckout/DoExpressCheckoutPayment"));
                }
                else
                {
                    TempData["Fail"] = failMessage;
                    return(View("_ErrorDisplay"));
                }
            }
            catch (Exception)
            {
                TempData["Fail"] = failMessage;
                return(View("_ErrorDisplay"));
            }
        }