Esempio n. 1
0
        public RateQueryResponse RateQuery(RateQueryRequest rqr)
        {
            // Validate the request

            List <ValidationResult> validationResult = Common.Validate(ref rqr);

            if (validationResult.Any())
            {
                throw new GloWSValidationException(validationResult);
            }

            LastJSONRequest  = JsonConvert.SerializeObject(rqr, Formatting.Indented);
            LastJSONResponse = SendRequestAndReceiveResponse(LastJSONRequest, "RateRequest");

            RateQueryResponse retval;

            try {
                // Deserialize the result back to an object.
                List <string> errors = new List <string>();

                retval = JsonConvert.DeserializeObject <RateQueryResponse>(LastJSONResponse, new JsonSerializerSettings()
                {
                    Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) {
                        errors.Add(args.ErrorContext.Error.Message);
                        args.ErrorContext.Handled = true;
                    }
                });
            }
            catch
            {
                retval = new RateQueryResponse();
            }

            return(retval);
        }
Esempio n. 2
0
        public RateQueryResponse RateQuery(string transcode, string reqtime, string systype, string username, string password, string openid,
                                           string ratePlanReservationActionType, string ratePlanCode, string timeUnitType, string startTime, string numberOfTimeUnits,
                                           string rateReservationActionType, string rateBasisTimeUnitType)
        {
            var obj = new RateQueryRequest()
            {
                Head = new Head()
                {
                    transcode = transcode,
                    reqtime   = reqtime,
                    systype   = systype,
                    username  = username,
                    password  = password,
                    openid    = openid,
                },
                Body = new RateQueryBody()
                {
                    RatePlans = new RatePlans()
                    {
                        RatePlan = new RatePlan()
                        {
                            reservationActionType = ratePlanReservationActionType,
                            ratePlanCode          = ratePlanCode,
                            TimeSpan = new Ks_Model.Requst.TimeSpan()
                            {
                                timeUnitType      = timeUnitType,
                                startTime         = startTime,
                                numberOfTimeUnits = numberOfTimeUnits,
                            },
                            Rates = new Ks_Model.Requst.Rates()
                            {
                                Rate = new Ks_Model.Requst.Rate()
                                {
                                    reservationActionType = rateReservationActionType,
                                    rateBasisTimeUnitType = rateBasisTimeUnitType,
                                }
                            }
                        }
                    }
                },
            };
            string            xmlObj = Common.SerializeToXml(obj, typeof(RateQueryRequest));
            var               res    = service.RateQuery(xmlObj);
            RateQueryResponse result = Common.Deserialize(typeof(RateQueryResponse), res) as RateQueryResponse;

            return(result);
        }
        private void BtnGo_Click(object sender, EventArgs e)
        {
            try
            {
                this.Enabled       = false;
                this.Cursor        = Cursors.WaitCursor;
                this.UseWaitCursor = true;

                MyDHLAPI api = new MyDHLAPI(Common.CurrentCredentials["Username"]
                                            , Common.CurrentCredentials["Password"]
                                            , Common.CurrentRestBaseUrl);

                RateQueryRequest rqr = new RateQueryRequest();
                RateRequest      rr  = new RateRequest
                {
                    /*** Request Header ***/
                    Request = new Request()
                    {
                        ServiceHeader = new ServiceHeader()
                        {
                            ShippingSystemPlatform        = "MyDHL API Test App",
                            ShippingSystemPlatformVersion = Application.ProductVersion,
                            Plugin        = "MyDHL API C# Library",
                            PluginVersion = api.GetVersion()
                        }
                    }
                };

                RequestedShipment rs = new RequestedShipment
                {
                    DropOffType                 = (cmbRequestCourier.SelectedIndex == 0 ? Enums.DropOffType.RequestCourier : Enums.DropOffType.RegularPickup)
                    , DeclaredValue             = ntxtDeclaredValue.Value
                    , DeclaredValueCurrencyCode = txtDeclaredCurrency.Text.Trim()
                    , NextBusinessDay           = Enums.YesNo.Yes
                    , ShipTimestamp             = DateTime.Now
                    , UnitOfMeasurement         = (cmbUOM.SelectedIndex == 0 ? Enums.UnitOfMeasurement.SI : Enums.UnitOfMeasurement.SU)
                    , PaymentInfo               = (Enums.TermsOfTrade)cmbTermsOfTrade.SelectedItem
                    , RequestValueAddedServices = (cbxShowAllServices.Checked ? Enums.YesNo.Yes : Enums.YesNo.No)
                    , Content = (cmbDutiable.SelectedIndex == 0 ? Enums.ShipmentType.Documents : Enums.ShipmentType.NonDocuments)
                };
                MyDHLAPI_REST_Library.Objects.RateQuery.Ship    s  = new MyDHLAPI_REST_Library.Objects.RateQuery.Ship();
                MyDHLAPI_REST_Library.Objects.RateQuery.Address sa = new MyDHLAPI_REST_Library.Objects.RateQuery.Address
                {
                    City          = txtShipperCity.Text.Trim()
                    , PostalCode  = txtShipperPostalCode.Text.Trim()
                    , USSateCode  = txtShipperUSState.Text.Trim()
                    , CountryCode = txtShipperCountry.Text.Trim()
                };
                s.Shipper = sa;
                MyDHLAPI_REST_Library.Objects.RateQuery.Address ra = new MyDHLAPI_REST_Library.Objects.RateQuery.Address
                {
                    City          = txtConsigneeCity.Text.Trim()
                    , PostalCode  = txtConsigneePostalCode.Text.Trim()
                    , USSateCode  = txtConsigneeUSState.Text.Trim()
                    , CountryCode = txtConsigneeCountry.Text.Trim()
                };
                s.Recipient = ra;
                rs.Ship     = s;
                // 961187381
                Billing b = new Billing
                {
                    ShipperAccountNumber   = txtAccountShipper.Text.Trim()
                    , BillingAccountNumber = txtAccountBilling.Text.Trim()
                    , ShippingPaymentType  = (rbtnPayShipper.Checked ? Enums.PaymentTypes.Shipper : Enums.PaymentTypes.Receiver)
                };
                rs.Billing = b;
                Packages p = new Packages();
                List <RequestedPackages> rps = new List <RequestedPackages>
                {
                    new RequestedPackages
                    {
                        PieceNumber = 1
                        , Weight    = new Weight {
                            Value = ntxtWeight.Value
                        }
                        , Dimensions = new Dimensions
                        {
                            Height   = ntxtHeight.Value
                            , Width  = ntxtWidth.Value
                            , Length = ntxtLength.Value
                        }
                    }
                };
                p.RequestedPackages = rps;
                rs.Packages         = p;

                if ((Enums.TermsOfTrade)cmbTermsOfTrade.SelectedItem
                    == Enums.TermsOfTrade.DDP)
                {
                    SpecialServices sss = new SpecialServices
                    {
                        Service = new List <SpecialService>()
                    };
                    sss.Service.Add(new SpecialService("DD"));
                    rs.SpecialServices = sss;
                }

                rs.GetDetailedRateBreakdown = Enums.YesNo.Yes;

                rr.RequestedShipment = rs;
                rqr.RateRequest      = rr;

                RateQueryResponse result = null;
                try
                {
                    result = api.RateQuery(rqr);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"{ex.Message}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _lastJsonRequest  = api.LastRateQueryJSONRequest;
                    _lastJsonResponse = api.LastRateQueryJSONResponse;
                    return;
                }

                _lastJsonRequest  = api.LastRateQueryJSONRequest;
                _lastJsonResponse = api.LastRateQueryJSONResponse;

                tvResult.Nodes.Clear();

                if (null == result.Services)
                {
                    if (null != result.RateResponse.Provider)
                    {
                        if (1 == result.RateResponse.Provider.Count)
                        {
                            Provider provider = result.RateResponse.Provider.First();
                            if (provider.Notification.Any())
                            {
                                foreach (Notification notificaiton in provider.Notification)
                                {
                                    tvResult.Nodes.Add(notificaiton.Code, notificaiton.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        // We got no results back
                        tvResult.Nodes.Add("NONE", "--NONE--");
                    }
                }
                else
                {
                    TreeNode servicesOkToShow         = new TreeNode("Show on website");
                    TreeNode servicesNeedingAgreement = new TreeNode("Pre-agreement required");
                    foreach (Service service in result.Services)
                    {
                        TreeNode tn = new TreeNode($"{service.ProductCode}: {service.TotalNet.Currency} {service.TotalNet.Amount:#,##0.00}");
                        tn.Nodes.Add($"Transit time: {service.TotalTransitDays} day(s)");
                        if (null != service.Charges)
                        {
                            foreach (Charge charge in service.Charges.Charge)
                            {
                                TreeNode ctn = null;
                                if (string.IsNullOrEmpty(charge.ChargeCode))
                                {
                                    ctn = new TreeNode($"{charge.ChargeType}");
                                }
                                else
                                {
                                    ctn = new TreeNode($"({charge.ChargeCode}) {charge.ChargeType}");
                                }

                                if (!string.IsNullOrEmpty(service.Charges.Currency) &&
                                    null != charge.ChargeAmount)
                                {
                                    TreeNode cctn =
                                        new TreeNode($"{service.Charges.Currency} {charge.ChargeAmount:#,##0.00}");
                                    ctn.Nodes.Add(cctn);
                                }
                                tn.Nodes.Add(ctn);
                            }
                        }

                        tn.ExpandAll();

                        if (service.CustomerAgreementInd == Enums.YesNo.No)
                        {
                            servicesOkToShow.Nodes.Add(tn);
                        }
                        else
                        {
                            servicesNeedingAgreement.Nodes.Add(tn);
                        }
                    }

                    servicesOkToShow.ExpandAll();
                    servicesNeedingAgreement.ExpandAll();

                    tvResult.Nodes.Add(servicesOkToShow);
                    tvResult.Nodes.Add(servicesNeedingAgreement);
                }

                tvResult.Nodes[0].EnsureVisible();
            }
            finally
            {
                this.Enabled       = true;
                this.Cursor        = Cursors.Default;
                this.UseWaitCursor = false;
            }
        }
Esempio n. 4
0
        private void BtnGo_Click(object sender, EventArgs e)
        {
            try
            {
                this.Enabled       = false;
                this.Cursor        = Cursors.WaitCursor;
                this.UseWaitCursor = true;

                GloWS glows = new GloWS(Common.username, Common.password, (Common.IsProduction ? Common.restProductionBaseUrl : Common.restTestingBaseUrl));

                RateQueryRequest  rqr = new RateQueryRequest();
                RateRequest       rr  = new RateRequest();
                RequestedShipment rs  = new RequestedShipment
                {
                    DropOffType                 = (cmbRequestCourier.SelectedIndex == 0 ? Enums.DropOffType.RequestCourier : Enums.DropOffType.RegularPickup)
                    , DeclaredValue             = ntxtDeclaredValue.Value
                    , DeclaredValueCurrencyCode = txtDeclaredCurrency.Text.Trim()
                    , NextBusinessDay           = Enums.YesNo.Yes
                    , ShipTimestamp             = DateTime.Now
                    , UnitOfMeasurement         = (cmbUOM.SelectedIndex == 0 ? Enums.UnitOfMeasurement.SI : Enums.UnitOfMeasurement.SU)
                    , PaymentInfo               = (Enums.TermsOfTrade)cmbTermsOfTrade.SelectedItem
                    , RequestValueAddedServices = (cbxShowAllServices.Checked ? Enums.YesNo.Yes : Enums.YesNo.No)
                    , Content = (cmbDutiable.SelectedIndex == 0 ? Enums.ShipmentType.Documents : Enums.ShipmentType.NonDocuments)
                };
                GloWS_REST_Library.Objects.RateQuery.Ship s = new GloWS_REST_Library.Objects.RateQuery.Ship();
                Address sa = new Address
                {
                    City          = txtShipperCity.Text.Trim()
                    , PostalCode  = txtShipperPostalCode.Text.Trim()
                    , USSateCode  = txtShipperUSState.Text.Trim()
                    , CountryCode = txtShipperCountry.Text.Trim()
                };
                s.Shipper = sa;
                Address ra = new Address
                {
                    City          = txtConsigneeCity.Text.Trim()
                    , PostalCode  = txtConsigneePostalCode.Text.Trim()
                    , USSateCode  = txtConsigneeUSState.Text.Trim()
                    , CountryCode = txtConsigneeCountry.Text.Trim()
                };
                s.Recipient = ra;
                rs.Ship     = s;
                // 961187381
                Billing b = new Billing
                {
                    ShipperAccountNumber   = txtAccountShipper.Text.Trim()
                    , BillingAccountNumber = txtAccountBilling.Text.Trim()
                    , ShippingPaymentType  = (rbtnPayShipper.Checked ? Enums.PaymentTypes.Shipper : Enums.PaymentTypes.Receiver)
                };
                rs.Billing = b;
                Packages p = new Packages();
                List <RequestedPackages> rps = new List <RequestedPackages>
                {
                    new RequestedPackages
                    {
                        PieceNumber = 1
                        , Weight    = new Weight {
                            Value = ntxtWeight.Value
                        }
                        , Dimensions = new Dimensions
                        {
                            Height   = ntxtHeight.Value
                            , Width  = ntxtWidth.Value
                            , Length = ntxtLength.Value
                        }
                    }
                };
                p.RequestedPackages = rps;
                rs.Packages         = p;

                if ((Enums.TermsOfTrade)cmbTermsOfTrade.SelectedItem
                    == Enums.TermsOfTrade.DDP)
                {
                    SpecialServices sss = new SpecialServices
                    {
                        Service = new List <SpecialService>()
                    };
                    sss.Service.Add(new SpecialService("DD"));
                    rs.SpecialServices = sss;
                }

                rr.RequestedShipment = rs;
                rqr.RateRequest      = rr;

                RateQueryResponse result = glows.RateQuery(rqr);

                _lastJsonRequest  = glows.LastJSONRequest;
                _lastJsonResponse = glows.LastJSONResponse;

                tvResult.Nodes.Clear();

                if (null == result.Services)
                {
                    if (null != result.RateResponse.Provider)
                    {
                        if (1 == result.RateResponse.Provider.Count)
                        {
                            Provider provider = result.RateResponse.Provider.First();
                            if (provider.Notification.Any())
                            {
                                foreach (Notification notificaiton in provider.Notification)
                                {
                                    tvResult.Nodes.Add(notificaiton.Code, notificaiton.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        // We got no results back
                        tvResult.Nodes.Add("NONE", "--NONE--");
                    }
                }
                else
                {
                    foreach (Service service in result.Services)
                    {
                        TreeNode tn = new TreeNode($"{service.ProductCode}: {service.TotalNet.Currency} {service.TotalNet.Amount:#,##0.00}");
                        if (null != service.Charges)
                        {
                            foreach (Charge charge in service.Charges.Charge)
                            {
                                TreeNode ctn =
                                    new TreeNode(
                                        $"{charge.ChargeType}");
                                TreeNode cctn =
                                    new TreeNode($"{service.Charges.Currency} {charge.ChargeAmount:#,##0.00}");
                                ctn.Nodes.Add(cctn);
                                tn.Nodes.Add(ctn);
                            }
                        }

                        tn.ExpandAll();
                        tvResult.Nodes.Add(tn);
                    }
                }

                tvResult.Nodes[0].EnsureVisible();
            }
            finally
            {
                this.Enabled       = true;
                this.Cursor        = Cursors.Default;
                this.UseWaitCursor = false;
            }
        }