コード例 #1
0
 public static RatesArrayFluent <T> RateFromRule <T>(this RatesArrayFluent <T> f, CarrierRule carrierRule, ServiceRule serviceRule, ParcelTypeRule parcelTypeRule)
     where T : class, IRates, new()
 {
     return(f
            .Carrier(carrierRule.Carrier)
            .ParcelType(parcelTypeRule.ParcelType)
            .Service(serviceRule.ServiceId));
 }
コード例 #2
0
        static void Main(string[] args)
        {
            var sandbox = new Session()
            {
                EndPoint = "https://api-sandbox.pitneybowes.com", Requester = new ShippingApiHttpRequest()
            };

            sandbox.AddConfigItem("ApiKey", "your api key");
            sandbox.AddConfigItem("ApiSecret", "your api secret");
            sandbox.AddConfigItem("ShipperID", "your shipper id");
            sandbox.AddConfigItem("DeveloperID", "your developer id");

            Model.RegisterSerializationTypes(sandbox.SerializationRegistry);
            Globals.DefaultSession = sandbox;

            var shipment = ShipmentFluent <Shipment> .Create()
                           .ToAddress((Address)AddressFluent <Address> .Create()
                                      .AddressLines("643 Greenway Rd")
                                      .PostalCode("28607")
                                      .CountryCode("US")
                                      .Verify())
                           .FromAddress((Address)AddressFluent <Address> .Create()
                                        .Company("Pitney Bowes Inc")
                                        .AddressLines("27 Waterview Drive")
                                        .CityTown("Shelton").StateProvince("CT").PostalCode("06484")
                                        .CountryCode("US")
                                        )
                           .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                   .Dimension(12, 12, 10)
                                   .Weight(16m, UnitOfWeight.OZ))
                           .Rates(RatesArrayFluent <Rates> .Create()
                                  .USPSPriority <Rates, Parameter>())
                           .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                      .ShippingLabel(ContentType.URL, Size.DOC_4X6, FileFormat.PDF))
                           .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                            .ShipperId("your shipper id") // ******* dont forget this one too *******
                                            )
                           .TransactionId(Guid.NewGuid().ToString().Substring(15));

            var label = Api.CreateShipment((Shipment)shipment).GetAwaiter().GetResult();

            if (label.Success)
            {
                var sw = new StreamWriter("label.pdf");
                foreach (var d in label.APIResponse.Documents)
                {
                    Api.WriteToStream(d, sw.BaseStream).GetAwaiter().GetResult();
                }
            }
        }
コード例 #3
0
        public static RatesArrayFluent <T> SuggestedTrackingServiceFromRule <T, S>(this RatesArrayFluent <T> f, ParcelTypeRule parcelTypeRule)
            where T : class, IRates, new()
            where S : class, ISpecialServices, new()
        {
            var r = f.Current();

            foreach (var s in r.SpecialServices)
            {
                if (s.SpecialServiceId == parcelTypeRule.SuggestedTrackableSpecialServiceId)
                {
                    return(f);
                }
            }
            var trackingService = new S
            {
                SpecialServiceId = parcelTypeRule.SuggestedTrackableSpecialServiceId,
                InputParameters  = new List <Parameter>()
            };

            trackingService.AddParameter(new Parameter("INPUT_VALUE", "0"));
            r.AddSpecialservices(trackingService);
            return(f);
        }
        static Shipment print(string s, int i, int j, ISession session)
        {
            var shipment = (Shipment)ShipmentFluent <Shipment> .Create()
                           .ToAddress((Address)AddressFluent <Address> .Create()
                                      .AddressLines("643 Greenway Rd")
                                      .PostalCode("28607")
                                      .CountryCode("US"))
                           .FromAddress((Address)AddressFluent <Address> .Create()
                                        .Company("Pitney Bowes Inc")
                                        .AddressLines("27 Waterview Drive")
                                        .CityTown("Shelton").StateProvince("CT").PostalCode("06484")
                                        .CountryCode("US")
                                        )
                           .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                   .Dimension(12, 12, 10)
                                   .Weight(16m, UnitOfWeight.OZ))
                           .Rates(RatesArrayFluent <Rates> .Create()
                                  .USPSPriority <Rates, Parameter>())
                           .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                      .ShippingLabel(ContentType.URL, Size.DOC_4X6, FileFormat.PDF))
                           .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                            .ShipperId(session.GetConfigItem("ShipperID"))
                                            )
                           .TransactionId(string.Format("{0}{1}{2}", s, i, j));

            var label = Api.CreateShipment(shipment, session).GetAwaiter().GetResult();

            if (!label.Success)
            {
                Console.WriteLine("Label {0} {1} failed:{1}", i, string.Format("{0}-{1}-{2}", s, i, j, label.HttpStatus));
                foreach (var e in label.Errors)
                {
                    Console.WriteLine("    {0} {1}", e.ErrorCode, e.Message);
                }
            }
            return(label.APIResponse);
        }
コード例 #5
0
        public static RatesArrayFluent <T> SpecialServiceFromRule <T, S>(this RatesArrayFluent <T> f, SpecialServicesRule rule)
            where T : class, IRates, new()
            where S : class, ISpecialServices, new()
        {
            var ss = new S
            {
                SpecialServiceId = rule.SpecialServiceId,
                InputParameters  = new List <Parameter>()
            };

            if (rule.InputParameterRules == null)
            {
                ss.AddParameter(new Parameter("INPUT_VALUE", "0"));
            }
            else
            {
                foreach (var ip in rule.InputParameterRules.Values)
                {
                    ss.AddParameter(new Parameter(ip.Name, ip.MinValue.ToString()));
                }
            }
            f.SpecialService <S>(ss);
            return(f);
        }
        public void TestShipmentDetailed()
        {
            var shipment = ShipmentFluent <Shipment> .Create()
                           .ToAddress((Address)AddressFluent <Address> .Create()
                                      .AddressLines("643 Greenway RD")
                                      .PostalCode("28607")
                                      .CountryCode("US")
                                      .Verify() // calls the service for address validation - will populate city and state from the zip
                                      )
                           .MinimalAddressValidation("true")
                           //.ShipperRatePlan(Globals.DefaultSession.GetConfigItem("RatePlan")) // use if you have a custom rate plan
                           .FromAddress((Address)AddressFluent <Address> .Create()
                                        .Company("Pitney Bowes Inc.")
                                        .AddressLines("27 Waterview Drive")
                                        .Residential(false)
                                        .CityTown("Shelton")
                                        .StateProvince("CT")
                                        .PostalCode("06484")
                                        .CountryCode("US")
                                        .Person("Paul Wright", "203-555-1213", "*****@*****.**")
                                        )
                           .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                   .Dimension(12, 12, 10)
                                   .Weight(16m, UnitOfWeight.OZ)
                                   )
                           .Rates(RatesArrayFluent <Rates> .Create()
                                  .USPSPriority <Rates, Parameter>()
                                  .InductionPostalCode("06484")
                                  )
                           .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                      .ShippingLabel(ContentType.BASE64, Size.DOC_4X6, FileFormat.ZPL2)
                                      )
                           .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                            .ShipperId(Globals.DefaultSession.GetConfigItem("ShipperID"))
                                            .AddToManifest()
                                            )
                           .TransactionId(Guid.NewGuid().ToString().Substring(15));


            var label = Api.CreateShipment((Shipment)shipment).GetAwaiter().GetResult();

            Assert.NotNull(label);
            Assert.True(label.Success);
            Assert.NotNull(label.ResponseObject as Shipment);
            Assert.NotNull(label.APIResponse.ParcelTrackingNumber);
            Assert.True(label.APIResponse.ParcelTrackingNumber != string.Empty);

            // var trackingRequest = new TrackingRequest
            // {
            //     Carrier = Carrier.USPS,
            //     TrackingNumber = label.APIResponse.ParcelTrackingNumber
            // };
            // var trackingResponse = Api.Tracking<TrackingStatus>(trackingRequest).GetAwaiter().GetResult();

            // Parcel Reprint
            var reprintRequest = new ReprintShipmentRequest()
            {
                Shipment = label.APIResponse.ShipmentId
            };

            var reprintResponse = Api.ReprintShipment <Shipment>(reprintRequest).GetAwaiter().GetResult();

            Assert.NotNull(reprintResponse);
            Assert.True(reprintResponse.Success);
            Assert.NotNull(reprintResponse.ResponseObject as Shipment);
            Assert.NotNull(reprintResponse.APIResponse.ParcelTrackingNumber);
            Assert.True(reprintResponse.APIResponse.ParcelTrackingNumber != string.Empty);

            using (var labelStream = new MemoryStream())
            {
                foreach (var d in reprintResponse.APIResponse.Documents)
                {
                    if (d.ContentType == ContentType.BASE64 && d.FileFormat == FileFormat.PNG)
                    {
                        // Multiple page png document
                        Api.WriteToStream(d, null,

                                          (stream, page) => // callback for each page
                        {
                            // append to the memory stream
                            return(labelStream);
                        },
                                          disposeStream: false
                                          ).GetAwaiter().GetResult();
                    }
                    else
                    {
                        Api.WriteToStream(d, labelStream).GetAwaiter().GetResult();
                    }
                }
                Assert.True(labelStream.Length > 0);
            }

            // manifest
            var manifest = ManifestFluent <Manifest> .Create()
                           .Carrier(Carrier.USPS)
                           .FromAddress(((Shipment)shipment).FromAddress)
                           .InductionPostalCode("06484")
                           .SubmissionDate(DateTime.Now)
                           .AddParameter <Parameter>(ManifestParameter.SHIPPER_ID, Globals.DefaultSession.GetConfigItem("ShipperID"))
                           .TransactionId(Guid.NewGuid().ToString().Substring(15));

            var manifestResponse = Api.CreateManifest <Manifest>(manifest).GetAwaiter().GetResult();

            Assert.NotNull(manifestResponse);
            if (manifestResponse.Success)
            {
                Assert.NotNull(manifestResponse.ResponseObject);
            }
            else
            {
                // making a shipment available to manifest seems to be an async process and the shipment just created might
                // not be available
                Assert.True(manifestResponse.Errors.Count == 1);
                Assert.True(manifestResponse.Errors[0].ErrorCode == "1110019");
            }

            using (var manifestStream = new MemoryStream())
            {
                foreach (var d in manifestResponse.APIResponse.Documents)
                {
                    Api.WriteToStream(d, manifestStream).GetAwaiter().GetResult();
                }
                Assert.True(manifestStream.Length > 0);
            }

            // Cancel the label

            var cancelRequest = new CancelShipmentRequest
            {
                Carrier          = Carrier.USPS,
                CancelInitiator  = CancelInitiator.SHIPPER,
                TransactionId    = Guid.NewGuid().ToString().Substring(15),
                ShipmentToCancel = label.APIResponse.ShipmentId
            };
            var cancelResponse = Api.CancelShipment(cancelRequest).GetAwaiter().GetResult();

            Assert.NotNull(cancelResponse);
            Assert.True(cancelResponse.Success);
            Assert.NotNull(cancelResponse.ResponseObject);
        }
コード例 #7
0
        static void Main(string[] args)
        {
            var sandbox = new Session()
            {
                EndPoint = "https://api-sandbox.pitneybowes.com", Requester = new ShippingApiHttpRequest()
            };

            var configs = new Dictionary <string, string>
            {
                { "ApiKey", "YOUR_API_KEY" },
                { "ApiSecret", "YOUR_API_SECRET" },
                { "RatePlan", "YOUR_RATE_PLAN" },
                { "ShipperID", "YOUR_SHIPPER_ID" },
                { "DeveloperID", "YOUR_DEVELOPER_ID" }
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder
            .AddInMemoryCollection(configs)
            .AddJsonFile(Globals.GetConfigPath("shippingapisettings.json"), optional: true, reloadOnChange: true);

            sandbox.GetConfigItem = (c) => configurationBuilder.Build()[c];
            Model.RegisterSerializationTypes(sandbox.SerializationRegistry);
            Globals.DefaultSession = sandbox;

            var shipment = (Shipment)ShipmentFluent <Shipment> .Create()
                           .ToAddress((Address)AddressFluent <Address> .Create()
                                      .AddressLines("643 Greenway Rd")
                                      .PostalCode("28607")
                                      .CountryCode("US")
                                      .Verify())
                           .FromAddress((Address)AddressFluent <Address> .Create()
                                        .Company("Pitney Bowes Inc")
                                        .AddressLines("27 Waterview Drive")
                                        .CityTown("Shelton").StateProvince("CT").PostalCode("06484")
                                        .CountryCode("US")
                                        )
                           .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                   .Dimension(12, 0.25M, 9)
                                   .Weight(3m, UnitOfWeight.OZ))
                           .Rates(RatesArrayFluent <Rates> .Create()
                                  .USPSPriority <Rates, Parameter>()
                                  .InductionPostalCode("06484")
                                  )
                           .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                      .ShippingLabel(ContentType.BASE64, Size.DOC_4X6, FileFormat.PNG))
                           .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                            .ShipperId(sandbox.GetConfigItem("ShipperID"))
                                            .MinimalAddressvalidation()
                                            )
                           .TransactionId(Guid.NewGuid().ToString().Substring(15));

            shipment.IncludeDeliveryCommitment = true;
            var label = Api.CreateShipment(shipment).GetAwaiter().GetResult();

            if (label.Success)
            {
                var sw = new StreamWriter("label.pdf");
                foreach (var d in label.APIResponse.Documents)
                {
                    Api.WriteToStream(d, sw.BaseStream).GetAwaiter().GetResult();
                }
            }
        }
コード例 #8
0
        static void Main(string[] args)
        {
            #region Example program setup
            SetupConfigProvider();
            SetupApiKey();
            #endregion

            #region ConfigureFramework

            var sandbox = new Session()
            {
                EndPoint = "https://api-sandbox.pitneybowes.com", Requester = new ShippingApiHttpRequest()
            };

            // Initialize framework

            // Hook in your concrete classes
            Model.RegisterSerializationTypes(sandbox.SerializationRegistry);

            // Hook in your config provider
            sandbox.GetConfigItem = (s) => Configuration[s];

            // Hook in your logger
            sandbox.LogWarning     = (s) => Logger.LogWarning(s);
            sandbox.LogError       = (s) => Logger.LogError(s);
            sandbox.LogConfigError = (s) => Logger.LogCritical(s);
            sandbox.LogDebug       = (s) => Logger.LogInformation(s);

            // Hook in your secure API key decryption
            sandbox.GetApiSecret = GetApiKey;

            if (Configuration["Mock"] == "true")
            {
                sandbox.Requester = new ShippingAPIMock();
            }

            Globals.DefaultSession = sandbox;
            #endregion
            #region CreateShipment

            try
            {
                // Create shipment
                var shipment = ShipmentFluent <Shipment> .Create()
                               .ToAddress((Address)AddressFluent <Address> .Create()
                                          .AddressLines("643 Greenway RD")
                                          .PostalCode("28607")
                                          .CountryCode("US")
                                          .Verify() // calls the service for address validation - will populate city and state from the zip
                                          )
                               .MinimalAddressValidation("true")
                               //.ShipperRatePlan(Globals.DefaultSession.GetConfigItem("RatePlan")) // use if you have a custom rate plan
                               .FromAddress((Address)AddressFluent <Address> .Create()
                                            .Company("Pitney Bowes Inc.")
                                            .AddressLines("27 Waterview Drive")
                                            .Residential(false)
                                            .CityTown("Shelton")
                                            .StateProvince("CT")
                                            .PostalCode("06484")
                                            .CountryCode("US")
                                            .Person("Paul Wright", "203-555-1213", "*****@*****.**")
                                            )
                               .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                       .Dimension(12, 12, 10)
                                       .Weight(16m, UnitOfWeight.OZ)
                                       )
                               .Rates(RatesArrayFluent <Rates> .Create()
                                      .USPSPriority <Rates, Parameter>()
                                      .InductionPostalCode("06484")
                                      )
                               .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                          .ShippingLabel(ContentType.BASE64, Size.DOC_4X6, FileFormat.ZPL2)
                                          )
                               .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                                .ShipperId(sandbox.GetConfigItem("ShipperID"))
                                                .AddToManifest()
                                                )
                               .TransactionId(Guid.NewGuid().ToString().Substring(15));


                var label = Api.CreateShipment((Shipment)shipment).GetAwaiter().GetResult();

                #endregion
                #region UseShipment
                if (label.Success)
                {
                    Console.WriteLine(label.APIResponse.ParcelTrackingNumber);

                    // Tracking

                    var trackingRequest = new TrackingRequest
                    {
                        Carrier        = Carrier.USPS,
                        TrackingNumber = label.APIResponse.ParcelTrackingNumber
                    };
                    var trackingResponse = Api.Tracking <TrackingStatus>(trackingRequest).GetAwaiter().GetResult();

                    // Parcel Reprint
                    var reprintRequest = new ReprintShipmentRequest()
                    {
                        Shipment = label.APIResponse.ShipmentId
                    };

                    var reprintResponse = Api.ReprintShipment <Shipment>(reprintRequest).GetAwaiter().GetResult();

                    // Write the label to disk
                    foreach (var d in reprintResponse.APIResponse.Documents)
                    {
                        if (d.ContentType == ContentType.BASE64 && d.FileFormat == FileFormat.PNG)
                        {
                            // Multiple page png document
                            Api.WriteToStream(d, null,

                                              (stream, page) => // callback for each page
                            {
                                // create a new file

                                if (stream != null)
                                {
                                    stream.Dispose();
                                }
                                string fileName = string.Format("{0}{1}p{2}.{3}", Path.GetTempPath(), reprintResponse.APIResponse.ShipmentId, page.ToString(), d.FileFormat.ToString());
                                Console.WriteLine("Document written to " + fileName);
                                return(new FileStream(fileName, FileMode.OpenOrCreate));
                            },
                                              disposeStream: true
                                              ).GetAwaiter().GetResult();
                        }
                        else
                        {
                            string fileName = string.Format("{0}{1}.{2}", Path.GetTempPath(), reprintResponse.APIResponse.ShipmentId, d.FileFormat.ToString());
                            using (StreamWriter sw = new StreamWriter(fileName))
                            {
                                Api.WriteToStream(d, sw.BaseStream).GetAwaiter().GetResult();
                                Console.WriteLine("Document written to " + fileName);
                            }
                        }
                    }
                }
                #endregion
                #region CreateManifest

                // Create the manifest

                var manifest = ManifestFluent <Manifest> .Create()
                               .Carrier(Carrier.USPS)
                               .FromAddress(((Shipment)shipment).FromAddress)
                               .InductionPostalCode("06484")
                               .SubmissionDate(DateTime.Now)
                               .AddParameter <Parameter>(ManifestParameter.SHIPPER_ID, sandbox.GetConfigItem("ShipperID"))
                               .TransactionId(Guid.NewGuid().ToString().Substring(15));

                var manifestResponse = Api.CreateManifest <Manifest>(manifest).GetAwaiter().GetResult();
                #endregion
                #region UseManifest
                if (manifestResponse.Success)
                {
                    foreach (var d in manifestResponse.APIResponse.Documents)
                    {
                        string fileName = string.Format("{0}{1}.{2}", Path.GetTempPath(), manifestResponse.APIResponse.ManifestId, d.FileFormat.ToString());
                        try
                        {
                            using (StreamWriter sw = new StreamWriter(fileName))
                            {
                                Api.WriteToStream(d, sw.BaseStream).GetAwaiter().GetResult();
                                Console.WriteLine("Document written to " + fileName);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
                #endregion
                #region SchedulePickup
                // Schedule a pickup

                var pickup = PickupFluent <Pickup> .Create()
                             .Carrier(Carrier.USPS)
                             .PackageLocation(PackageLocation.MailRoom)
                             .PickupAddress(((Shipment)shipment).FromAddress)
                             .PickupDate(DateTime.Now.AddDays(1))
                             .AddPickupSummary <PickupCount, ParcelWeight>(PickupService.PM, 1, 16M, UnitOfWeight.OZ)
                             .TransactionId(Guid.NewGuid().ToString().Substring(15));

                var pickupResponse = Api.Schedule <Pickup>(pickup).GetAwaiter().GetResult();
                #endregion
                #region UsePickup
                // Cancel pickup

                if (pickupResponse.Success)
                {
                    pickup.Cancel();
                }

                // Cancel the label

                if (label.Success)
                {
                    var cancelRequest = new CancelShipmentRequest
                    {
                        Carrier          = Carrier.USPS,
                        CancelInitiator  = CancelInitiator.SHIPPER,
                        TransactionId    = Guid.NewGuid().ToString().Substring(15),
                        ShipmentToCancel = label.APIResponse.ShipmentId
                    };
                    var cancelResponse = Api.CancelShipment(cancelRequest).GetAwaiter().GetResult();
                }
                #endregion
                #region TransactionReports
                // Transaction report with IEnumerable

                var transactionsReportRequest = new ReportRequest()
                {
                    FromDate    = DateTimeOffset.Parse("6/30/2019"),
                    ToDate      = DateTimeOffset.Now,
                    DeveloperId = sandbox.GetConfigItem("DeveloperID")
                };
                foreach (var t in TransactionsReport <Transaction> .Report(transactionsReportRequest, x => x.CreditCardFee == null || x.CreditCardFee > 10.0M, maxPages: 2))
                {
                    Console.WriteLine(t.DestinationAddress);
                }

                // Transaction report with LINQ
                TransactionsReport <Transaction> report = new TransactionsReport <Transaction>(sandbox.GetConfigItem("DeveloperID"), maxPages: 2);
                var query = from transaction in report
                            where transaction.TransactionDateTime >= DateTimeOffset.Parse("6/30/2019") && transaction.TransactionDateTime <= DateTimeOffset.Now && transaction.TransactionType == TransactionType.POSTAGE_PRINT
                            select new { transaction.TransactionId };
                foreach (var obj in query)
                {
                    Console.WriteLine(obj);
                }

                #endregion
                #region CarrierRules

                // Download the carrier rules
                var req = new RatingServicesRequest()
                {
                    Carrier                = Carrier.USPS,
                    OriginCountryCode      = "US",
                    DestinationCountryCode = "US"
                };
                var res = CarrierRulesMethods.RatingServices(req).GetAwaiter().GetResult();
                if (res.Success)
                {
                    // validate a shipment
                    var v = new ShipmentValidator();
                    if (!v.Validate((Shipment)shipment, res.APIResponse))
                    {
                        Console.WriteLine("Shipment is not valid: {0}", v.Reason);
                    }

                    // do some interesting things with the rules
                    var ruleRep = new RuleReport
                    {
                        CarrierRules = new CarrierRule[1] {
                            res.APIResponse
                        }
                    };

                    // find possible services to fit a parcel
                    var to     = ((Shipment)shipment).ToAddress;
                    var from   = ((Shipment)shipment).FromAddress;
                    var parcel = ((Shipment)shipment).Parcel;

                    // match countries
                    ruleRep.CarrierRuleFilter = (rule) => rule.OriginCountry == from.CountryCode && rule.DestinationCountry == to.CountryCode;
                    // make sure parcel fits, is trackable and supports insurance, commercial base pricing
                    ruleRep.ParcelTypeRuleFilter = (rule) =>
                                                   rule.FitsDimensions(parcel.Dimension) &&
                                                   rule.HoldsWeight(parcel.Weight) &&
                                                   !(rule.Trackable == Trackable.NON_TRACKABLE) &&
                                                   rule.SpecialServiceRules.ContainsKey(SpecialServiceCodes.Ins) &&
                                                   rule.RateTypeId == "CONTRACT_RATES";

                    Shipment cheapestOption = null;
                    foreach (var ruleItem in ruleRep)
                    {
                        // create the shipment from looked up values
                        var newShipment = ShipmentFluent <Shipment> .Create()
                                          .ToAddress(to)
                                          .FromAddress(from)
                                          .MinimalAddressValidation("true")
                                          //.ShipperRatePlan(Globals.DefaultSession.GetConfigItem("RatePlan"))
                                          .Parcel(parcel)
                                          .Rates(RatesArrayFluent <Rates> .Create()
                                                 .Add()
                                                 .RateFromRule(ruleItem.Item1, ruleItem.Item2, ruleItem.Item3)
                                                 .SpecialServiceFromRule <Rates, SpecialServices>(ruleItem.Item4)
                                                 .SuggestedTrackingServiceFromRule <Rates, SpecialServices>(ruleItem.Item3)
                                                 .InductionPostalCode(from.PostalCode)
                                                 )
                                          .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                                     .ShippingLabel()
                                                     )
                                          .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                                           .ShipperId(sandbox.GetConfigItem("ShipperID"))
                                                           .AddToManifest()
                                                           )
                                          .TransactionId(Guid.NewGuid().ToString().Substring(15));

                        // rate the parcel
                        var ratesResponse = Api.Rates <Shipment>((Shipment)newShipment).GetAwaiter().GetResult();
                        if (ratesResponse.Success)
                        {
                            if (cheapestOption == null || ratesResponse.APIResponse.Rates.First().TotalCarrierCharge < cheapestOption.Rates.First().TotalCarrierCharge)
                            {
                                cheapestOption = ratesResponse.APIResponse;
                            }
                            Console.WriteLine("{0},{1},{2},{3},{4},{5},{6} --- ${7}",
                                              ruleItem.Item1.Carrier,
                                              ruleItem.Item1.OriginCountry,
                                              ruleItem.Item1.DestinationCountry,
                                              ruleItem.Item2.ServiceId,
                                              ruleItem.Item3.ParcelType,
                                              ruleItem.Item3.RateTypeId,
                                              ruleItem.Item4 == null ? String.Empty : ruleItem.Item4.SpecialServiceId.ToString(),
                                              ratesResponse.APIResponse.Rates.First().TotalCarrierCharge);
                        }
                    }
                    //get the label
                    cheapestOption.TransactionId = Guid.NewGuid().ToString().Substring(15);
                    var newLabel = Api.CreateShipment(cheapestOption).GetAwaiter().GetResult();
                    if (newLabel.Success)
                    {
                        string fileName = string.Format("{0}{1}.{2}", Path.GetTempPath(), newLabel.APIResponse.ShipmentId, "PDF");
                        using (StreamWriter sw = new StreamWriter(fileName))
                        {
                            Api.WriteToStream(newLabel.APIResponse.Documents.First(), sw.BaseStream).GetAwaiter().GetResult();
                            Console.WriteLine("Document written to " + fileName);
                        }
                    }
                }
                foreach (var u in sandbox.Counters.Keys)
                {
                    Console.WriteLine(u);
                    Console.WriteLine("Errors: {0}", sandbox.Counters[u].ErrorCount);
                    foreach (var b in sandbox.Counters[u].CallHistogram.Keys)
                    {
                        Console.WriteLine("{0} {1}", 10 * b, sandbox.Counters[u].CallHistogram[b]);
                    }
                }
            }
            catch (ShippingAPIException e)
            {
                Console.WriteLine("ShippingAPI exception" + e.Message);
                if (e.ErrorResponse != null && e.ErrorResponse.Errors != null)
                {
                    foreach (var l in e.ErrorResponse.Errors)
                    {
                        Console.WriteLine(l.ErrorCode + " " + l.Message);
                    }
                }
            }

            #endregion
        }
コード例 #9
0
        static void Main(string[] args)
        {
            var sandbox = new Session()
            {
                EndPoint  = "https://api-sandbox.pitneybowes.com",
                Requester = new ShippingApiHttpRequest()
            };
            var configs = new Dictionary <string, string>
            {
                { "ApiKey", "<API_KEY>" },
                { "ApiSecret", "<API_Secret>" },
                { "RatePlan", "<Rate_Plan>" },
                { "ShipperID", "<Shipper_Id>" },
            };
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(configs).AddJsonFile(Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "shippingapisettings.json"), optional: true, reloadOnChange: true);



            sandbox.GetConfigItem = (c) => configurationBuilder.Build()[c];
            Model.RegisterSerializationTypes(sandbox.SerializationRegistry);
            Globals.DefaultSession = sandbox;
            var shipment = (Shipment)ShipmentFluent <Shipment> .Create()
                           .ToAddress((Address)AddressFluent <Address> .Create()
                                      .Company("Shop")
                                      .Name("Mary Jones")
                                      .Phone("620-555-0000")
                                      .Email("*****@*****.**")
                                      .AddressLines("284 W Fulton")
                                      .CityTown("Garden City").StateProvince("KS")
                                      .PostalCode("67846")
                                      .CountryCode("US")
                                      )
                           .FromAddress((Address)AddressFluent <Address> .Create()
                                        .Company("Supplies")
                                        .Name("Kathryn Smith")
                                        .Phone("334-000-0000 ")
                                        .Email("*****@*****.**")
                                        .AddressLines("2352 Bent Creek Rd")
                                        .CityTown("Auburn").StateProvince("AL").PostalCode("36830")
                                        .CountryCode("US")
                                        )
                           .Parcel((Parcel)ParcelFluent <Parcel> .Create()
                                   .Weight(20, UnitOfWeight.OZ))
                           .Rates(RatesArrayFluent <Rates> .Create().
                                  Add().Carrier(Carrier.FedEx)
                                  .Service(Services.TwoDA)
                                  .ParcelType(ParcelType.PKG))
                           .Documents((List <IDocument>)DocumentsArrayFluent <Document> .Create()
                                      .ShippingLabel(ContentType.URL, Size.DOC_4X6, FileFormat.PDF))
                           .ShipmentOptions(ShipmentOptionsArrayFluent <ShipmentOptions> .Create()
                                            .ShipperId(sandbox.GetConfigItem("ShipperID"))
                                            )
                           .TransactionId(Guid.NewGuid().ToString().Substring(15));



            shipment.IncludeDeliveryCommitment = true;
            var shipmentreq = (Shipment)shipment;

            shipmentreq.IncludeDeliveryCommitment = true;
            var label = Api.CreateShipment(shipment).GetAwaiter().GetResult();

            if (label.Success)
            {
                var sw = new StreamWriter("label.PDF");
                foreach (var d in label.APIResponse.Documents)
                {
                    Api.WriteToStream(d, sw.BaseStream).GetAwaiter().GetResult();
                }
            }
        }