-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStampsShipmentProvider.cs
More file actions
128 lines (108 loc) · 4.4 KB
/
Copy pathStampsShipmentProvider.cs
File metadata and controls
128 lines (108 loc) · 4.4 KB
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
using EasyKeys.Shipping.Abstractions.Extensions;
using EasyKeys.Shipping.Abstractions.Models;
using EasyKeys.Shipping.Stamps.Abstractions.Services;
using EasyKeys.Shipping.Stamps.Rates.Extensions;
using EasyKeys.Shipping.Stamps.Rates.Models;
using EasyKeys.Shipping.Stamps.Shipment.Extensions;
using EasyKeys.Shipping.Stamps.Shipment.Models;
using Microsoft.Extensions.Logging;
using StampsClient.v111;
namespace EasyKeys.Shipping.Stamps.Shipment;
public class StampsShipmentProvider : IStampsShipmentProvider
{
private readonly IStampsClientService _stampsClient;
private readonly ILogger<StampsShipmentProvider> _logger;
public StampsShipmentProvider(
IStampsClientService stampsClientService,
ILogger<StampsShipmentProvider> logger)
{
_stampsClient = stampsClientService ?? throw new ArgumentNullException(nameof(stampsClientService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<ShipmentLabel> CreateShipmentAsync(
Shipping.Abstractions.Models.Shipment shipment,
RateOptions rateOptions,
ShipmentDetails shipmentDetails,
CancellationToken cancellationToken = default)
{
var request = new CreateIndiciumRequest().MapToShipmentRequest(
isDomestic: shipment.IsDomestic(),
weightLb: (double)shipment.GetTotalWeight(),
shipmentDetails: shipmentDetails,
rateOptions: rateOptions);
request.Rate = new RateV40().MapToRate(shipment, rateOptions);
request.RedirectTo = request.Rate.From;
return await GetLabelAsync(request, shipmentDetails.LabelOptions.ImageType, cancellationToken);
}
public async Task<ShipmentCancelledResult> CancelShipmentAsync(string trackingId, CancellationToken cancellationToken = default)
{
var result = new ShipmentCancelledResult();
try
{
var request = new CancelIndiciumRequest()
{
Item1 = trackingId
};
await _stampsClient.CancelIndiciumAsync(request, cancellationToken);
return result;
}
catch (Exception ex)
{
result.Errors.Add(ex.Message);
}
return result;
}
private async Task<ShipmentLabel> GetLabelAsync(CreateIndiciumRequest request, Models.ImageType imageType, CancellationToken cancellationToken)
{
var shipmentLabel = new ShipmentLabel();
try
{
var response = await _stampsClient.CreateIndiciumAsync(request, cancellationToken);
var surcharge = SetSurcharges(response);
shipmentLabel.Labels = new List<PackageLabelDetails>()
{
new PackageLabelDetails()
{
ProviderLabelId = response.StampsTxID.ToString(),
ImageType = imageType.Name,
TrackingId = response.TrackingNumber,
Bytes = response.ImageData.ToList(),
TotalCharges = new ShipmentCharges()
{
SurchargesList = surcharge,
Surcharges = surcharge.Values.Sum(),
NetCharge = response.Rate.Amount,
BaseCharge = response.Rate.Amount,
},
TotalCharges2 = new ShipmentCharges()
{
SurchargesList = surcharge,
Surcharges = surcharge.Values.Sum(),
NetCharge = response.Rate.Amount,
BaseCharge = response.Rate.Amount,
}
}
};
}
catch (Exception ex)
{
var error = ex?.InnerException?.Message ?? ex?.Message ?? string.Empty;
_logger.LogError("{name} : {message}", nameof(StampsShipmentProvider), error);
shipmentLabel.InternalErrors.Add(error);
}
return shipmentLabel;
}
private Dictionary<string, decimal> SetSurcharges(CreateIndiciumResponse response)
{
var surcharges = new Dictionary<string, decimal>();
if (response?.Rate?.Surcharges == null)
{
return surcharges;
}
foreach (var surcharge in response.Rate.Surcharges)
{
surcharges[surcharge.SurchargeType.ToString()] = surcharge.Amount;
}
return surcharges;
}
}