public bool DoCheckoutPayment(string finalPaymentAmount, string token, string PayerID, ref NVPCodec decoder, ref string retMsg)
        {
            if (bSandbox)
            {
                pEndPointURL = pEndPointURL_SB;
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "DoExpressCheckoutPayment";
            encoder["TOKEN"] = token;
            encoder["PAYERID"] = PayerID;
            encoder["PAYMENTREQUEST_0_AMT"] = finalPaymentAmount;
            encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "EUR";
            encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                    "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                    "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }
        private string buildCredentialsNVPString()
        {
            NVPCodec codec = new NVPCodec();

            if (!IsEmpty(APIUsername))
                codec["USER"] = APIUsername;

            if (!IsEmpty(APIPassword))
                codec[PWD] = APIPassword;

            if (!IsEmpty(APISignature))
                codec[SIGNATURE] = APISignature;

            if (!IsEmpty(Subject))
                codec["SUBJECT"] = Subject;

            codec["VERSION"] = "88.0";

            return codec.Encode();
        }
        public bool ShortcutExpressCheckout(Cart cart, string amt, ref string token, ref string retMsg)
        {
            int i;

            if (bSandbox)
            {
                pEndPointURL = pEndPointURL_SB;
                host = host_SB;
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "SetExpressCheckout";
            encoder["RETURNURL"] = returnURL;
            encoder["CANCELURL"] = cancelURL;
            encoder["BRANDNAME"] = "NCIRL Web Store";
            encoder["PAYMENTREQUEST_0_AMT"] = amt;
            encoder["PAYMENTREQUEST_0_ITEMAMT"] = amt;
            encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
            encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "EUR";

            i = 0;
            foreach (var line in cart.Lines)
            {
                encoder["L_PAYMENTREQUEST_0_NAME" + i] = line.Product.Name.ToString();
                encoder["L_PAYMENTREQUEST_0_AMT" + i] = line.Product.Price.ToString();
                encoder["L_PAYMENTREQUEST_0_QTY" + i] = line.Quantity.ToString();
                i++;
            }

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                token = decoder["TOKEN"];
                string ECURL = "https://" + host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token;
                retMsg = ECURL;
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                    "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                    "Desc2=" + decoder["L_LONGMESSAGE0"];
                return false;
            }
        }
        public bool GetCheckoutDetails(string token, ref string PayerID, ref NVPCodec decoder, ref string retMsg)
        {
            if (bSandbox)
            {
                pEndPointURL = pEndPointURL_SB;
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "GetExpressCheckoutDetails";
            encoder["TOKEN"] = token;

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                PayerID = decoder["PAYERID"];
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                    "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                    "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }
        public ViewResult CheckoutReviewOrderCont()
        {
            int orderID;
            int currentOrderId = 0;
            string retMsg = "";
            string token = "";
            string finalPaymentAmount = "";
            string PayerID = "";

            NVPAPICaller payPalCaller = new NVPAPICaller();
            payPalCaller.SetCredentials();

            NVPCodec decoder = new NVPCodec();

            token = UserSessionData.Token.ToString();
            finalPaymentAmount = UserSessionData.PaymentAmount.ToString();
            PayerID = UserSessionData.PayerID;
            currentOrderId = UserSessionData.OrderID;

            bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
            if (ret)
            {
                // Retrieve PayPal confirmation value.
                string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();

                //Update the order
                orderID = UserSessionData.OrderID;
                orderID = UpdateOrderConfirmation(orderID, PaymentConfirmation);

                //Reset session data
                UserSessionData.PaymentAmount = 0;
                UserSessionData.OrderID = 0;

                ViewBag.PaymentConfirmation = PaymentConfirmation;

                //Reduce the quantity of products in stock
                IEnumerable<OrderItem> orderItems = orderItemRepository.OrderItems.Where(x => x.OrderID == orderID);
                foreach (var orderItem in orderItems)
                {
                    UpdateProductQuantity(orderItem.ProductID, orderItem.Quantity);
                }

                return View("CheckoutReviewTrans");
            }
            else
            {
                return View("CheckoutError");
            }
        }
        public ViewResult CheckoutReview()
        {
            int orderID;

            string firstName, lastName, company, email,
                streetLine1, streetLine2, streetLine3,
                city, postalCode, county, country;

            string retMsg = "";
            string token = "";
            decimal paymentAmountOnCheckout = 0;
            string PayerID = "";

            NVPAPICaller payPalCaller = new NVPAPICaller();
            payPalCaller.SetCredentials();

            NVPCodec decoder = new NVPCodec();

            token = UserSessionData.Token.ToString();
            paymentAmountOnCheckout = UserSessionData.PaymentAmount;

            bool ret = payPalCaller.GetCheckoutDetails(token, ref PayerID, ref decoder, ref retMsg);
            if (ret)
            {

                // Verify total payment amount as set on CheckoutStart.aspx.
                try
                {
                    decimal paymentAmoutFromPayPal = Convert.ToDecimal(decoder["AMT"].ToString());
                    if (paymentAmountOnCheckout != paymentAmoutFromPayPal)
                    {
                        return View("CheckoutError");
                    }
                }
                catch (Exception)
                {
                    return View("CheckoutError");
                }

                //Session information
                UserSessionData.PayerID = PayerID;
                orderID = UserSessionData.OrderID;

                //Update the order with PayPal Informatin
                company = string.Empty; city = string.Empty; county = string.Empty;
                //OrderDate = Convert.ToDateTime(decoder["TIMESTAMP"].ToString());
                //Username = User.Identity.Name;
                firstName = decoder["FIRSTNAME"].ToString();
                lastName = decoder["LASTNAME"].ToString();
                streetLine1 = decoder["SHIPTOSTREET"].ToString();
                streetLine2 = decoder["SHIPTOCITY"].ToString();
                streetLine3 = decoder["SHIPTOSTATE"].ToString();
                postalCode = decoder["SHIPTOZIP"].ToString();
                country = decoder["SHIPTOCOUNTRYCODE"].ToString();
                email = decoder["EMAIL"].ToString();
                //Total = Convert.ToDecimal(decoder["AMT"].ToString());
                orderID = UpdateOrderShipTo(orderID, firstName, lastName, company, email,
                    streetLine1, streetLine2, streetLine3,
                    city, postalCode, county, country);

                //Prepare for display
                PayPalReview ppReview = new PayPalReview();
                ppReview.OrderID = orderID.ToString();
                ppReview.OrderDate = decoder["TIMESTAMP"].ToString();
                ppReview.Username = User.Identity.Name;
                ppReview.FirstName = firstName;
                ppReview.LastName = lastName;
                ppReview.Address = streetLine1;
                ppReview.City = streetLine2;
                ppReview.State = streetLine3;
                ppReview.PostalCode = postalCode;
                ppReview.Email = email;
                ppReview.TotalAmount = paymentAmountOnCheckout.ToString();
                ViewBag.PayPalReview = ppReview;

                return View("CheckoutReviewOrder");
            }
            else
            {
                return View("CheckoutError");
            }
        }