public async Task <Shipment> GetRatesAsync(
        Shipment shipment,
        ServiceType serviceType             = ServiceType.DEFAULT,
        CancellationToken cancellationToken = default)
    {
        var client = new RatePortTypeClient(
            RatePortTypeClient.EndpointConfiguration.RateServicePort,
            _options.Url);

        try
        {
            var request = CreateRateRequest(shipment, serviceType);

            var serviceRequest = new getRatesRequest(request);

            var reply = await client.getRatesAsync(serviceRequest);

            if (reply.RateReply != null)
            {
                ProcessReply(reply.RateReply, shipment);
                ProcessErrors(reply.RateReply, shipment);
            }
            else
            {
                shipment.InternalErrors.Add($"FedEx provider: API returned NULL result");
            }
        }
        catch (Exception ex)
        {
            shipment.InternalErrors.Add($"FedEx provider exception: {ex.Message}");
        }

        return(shipment);
    }
Beispiel #2
0
        /// <summary>
        /// Gets rates
        /// </summary>
        public override async Task GetRates()
        {
            var request = CreateRateRequest();
            var service = new RatePortTypeClient(UseProduction);

            try
            {
                // Call the web service passing in a RateRequest and returning a RateReply
                var reply = await service.getRatesAsync(request).ConfigureAwait(false);

                if (reply.RateReply != null)
                {
                    ProcessReply(reply.RateReply);
                    ProcessErrors(reply.RateReply);
                }
                else
                {
                    AddInternalError($"FedEx provider: API returned NULL result");
                }
            }
            catch (Exception e)
            {
                AddInternalError($"FedEx provider exception: {e.Message}");
            }
        }
        public async Task <IActionResult> QuoteShipment(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "rating"), RequestBodyType(typeof(ShipQuoteRequest), "failed order")] HttpRequest req,
            ILogger log)
        {
            try
            {
                var jsonReq  = new StreamReader(req.Body).ReadToEnd();
                var reqBody  = JsonConvert.DeserializeObject <ShipQuoteRequest>(jsonReq);
                var rateType = reqBody.RateType.ToLower();
                log.LogInformation(@"Request body: {ReqBody}", reqBody);
                log.LogInformation(@"Rate type: {RateType}", rateType);

                if (!UPSRating.rateTypes.Contains(rateType))
                {
                    log.LogWarning("Invalid rate type.");

                    return(new BadRequestObjectResult("Invalid rateType")
                    {
                        Value = "rateType must be one of the following: " + string.Join(", ", UPSRating.rateTypes),
                        StatusCode = 404
                    });
                }

                var client   = new RatePortTypeClient();
                var security = UPSRequestHelper.CreateUPSSecurity();

                var upsRating = new UPSRating(rateType, reqBody.OriginAddress, reqBody.DestinationAddress, reqBody.Package);

                upsRating.rateResponse = await client.ProcessRateAsync(security, upsRating.rateRequest);

                log.LogInformation(@"UPS Response: {RateResponse}", upsRating.rateResponse);
                log.LogInformation($"Total ship cost: {upsRating.TotalShipCost}");

                return(new OkObjectResult(upsRating.TotalShipCost));
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                log.LogError(ex.StackTrace);

                var title        = "Exception in QuoteShipment";
                var text         = $"Error message: {ex.Message}. Stacktrace: {ex.StackTrace}";
                var teamsMessage = new TeamsMessage(title, text, "red", ratingErrorLogsUrl);
                teamsMessage.LogToTeams(teamsMessage);
                return(new StatusCodeResult(500));
            }
        }
Beispiel #4
0
        public ActionResult Index()
        {
            UPSSecurity upss = new UPSSecurity();


            UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();

            upssSvcAccessToken.AccessLicenseNumber = "3D6A1DD5F39023B5";
            upss.ServiceAccessToken = upssSvcAccessToken;
            UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();

            upssUsrNameToken.Username = "******";
            upssUsrNameToken.Password = "******";
            upss.UsernameToken        = upssUsrNameToken;

            RateRequest rateRequest = new RateRequest();

            RequestType request = new RequestType();

            String[] requestOption = { "Shoptimeintransit" };
            request.RequestOption = requestOption;
            rateRequest.Request   = request;


            ShipmentType shipment = new ShipmentType();

            TimeInTransitRequestType Time_Tran = new TimeInTransitRequestType();
            var packbillcode = "03";

            Time_Tran.PackageBillType        = packbillcode;
            shipment.DeliveryTimeInformation = Time_Tran;

            ShipperType shipper = new ShipperType();

            // shipper.ShipperNumber = "Your Shipper Number";

            var shipperAddress = new AddressType();

            // String[] addressLine = { "5555 main", "4 Case Cour", "Apt 3B" };
            //shipperAddress.AddressLine = addressLine;
            shipperAddress.City              = "San Diego";
            shipperAddress.PostalCode        = "92101";
            shipperAddress.StateProvinceCode = "CA";
            shipperAddress.CountryCode       = "US";
            // shipperAddress.AddressLine = addressLine;
            shipper.Address  = shipperAddress;
            shipment.Shipper = shipper;
            ShipFromType shipFrom        = new ShipFromType();
            var          shipFromAddress = new ShipAddressType();

            //shipFromAddress.AddressLine = addressLine;
            shipFromAddress.City              = "San Diego";
            shipFromAddress.PostalCode        = "92101";
            shipFromAddress.StateProvinceCode = "CA";
            shipFromAddress.CountryCode       = "US";
            shipFrom.Address  = shipFromAddress;
            shipment.ShipFrom = shipFrom;


            ShipToType        shipTo        = new ShipToType();
            ShipToAddressType shipToAddress = new ShipToAddressType();

            //String[] addressLine1 = { "10 E. Ritchie Way", "2", "Apt 3B" };
            //shipToAddress.AddressLine = addressLine1;
            shipToAddress.City              = "Canton";
            shipToAddress.PostalCode        = "02021";
            shipToAddress.StateProvinceCode = "MA";
            shipToAddress.CountryCode       = "US";
            shipTo.Address  = shipToAddress;
            shipment.ShipTo = shipTo;

            CodeDescriptionType service = new CodeDescriptionType();

            //Below code uses dummy date for reference. Please udpate as required.
            service.Code     = "02";
            shipment.Service = service;

            PackageType       package       = new PackageType();
            PackageWeightType packageWeight = new PackageWeightType();

            packageWeight.Weight = "1";
            CodeDescriptionType uom = new CodeDescriptionType();

            uom.Code        = "LBS";
            uom.Description = "pounds";
            packageWeight.UnitOfMeasurement = uom;
            package.PackageWeight           = packageWeight;
            CodeDescriptionType packType = new CodeDescriptionType();

            packType.Code         = "02";
            package.PackagingType = packType;
            PackageType[] pkgArray = { package };
            shipment.Package     = pkgArray;
            rateRequest.Shipment = shipment;
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11; //This line will ensure the latest security protocol for consuming the web service call.
            // Console.WriteLine(rateRequest);
            var          client       = new RatePortTypeClient();
            RateResponse rateResponse = client.ProcessRate(upss, rateRequest);

            var model = new Rate_Package()
            {
                Response = rateResponse
            };

            return(View(model));
        }
Beispiel #5
0
        private void RequestRate(UPSSecurity upss, RatePackage rPackage)
        {
            RateRequest rateRequest = new RateRequest();

            UPSRateRef.RequestType request = new UPSRateRef.RequestType();
            String[] requestOption         = { "Shop" };
            request.RequestOption = requestOption;
            rateRequest.Request   = request;

            ShipmentType shipment = new ShipmentType();

            ShipperType shipper        = new ShipperType();
            AddressType shipperAddress = new AddressType();

            shipperAddress.City        = SHIPFROM_CITY;
            shipperAddress.PostalCode  = SHIPFROM_POSTALCODE;
            shipperAddress.CountryCode = SHIPFROM_COUNTRYCODE;
            shipper.Address            = shipperAddress;
            shipment.Shipper           = shipper;

            ShipFromType    shipFrom        = new ShipFromType();
            ShipAddressType shipFromAddress = new ShipAddressType();

            shipFromAddress.City        = SHIPFROM_CITY;
            shipFromAddress.PostalCode  = SHIPFROM_POSTALCODE;
            shipFromAddress.CountryCode = SHIPFROM_COUNTRYCODE;
            shipFrom.Address            = shipFromAddress;
            shipment.ShipFrom           = shipFrom;

            ShipToType        shipTo        = new ShipToType();
            ShipToAddressType shipToAddress = new ShipToAddressType();

            shipToAddress.City        = SHIPTO_CITY;
            shipToAddress.PostalCode  = SHIPTO_POSTALCODE;
            shipToAddress.CountryCode = SHIPTO_COUNTRYCODE;
            shipTo.Address            = shipToAddress;
            shipment.ShipTo           = shipTo;

            PackageType       package       = new PackageType();
            PackageWeightType packageWeight = new PackageWeightType();

            packageWeight.Weight = WEIGHT;
            UPSRateRef.CodeDescriptionType uom = new UPSRateRef.CodeDescriptionType();
            uom.Code = MESUREMENT;
            //uom.Description = MESUREMENT_DISCRIPTION;
            packageWeight.UnitOfMeasurement = uom;
            package.PackageWeight           = packageWeight;
            UPSRateRef.CodeDescriptionType packType = new UPSRateRef.CodeDescriptionType();
            packType.Code         = "02";
            package.PackagingType = packType;
            PackageType[] pkgArray = { package };
            shipment.Package = pkgArray;

            rateRequest.Shipment = shipment;

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11; //This line will ensure the latest security protocol for consuming the web service call.

            RatePortTypeClient client       = new RatePortTypeClient();
            RateResponse       rateResponse = client.ProcessRate(upss, rateRequest);


            foreach (var item in rateResponse.RatedShipment)
            {
                item.Service.Description = ServiceCode.GetName(item.Service.Code);
            }

            rPackage.rateResponse = rateResponse;
        }