Ejemplo n.º 1
0
        /// <summary>
        /// Controller to fulfill orders as required.
        /// </summary>
        /// <param name="ShopUrl">web url of the store</param>
        /// <param name="OrderIds">orderIds to be fulfilled</param>
        /// <param name="LabelNumbers">Label numbers to be added</param>
        /// <returns></returns>
        public async Task <ActionResult> OrdersFulfillment(string ShopUrl, string OrderIds, string LabelNumbers)
        {
            //Db connection to query store details
            DbEngine conn = new DbEngine();
            //get store Shopify's token to access API
            string token = conn.GetStringValues(ShopUrl, "ShopifyToken");

            if (!OrderIds.Contains(","))
            {//If there in only one order
                //new ShopifyAPI objects to query
                ShopifyAPI newApi = new ShopifyAPI();
                //get fulfillment ids (if existed on this order)
                string fulfillments = await newApi.GetFulfillment(ShopUrl, token, OrderIds);

                if (fulfillments == "")
                {//if not fulfilled yet, fulfill it
                    string fulfillmentId = await newApi.NewFulfillment(ShopUrl, token, OrderIds, LabelNumbers);
                }
                else
                {//if fulfilled, update tracking information
                    string fulfillmentId = await newApi.UpdateFulfillment(ShopUrl, token, OrderIds, fulfillments, LabelNumbers);
                }
            }
            else
            {//more than one order
                //get the list of orderIds
                List <string> orderIds = OrderIds.Split(',').ToList();
                foreach (string id in orderIds)
                {//loop through orderIds list and fulfill/update fulfillment as per required
                    ShopifyAPI newApi       = new ShopifyAPI();
                    string     fulfillments = await newApi.GetFulfillment(ShopUrl, token, OrderIds);

                    if (fulfillments == "")
                    {//if not fulfilled yet, fulfill it
                        string fulfillmentId = await newApi.NewFulfillment(ShopUrl, token, OrderIds, LabelNumbers);
                    }
                    else
                    {//if fulfilled, update tracking information
                        string fulfillmentId = await newApi.UpdateFulfillment(ShopUrl, token, OrderIds, fulfillments, LabelNumbers);
                    }
                }
            }

            Response.Write("<input id='shopUrl' type='hidden'  value='" + ShopUrl + "'>");//passing shopUrl to View() for further queries
            return(View());
        }
        /// <summary>
        /// NewInternationalConsignment controller
        /// Based on the specified order IDs by Shopify, write required variables
        /// to front-end in hidden inputs for further queries
        /// International only process one order at a time
        /// Filter out domestic orders
        /// </summary>
        public async Task <ActionResult> NewInternationalConsignment(string shop, string[] ids)
        {
            //Get order numbers
            string orders = Request.QueryString["ids[]"];

            if (orders.Contains(','))
            {
                Response.Write("<input id='shopUrl' type='hidden' value='" + shop + "'>");            //passing shopUrl to View() for further queries
                Response.Write("<input id='addString' type='hidden' value='" + "MoreThanOne" + "'>"); //passing shopUrl to View() for further queries
                return(View());
            }
            else
            {
                //DB connection required to query store details
                DbEngine conn = new DbEngine();
                //Get Shopify Token to access Shopify API
                string token = conn.GetStringValues(shop, "ShopifyToken");
                int    cCode = conn.GetIntergerValues(shop, "CountryCode");
                //ShopifyAPI object
                ShopifyAPI api = new ShopifyAPI();

                Order o = await api.GetOrder(shop, token, orders);

                string address = "";
                string email   = "";
                JavaScriptSerializer jsonSerialiser = new JavaScriptSerializer();


                if (o.ShippingAddress.Country != "New Zealand")
                {
                    address = jsonSerialiser.Serialize(o.ShippingAddress);
                    email   = o.Email;
                }

                Response.Write("<input id='shopUrl' type='hidden' value='" + shop + "'>");      //passing shopUrl to View() for further queries
                Response.Write("<input id='emailString' type='hidden' value='" + email + "'>"); //passing address string to View() for further queries
                Response.Write("<input id='addString' type='hidden' value='" + address + "'>"); //passing address string to View() for further queries
                return(View());
            }
        }
        /// <summary>
        /// NewConsignment controller
        /// Based on the specified order IDs by Shopify, write required variables
        /// to front-end in hidden inputs for further queries
        /// Used for both single or multiple orders
        /// Filter out international orders
        /// </summary>
        public async Task <ActionResult> NewConsignment(string shop, string[] ids, string id)
        {
            //Get order numbers
            string orders;

            if (id != null)
            {
                orders = id;
            }
            else
            {
                orders = Request.QueryString["ids[]"];
                if (orders == null)
                {
                    orders = Request.QueryString["ids[][]"];
                }
            }


            //required objects
            List <string> orderIds                      = new List <string>();               //list of orderIds
            List <string> processingOrderIds            = new List <string>();               //list of orderIds
            List <string> processingOrderPreferences    = new List <string>();               //list of orderIds
            List <Order>  orderDetails                  = new List <Order>();                //list of order details
            List <ShopifySharp.Address> deliveryAddress = new List <ShopifySharp.Address>(); //list of delivery details
            List <string> emails = new List <string>();                                      //list of emails addresses

            if (orders == null || orders == "")                                              //No order select (in case customer reach this page from outside of their admin page
            {
                return(View());                                                              //NOTE: might need to redirect them to their admin page
            }

            if (orders.Contains(','))//if there are more than one order
            {
                //get a list of order numbers received
                orderIds = orders.Split(',').ToList();
            }
            else
            {
                //get a list of ONE order number
                orderIds.Add(orders);
            }
            //DB connection required to query store details
            DbEngine conn = new DbEngine();
            //Get Shopify Token to access Shopify API
            string token = conn.GetStringValues(shop, "ShopifyToken");
            int    cCode = conn.GetIntergerValues(shop, "CountryCode");
            List <CustomParcel> lCustomParcels = conn.GetCustomParcel(shop);
            //ShopifyAPI object
            ShopifyAPI api = new ShopifyAPI();

            //foreach order number from list, get a list of delivery details
            for (int i = 0; i < orderIds.Count; i++)
            {
                //get the order with order number
                Order k = await api.GetOrder(shop, token, orderIds[i]);

                if (k.ShippingAddress != null)
                {//if shipping address exist, add to list of delivery details
                    bool check = true;
                    //check if order is international
                    switch (cCode)
                    {
                    case 6:
                        if (k.ShippingAddress.Country != "New Zealand")
                        {
                            check = false;
                        }
                        break;

                    case 1:
                        if (k.ShippingAddress.Country != "Australia")
                        {
                            check = false;
                        }
                        break;
                    }
                    //check if duplicate addresses
                    if (deliveryAddress.Count > 0)
                    {
                        for (var l = 0; l < deliveryAddress.Count; l++)
                        {
                            if (deliveryAddress[l].Name == k.ShippingAddress.Name)
                            {
                                check = false;
                                break;
                            }
                        }
                    }
                    if (check == true)
                    {
                        deliveryAddress.Add(k.ShippingAddress);
                        emails.Add(k.Email);
                        processingOrderIds.Add(orderIds[i]);
                        processingOrderPreferences.Add(k.OrderNumber.ToString());
                    }
                }
                orderDetails.Add(k);//add order details into list of order details
            }

            ////jsonserialiser object to form json from list
            JavaScriptSerializer jsonSerialiser = new JavaScriptSerializer();
            ////creating json about orders to pass back to View()
            //string orderJson = jsonSerialiser.Serialize(orderDetails);
            string stringCP = jsonSerialiser.Serialize(lCustomParcels);

            string stringOrderReference = string.Join(",", processingOrderPreferences);


            //Forming data to pass back
            string address     = "";
            string note        = "";
            string addresses   = "";
            string strOrderIds = "";

            if (deliveryAddress.Count == 0)
            {//No delivery address found
                address = "NoAddress";
            }
            else if (deliveryAddress.Count > 1)
            {//More than one addresses found
                address     = "MoreThanOne";
                addresses   = jsonSerialiser.Serialize(deliveryAddress);
                strOrderIds = string.Join(",", processingOrderIds);
            }
            else
            {//one address
                address = jsonSerialiser.Serialize(deliveryAddress[0]);
                switch (cCode)
                {
                case 6:
                    if (deliveryAddress[0].Country != "New Zealand")
                    {
                        address = "International";
                    }
                    break;

                case 1:
                    if (deliveryAddress[0].Country != "Australia")
                    {
                        address = "International";
                    }
                    break;
                }
                for (int i = 0; i < orderDetails.Count; i++)
                {
                    if (orderDetails[i].Note != "")
                    {
                        note += orderDetails[i].Note;
                    }
                }
            }


            Response.Write("<input id='shopUrl' type='hidden' value='" + shop + "'>");                        //passing shopUrl to View() for further queries
            Response.Write("<input id='cpStrings' type='hidden' value='" + stringCP + "'>");                  //passing Custom Parcels if any to View() for further queries
            Response.Write("<input id='orderReference' type='hidden' value='" + stringOrderReference + "'>"); //passing order reference number if any to View() for further queries
            Response.Write("<input id='countryCode' type='hidden' value='" + cCode + "'>");                   //passing countryCode to View() for further queries
            Response.Write("<input id='orderDetails' type='hidden' value='" + orders + "'>");                 //passing orderIds to View() for further queries
            Response.Write("<input id='deliveryAddress' type='hidden' value='" + address + "'>");             //passing address to View() for further queries
            Response.Write("<input id='ordersAddresses' type='hidden' value='" + addresses + "'>");           //passing ordersdetails to View() for further queries
            Response.Write("<input id='orderIds' type='hidden' value='" + strOrderIds + "'>");                //passing ordersdetails to View() for further queries
            if (emails.Count >= 1)
            {
                Response.Write("<input id='emailAddress' type='hidden' value='" + string.Join(",", emails) + "'>");                   //passing email address
            }
            if (note != "")
            {
                Response.Write("<input id='specialInstruction' type='hidden' value='" + note + "'>");
            }
            return(View());
        }
Ejemplo n.º 4
0
        static void getTest()
        {
            string shopifyAPIKey      = Properties.Settings.Default.shopifyAPIKey;
            string shopifyAPIPassword = Properties.Settings.Default.shopifyAPIPassword;

            string shopifyStore = Properties.Settings.Default.shopifyStore;

            ShopifyAPI shopifyAPI = new ShopifyAPI(shopifyStore, shopifyAPIKey, shopifyAPIPassword);

            List <dynamic> dyn        = shopifyAPI.getOrders("");
            StreamWriter   swOrder    = new StreamWriter(@"c:\temp\orders.txt");
            StreamWriter   swOrderRaw = new StreamWriter(@"c:\temp\ordersRaw.txt");

            foreach (var x in dyn)
            {
                foreach (var n in x["orders"])
                {
                    swOrderRaw.WriteLine(n);
                    Console.WriteLine("id: " + n["id"].Value);
                    Console.WriteLine("email: " + n["email"].Value);
                    swOrder.WriteLine("id: " + n["id"].Value);
                    swOrder.WriteLine("email: " + n["email"].Value);
                }
            }
            swOrder.Close();
            swOrderRaw.Close();

            dyn = shopifyAPI.getProducts("");
            StreamWriter swProduct    = new StreamWriter(@"c:\temp\products.txt");
            StreamWriter swProductRaw = new StreamWriter(@"c:\temp\productsRaw.txt");

            foreach (var x in dyn)
            {
                foreach (var n in x["products"])
                {
                    swProductRaw.WriteLine(n);
                    Console.WriteLine("id: " + n["id"].Value);
                    Console.WriteLine("title: " + n["title"].Value);
                    swProduct.WriteLine("id: " + n["id"].Value);
                    swProduct.WriteLine("title: " + n["title"].Value);

                    foreach (var v in n["variants"])
                    {
                        Console.WriteLine("   variant title: " + v["title"].Value);
                        Console.WriteLine("   variant sku: " + v["sku"].Value);
                        Console.WriteLine("   variant barcode: " + v["barcode"].Value);
                        swProduct.WriteLine("   variant title: " + v["title"].Value);
                        swProduct.WriteLine("   variant sku: " + v["sku"].Value);
                        swProduct.WriteLine("   variant barcode: " + v["barcode"].Value);
                    }
                }
            }
            swProduct.Close();
            swProductRaw.Close();
            List <dynamic> dyn2 = shopifyAPI.getCustomers("");

            StreamWriter sw    = new StreamWriter(@"c:\temp\customers.txt");
            StreamWriter swRaw = new StreamWriter(@"c:\temp\customersRaw.txt");

            foreach (var x in dyn2)
            {
                foreach (var n in x["customers"])
                {
                    swRaw.WriteLine(n);
                    Console.WriteLine("Customer email : " + n["email"].Value);
                    Console.WriteLine("    First name : " + n["first_name"].Value);
                    Console.WriteLine("     Last name : " + n["last_name"].Value);
                    Console.WriteLine("            id : " + n["id"].Value);
                    sw.WriteLine("            id : " + n["id"].Value);
                    sw.WriteLine("Customer email : " + n["email"].Value);
                    sw.WriteLine("    First name : " + n["first_name"].Value);
                    sw.WriteLine("     Last name : " + n["last_name"].Value);
                    try
                    {
                        Console.WriteLine(n["invalid"].Value);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("got error");
                    }
                    try
                    {
                        Console.WriteLine(n["id"].Value);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error");
                    }
                }
            }


            sw.Close();
            swRaw.Close();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Listen to calls from shopify admin page, receive shop url and order ids. Parse orders and pass details to View()
        /// </summary>
        /// <param name="shop">shopUrl received from Shopify</param>
        /// <param name="ids">orderIds received from Shopify</param>
        /// <returns></returns>
        public async Task <ActionResult> NewConsignment(string shop, string[] ids)
        {
            //Get order numbers
            string orders = Request.QueryString["ids[]"];
            //required objects
            List <string>  orderIds        = new List <string>();  //list of orderIds
            List <Order>   orderDetails    = new List <Order>();   //list of order details
            List <Address> deliveryAddress = new List <Address>(); //list of delivery details
            List <string>  emails          = new List <string>();  //list of emails addresses

            if (orders == null)                                    //No order select (in case customer reach this page from outside of their admin page
            {
                return(View());                                    //NOTE: might need to redirect them to their admin page
            }

            if (orders.Contains(','))//if there are more than one order
            {
                //get a list of order numbers received
                orderIds = orders.Split(',').ToList();
            }
            else
            {
                //get a list of ONE order number
                orderIds.Add(orders);
            }
            //DB connection required to query store details
            DbEngine conn = new DbEngine();
            //Get Shopify Token to access Shopify API
            string token = conn.GetStringValues(shop, "ShopifyToken");
            int    cCode = conn.GetIntergerValues(shop, "CountryCode");
            //ShopifyAPI object
            ShopifyAPI api = new ShopifyAPI();

            //foreach order number from list, get a list of delivery details
            for (int i = 0; i < orderIds.Count; i++)
            {
                //get the order with order number
                Order k = await api.GetOrder(shop, token, orderIds[i]);

                if (k.ShippingAddress != null)
                {//if shipping address exist, add to list of delivery details
                    bool check = true;
                    if (deliveryAddress.Count > 0)
                    {
                        for (var l = 0; l < deliveryAddress.Count; l++)
                        {
                            if (deliveryAddress[l].Name == k.ShippingAddress.Name)
                            {
                                check = false;
                            }
                        }
                    }
                    if (check == true)
                    {
                        deliveryAddress.Add(k.ShippingAddress);
                        emails.Add(k.Email);
                    }
                }
                orderDetails.Add(k);//add order details into list of order details
            }

            ////jsonserialiser object to form json from list
            JavaScriptSerializer jsonSerialiser = new JavaScriptSerializer();
            ////creating json about orders to pass back to View()
            //string orderJson = jsonSerialiser.Serialize(orderDetails);


            //creating json about delivery address to pass back to View()
            string address = "";
            string note    = "";

            if (deliveryAddress.Count == 0)
            {//No delivery address found
                address = "NoAddress";
            }
            else if (deliveryAddress.Count > 1)
            {//More than one addresses found
                address = "MoreThanOne";
            }
            else
            {//one address
                address = jsonSerialiser.Serialize(deliveryAddress[0]);
                for (int i = 0; i < orderDetails.Count; i++)
                {
                    if (orderDetails[i].Note != "")
                    {
                        note += orderDetails[i].Note;
                    }
                }
            }


            Response.Write("<input id='shopUrl' type='hidden' value='" + shop + "'>");            //passing shopUrl to View() for further queries
            Response.Write("<input id='countryCode' type='hidden' value='" + cCode + "'>");       //passing countryCode to View() for further queries
            Response.Write("<input id='orderDetails' type='hidden' value='" + orders + "'>");     //passing orderIds to View() for further queries
            Response.Write("<input id='deliveryAddress' type='hidden' value='" + address + "'>"); //passing address to View() for further queries
            if (emails.Count >= 1)
            {
                Response.Write("<input id='emailAddress' type='hidden' value='" + emails[0] + "'>");                //passing email address
            }
            if (note != "")
            {
                Response.Write("<input id='specialInstruction' type='hidden' value='" + note + "'>");
            }
            return(View());
        }