-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAustraliaPostComputationMethod.cs
408 lines (361 loc) · 16.6 KB
/
AustraliaPostComputationMethod.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Routing;
using Newtonsoft.Json.Linq;
using Grand.Core;
using Grand.Core.Domain.Directory;
using Grand.Core.Domain.Shipping;
using Grand.Core.Plugins;
using Grand.Services.Configuration;
using Grand.Services.Directory;
using Grand.Services.Localization;
using Grand.Services.Shipping;
using Grand.Services.Shipping.Tracking;
namespace Grand.Plugin.Shipping.AustraliaPost
{
/// <summary>
/// Australia post computation method
/// </summary>
public class AustraliaPostComputationMethod : BasePlugin, IShippingRateComputationMethod
{
#region Constants
private const int MIN_LENGTH = 50; // 5 cm
private const int MIN_WEIGHT = 500; // 500 g
private const int MAX_LENGTH = 1050; // 105 cm
private const int MAX_DOMESTIC_WEIGHT = 22000; // 22 Kg
private const int MAX_INTERNATIONAL_WEIGHT = 20000; // 20 Kg
private const int MIN_GIRTH = 160; // 16 cm
private const int MAX_GIRTH = 1400; // 140 cm
private const int ONE_KILO = 1000; // 1 kg
private const int ONE_CENTIMETER = 10; // 1 cm
private const string GATEWAY_URL_INTERNACIONAL_ALLOWED_SERVICES = "https://digitalapi.auspost.com.au/postage/parcel/international/service.json";
private const string GATEWAY_URL_DOMESTIC_ALLOWED_SERVICES = "https://digitalapi.auspost.com.au/postage/parcel/domestic/service.json";
#endregion
#region Fields
private readonly ICurrencyService _currencyService;
private readonly IMeasureService _measureService;
private readonly IShippingService _shippingService;
private readonly ICountryService _countryService;
private readonly ISettingService _settingService;
private readonly AustraliaPostSettings _australiaPostSettings;
#endregion
#region Ctor
public AustraliaPostComputationMethod(ICurrencyService currencyService,
IMeasureService measureService, IShippingService shippingService,
ICountryService countryService, ISettingService settingService, AustraliaPostSettings australiaPostSettings)
{
this._currencyService = currencyService;
this._measureService = measureService;
this._shippingService = shippingService;
this._countryService = countryService;
this._settingService = settingService;
this._australiaPostSettings = australiaPostSettings;
}
#endregion
#region Utilities
private MeasureWeight GatewayMeasureWeight
{
get
{
return this._measureService.GetMeasureWeightBySystemKeyword("grams");
}
}
private MeasureDimension GatewayMeasureDimension
{
get
{
return this._measureService.GetMeasureDimensionBySystemKeyword("millimetres");
}
}
private int GetWeight(GetShippingOptionRequest getShippingOptionRequest)
{
var totalWeigth = _shippingService.GetTotalWeight(getShippingOptionRequest);
int value = Convert.ToInt32(Math.Ceiling(this._measureService.ConvertFromPrimaryMeasureWeight(totalWeigth, this.GatewayMeasureWeight)));
return (value < MIN_WEIGHT ? MIN_WEIGHT : value);
}
private IList<ShippingOption> RequestShippingOptions(string countryTwoLetterIsoCode, string fromPostcode, string toPostcode, decimal weight, int length, int width, int heigth, int totalPackages)
{
var shippingOptions = new List<ShippingOption>();
var cultureInfo = new CultureInfo("en-AU");
var sb = new StringBuilder();
switch (countryTwoLetterIsoCode)
{
case "AU":
sb.AppendFormat(GATEWAY_URL_DOMESTIC_ALLOWED_SERVICES);
sb.AppendFormat("?from_postcode={0}&", fromPostcode);
sb.AppendFormat("to_postcode={0}&", toPostcode);
sb.AppendFormat("length={0}&", length);
sb.AppendFormat("width={0}&", width);
sb.AppendFormat("height={0}&", heigth);
break;
default:
sb.AppendFormat(GATEWAY_URL_INTERNACIONAL_ALLOWED_SERVICES);
sb.AppendFormat("?country_code={0}&", countryTwoLetterIsoCode);
break;
}
sb.AppendFormat("weight={0}", weight.ToString(cultureInfo.NumberFormat));
var request = WebRequest.Create(sb.ToString()) as HttpWebRequest;
request.Headers.Add("AUTH-KEY", _australiaPostSettings.ApiKey);
request.Method = "GET";
Stream stream;
try
{
var response = request.GetResponse();
stream = response.GetResponseStream();
}
catch (WebException ex)
{
stream = ex.Response.GetResponseStream();
}
//parse json from response
using (var reader = new StreamReader(stream))
{
var json = reader.ReadToEnd();
if (!string.IsNullOrEmpty(json))
{
var parsed = JObject.Parse(json);
JToken jToken;
try
{
jToken = parsed["services"]["service"];
if (jToken != null)
{
var options = (JArray)jToken;
foreach (var option in options)
{
var service = (JObject)option;
var shippingOption = service.ParseShippingOption(_currencyService);
if (shippingOption != null)
{
shippingOption.Rate = shippingOption.Rate * totalPackages;
shippingOptions.Add(shippingOption);
}
}
}
}
catch (Exception /*ex*/)
{
//if the size or weight of the parcel exceeds the allowable,
//and if for example the set(or not set) the wrong postal code,
//the Australia Post errorMessage returns the error text.
//As a result, the client can not use the services of the service
jToken = parsed["error"]["errorMessage"];
if (jToken != null)
{
var error = (JValue)jToken;
throw new NopException(error.Value.ToString());
}
throw new Exception("Response is not valid.");
}
}
else
{
throw new Exception("Response is not valid.");
}
}
return shippingOptions;
}
#endregion
#region Methods
/// <summary>
/// Gets available shipping options
/// </summary>
/// <param name="getShippingOptionRequest">A request for getting shipping options</param>
/// <returns>Represents a response of getting shipping rate options</returns>
public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
{
if (getShippingOptionRequest == null)
throw new ArgumentNullException("getShippingOptionRequest");
var response = new GetShippingOptionResponse();
if (getShippingOptionRequest.Items == null)
{
response.AddError("No shipment items");
return response;
}
if (string.IsNullOrEmpty(getShippingOptionRequest.ZipPostalCodeFrom))
{
response.AddError("Shipping origin zip is not set");
return response;
}
if(getShippingOptionRequest.ShippingAddress == null)
{
response.AddError("Shipping address is not specified");
return response;
}
var country = _countryService.GetCountryById(getShippingOptionRequest.ShippingAddress.CountryId);
if (country == null)
{
response.AddError("Shipping country is not specified");
return response;
}
if (getShippingOptionRequest.ShippingAddress == null)
{
response.AddError("Shipping address is not set");
return response;
}
if (string.IsNullOrEmpty(getShippingOptionRequest.ShippingAddress.ZipPostalCode))
{
response.AddError("Shipping zip (postal code) is not set");
return response;
}
string zipPostalCodeFrom = getShippingOptionRequest.ZipPostalCodeFrom;
string zipPostalCodeTo = getShippingOptionRequest.ShippingAddress.ZipPostalCode;
int weight = GetWeight(getShippingOptionRequest);
decimal lengthTmp, widthTmp, heightTmp;
_shippingService.GetDimensions(getShippingOptionRequest.Items, out widthTmp, out lengthTmp, out heightTmp);
int length = Math.Max(Convert.ToInt32(Math.Ceiling(this._measureService.ConvertFromPrimaryMeasureDimension(lengthTmp, this.GatewayMeasureDimension))), MIN_LENGTH);
int width = Math.Max(Convert.ToInt32(Math.Ceiling(this._measureService.ConvertFromPrimaryMeasureDimension(widthTmp, this.GatewayMeasureDimension))), MIN_LENGTH);
int height = Math.Max(Convert.ToInt32(Math.Ceiling(this._measureService.ConvertFromPrimaryMeasureDimension(heightTmp, this.GatewayMeasureDimension))), MIN_LENGTH);
//estimate packaging
int totalPackagesDims = 1;
int totalPackagesWeights = 1;
if (length > MAX_LENGTH || width > MAX_LENGTH || height > MAX_LENGTH)
{
totalPackagesDims = Convert.ToInt32(Math.Ceiling((decimal)Math.Max(Math.Max(length, width), height) / MAX_LENGTH));
}
int maxWeight = country.TwoLetterIsoCode.Equals("AU") ? MAX_DOMESTIC_WEIGHT : MAX_INTERNATIONAL_WEIGHT;
if (weight > maxWeight)
{
totalPackagesWeights = Convert.ToInt32(Math.Ceiling((decimal)weight / (decimal)maxWeight));
}
var totalPackages = totalPackagesDims > totalPackagesWeights ? totalPackagesDims : totalPackagesWeights;
if (totalPackages == 0)
totalPackages = 1;
if (totalPackages > 1)
{
//recalculate dims, weight
weight = weight / totalPackages;
height = height / totalPackages;
width = width / totalPackages;
length = length / totalPackages;
if (weight < MIN_WEIGHT)
weight = MIN_WEIGHT;
if (height < MIN_LENGTH)
height = MIN_LENGTH;
if (width < MIN_LENGTH)
width = MIN_LENGTH;
if (length < MIN_LENGTH)
length = MIN_LENGTH;
}
int girth = height + height + width + width;
if (girth < MIN_GIRTH)
{
height = MIN_LENGTH;
width = MIN_LENGTH;
}
if (girth > MAX_GIRTH)
{
height = MAX_LENGTH / 4;
width = MAX_LENGTH / 4;
}
// Australia post takes the dimensions in centimeters and weight in kilograms,
// so dimensions should be converted and rounded up from millimeters to centimeters,
// grams should be converted to kilograms and rounded to two decimal.
length = length / ONE_CENTIMETER + (length % ONE_CENTIMETER > 0 ? 1 : 0);
width = width / ONE_CENTIMETER + (width % ONE_CENTIMETER > 0 ? 1 : 0);
height = height / ONE_CENTIMETER + (height % ONE_CENTIMETER > 0 ? 1 : 0);
var kgWeight = Math.Round(weight / (decimal)ONE_KILO, 2);
try
{
var shippingOptions = RequestShippingOptions(country.TwoLetterIsoCode,
zipPostalCodeFrom, zipPostalCodeTo, kgWeight, length, width, height, totalPackages);
foreach (var shippingOption in shippingOptions)
{
response.ShippingOptions.Add(shippingOption);
}
}
catch (NopException ex)
{
response.AddError(ex.Message);
return response;
}
catch (Exception)
{
response.AddError("Australia Post Service is currently unavailable, try again later");
return response;
}
foreach (var shippingOption in response.ShippingOptions)
{
shippingOption.Rate += _australiaPostSettings.AdditionalHandlingCharge;
}
return response;
}
/// <summary>
/// Gets fixed shipping rate (if shipping rate computation method allows it and the rate can be calculated before checkout).
/// </summary>
/// <param name="getShippingOptionRequest">A request for getting shipping options</param>
/// <returns>Fixed shipping rate; or null in case there's no fixed shipping rate</returns>
public decimal? GetFixedRate(GetShippingOptionRequest getShippingOptionRequest)
{
return null;
}
/// <summary>
/// Gets a route for provider configuration
/// </summary>
/// <param name="actionName">Action name</param>
/// <param name="controllerName">Controller name</param>
/// <param name="routeValues">Route values</param>
public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "Configure";
controllerName = "ShippingAustraliaPost";
routeValues = new RouteValueDictionary { { "Namespaces", "Grand.Plugin.Shipping.AustraliaPost.Controllers" }, { "area", null } };
}
/// <summary>
/// Install plugin
/// </summary>
public override void Install()
{
//settings
var settings = new AustraliaPostSettings
{
AdditionalHandlingCharge = 0
};
_settingService.SaveSetting(settings);
//locales
this.AddOrUpdatePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.ApiKey", "Australia Post API Key");
this.AddOrUpdatePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.ApiKey.Hint", "Specify Australia Post API Key.");
this.AddOrUpdatePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.AdditionalHandlingCharge", "Additional handling charge");
this.AddOrUpdatePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.AdditionalHandlingCharge.Hint", "Enter additional handling fee to charge your customers.");
base.Install();
}
/// <summary>
/// Uninstall plugin
/// </summary>
public override void Uninstall()
{
//settings
_settingService.DeleteSetting<AustraliaPostSettings>();
//locales
this.DeletePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.ApiKey");
this.DeletePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.ApiKey.Hint");
this.DeletePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.AdditionalHandlingCharge");
this.DeletePluginLocaleResource("Plugins.Shipping.AustraliaPost.Fields.AdditionalHandlingCharge.Hint");
base.Uninstall();
}
#endregion
#region Properties
/// <summary>
/// Gets a shipping rate computation method type
/// </summary>
public ShippingRateComputationMethodType ShippingRateComputationMethodType
{
get
{
return ShippingRateComputationMethodType.Realtime;
}
}
/// <summary>
/// Gets a shipment tracker
/// </summary>
public IShipmentTracker ShipmentTracker
{
get { return null; }
}
#endregion
}
}