public static string ParseForMobile(string transId, OrderInfo orderInfo, string POrderno, int responseCode, int responsereasonCode, string responsetext)
        {
            try
            {
                var ot = new OrderDetailsCollection();
                var odinfo = new OrderDetailsInfo();
                var cms = new CartManageSQLProvider();
                var cf = new CommonInfo
                             {StoreID = orderInfo.StoreId, PortalID = orderInfo.PortalId, AddedBy = orderInfo.AddedBy};
                // UpdateOrderDetails
              
                odinfo.OrderID = orderInfo.OrderId;
                odinfo.TransactionID = odinfo.ResponseCode.ToString(transId);
                odinfo.InvoiceNumber = orderInfo.InvoiceNumber;
                odinfo.PurchaseOrderNumber = Convert.ToString(POrderno);
                odinfo.ResponseCode = Convert.ToInt32(responseCode);
                odinfo.ResponseReasonCode = Convert.ToInt32(responsereasonCode);
                odinfo.ResponseReasonText = Convert.ToString(responsetext);
                ot.ObjOrderDetails = odinfo;
                ot.ObjCommonInfo = cf;
                odinfo.OrderStatusID = 8;
                AspxOrderController.UpdateOrderDetails(ot);
                AspxCommonInfo aspxCommonObj = new AspxCommonInfo();
                aspxCommonObj.CustomerID = orderInfo.CustomerId;
                aspxCommonObj.SessionCode = orderInfo.SessionCode;
                aspxCommonObj.StoreID = orderInfo.StoreId;
                aspxCommonObj.PortalID = orderInfo.PortalId;
                cms.ClearCartAfterPayment(aspxCommonObj);
                return "This transaction has been approved";
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// update the quantity of items after order and also update the coupon code 
        /// that used on that order
        /// 
        /// </summary>
        /// /// <param name="orderInfo">info of order </param>
        /// <param name="itemsInfo">collection of items </param>
        /// <param name="coupon">and coupon code in coponCode#codeapplided  as single string</param>
        /// <param name="storeId">id of the store</param>
        /// <param name="portalId">id of the portal</param>
        /// <param name="userName">userName of customer</param>
        public static void UpdateItemQuantityAndCoupon(OrderInfo orderInfo, List<OrderItem> itemsInfo, List<CouponSession> coupons, int storeId, int portalId, string userName)
        {
            try
            {
                string error = "";
                try
                {

                    foreach (var coupon in coupons)
                    {
                        var paraMeter = new List<KeyValuePair<string, object>>
                                                {
                                                    new KeyValuePair<string, object>("@CouponCode",coupon.Key),
                                                    new KeyValuePair<string, object>("@StoreID", storeId),
                                                    new KeyValuePair<string, object>("@PortalID", portalId),
                                                    new KeyValuePair<string, object>("@UserName", userName),
                                                    new KeyValuePair<string, object>("@CouponUsedCount",coupon.AppliedCount),
                                                    new KeyValuePair<string, object>("@OrderID", orderInfo.OrderId)

                                                };
                        var sqlH = new SQLHandler();
                        sqlH.ExecuteNonQuery("usp_Aspx_UpdateCouponUserRecord", paraMeter);
                    }

                }
                catch (Exception ex)
                {
                    error = ex.InnerException.ToString();
                }


                foreach (var item in itemsInfo)
                {
                    if (item.IsDownloadable == false || item.IsGiftCard == false)
                    {
                        var paraMeter = new List<KeyValuePair<string, object>>
                                            {
                                                new KeyValuePair<string, object>("@StoreID", storeId),
                                                new KeyValuePair<string, object>("@PortalID", portalId),
                                                new KeyValuePair<string, object>("@AddedBy", userName),
                                                new KeyValuePair<string, object>("@ItemID", item.ItemId),
                                                new KeyValuePair<string, object>("@Quantity", item.Quantity),
                                                new KeyValuePair<string, object>("@OrderID", orderInfo.OrderId),
                                                new KeyValuePair<string, object>("@CostVariantsIDs", item.CostVariantIds)
                                            };
                        var sqlH = new SQLHandler();
                        sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_UpdateItemQuantitybyOrder]", paraMeter);
                        if (!string.IsNullOrEmpty(error))
                        {
                            throw new Exception(error);
                        }
                    }
                }



            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public void ParseAfterIPNMobile(OrderInfo orderInfo,List<CouponSession> coupons,List<OrderItem> itemsInfo,string postData,UserAddressInfo billingAddress,UserAddressInfo shippingAddress, string templateName, string addressPath)
        {

            string transId = string.Empty;

            try
            {

                String[] stringArray = postData.Split('\n');
                var ot = new OrderDetailsCollection();
                var odinfo = new OrderDetailsInfo();
                var cms = new CartManageSQLProvider();
                var cf = new CommonInfo
                             {StoreID = orderInfo.StoreId, PortalID = orderInfo.PortalId, AddedBy = orderInfo.AddedBy};
                // UpdateOrderDetails
            
                var sqlH = new SQLHandler();
                // use split to split array we already have using "=" as delimiter

                int i;
                for (i = 1; i < stringArray.Length - 1; i++)
                {
                    String[] stringArray1 = stringArray[i].Split('=');

                    String sKey = stringArray1[0];
                    String sValue = HttpUtility.UrlDecode(stringArray1[1]);

                    // set string vars to hold variable names using a switch
                    switch (sKey)
                    {
                        case "payment_status":
                            odinfo.ResponseReasonText = Convert.ToString(sValue);
                            break;

                        case "mc_fee":
                            // ph.PaymentFee = Convert.ToDouble(sValue);
                            break;

                        case "payer_email":
                            // ph.PayerEmail = Convert.ToString(sValue);
                            break;

                        case "Tx Token":
                            // ph.TxToken = Convert.ToString(sValue);
                            break;

                        case "txn_id":
                            odinfo.TransactionID = Convert.ToString(sValue);
                            transId = Convert.ToString(sValue);
                            break;

                    }
                }

                ot.ObjCommonInfo = cf;
                //odinfo.OrderStatusID = 8; var orderInfo = (OrderInfo) HttpContext.Current.Session["OrderCollection"];

                UpdateItemQuantityAndCoupon(orderInfo, itemsInfo, coupons, orderInfo.StoreId, orderInfo.PortalId,
                                            orderInfo.AddedBy);
                AspxCommonInfo aspxCommonObj = new AspxCommonInfo();
                aspxCommonObj.CustomerID = orderInfo.CustomerId;
                aspxCommonObj.SessionCode = orderInfo.SessionCode;
                aspxCommonObj.StoreID = orderInfo.StoreId;
                aspxCommonObj.PortalID = orderInfo.PortalId;
                cms.ClearCartAfterPayment(aspxCommonObj);
                EmailTemplate.SendEmailForOrderMobile(orderInfo, billingAddress, shippingAddress, addressPath,
                                                      templateName, transId);


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void SendEmailForOrderMobile(OrderInfo orderInfo, UserAddressInfo billingAddress,
                                                   UserAddressInfo shippingAddress, string addressPath,
                                                   string templateName, string transID)
        {
            StoreSettingConfig ssc = new StoreSettingConfig();
            SageFrameConfig pagebase = new SageFrameConfig();
            string sendOrderNotice = ssc.GetStoreSettingsByKey(StoreSetting.SendOrderNotification, orderInfo.StoreId,
                                                               orderInfo.PortalId, orderInfo.CultureName);
            string logosrc = ssc.GetStoreSettingsByKey(StoreSetting.StoreLogoURL, orderInfo.StoreId, orderInfo.PortalId,
                                                       orderInfo.CultureName);
            string inquiry = ssc.GetStoreSettingsByKey(StoreSetting.SendEcommerceEmailsFrom, orderInfo.StoreId,
                                                       orderInfo.PortalId, orderInfo.CultureName);
            string storeName = ssc.GetStoreSettingsByKey(StoreSetting.StoreName, orderInfo.StoreId, orderInfo.PortalId,
                                                         orderInfo.CultureName);

            if (bool.Parse(sendOrderNotice))
            {
                List<MessageManagementInfo> template =
                    MessageManagementController.GetMessageTemplateByMessageTemplateTypeID(
                        SystemSetting.ORDER_PLACED, orderInfo.PortalId);
                foreach (MessageManagementInfo messageToken in template)
                {

                    string messageTemplate = messageToken.Body;
                    if (template != null)
                    {
                        string[] tokens = GetAllToken(messageTemplate);
                        foreach (string token in tokens)
                        {
                            switch (token)
                            {
                                case "%OrderRemarks%":
                                    messageTemplate = messageTemplate.Replace(token, orderInfo.Remarks);
                                    break;
                                case "%InvoiceNo%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              orderInfo.InvoiceNumber);
                                    break;
                                case "%OrderID%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              orderInfo.OrderId.ToString());
                                    break;
                                case "%BillingAddress%":
                                    string billing = billingAddress.FirstName + " " +
                                                     billingAddress.LastName +
                                                     "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";

                                    if (billingAddress.CompanyName != null)
                                    {
                                        billing += billingAddress.CompanyName +
                                                   "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";
                                    }

                                    billing += billingAddress.City + ", " +
                                               billingAddress.Address +
                                               "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                               billingAddress.Country +
                                               "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                               billingAddress.EmailAddress +
                                               "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                               billingAddress.Phone;
                                    messageTemplate = messageTemplate.Replace(token, billing);
                                    break;
                                case "%ShippingAddress%":
                                    string shipping = "";
                                    if (!orderInfo.IsDownloadable || shippingAddress != null)
                                    {
                                        if (orderInfo.IsMultipleCheckOut == false)
                                        {
                                            shipping = shippingAddress.FirstName + " " +
                                                       shippingAddress.LastName +
                                                       "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";
                                            if (shippingAddress.CompanyName != null)
                                            {
                                                shipping += shippingAddress.CompanyName +
                                                            "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";
                                            }

                                            shipping += shippingAddress.City + ", " +
                                                        shippingAddress.Address +
                                                        "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                                        shippingAddress.Country +
                                                        "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                                        shippingAddress.EmailAddress +
                                                        "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">" +
                                                        shippingAddress.Phone +
                                                        "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";

                                        }
                                        else
                                        {
                                            shipping = "Multiple addresses<br />Plese log in to view." +
                                                       "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";
                                        }

                                    }
                                    else
                                    {
                                        shipping = "Your Ordered Item is Downloadable Item." +
                                                   "</td></tr><tr><td height=\"32\" style=\"border-bottom:thin dashed #d1d1d1; padding:10px 0 5px 10px; font:normal 12px Arial, Helvetica, sans-serif\">";
                                    }

                                    messageTemplate = messageTemplate.Replace(token, shipping);
                                    break;
                                case "%UserFirstName%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              billingAddress.FirstName);
                                    break;
                                case "%UserLastName%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              billingAddress.LastName);
                                    break;
                                case "%TransactionID%":
                                    messageTemplate = messageTemplate.Replace(token, transID);
                                    break;
                                case "%PaymentMethodName%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              orderInfo.PaymentMethodName);
                                    break;
                                case "%DateTimeDay%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              DateTime.Now.ToString("dddd, dd MMMM yyyy"));
                                    break;
                                case "%DateYear%":
                                    messageTemplate = messageTemplate.Replace(token, DateTime.Now.Year.ToString());
                                    break;
                                case "%CustomerID%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              orderInfo.CustomerId.ToString());
                                    break;
                                case "%PhoneNo%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              billingAddress.Phone);
                                    break;
                                case "%AccountLogin%":
                                    string account = "";
                                    if (orderInfo.AddedBy.ToLower() == "anonymoususer" &&
                                        orderInfo.CustomerId == 0)
                                    {
                                        // future login process for annoymoususr 
                                        account +=
                                            "Please Register and log in to your ";

                                        account += "<a  style=\"color: rgb(39, 142, 230);\"  href=" + addressPath +
                                                   "User-Registration.aspx" + ">account</a>";
                                    }
                                    else
                                    {
                                        account +=
                                            "  Please log in to your ";

                                        account += " <a style=\"color: rgb(39, 142, 230);\"  href=" + addressPath +
                                                   "Login.aspx" + ">account</a>";
                                    }
                                    messageTemplate = messageTemplate.Replace(token, account);
                                    break;
                                case "%LogoSource%":
                                    // string src = " http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + "/Templates/" + templateName + "/images/aspxcommerce.png";
                                    string src = " http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] +
                                                 "/" + logosrc;
                                    messageTemplate = messageTemplate.Replace(token, src);
                                    break;
                                case "%DateTime%":
                                    messageTemplate = messageTemplate.Replace(token,
                                                                              DateTime.Now.ToString("dd MMMM yyyy "));

                                    break;
                                case "%InquiryEmail%":
                                    string x =
                                        "<a  target=\"_blank\" style=\"text-decoration: none;color: #226ab7;font-style: italic;\" href=\"mailto:" +
                                        inquiry + "\" >" + inquiry + "</a>";
                                    messageTemplate = messageTemplate.Replace(token, x);
                                    break;
                                case "%StoreName%":
                                    messageTemplate = messageTemplate.Replace(token, storeName);
                                    break;
                            }
                        }
                        // return messageTemplate;
                        string emailStoreAdmin = ssc.GetStoreSettingsByKey(StoreSetting.SendEcommerceEmailsFrom,
                                                                           orderInfo.StoreId, orderInfo.PortalId
                                                                           , orderInfo.CultureName);
                        string emailSuperAdmin = pagebase.GetSettingsByKey(SageFrameSettingKeys.SuperUserEmail);
                        string emailSiteAdmin = pagebase.GetSettingsByKey(SageFrameSettingKeys.SiteAdminEmailAddress);
                        MailHelper.SendMailNoAttachment(emailStoreAdmin, billingAddress.EmailAddress,
                                                        messageToken.Subject
                                                        , messageTemplate, emailSiteAdmin, emailSuperAdmin);

                    }
                }
            }
        }