Ejemplo n.º 1
0
        static async Task <Address_EP> GetAddressByIdAsync(string path)
        {
            Address_EP          address  = null;
            HttpResponseMessage response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                address = await response.Content.ReadAsAsync <Address_EP>();

                //address = await response.Content.ReadAsStringAsync();
            }
            return(address);
        }
Ejemplo n.º 2
0
        protected async void btnSubmitShipping_Click(object sender, EventArgs e)
        {
            HiddenField hdn = Page.FindControl("hdnToAddress") as HiddenField;
            string      serializedAddress = hdn.Value;

            var        serializer = new JavaScriptSerializer();
            Address_EP toAddress  = serializer.Deserialize <Address_EP>(serializedAddress);

            var    task  = Helpers.GetShippingLabelURL(AbsenceId, toAddress);
            string label = await task;

            Response.Redirect(Request.RawUrl);
            //ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "ResumeExistingShippingLabelWorkflowFunctionality()", true);
        }
Ejemplo n.º 3
0
        private static async Task <string> GetShippingLabelFromEasyPostRestApi(string absenceId, Address_EP myToAddress)
        {
            string shippingLabelUrl;
            string API_Key = GlobalVars.GetEasyPostApiKey();

            try
            {
                Address_EP toAddress = new Address_EP(myToAddress.Id, myToAddress.Company, myToAddress.Street1, myToAddress.Street2, myToAddress.City, myToAddress.State, myToAddress.Country, myToAddress.Zip, myToAddress.Phone);
                shippingLabelUrl = await EasyPostService.GetShippingLabelURL(absenceId, toAddress);

                return(shippingLabelUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 4
0
        public static async Task Main(string[] args)
        {
            Address_EP address = new Address_EP
            {
                Company = "JUSTIN ROYER",
                Street1 = "1905 ANNIN STREET",
                City    = "PHILADELPHIA",
                State   = "PA",
                Zip     = "19146",
                Country = "US",
                Phone   = "111-222-3333"
            };
            string url = await G2G_LIB.EasyPostService.GetShippingLabelURL("asdf", address);

            Console.WriteLine(url);
            Console.ReadLine();

            //G2G_LIB.EasyPostService.Main();
        }
Ejemplo n.º 5
0
        static async Task <Uri> CreateAddressAsync(Address_EP newAddress)
        {
            string mystring = newAddress.FormattedParameterString();
            HttpResponseMessage response = await client.PostAsync("addresses?" + mystring, null);

            response.EnsureSuccessStatusCode();

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var address = await response.Content.ReadAsAsync <Address_EP>();

                //var address = response.Content.ReadAsStringAsync().Result;
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            // return URI of the created resource.
            return(response.Headers.Location);
        }
Ejemplo n.º 6
0
 public static async Task <string> GetShippingLabelURL(string absenceId, Address_EP myToAddress)
 {
     return(await GetShippingLabelFromEasyPostRestApi(absenceId, myToAddress));
 }
Ejemplo n.º 7
0
        public static async Task <string> GetShippingLabelURL(string absenceId, Address_EP myToAddress)
        {
            string shippingLabelUrl = "";

            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                Address_EP  toAddress;
                Address_EP  fromAddress;
                Parcel_EP   parcel;
                Shipment_EP shipment;

                absenceId = Helpers.ParseStringToRemoveBrackets(absenceId);
                Absence _absenceService = new Absence();

                string username = GlobalVars.GetEasyPostApiKey();
                string password = "";

                string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
                if (client.BaseAddress != null)
                {
                    client.Dispose();
                    client = new HttpClient();
                }
                client.BaseAddress = new Uri("https://api.easypost.com/v2/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", svcCredentials);

                //CREATE TO ADDRESS
                try
                {
                    Uri url = await CreateAddressAsync(myToAddress);

                    string addressId = url.OriginalString.Remove(0, url.OriginalString.IndexOf("addresses"));

                    toAddress = await GetAddressByIdAsync(addressId);
                }
                catch (Exception ex)
                {
                    client.Dispose();
                    Exception e = new Exception(ex.Message + " / Failed to create To Address");
                    throw e;
                }

                //CREATE FROM ADDRESS
                try
                {
                    Address_EP address = new Address_EP
                    {
                        Company = "DEALSHIELD RETURNS DEPARTMENT",
                        Street1 = "2002 SUMMIT BLVD",
                        Street2 = "STE 1000",
                        City    = "BROOKHAVEN",
                        State   = "GA",
                        Zip     = "30319",
                        Country = "US",
                        Phone   = "855-246-5556"
                    };

                    var url = await CreateAddressAsync(address);

                    string addressId = url.OriginalString.Remove(0, url.OriginalString.IndexOf("addresses"));

                    fromAddress = await GetAddressByIdAsync(addressId);
                }
                catch (Exception ex)
                {
                    client.Dispose();
                    Exception e = new Exception(ex.Message + " / Failed to create From Address");
                    throw e;
                }

                //CREATE PARCEL
                try
                {
                    Parcel_EP newParcel = new Parcel_EP()
                    {
                        Weight             = "3",
                        Predefined_Package = "FedExEnvelope"
                    };

                    //FOR TESTING
                    //Parcel_EP newParcel = new Parcel_EP()
                    //{
                    //    Weight = "3",
                    //    Width = "8",
                    //    Length = "11",
                    //    Height = ".25"
                    //};

                    var url = await CreateParcelAsync(newParcel);

                    string parcelId = url.OriginalString.Remove(0, url.OriginalString.IndexOf("parcels"));

                    parcel = await GetParcelByIdAsync(parcelId);
                }
                catch (Exception ex)
                {
                    client.Dispose();
                    Exception e = new Exception(ex.Message + " / Failed to create Parcel");
                    throw e;
                }

                //CREATE SHIPMENT
                try
                {
                    Dictionary <string, object> options = new Dictionary <string, object>();
                    options.Add("label_format", "PDF");

                    Shipment_EP newShipment = new Shipment_EP()
                    {
                        Parcel       = parcel,
                        To_Address   = toAddress,
                        From_Address = fromAddress,
                        Reference    = "ShipmentRef",
                        Options      = options
                    };

                    var url = await CreateShipmentAsync(newShipment);

                    string shipmentId = url.OriginalString.Remove(0, url.OriginalString.IndexOf("shipments"));

                    shipment = await GetShipmentByIdAsync(shipmentId);

                    if (shipment != null && shipment.Rates.Count > 0)
                    {
                        //search shipment for cheapest rate
                        ShipmentRate_EP cheapestRate = shipment.GetLowestRate("FedEx", "STANDARD_OVERNIGHT");
                        if (cheapestRate != null)
                        {
                            try
                            {
                                //buy rate
                                Shipment_EP puchasedShipment = await BuyShipmentRate(shipment.Id, cheapestRate);

                                shippingLabelUrl = puchasedShipment.Postage_Label.Label_Url;

                                client.Dispose();

                                _absenceService.UpdateDisbursementShippingLabel(shippingLabelUrl, absenceId);
                            }
                            catch (Exception ex)
                            {
                                client.Dispose();
                                Exception e = new Exception(ex.Message + " / Failed to buy Postage Label");
                                throw e;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    client.Dispose();
                    Exception e = new Exception(ex.Message + " / Failed to create Shipment");
                    throw e;
                }
            }
            catch (Exception ex)
            {
                client.Dispose();
                Exception e = new Exception(ex.Message + " / Something went wrong.");
                throw e;
            }
            finally
            {
                client.Dispose();
            }

            client.Dispose();
            return(shippingLabelUrl);
        }