-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUspsRateConfigurator.cs
308 lines (259 loc) · 9.41 KB
/
UspsRateConfigurator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using EasyKeys.Shipping.Abstractions;
using EasyKeys.Shipping.Abstractions.Models;
namespace EasyKeys.Shipping.Usps.Rates;
public class UspsRateConfigurator
{
public UspsRateConfigurator(
Address origin,
Address destination,
Package package,
DateTime? shipDate = null)
{
// since USPS is not highest priority it is okay to add some time for processing it.
var solidDate = shipDate ?? DateTime.Now.AddDays(1);
if (destination.IsUnitedStatesAddress())
{
// weight > 15 oz
if (package.Weight <= 15m / 16)
{
ConfigureDomesticFirstClass(origin, destination, package, solidDate);
ConfigureDomesticSmallFlatRate(origin, destination, GetSmallFlatBox(package.Weight), solidDate);
}
else
{
ConfigureDomesticPriority(origin, destination, package, solidDate);
}
}
else
{
ConfigureIntlFirstClass(origin, destination, package, solidDate);
ConfigureIntlPriorityMail(origin, destination, package, solidDate);
}
}
public List<(Shipment shipment, UspsRateOptions rateOptions)> Shipments { get; } = new List<(Shipment shipment, UspsRateOptions rateOptions)>();
public decimal StampsRate { get; private set; }
public static Package GetPackage(
Address destination,
decimal weight,
Dimensions size)
{
var package = new Package(size, weight);
if (destination.IsUnitedStatesAddress())
{
if (package.Weight <= 15m / 16)
{
return GetFirstClassPackage(package.Weight);
}
else
{
if (package.IsSmallFlatRateBox())
{
return GetSmallFlatBox(package.Weight);
}
if (package.IsMediumFlatRateBox())
{
return GetMediumFlatBox(package.Weight);
}
}
}
else
{
return GetIntlBox(package.Weight);
}
return package;
}
/// <summary>
/// Create First Class package weight 0 - 15 oz.
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
public static Package GetFirstClassPackage(decimal weight)
{
if (weight > 15m / 16)
{
throw new ArgumentException("Exceeds first class weight", nameof(weight));
}
var ozW = weight * 16;
if (ozW >= 13m || ozW >= 15m)
{
weight = 13m / 16;
}
return new Package(11m, 6m, 0.75m, weight, 0m);
}
/// <summary>
/// https://store.usps.com/store/product/shipping-supplies/priority-mail-small-flat-rate-box-P_SMALL_FRB
/// Small Flat Rate Box up to 70 lbs. 8 11⁄16″ x 5 7⁄16″ x 1 3⁄4″ usps: $8.45 stamps.com: $7.90
/// https://www.stamps.com/usps/priority-mail-flat-rate/.
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
public static Package GetSmallFlatBox(decimal weight)
{
return new Package(8m, 5m, 1m, weight, 0m);
}
/// <summary>
/// Medium Flat Rate Box - Top Loading up to 70 lbs. 11 1⁄4″ x 8 3⁄4″ x 6″ usps: $15.50 stamps: $13.75
/// https://www.stamps.com/usps/priority-mail-flat-rate/.
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
public static Package GetMediumFlatBox(decimal weight)
{
return new Package(11m, 8m, 5m, weight, 0m);
}
/// <summary>
/// First Class International ® $7.90 Large Envelope or Flat.
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
public static Package GetIntlEnvelope(decimal weight)
{
if (weight > 4m)
{
throw new ArgumentException("Exceeds first class weight", nameof(weight));
}
return new Package(11m, 6m, 0.75m, weight, 0m);
}
/// <summary>
/// https://www.usps.com/international/priority-mail-international.htm
/// Maximum weight for Priority Mail International Flat Rate Envelopes and small Flat Rate Boxes is 4 lbs.
/// Fast international delivery time: 6–10 business days.
/// </summary>
/// <param name="weight"></param>
/// <returns></returns>
public static Package GetIntlBox(decimal weight)
{
return new Package(8m, 5m, 1m, weight, 0m);
}
private void ConfigureDomesticFirstClass(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
if (package.Weight > 15m / 16)
{
throw new ArgumentException("First Class Mail package can't be greater than 15 oz", nameof(package.Weight));
}
// domestic first class package
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(FirstClassPackageType.PACKAGE_SERVICE_RETAIL), shipDate);
var shipment = new Shipment(origin, destination, packages, shipOptions);
var rateOptions = new UspsRateOptions
{
ServiceName = "First Class",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(5)
};
Shipments.Add((shipment, rateOptions));
}
private void ConfigureDomesticPriority(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
if (package.Weight > 70m)
{
throw new ArgumentException("Priority Mail package can't be greater than 70 lbs", nameof(package.Weight));
}
if (package.IsSmallFlatRateBox())
{
ConfigureDomesticSmallFlatRate(origin, destination, package, shipDate);
return;
}
if (package.IsMediumFlatRateBox())
{
ConfigureDomesticMediumFlatRate(origin, destination, package, shipDate);
return;
}
ConfigureLargePackage(origin, destination, package, shipDate);
}
private void ConfigureLargePackage(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(PriorityPackageType.VARIABLE), shipDate);
var shipment = new Shipment(origin, destination, packages, shipOptions);
var rateOptions = new UspsRateOptions
{
ServiceName = "Priority",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(3)
};
Shipments.Add((shipment, rateOptions));
}
private void ConfigureDomesticSmallFlatRate(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(PriorityPackageType.SM_FLAT_RATE_BOX), shipDate);
StampsRate = 7.90m;
var shipment = new Shipment(origin, destination, packages, shipOptions);
var rateOptions = new UspsRateOptions
{
ServiceName = "Priority",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(3)
};
Shipments.Add((shipment, rateOptions));
}
private void ConfigureDomesticMediumFlatRate(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(PriorityPackageType.MD_FLAT_RATE_BOX), shipDate);
StampsRate = 13.75m;
var shipment = new Shipment(origin, destination, packages, shipOptions);
var rateOptions = new UspsRateOptions
{
ServiceName = "Priority",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(3)
};
Shipments.Add((shipment, rateOptions));
}
private void ConfigureIntlPriorityMail(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(IntlPackageType.PACKAGE), shipDate);
var shipment = new Shipment(origin, destination, packages, shipOptions);
// var rateOptions = new UspsRateOptions { ServiceName = "All" };
var rateOptions = new UspsRateOptions
{
ServiceName = "Priority Mail International",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(14)
};
Shipments.Add((shipment, rateOptions));
}
private void ConfigureIntlFirstClass(
Address origin,
Address destination,
Package package,
DateTime shipDate)
{
if (package.Weight > 4m)
{
throw new ArgumentException("Package can't be greater than 4 lbs", nameof(package.Weight));
}
var packages = new List<Package> { package };
var shipOptions = new ShipmentOptions(nameof(IntlPackageType.ALL), shipDate);
StampsRate = 7.90m;
var shipment = new Shipment(origin, destination, packages, shipOptions);
var rateOptions = new UspsRateOptions
{
ServiceName = "First-Class Package International Service",
DefaultGuaranteedDelivery = shipDate.AddBusinessDays(21)
};
Shipments.Add((shipment, rateOptions));
}
}