-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
351 lines (318 loc) · 12.9 KB
/
Helpers.cs
File metadata and controls
351 lines (318 loc) · 12.9 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
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
using Dynamicweb.Core;
using Dynamicweb.Ecommerce.International;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Extensions;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Environment;
using Dynamicweb.Security.UserManagement;
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors;
using Dynamicweb.Ecommerce.Orders.Discounts;
namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration
{
/// <summary>
/// Class Helpers.
/// </summary>
internal static class Helpers
{
/// <summary>
/// Adds the child XML node to cart.
/// </summary>
/// <param name="xDoc">The x document.</param>
/// <param name="parent">The parent.</param>
/// <param name="nodeName">Name of the node.</param>
/// <param name="nodeValue">The node value.</param>
public static void AddChildXmlNodeToCart(XmlDocument xDoc, XmlElement parent, string nodeName, string nodeValue)
{
XmlElement node = xDoc.CreateElement("column");
node.SetAttribute("columnName", nodeName);
node.InnerText = nodeValue;
parent.AppendChild(node);
}
/// <summary>
/// Adds the child XML node to product list.
/// </summary>
/// <param name="xDoc">The x document.</param>
/// <param name="parent">The parent.</param>
/// <param name="nodeName">Name of the node.</param>
/// <param name="nodeValue">The node value.</param>
public static void AddChildXmlNodeToProductList(XmlDocument xDoc, XmlElement parent, string nodeName, string nodeValue)
{
XmlElement node = xDoc.CreateElement(nodeName);
node.InnerText = nodeValue;
parent.AppendChild(node);
}
/// <summary>
/// Returns the relevant Enum Value. If input can be parsed to Enum type, then the enum value is returned. Otherwise defaultValue is returned.
/// </summary>
/// <typeparam name="T">Type of Enum to parse value from</typeparam>
/// <param name="input">To be parsed as Enum value.</param>
/// <param name="defaultValue">Default value if input was not valid enum value.</param>
/// <returns>Parsed or default enum value</returns>
public static T GetEnumValueFromString<T>(string input, T defaultValue)
{
if (string.IsNullOrEmpty(input))
{
return defaultValue;
}
T value;
try
{
value = (T)Enum.Parse(typeof(T), input, true);
}
catch (Exception)
{
return defaultValue;
}
return value;
}
/// <summary>
/// Compares two prices.
/// </summary>
/// <param name="price1">The price to compare.</param>
/// <param name="price2">The price to compare with.</param>
/// <returns></returns>
public static bool IsDifferentPrice(double price1, double price2)
{
return IsDifferent(price1, price2);
}
/// <summary>
/// Compares two stock levels.
/// </summary>
/// <param name="stock1">The stock to compare.</param>
/// <param name="stock2">The stock to compare with.</param>
public static bool IsDifferentStock(double stock1, double stock2)
{
return IsDifferent(stock1, stock2);
}
/// <summary>
/// Orders the identifier.
/// </summary>
/// <param name="order">The order.</param>
/// <returns>System.String.</returns>
public static string OrderIdentifier(Order order)
{
string ret = $"{order.Id}.{order.CurrencyCode}";
foreach (OrderLine ol in order.OrderLines)
{
ret += $".{ol.Quantity}.{ol.ProductId}.{ol.ProductVariantId}.{ol.ProductNumber}.{ol.ProductName}.{ol.OrderLineType}";
}
return ret;
}
/// <summary>
/// Parses the response to XML.
/// </summary>
/// <param name="response">The response.</param>
/// <param name="responseDoc">The response document.</param>
/// <returns><c>true</c> if response was parsed to xml, <c>false</c> otherwise.</returns>
public static bool ParseResponseToXml(string response, Logger logger, out XmlDocument responseDoc)
{
bool result = true;
responseDoc = new XmlDocument();
try
{
responseDoc.LoadXml(response);
}
catch (Exception e)
{
result = false;
logger.Log(ErrorLevel.ResponseError, $"Response is not valid XML format: '{e.Message}'.");
}
return result;
}
/// <summary>
/// Converts the specified value to a double.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.Double.</returns>
public static double ToDouble(Settings settings, Logger logger, string value)
{
double result;
CultureInfo cultureInfo = GetCultureInfo(settings, logger);
if (cultureInfo != null)
{
try
{
result = double.Parse(value, cultureInfo);
}
catch (FormatException ex)
{
logger.Log(ErrorLevel.Error, $"Error parse '{value}' to double in '{cultureInfo.Name}' culture: '{ex.Message}'.");
result = Converter.ToDouble(value);
}
}
else
{
result = Converter.ToDouble(value);
}
return result;
}
/// <summary>
/// Gets the culture information.
/// </summary>
/// <returns>CultureInfo.</returns>
internal static CultureInfo GetCultureInfo(Settings settings, Logger logger)
{
string culture = settings.NumberFormatCulture;
CultureInfo result;
if (!string.IsNullOrEmpty(culture))
{
result = GetCultureInfo(culture, logger, true);
}
else
{
result = CultureInfo.InvariantCulture;
}
return result;
}
private static bool IsDifferent(double x, double y)
{
var tolerance = 0.0000001;
var isDifferent = Math.Abs(x - y) > tolerance;
return isDifferent;
}
internal static CultureInfo GetCultureInfo(string cultureInfo, Logger logger, bool logException = false)
{
CultureInfo result = null;
if (!string.IsNullOrEmpty(cultureInfo))
{
try
{
result = CultureInfo.GetCultureInfo(cultureInfo);
}
catch (CultureNotFoundException exception)
{
if (logException)
{
logger.Log(ErrorLevel.Error, $"Culture '{cultureInfo}' not found. Message: '{exception.Message}'.");
}
}
}
return result;
}
internal static string GetDiscountValue(Settings settings, Discount discount, OrderLine orderLine, Logger logger)
{
string discountValue = string.Empty;
switch (discount.DiscountType)
{
case DiscountTypes.Amount:
discountValue = discount.Amount.ToIntegrationString(settings, logger);
break;
case DiscountTypes.Percentage:
discountValue = discount.Percentage.ToIntegrationString(settings, logger);
break;
case DiscountTypes.Product:
discountValue = orderLine.ProductId;
break;
case DiscountTypes.AmountFromField:
discountValue = discount.AmountProductFieldName;
break;
case DiscountTypes.Shipping:
discountValue = discount.ShippingAmount.ToIntegrationString(settings, logger) + (discount.ShippingAmount != 0 ? orderLine.Order.Currency.Code : string.Empty);
break;
}
return discountValue;
}
internal static void SaveTranslation(string key, string value)
{
Rendering.Translation.Translation.SetTranslation(key, value, ExecutingContext.GetCulture(), Rendering.Translation.KeyScope.DesignsShared, null, null);
}
internal static string GetTranslation(string key)
{
string result = null;
if (Context.Current?.Items != null && Context.Current.Items.Contains(key))
{
result = (string)Context.Current.Items[key];
}
else
{
Rendering.Translation.TranslationEntryCollection translations = Rendering.Translation.Translation.GetTranslations(key,
Rendering.Translation.KeyScope.DesignsShared, null);
if (translations?.Values != null)
{
CultureInfo culture = ExecutingContext.GetCulture(true);
Rendering.Translation.TranslationEntry translationEntry = translations.Values.FirstOrDefault(v => string.Equals(v?.CultureName, culture?.Name, StringComparison.OrdinalIgnoreCase));
if (translationEntry != null && !string.IsNullOrEmpty(translationEntry.Value))
{
result = translationEntry.Value;
}
if (Context.Current?.Items != null)
Context.Current.Items[key] = result;
}
}
return result;
}
internal static User GetCurrentExtranetUser()
{
var user = UserContext.Current.User;
if (user == null && !string.IsNullOrEmpty(Context.Current?.Request?["UserId"]))
{
var userId = Core.Converter.ToInt32(Security.SystemTools.Crypto.Decrypt(Context.Current.Request["UserId"]));
user = UserManagementServices.Users.GetUserById(userId);
}
return user;
}
internal static string GetStateLabel(OrderState state)
{
var orderFlow = Services.OrderFlows.GetAllFlows().FirstOrDefault(of => of.ID.Equals(state.OrderFlowId));
if (orderFlow == null)
{
return state.GetName(Services.Languages.GetDefaultLanguageId());
}
return string.Concat(state.GetName(Services.Languages.GetDefaultLanguageId()), " (", orderFlow.Name, ")");
}
internal static string GetRequestUnitId()
{
return Context.Current?.Request?["UnitID"];
}
internal static bool CanCheckPrice(Settings settings, Product product, User user)
{
if (!Global.IsIntegrationActive(settings)
|| !settings.EnableLivePrices
|| Global.IsProductLazyLoad(settings)
|| (user == null && !settings.LiveProductInfoForAnonymousUsers)
|| (user != null && user.IsLiveIntegrationPricesDisabled())
|| !Connector.IsWebServiceConnectionAvailable(settings, SubmitType.Live)
|| product == null
|| string.IsNullOrEmpty(product.Id)
|| string.IsNullOrEmpty(product.Number))
{
return false;
}
return true;
}
internal static Currency GetCurrentCurrency()
{
var currency = Common.Context.Currency;
if (currency == null && !string.IsNullOrEmpty(Context.Current?.Request?["CurrencyId"]))
{
currency = Services.Currencies.GetCurrency(Context.Current?.Request?["CurrencyId"]);
}
return currency;
}
#region Session Hash helper methods
/// <summary>
/// Calculates the hash.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.String.</returns>
internal static string CalculateHash(string request)
{
using var hashEngine = new System.Security.Cryptography.HMACMD5(new byte[] { 14, 4, 78 });
byte[] hash = hashEngine.ComputeHash(Encoding.UTF8.GetBytes(request));
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += $"{x:x2}";
}
return hashString;
}
#endregion Session Hash helper methods
}
}