Пример #1
0
        public void ValidShipment_ResultIsValid()
        {
            var shipment = GetValidShipment(ImportNotificationPreconsentedId);

            var result = validator.Validate(shipment);

            Assert.True(result.IsValid);
        }
Пример #2
0
        public (bool isValid, IEnumerable <ValidationResult> errors) Validate()
        {
            var validator = new ShipmentValidator();
            var result    = validator.Validate(this);

            if (result.IsValid)
            {
                return(true, null);
            }

            return(false, result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new [] { item.PropertyName })));
        }
 protected virtual void ApplyRuleForShipments(CartValidationContext cartContext, ValidationContext <CartValidationContext> context)
 {
     cartContext.CartAggregate.Cart.Shipments?.Apply(shipment =>
     {
         var shipmentContext = new ShipmentValidationContext
         {
             Shipment           = shipment,
             AvailShippingRates = cartContext.AvailShippingRates
         };
         var result = ShipmentValidator.Validate(shipmentContext);
         result.Errors.Apply(x => context.AddFailure(x));
     });
 }
Пример #4
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
        }