Skip to content

Commit ab4e34b

Browse files
committed
Custom model binder, Parser service for managing settings in Umbraco 8 and Umbraco 9/10
1 parent f42198d commit ab4e34b

File tree

15 files changed

+275
-86
lines changed

15 files changed

+275
-86
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration
4+
{
5+
public interface ISettingsParser
6+
{
7+
IEnumerable<string> AsEnumerable(string propertyName);
8+
9+
Dictionary<string, string> AsDictionary(string propertyName);
10+
}
11+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#if NETCOREAPP
2+
using Microsoft.AspNetCore.Mvc.ModelBinding;
3+
#else
4+
using System.Web.Http.Controllers;
5+
using System.Web.Http.ModelBinding;
6+
#endif
7+
8+
using System;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
12+
13+
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration
14+
{
15+
public sealed class NotificationModelBinder : IModelBinder
16+
{
17+
#if NETCOREAPP
18+
public Task BindModelAsync(ModelBindingContext bindingContext)
19+
{
20+
if (bindingContext == null)
21+
{
22+
throw new ArgumentNullException(nameof(bindingContext));
23+
}
24+
25+
var transactionId = bindingContext.ValueProvider.GetValue(Constants.NotificationProperty.TransactionId);
26+
27+
var uniqueId = bindingContext.ValueProvider.GetValue(Constants.NotificationProperty.UniqueId);
28+
29+
var status = bindingContext.ValueProvider.GetValue(Constants.NotificationProperty.Status);
30+
31+
var model = new NotificationDto
32+
{
33+
TransactionId = transactionId.FirstValue.ToString(),
34+
UniqueId = uniqueId.FirstValue.ToString(),
35+
Status = status.FirstValue.ToString()
36+
};
37+
38+
bindingContext.Result = ModelBindingResult.Success(model);
39+
40+
return Task.CompletedTask;
41+
}
42+
#else
43+
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
44+
{
45+
var notification = new NotificationDto();
46+
47+
var transactionIdValueResult = bindingContext.ValueProvider
48+
.GetValue(CreateFullPropertyName(bindingContext, Constants.NotificationProperty.TransactionId));
49+
if (transactionIdValueResult != null)
50+
{
51+
notification.TransactionId = transactionIdValueResult.AttemptedValue;
52+
}
53+
54+
var uniqueIdValueResult = bindingContext.ValueProvider.GetValue(CreateFullPropertyName(bindingContext, Constants.NotificationProperty.UniqueId));
55+
if (uniqueIdValueResult != null)
56+
notification.UniqueId= uniqueIdValueResult.AttemptedValue;
57+
58+
var statusValueResult = bindingContext.ValueProvider.GetValue(CreateFullPropertyName(bindingContext, Constants.NotificationProperty.Status));
59+
if (statusValueResult != null)
60+
{
61+
notification.Status = statusValueResult.AttemptedValue;
62+
}
63+
64+
bindingContext.Model = notification;
65+
66+
bindingContext.ValidationNode.ValidateAllProperties = true;
67+
68+
return true;
69+
}
70+
#endif
71+
72+
private string CreateFullPropertyName(ModelBindingContext bindingContext, string propertyName)
73+
{
74+
if (string.IsNullOrEmpty(bindingContext.ModelName))
75+
{
76+
return propertyName;
77+
}
78+
return bindingContext.ModelName + "." + propertyName;
79+
}
80+
81+
}
82+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#if NETCOREAPP
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Options;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration
9+
{
10+
public class ObjectParser : ISettingsParser
11+
{
12+
private readonly PaymentProviderSettings _settings;
13+
14+
public ObjectParser(IOptions<PaymentProviderSettings> options)
15+
{
16+
_settings = options.Value;
17+
}
18+
19+
public Dictionary<string, string> AsDictionary(string propertyName)
20+
{
21+
var property = _settings.GetType().GetProperty(propertyName);
22+
23+
if (property == null) throw new ArgumentNullException(nameof(ObjectParser));
24+
25+
var propertyValues = property.GetValue(_settings) as Dictionary<string, string>;
26+
27+
return propertyValues;
28+
}
29+
30+
public IEnumerable<string> AsEnumerable(string propertyName)
31+
{
32+
var property = _settings.GetType().GetProperty(propertyName);
33+
34+
if (property == null) throw new ArgumentNullException(nameof(ObjectParser));
35+
36+
var propertyValues = property.GetValue(_settings);
37+
38+
return propertyValues != null ? propertyValues as IEnumerable<string> : Enumerable.Empty<string>();
39+
}
40+
}
41+
}
42+
#endif

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Configuration/PaymentProviderSettings.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System.Collections.Specialized;
1+
using System.Collections.Generic;
2+
using System.Collections.Specialized;
23

34
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration
45
{
56
public class PaymentProviderSettings
67
{
78
public PaymentProviderSettings()
89
{
9-
1010
}
1111

1212
public PaymentProviderSettings(NameValueCollection appSettings)
@@ -23,9 +23,13 @@ public PaymentProviderSettings(NameValueCollection appSettings)
2323

2424
Usage = appSettings[Constants.Configuration.UsageKey];
2525

26+
#if NETFRAMEWORK
27+
Currencies = appSettings[Constants.Configuration.CurrenciesKey];
28+
2629
TransactionTypes = appSettings[Constants.Configuration.TransactionTypesKey];
2730

2831
MappingFields = appSettings[Constants.Configuration.MappingFieldsKey];
32+
#endif
2933

3034
UmbracoBaseUrl = appSettings[Constants.Configuration.UmbracoBaseUrlKey];
3135
}
@@ -44,10 +48,20 @@ public PaymentProviderSettings(NameValueCollection appSettings)
4448

4549
public string UmbracoBaseUrl { get; set; }
4650

51+
#if NETCOREAPP
52+
public Dictionary<string, string> Currencies { get; set; }
53+
54+
public string[] TransactionTypes { get; set; }
55+
56+
public string[] MappingFields { get; set; }
57+
#else
4758
public string Currencies { get; set; }
4859

49-
public string TransactionTypes { get; set; }
60+
public string TransactionTypes { get; set; }
61+
62+
public string MappingFields { get; set; }
63+
#endif
64+
5065

51-
public string MappingFields { get; set; }
5266
}
5367
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#if NETFRAMEWORK
2+
using System;
3+
using System.Configuration;
4+
using System.Collections.Generic;
5+
6+
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration;
7+
8+
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Services
9+
{
10+
public class StringParser : ISettingsParser
11+
{
12+
private readonly PaymentProviderSettings _settings;
13+
14+
public StringParser()
15+
{
16+
_settings = new PaymentProviderSettings(ConfigurationManager.AppSettings);
17+
}
18+
19+
public IEnumerable<string> AsEnumerable(string propertyName)
20+
{
21+
var property = _settings.GetType().GetProperty(propertyName);
22+
23+
if (property == null) throw new ArgumentNullException(nameof(StringParser));
24+
25+
return property.GetValue(_settings).ToString().Split(';');
26+
}
27+
28+
public Dictionary<string, string> AsDictionary(string propertyName)
29+
{
30+
var dict = new Dictionary<string, string>();
31+
32+
var property = _settings.GetType().GetProperty(propertyName);
33+
34+
if (property == null) throw new ArgumentNullException(nameof(StringParser));
35+
36+
var propertyValues = property.GetValue(_settings).ToString().Split(';');
37+
38+
foreach(var propertyValue in propertyValues)
39+
{
40+
var value = propertyValue.Split(',');
41+
42+
if(value.Length > 1)
43+
dict.Add(value[0], value[1]);
44+
}
45+
46+
return dict;
47+
}
48+
49+
}
50+
}
51+
#endif

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Constants.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,14 @@ public static class RootNode
5151

5252
public const string WpfReconcile = "wpf_reconcile";
5353
}
54+
55+
public static class NotificationProperty
56+
{
57+
public const string TransactionId = "wpf_transaction_id";
58+
59+
public const string UniqueId = "wpf_unique_id";
60+
61+
public const string Status = "wpf_status";
62+
}
5463
}
5564
}

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Controllers/PaymentProviderController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public PaymentProviderController(PaymentService paymentService, IRecordStorage r
4949
}
5050

5151
[HttpPost]
52+
#if NETCOREAPP
53+
public HttpResponseMessage NotifyPayment(string formId, string recordUniqueId, string statusFieldId, [FromForm] NotificationDto notificationDto)
54+
#else
5255
public HttpResponseMessage NotifyPayment(string formId, string recordUniqueId, string statusFieldId, [FromBody] NotificationDto notificationDto)
56+
#endif
5357
{
5458
try
5559
{

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Helpers/CurrencyHelper.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
11
using System.Collections.Generic;
2-
32
using System.Linq;
43

5-
#if NETCOREAPP
6-
using Microsoft.Extensions.Options;
7-
#else
8-
using System.Configuration;
9-
#endif
10-
114
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration;
125
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
136

147
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers
158
{
169
public class CurrencyHelper
1710
{
18-
#if NETCOREAPP
19-
private readonly PaymentProviderSettings _paymentProviderSettings;
11+
private readonly ISettingsParser _settingsParser;
2012

21-
public CurrencyHelper(IOptions<PaymentProviderSettings> options)
13+
public CurrencyHelper(ISettingsParser settingsParser)
2214
{
23-
_paymentProviderSettings = options.Value;
15+
_settingsParser = settingsParser;
2416
}
25-
#endif
2617

2718
public IEnumerable<CurrencyDto> GetCurrencies()
2819
{
29-
#if NETCOREAPP
30-
var currencySettings = _paymentProviderSettings.Currencies;
31-
#else
32-
var currencySettings = ConfigurationManager.AppSettings[Constants.Configuration.CurrenciesKey];
33-
#endif
20+
var currencies = _settingsParser.AsDictionary(nameof(PaymentProviderSettings.Currencies));
3421

35-
if (string.IsNullOrEmpty(currencySettings)) return Enumerable.Empty<CurrencyDto>();
22+
if (currencies.Count == 0) return Enumerable.Empty<CurrencyDto>();
3623

3724
try
3825
{
39-
var currenciesArr = currencySettings.Split(';');
40-
41-
return currenciesArr.Select(p =>
42-
{
43-
var currencyObj = p.Split(',');
44-
return new CurrencyDto { Code = currencyObj[0], Name = currencyObj[1] };
45-
});
26+
return currencies.Select(p => new CurrencyDto { Code = p.Key, Name = p.Value });
4627
}
4728
catch
4829
{

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/Helpers/MappingFieldHelper.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
#if NETCOREAPP
2-
using Microsoft.Extensions.Options;
3-
#else
4-
using System.Configuration;
5-
#endif
6-
7-
using System.Collections.Generic;
1+
using System.Collections.Generic;
82
using System.Linq;
93

104
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration;
@@ -13,33 +7,20 @@ namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers
137
{
148
public class MappingFieldHelper
159
{
16-
private readonly PaymentProviderSettings _paymentProviderSettings;
10+
private readonly ISettingsParser _settingsParser;
1711

18-
#if NETCOREAPP
19-
public MappingFieldHelper(IOptions<PaymentProviderSettings> options)
20-
{
21-
_paymentProviderSettings = options.Value;
22-
}
23-
#else
24-
public MappingFieldHelper()
12+
public MappingFieldHelper(ISettingsParser settingsParser)
2513
{
26-
_paymentProviderSettings = new PaymentProviderSettings(ConfigurationManager.AppSettings);
14+
_settingsParser = settingsParser;
2715
}
28-
#endif
2916

3017
public IEnumerable<string> GetMappings()
3118
{
32-
if (string.IsNullOrEmpty(_paymentProviderSettings.MappingFields))
33-
return Enumerable.Empty<string>();
34-
35-
try
36-
{
37-
return _paymentProviderSettings.MappingFields.Split(';');
38-
}
39-
catch
40-
{
41-
return Enumerable.Empty<string>();
42-
}
19+
var mappings = _settingsParser.AsEnumerable(nameof(PaymentProviderSettings.MappingFields));
20+
21+
if (mappings.Count() == 0) return Enumerable.Empty<string>();
22+
23+
return mappings;
4324

4425
}
4526

0 commit comments

Comments
 (0)