|
1 | 1 | using System.Xml.Linq;
|
2 |
| - |
3 | 2 | using Nop.Core;
|
4 | 3 | using Nop.Core.Domain.Shipping;
|
5 | 4 | using Nop.Plugin.Shipping.USPS.Domain;
|
@@ -306,6 +305,17 @@ private string CreateTrackRequest(string trackingNumber)
|
306 | 305 | return document.ToString(SaveOptions.DisableFormatting);
|
307 | 306 | }
|
308 | 307 |
|
| 308 | + private string CreateTransitTimeRequest(TransitDaysAPI postageApiType, string originPostalCode, string destinationPostalCode) |
| 309 | + { |
| 310 | + var document = new XDocument( |
| 311 | + new XElement($"{postageApiType}Request", new XAttribute("USERID", _uspsSettings.Username), |
| 312 | + new XElement("OriginZip", originPostalCode), |
| 313 | + new XElement("DestinationZip", destinationPostalCode)) |
| 314 | + ); |
| 315 | + |
| 316 | + return document.ToString(SaveOptions.DisableFormatting); |
| 317 | + } |
| 318 | + |
309 | 319 | /// <summary>
|
310 | 320 | /// USPS country hacks
|
311 | 321 | /// The USPS wants the NAME of the country for international shipments rather than one of the ISO codes
|
@@ -400,7 +410,7 @@ private async Task<int> GetWeightAsync(GetShippingOptionRequest shippingOptionRe
|
400 | 410 | //get rate response
|
401 | 411 | var rateResponse = await _uspsHttpClient.GetRatesAsync(requestString, isDomestic);
|
402 | 412 |
|
403 |
| - return await ParseResponse(rateResponse, shippingOptionRequest); |
| 413 | + return await ParseResponseAsync(rateResponse, shippingOptionRequest); |
404 | 414 | }
|
405 | 415 | catch (Exception ex)
|
406 | 416 | {
|
@@ -440,7 +450,7 @@ private async Task<bool> IsDomesticRequestAsync(GetShippingOptionRequest getShip
|
440 | 450 | return false;
|
441 | 451 | }
|
442 | 452 |
|
443 |
| - private async Task<(IList<ShippingOption> shippingOptions, IList<string> errors)> ParseResponse(RateResponse response, GetShippingOptionRequest request) |
| 453 | + private async Task<(IList<ShippingOption> shippingOptions, IList<string> errors)> ParseResponseAsync(RateResponse response, GetShippingOptionRequest request) |
444 | 454 | {
|
445 | 455 | var shippingOptions = new List<ShippingOption>();
|
446 | 456 |
|
@@ -485,50 +495,33 @@ bool isPostageOffered(Postage p)
|
485 | 495 | async Task<int?> getTransitDaysAsync(string service)
|
486 | 496 | {
|
487 | 497 | //parse out service to make request to correct API and fill Rool Element name
|
488 |
| - var mailType = default(USPSHttpClient.TransitDaysAPI?); |
| 498 | + var mailType = default(TransitDaysAPI?); |
| 499 | + |
489 | 500 | if (service.Contains("priority", StringComparison.InvariantCultureIgnoreCase))
|
490 |
| - { |
491 |
| - mailType = USPSHttpClient.TransitDaysAPI.PriorityMail; |
492 |
| - } |
493 |
| - else if (service.Contains("firstclass", StringComparison.InvariantCultureIgnoreCase) || |
494 |
| - service.Contains("first class", StringComparison.InvariantCultureIgnoreCase)) |
495 |
| - { |
496 |
| - mailType = USPSHttpClient.TransitDaysAPI.FirstClassMail; |
497 |
| - } |
498 |
| - else if (service.Contains("express", StringComparison.InvariantCultureIgnoreCase)) |
499 |
| - { |
500 |
| - mailType = USPSHttpClient.TransitDaysAPI.ExpressMailCommitment; |
501 |
| - } |
| 501 | + mailType = TransitDaysAPI.PriorityMail; |
502 | 502 |
|
503 |
| - if (mailType.HasValue) |
504 |
| - { |
505 |
| - var transitResponse = await _uspsHttpClient.GetTransitTimeAsync(mailType.Value, |
506 |
| - CommonHelper.EnsureMaximumLength(CommonHelper.EnsureNumericOnly(request.ZipPostalCodeFrom), 5), |
507 |
| - CommonHelper.EnsureMaximumLength(CommonHelper.EnsureNumericOnly(request.ShippingAddress.ZipPostalCode), 5), _uspsSettings.Username); |
508 |
| - if (transitResponse != null && |
509 |
| - transitResponse.Days.HasValue) |
510 |
| - { |
511 |
| - var transitDays = transitResponse.Days.Value + 1; //add a day to process order. |
| 503 | + if (service.Contains("firstclass", StringComparison.InvariantCultureIgnoreCase) || service.Contains("first class", StringComparison.InvariantCultureIgnoreCase)) |
| 504 | + mailType = TransitDaysAPI.FirstClassMail; |
512 | 505 |
|
513 |
| - var today = DateTime.UtcNow.ToLocalTime(); |
514 |
| - //assuming 5-day work week here and weekend order will not be shipped until monday at earliest, may want to add options to configuration to provide most accurate estimate. |
515 |
| - while (today.DayOfWeek == DayOfWeek.Friday || |
516 |
| - today.DayOfWeek == DayOfWeek.Saturday || |
517 |
| - today.DayOfWeek == DayOfWeek.Sunday) |
518 |
| - { |
519 |
| - today = today.AddDays(1); |
520 |
| - transitDays += 1; |
521 |
| - } |
| 506 | + if (service.Contains("express", StringComparison.InvariantCultureIgnoreCase)) |
| 507 | + mailType = TransitDaysAPI.ExpressMailCommitment; |
522 | 508 |
|
523 |
| - return transitDays; |
524 |
| - } |
525 |
| - } |
| 509 | + if (mailType is null) |
| 510 | + return null; |
| 511 | + |
| 512 | + static string formatZip(string zipCode) => CommonHelper.EnsureMaximumLength(CommonHelper.EnsureNumericOnly(zipCode), 5); |
526 | 513 |
|
527 |
| - return null; |
| 514 | + var requestString = CreateTransitTimeRequest( |
| 515 | + mailType.Value, |
| 516 | + formatZip(request.ZipPostalCodeFrom), |
| 517 | + formatZip(request.ShippingAddress.ZipPostalCode)); |
| 518 | + |
| 519 | + var transitResponse = await _uspsHttpClient.GetTransitTimeAsync(mailType.Value, requestString); |
| 520 | + |
| 521 | + return transitResponse?.Days; |
528 | 522 | }
|
529 | 523 | }
|
530 | 524 |
|
531 |
| - |
532 | 525 | private async Task<IList<ShipmentStatusEvent>> TrackAsync(string requestString)
|
533 | 526 | {
|
534 | 527 | var response = await _uspsHttpClient.GetTrackEventsAsync(requestString);
|
|
0 commit comments