Skip to content

Commit 5ecc077

Browse files
authored
Merge pull request #83 from easykeys/version/6.4.2
Add FedEx One Rate option to ShipmentOptions and update tests
2 parents 5fab866 + 7eb5868 commit 5ecc077

8 files changed

Lines changed: 147 additions & 6 deletions

File tree

GitVersion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mode: ContinuousDelivery
2-
next-version: 6.4.1
2+
next-version: 6.4.2
33
increment: Patch
44
major-version-bump-message: '\+semver:\s?(breaking|major|release)'
55
minor-version-bump-message: '\+semver:\s?(feat|feature|minor)'

src/EasyKeys.Shipping.Abstractions/Models/ShipmentOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public ShipmentOptions(
4444
/// </summary>
4545
public string DropOffType { get; set; } = "REGULAR_PICKUP";
4646

47+
/// <summary>
48+
/// <para>FedEx One Rate option. If true, the FedEx rate provider will return only FedEx One Rate options.</para>
49+
/// </summary>
50+
public bool FedexOneRate { get; set; }
51+
4752
/// <summary>
4853
/// Input fedex account number wished to be used.
4954
/// </summary>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using EasyKeys.Shipping.Abstractions.Models;
2+
using EasyKeys.Shipping.FedEx.Abstractions.Models;
3+
4+
namespace EasyKeys.Shipping.FedEx.Abstractions.Extensions;
5+
6+
public static class ShipmentExtensions
7+
{
8+
public static bool IsEligibleForFedExOneRate(this Shipment shipment)
9+
{
10+
return shipment.DestinationAddress.IsUnitedStatesAddress()
11+
&& shipment.OriginAddress.IsUnitedStatesAddress()
12+
&& shipment.Options.PackagingType != FedExPackageType.YourPackaging.Name;
13+
}
14+
}

src/EasyKeys.Shipping.FedEx.Rates/RestApi/Impl/FedexRateProvider.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using EasyKeys.Shipping.Abstractions.Models;
2+
using EasyKeys.Shipping.FedEx.Abstractions.Extensions;
23
using EasyKeys.Shipping.FedEx.Abstractions.Models;
34
using EasyKeys.Shipping.FedEx.Abstractions.OpenApis.V1.RatesAndTransitTimes;
45
using EasyKeys.Shipping.FedEx.Abstractions.Options;
@@ -132,6 +133,14 @@ public async Task<Shipment> GetRatesAsync(Shipment shipment, FedExServiceType? s
132133
};
133134
}
134135

136+
if (shipment.Options.FedexOneRate && shipment.IsEligibleForFedExOneRate())
137+
{
138+
ratesRequest.RequestedShipment.ShipmentSpecialServices = new RequestedShipmentSpecialServicesRequested
139+
{
140+
SpecialServiceTypes = new List<string> { "FEDEX_ONE_RATE" }
141+
};
142+
}
143+
135144
var token = await _authService.GetTokenAsync(cancellationToken);
136145

137146
var rates = await _client.Rate_and_Transit_timesAsync(

src/EasyKeys.Shipping.FedEx.Shipment/RestApi/Impl/FedExShipmentProvider.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using EasyKeys.Shipping.Abstractions;
22
using EasyKeys.Shipping.Abstractions.Extensions;
33
using EasyKeys.Shipping.Abstractions.Models;
4+
using EasyKeys.Shipping.FedEx.Abstractions.Extensions;
45
using EasyKeys.Shipping.FedEx.Abstractions.Models;
56
using EasyKeys.Shipping.FedEx.Abstractions.OpenApis.V1.Ship;
67
using EasyKeys.Shipping.FedEx.Abstractions.Options;
@@ -347,6 +348,21 @@ public async Task<ShipmentLabel> CreateShipmentAsync(FedExServiceType serviceTyp
347348
}
348349
}
349350

351+
if (shipment.Options.FedexOneRate && shipment.IsEligibleForFedExOneRate())
352+
{
353+
if(shipmentRequest.RequestedShipment.ShipmentSpecialServices == null)
354+
{
355+
shipmentRequest.RequestedShipment.ShipmentSpecialServices = new ShipmentSpecialServicesRequested
356+
{
357+
SpecialServiceTypes = ["FEDEX_ONE_RATE"]
358+
};
359+
}
360+
else
361+
{
362+
shipmentRequest.RequestedShipment.ShipmentSpecialServices?.SpecialServiceTypes?.Add("FEDEX_ONE_RATE");
363+
}
364+
}
365+
350366
switch (shipmentDetails.PaymentType.Name)
351367
{
352368
case "SENDER":

test/EasyKeys.Shipping.FuncTest/FedEx/FedExRateProviderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using EasyKeys.Shipping.Abstractions;
44
using EasyKeys.Shipping.Abstractions.Models;
5+
using EasyKeys.Shipping.FedEx.Abstractions.Extensions;
56
using EasyKeys.Shipping.FedEx.Abstractions.Models;
67
using EasyKeys.Shipping.FedEx.Rates;
78

@@ -39,6 +40,35 @@ public FedExRateProviderTests(ITestOutputHelper output)
3940
new object[] { 33.3M, 2, FedExPackageType.FedExPak.Name },
4041
};
4142

43+
[Fact]
44+
public async Task Return_Fedex_One_Rates_Successfully()
45+
{
46+
var rateServices = _sp.GetServices<IFedExRateProvider>();
47+
foreach (var rateService in rateServices)
48+
{
49+
var destination = new Address("1550 central ave", "Riverside", "CA", "92507", "US", isResidential: true);
50+
var package = FedExRateConfigurator.GetFedExEnvelop(0.5M,199m);
51+
var config = new FedExRateConfigurator(_origin, destination, package, true, DateTime.Now);
52+
foreach (var (shipment, serviceType) in config.Shipments)
53+
{
54+
if (!shipment.IsEligibleForFedExOneRate())
55+
{
56+
continue;
57+
}
58+
59+
shipment.Options.FedexOneRate = true;
60+
61+
var rates = await rateService.GetRatesAsync(shipment, serviceType);
62+
foreach (var rate in rates.Rates)
63+
{
64+
_output.WriteLine("{0}: {1}-{2}-{3}-{4}-{5}-{6}", rateService.GetType().FullName, rate.Name, rate.PackageType, rate.GuaranteedDelivery, rate.TotalCharges, rate.TotalCharges2, rate.SaturdayDelivery);
65+
}
66+
67+
Assert.False(rates.InternalErrors.Any());
68+
}
69+
}
70+
}
71+
4272
[Theory]
4373
[MemberData(nameof(Data))]
4474
public async Task Return_FedEx_Rates_For_Envelope_Or_Pak_Successfully(decimal weight, int count, string packageType)

test/EasyKeys.Shipping.FuncTest/FedEx/FedExShipmentProviderTests.cs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FedExShipmentProviderTests
1818
public FedExShipmentProviderTests(ITestOutputHelper output)
1919
{
2020
_origin = new Address("11407 Granite St", "Charlotte", "NC", "28273", "US");
21-
_domestic = new Address("1550 central ave", "Riverside", "CA", "92507", "US");
21+
_domestic = new Address("1550 central ave", "Riverside", "CA", "92507", "US", isResidential:true);
2222
_international = new Address("3601 72 Ave Se", "Calgary", "AB", "T2C 2K3", "CA", isResidential: false);
2323
_international = new Address("285 Wang Fu Jing Avenue", "BEIJING", "", "100006", "CN", isResidential: false);
2424

@@ -81,10 +81,76 @@ public async Task CreateDelete_Labels_ChargeRecipient_For_Domestic_Shipments_Asy
8181
var label = await provider.CreateShipmentAsync(stype, shipment, shipmentDetails, CancellationToken.None);
8282

8383
Assert.NotNull(label);
84+
_output.WriteLine($"net charge : {label.Labels.First().TotalCharges.NetCharge}, surcharge : {label.Labels.First().TotalCharges.Surcharges} , basecharge : {label.Labels.First().TotalCharges.BaseCharge}");
85+
8486
Assert.True(label.InternalErrors.Any());
8587
}
8688
}
8789

90+
[Fact]
91+
public async Task CreateDelete_One_Rate_Labels_For_Domestic_Shipments_Async()
92+
{
93+
var packages = new List<Package>
94+
{
95+
// fedex envelope
96+
FedExRateConfigurator.GetFedExEnvelop(0.05M,0m),
97+
};
98+
99+
var configurator = new FedExRateConfigurator(
100+
_origin,
101+
_domestic,
102+
packages.First(),
103+
true,
104+
DateTime.Now);
105+
106+
var stype = FedExServiceType.FedExSecondDay;
107+
var ptype = FedExPackageType.FedExEnvelope;
108+
109+
var shipmentOptions = new ShipmentOptions(ptype.Name, DateTime.Now);
110+
shipmentOptions.FedexOneRate = true;
111+
var shipment = new Shipment(_origin, _domestic, packages, shipmentOptions);
112+
113+
var (sender, recipient) = TestShipments.CreateContactInfo();
114+
115+
var shipmentDetails = new EasyKeys.Shipping.FedEx.Shipment.Models.ShipmentDetails
116+
{
117+
Sender = sender,
118+
Recipient = recipient,
119+
120+
TransactionId = "1234",
121+
122+
PaymentType = FedExPaymentType.Sender,
123+
124+
RateRequestType = "list",
125+
126+
LabelOptions = new EasyKeys.Shipping.FedEx.Shipment.Models.LabelOptions()
127+
{
128+
LabelFormatType = "COMMON2D",
129+
ImageType = "PNG",
130+
}
131+
};
132+
133+
foreach (var provider in _providers)
134+
{
135+
var label = await provider.CreateShipmentAsync(stype, shipment, shipmentDetails, CancellationToken.None);
136+
137+
Assert.NotNull(label);
138+
Assert.False(label.InternalErrors.Any());
139+
140+
Assert.True(label?.Labels.Any(x => x?.Bytes?.Count > 0));
141+
142+
_output.WriteLine($"net charge : {label.Labels.First().TotalCharges.NetCharge}, surcharge : {label.Labels.First().TotalCharges.Surcharges} , basecharge : {label.Labels.First().TotalCharges.BaseCharge}");
143+
// Path to save the PNG file
144+
var filePath = $"{provider}-{provider.GetType().FullName}-{stype.ServiceName}-domestic-output.png";
145+
146+
// Write the byte array to a file
147+
File.WriteAllBytes(filePath, label.Labels.First().Bytes.First());
148+
149+
var result = await provider.CancelShipmentAsync(label.Labels.First().TrackingId, CancellationToken.None);
150+
Assert.True(result.Succeeded);
151+
}
152+
}
153+
88154
[Fact]
89155
public async Task CreateDelete_Labels_For_Domestic_Shipments_Async()
90156
{
@@ -101,7 +167,7 @@ public async Task CreateDelete_Labels_For_Domestic_Shipments_Async()
101167
true,
102168
DateTime.Now);
103169

104-
var stype = FedExServiceType.FedExStandardOvernight;
170+
var stype = FedExServiceType.FedExSecondDay;
105171
var ptype = FedExPackageType.FedExEnvelope;
106172

107173
var shipmentOptions = new ShipmentOptions(ptype.Name, DateTime.Now);

test/EasyKeys.Shipping.FuncTest/TestHelpers/ServiceProviderInstance.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ public static IServiceProvider GetFedExServices(ITestOutputHelper output)
7575

7676
services.AddSingleton<IConfiguration>(configBuilder.Build());
7777
//services.AddWebServicesFedExDocumentProvider();
78-
services.AddWebServicesFedExRateProvider();
79-
services.AddWebServicesFedExAddressValidationProvider();
78+
//services.AddWebServicesFedExRateProvider();
79+
//services.AddWebServicesFedExAddressValidationProvider();
80+
//services.AddWebServicesFedExShipmenProvider();
81+
8082
services.AddFedExClient();
81-
services.AddWebServicesFedExShipmenProvider();
8283
services.AddFedExTrackingProvider();
8384

8485
// adress validation apis

0 commit comments

Comments
 (0)