Пример #1
0
        public ZkSuccess GetApiCallSuccess(string jsonResponse)
        {
            ZkSuccess zkSuccess = new ZkSuccess();

            zkSuccess.success       = false;
            zkSuccess.error_message = jsonResponse;
            zkSuccess.detail        = string.Empty;

            try
            {
                JObject responseObj = JObject.Parse(jsonResponse);
                if (responseObj["error"] == null)
                {
                    zkSuccess.success       = true;
                    zkSuccess.error_message = null;
                    zkSuccess.detail        = null;
                }
                else
                {
                    string errorMessage = responseObj["error"]["message"] != null? responseObj["error"]["message"].ToString(): string.Empty;
                    string errorDetail  = responseObj["error"]["detail"] != null ? responseObj["error"]["detail"].ToString() : string.Empty;
                    zkSuccess.error_message = errorMessage;
                    zkSuccess.detail        = errorDetail;
                }
            }
            catch (Exception)
            {
            }

            return(zkSuccess);
        }
Пример #2
0
        public ShipmentStatus TrackShipment(string trackingNumber, CarrierAccount carrierAccount)
        {
            ShipmentStatus shipmentStatus = null;

            try
            {
                dynamic dataObject = new ExpandoObject();
                dynamic track      = new ExpandoObject();
                track.shipping_account = carrierAccount.id;
                track.carrier          = carrierAccount.carrier;
                track.tracking_number  = trackingNumber;
                track.test             = Test;

                dataObject.track = track;
                string payload      = JsonConvert.SerializeObject(dataObject);
                string jsonResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.POST, "/track", payload);

                ZkSuccess zkSuccess = GetApiCallSuccess(jsonResponse);
                if (zkSuccess.success == true)
                {
                    shipmentStatus = JsonConvert.DeserializeObject <ShipmentStatus>(jsonResponse);
                }
                else
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(shipmentStatus);
        }
Пример #3
0
        public CarrierDetail GetCarrierDetails(string carrierName)
        {
            CarrierDetail carrierDetail = null;

            try
            {
                string jsonResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.GET, $"/meta/carrier/{carrierName}");

                ZkSuccess zkSuccess = GetApiCallSuccess(jsonResponse);
                if (zkSuccess.success == true)
                {
                    carrierDetail = JsonConvert.DeserializeObject <CarrierDetail>(jsonResponse);
                }
                else
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(carrierDetail);
        }
Пример #4
0
        public string GetCarriers()
        {
            string carriersResult = string.Empty;

            try
            {
                carriersResult = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.GET, "/meta/carriers");
                ZkSuccess zkSuccess = GetApiCallSuccess(carriersResult);
                if (zkSuccess.success == false)
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(carriersResult);
        }
Пример #5
0
        public CarrierAccount AddCarrierAccount(string carrierJson)
        {
            CarrierAccount carrierAccount = null;

            try
            {
                string jsonResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.POST, "/shippingaccount", carrierJson);

                if (!string.IsNullOrEmpty(jsonResponse))
                {
                    ZkSuccess zkSuccess = GetApiCallSuccess(jsonResponse);
                    if (zkSuccess.success == true)
                    {
                        JObject dataObj           = JObject.Parse(jsonResponse);
                        JObject carrierAccountObj = JObject.Parse(dataObj["shipping_account"].ToString());
                        carrierAccount              = new CarrierAccount();
                        carrierAccount.auth         = carrierAccountObj["auth"].ToString();
                        carrierAccount.name         = carrierAccountObj["name"].ToString();
                        carrierAccount.id           = Int64.Parse(carrierAccountObj["id"].ToString());
                        carrierAccount.carrier      = carrierAccountObj["carrier"].ToString();
                        carrierAccount.dim_units    = carrierAccountObj["dim_units"].ToString();
                        carrierAccount.weight_units = carrierAccountObj["weight_units"].ToString();
                        carrierAccount.currency     = carrierAccountObj["currency"].ToString();
                    }
                    else
                    {
                        throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(carrierAccount);
        }
Пример #6
0
        public Pickup Pickup(Pickup pickup)
        {
            Pickup pickupResult = null;

            try
            {
                JObject dataObj = new JObject();
                dataObj["pickup"] = JObject.FromObject(pickup);

                List <Shipment> shipments   = pickup.shipments;
                JArray          shipmentArr = new JArray();
                foreach (Shipment shipment in shipments)
                {
                    dynamic shipmentObj = new ExpandoObject();

                    Address recipient    = shipment.recipient;
                    dynamic recipientObj = new ExpandoObject();
                    recipientObj.street1     = recipient.street1;
                    recipientObj.city        = recipient.city;
                    recipientObj.state       = recipient.state;
                    recipientObj.postal_code = recipient.postal_code;
                    recipientObj.country     = recipient.country;
                    recipientObj.company     = recipient.company;
                    recipientObj.phone       = recipient.phone;
                    recipientObj.name        = recipient.name;
                    recipientObj.email       = recipient.email;


                    shipmentObj.recipient = recipientObj;

                    JArray         packagesArr = new JArray();
                    List <Package> packages    = shipment.packages;
                    foreach (Package package in packages)
                    {
                        dynamic packageObj = new ExpandoObject();
                        packageObj.weight = (int)package.weight;
                        packageObj.value  = (int)package.value;
                        packageObj.length = package.length;
                        packageObj.width  = package.width;
                        packageObj.height = package.height;

                        packagesArr.Add(JObject.FromObject(packageObj));
                    }

                    shipmentObj.packages = packagesArr;
                    shipmentArr.Add(JObject.FromObject(shipmentObj));
                }

                dataObj["pickup"]["shipments"] = shipmentArr;



                string jsonObj      = JsonConvert.SerializeObject(dataObj);
                string jsonResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.POST, "/pickup", jsonObj);

                ZkSuccess zkSuccess = GetApiCallSuccess(jsonResponse);
                if (zkSuccess.success == true)
                {
                    JObject responseObj = JObject.Parse(jsonResponse);
                    pickupResult = JsonConvert.DeserializeObject <Pickup>(responseObj["pickup"].ToString());
                }
                else
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(pickupResult);
        }
Пример #7
0
        public Shipment Ship(Shipment shipment)
        {
            Shipment submittedShipment = null;

            try
            {
                dynamic dataObject = new ExpandoObject();

                //Shipment details


                dynamic shipmentObj = new ExpandoObject();
                shipmentObj.carrier          = shipment.carrier;
                shipmentObj.type             = shipment.type;
                shipmentObj.test             = Test;
                shipmentObj.debug            = shipment.debug;
                shipmentObj.dim_units        = shipment.dim_units;
                shipmentObj.weight_units     = shipment.weight_units;
                shipmentObj.currency         = shipment.currency;
                shipmentObj.shipping_account = shipment.shipping_account;
                shipmentObj.service          = shipment.service;
                shipmentObj.ship_date        = shipment.ship_date.ToString("yyyy-MM-dd");
                shipmentObj.label_type       = shipment.label_type;
                shipmentObj.packaging        = shipment.packaging;



                //sender
                Address sender = shipment.sender;

                dynamic senderObj = new ExpandoObject();
                senderObj.street1     = sender.street1;
                senderObj.city        = sender.city;
                senderObj.state       = sender.state;
                senderObj.postal_code = sender.postal_code;
                senderObj.country     = sender.country;
                senderObj.company     = sender.company;
                senderObj.phone       = sender.phone;
                senderObj.name        = sender.name;
                senderObj.email       = sender.email;

                //recipient
                Address recipient = shipment.recipient;

                dynamic recipientObj = new ExpandoObject();
                recipientObj.street1     = recipient.street1;
                recipientObj.city        = recipient.city;
                recipientObj.state       = recipient.state;
                recipientObj.postal_code = recipient.postal_code;
                recipientObj.country     = recipient.country;
                recipientObj.company     = recipient.company;
                recipientObj.phone       = recipient.phone;
                recipientObj.name        = recipient.name;
                recipientObj.email       = recipient.email;



                JArray packagesArray = new JArray();
                if (shipment.packages != null)
                {
                    if (shipment.packages.Count == 0)
                    {
                        throw new Exception("No packages were found for this shipment.");
                    }
                    else
                    {
                        foreach (Package package in shipment.packages)
                        {
                            dynamic packageObj = new ExpandoObject();
                            packageObj.weight = package.weight;
                            packageObj.value  = package.value;
                            packageObj.length = package.length;
                            packageObj.width  = package.width;
                            packageObj.height = package.height;
                            //packageObj.description = package.description;
                            packageObj.carrier_specific = new JArray();

                            if (!string.IsNullOrEmpty(package.carrier_specific))
                            {
                                packageObj.carrier_specific = JArray.Parse(package.carrier_specific);
                            }

                            packagesArray.Add(JObject.FromObject(packageObj));
                        }
                    }
                }
                else
                {
                    throw new Exception("No packages were found for this shipment.");
                }

                JArray referencesArray = new JArray();

                if (shipment.references != null)
                {
                    foreach (ZkField reference in shipment.references)
                    {
                        dynamic referenceObj = new ExpandoObject();
                        referenceObj.type  = reference.type;
                        referenceObj.value = reference.value;

                        referencesArray.Add(JObject.FromObject(referenceObj));
                    }
                }

                shipmentObj.sender     = senderObj;
                shipmentObj.recipient  = recipientObj;
                shipmentObj.packages   = packagesArray;
                shipmentObj.references = referencesArray;

                JObject shipmentJObj = JObject.FromObject(shipmentObj);

                if (!string.IsNullOrEmpty(shipment.additional_fields))
                {
                    JObject shipmentExtraFields = JObject.Parse(shipment.additional_fields);
                    foreach (var x in shipmentExtraFields.PropertyValues())
                    {
                        string propertyName = x.Path;
                        shipmentJObj[propertyName] = shipmentExtraFields[propertyName];
                    }
                }

                dataObject.shipment = shipmentJObj;

                string jsonObj      = JsonConvert.SerializeObject(dataObject);
                string jsonResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.POST, "/ship", jsonObj);


                ZkSuccess zkSuccess = GetApiCallSuccess(jsonResponse);
                if (zkSuccess.success == true)
                {
                    JObject responseObj = JObject.Parse(jsonResponse);
                    if (responseObj["shipment"] != null)
                    {
                        submittedShipment = JsonConvert.DeserializeObject <Shipment>(responseObj["shipment"].ToString());
                    }
                }
                else
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(submittedShipment);
        }
Пример #8
0
        public string GetRates(Shipment shipment)
        {
            string ratesResponse = string.Empty;

            try
            {
                dynamic dataObject = new ExpandoObject();

                //Shipment details


                dynamic shipmentObj = new ExpandoObject();
                shipmentObj.carrier          = shipment.carrier;
                shipmentObj.type             = shipment.type;
                shipmentObj.test             = Test;
                shipmentObj.dim_units        = shipment.dim_units;
                shipmentObj.weight_units     = shipment.weight_units;
                shipmentObj.currency         = shipment.currency;
                shipmentObj.shipping_account = shipment.shipping_account;

                //sender
                Address sender = shipment.sender;

                dynamic senderObj = new ExpandoObject();
                senderObj.street1     = sender.street1;
                senderObj.city        = sender.city;
                senderObj.state       = sender.state;
                senderObj.postal_code = sender.postal_code;
                senderObj.country     = sender.country;

                //recipient
                Address recipient = shipment.recipient;

                dynamic recipientObj = new ExpandoObject();
                recipientObj.street1     = recipient.street1;
                recipientObj.city        = recipient.city;
                recipientObj.state       = recipient.state;
                recipientObj.postal_code = recipient.postal_code;
                recipientObj.country     = recipient.country;

                JArray packagesArray = new JArray();
                if (shipment.packages != null)
                {
                    if (shipment.packages.Count == 0)
                    {
                        throw new Exception("No packages were found for this shipment.");
                    }
                    else
                    {
                        foreach (Package packageObj in shipment.packages)
                        {
                            dynamic package = new ExpandoObject();
                            package.weight = packageObj.weight;
                            package.value  = packageObj.value;
                            package.length = packageObj.length;
                            package.width  = packageObj.width;
                            package.height = packageObj.height;
                            packagesArray.Add(JObject.FromObject(package));
                        }
                    }
                }
                else
                {
                    throw new Exception("No packages were found for this shipment.");
                }

                shipmentObj.sender    = senderObj;
                shipmentObj.recipient = recipientObj;
                shipmentObj.packages  = packagesArray;

                dataObject.shipment = shipmentObj;

                string jsonObj = JsonConvert.SerializeObject(dataObject);
                ratesResponse = DataLayer.getResponseFromApi(ApiKey, DataLayer.WebMethod.POST, "/rate", jsonObj);

                ZkSuccess zkSuccess = GetApiCallSuccess(ratesResponse);
                if (zkSuccess.success == false)
                {
                    throw new Exception(zkSuccess.error_message + " " + zkSuccess.detail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(ratesResponse);
        }