Skip to content

Commit a2aabf5

Browse files
committed
PR updates
1 parent 1737619 commit a2aabf5

File tree

8 files changed

+98
-80
lines changed

8 files changed

+98
-80
lines changed

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currencies.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Umbraco.Forms.Integrations.Commerce.EMerchantPay/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currency.controller.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var vm = this;
44

5+
if ($scope.setting && $scope.setting.value) vm.selectedCurrency = $scope.setting.value;
6+
57
umbracoFormsIntegrationsCommerceEMerchantPayResource.getCurrencies().then(function (response) {
68
vm.currencies = response;
79
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public PaymentProviderSettings(NameValueCollection appSettings)
1919

2020
Password = appSettings[Constants.Configuration.PasswordKey];
2121

22+
Supplier = appSettings[Constants.Configuration.SupplierKey];
23+
24+
Usage = appSettings[Constants.Configuration.UsageKey];
25+
2226
UmbracoBaseUrl = appSettings[Constants.Configuration.UmbracoBaseUrlKey];
2327
}
2428

@@ -30,6 +34,12 @@ public PaymentProviderSettings(NameValueCollection appSettings)
3034

3135
public string Password { get; set; }
3236

37+
public string Usage { get; set; }
38+
39+
public string Supplier { get; set; }
40+
3341
public string UmbracoBaseUrl { get; set; }
42+
43+
public string Currencies { get; set; }
3444
}
3545
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public static class Configuration
3333

3434
public const string PasswordKey = "Umbraco.Forms.Integrations.Commerce.eMerchantPay.Password";
3535

36+
public const string SupplierKey = "Umbraco.Forms.Integrations.Commerce.eMerchantPay.Supplier";
37+
38+
public const string UsageKey = "Umbraco.Forms.Integrations.Commerce.eMerchantPay.Usage";
39+
40+
public const string CurrenciesKey = "Umbraco.Forms.Integrations.Commerce.eMerchantPay.Currencies";
41+
3642
public const string UmbracoBaseUrlKey = "Umbraco.Forms.Integrations.Commerce.eMerchantPay.UmbracoBaseUrl";
3743
}
3844

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

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.CompilerServices;
56
using System.Threading;
67

78
#if NETCOREAPP
@@ -18,7 +19,7 @@
1819
using Umbraco.Web.WebApi;
1920
#endif
2021
using Newtonsoft.Json;
21-
22+
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers;
2223
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
2324

2425

@@ -27,47 +28,14 @@ namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Controllers
2728
[PluginController("UmbracoFormsIntegrationsCommerceEmerchantPay")]
2829
public class EmerchantPayController : UmbracoAuthorizedApiController
2930
{
30-
private static readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
31-
32-
#if NETCOREAPP
33-
private readonly IWebHostEnvironment _webHostEnvironment;
31+
private readonly CurrencyHelper _currencyHelper;
3432

35-
public EmerchantPayController(IWebHostEnvironment webHostEnvironment)
33+
public EmerchantPayController(CurrencyHelper currencyHelper)
3634
{
37-
_webHostEnvironment = webHostEnvironment;
35+
_currencyHelper = currencyHelper;
3836
}
39-
#endif
4037

4138
[HttpGet]
42-
public IEnumerable<CurrencyDto> GetCurrencies()
43-
{
44-
#if NETCOREAPP
45-
string currenciesFilePath =
46-
$"{_webHostEnvironment.ContentRootPath}/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currencies.json";
47-
#else
48-
string currenciesFilePath =
49-
HttpContext.Current.Server.MapPath(
50-
"~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currencies.json");
51-
#endif
52-
53-
_lock.EnterReadLock();
54-
55-
try
56-
{
57-
var content = System.IO.File.ReadAllText(currenciesFilePath);
58-
59-
var d = JsonConvert.DeserializeObject<IEnumerable<CurrencyDto>>(content);
60-
61-
return d;
62-
}
63-
catch (FileNotFoundException ex)
64-
{
65-
return Enumerable.Empty<CurrencyDto>();
66-
}
67-
catch (Exception ex)
68-
{
69-
return Enumerable.Empty<CurrencyDto>();
70-
}
71-
}
39+
public IEnumerable<CurrencyDto> GetCurrencies() => _currencyHelper.GetCurrencies();
7240
}
7341
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
3+
using System.Linq;
4+
5+
#if NETCOREAPP
6+
using Microsoft.Extensions.Options;
7+
#else
8+
using System.Configuration;
9+
#endif
10+
11+
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration;
12+
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Models.Dtos;
13+
14+
namespace Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers
15+
{
16+
public class CurrencyHelper
17+
{
18+
#if NETCOREAPP
19+
private readonly PaymentProviderSettings _paymentProviderSettings;
20+
21+
public CurrencyHelper(IOptions<PaymentProviderSettings> options)
22+
{
23+
_paymentProviderSettings = options.Value;
24+
}
25+
#endif
26+
27+
public IEnumerable<CurrencyDto> GetCurrencies()
28+
{
29+
#if NETCOREAPP
30+
var currencySettings = _paymentProviderSettings.Currencies;
31+
#else
32+
var currencySettings = ConfigurationManager.AppSettings[Constants.Configuration.CurrenciesKey];
33+
#endif
34+
35+
if (string.IsNullOrEmpty(currencySettings)) return Enumerable.Empty<CurrencyDto>();
36+
37+
try
38+
{
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+
});
46+
}
47+
catch
48+
{
49+
return Enumerable.Empty<CurrencyDto>();
50+
}
51+
52+
}
53+
54+
}
55+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Umbraco.Cms.Core.Composing;
55
using Umbraco.Cms.Core.DependencyInjection;
6+
using Umbraco.Forms.Core.Providers;
67
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Configuration;
78
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Helpers;
89
using Umbraco.Forms.Integrations.Commerce.EMerchantPay.Services;
@@ -24,11 +25,16 @@ public void Compose(IUmbracoBuilder builder)
2425
.AddOptions<PaymentProviderSettings>()
2526
.Bind(builder.Config.GetSection(Constants.Configuration.Settings));
2627

28+
builder.WithCollectionBuilder<WorkflowCollectionBuilder>()
29+
.Add<PaymentProviderWorkflow>();
30+
2731
builder.Services.AddSingleton<ConsumerService>();
2832

2933
builder.Services.AddSingleton<PaymentService>();
3034

3135
builder.Services.AddSingleton<UrlHelper>();
36+
37+
builder.Services.AddSingleton<CurrencyHelper>();
3238
}
3339
#else
3440
public void Compose(Composition composition)
@@ -38,6 +44,8 @@ public void Compose(Composition composition)
3844
composition.Register<PaymentService>(Lifetime.Singleton);
3945

4046
composition.Register<UrlHelper>(Lifetime.Singleton);
47+
48+
composition.Register<CurrencyHelper>(Lifetime.Singleton);
4149
}
4250
#endif
4351
}

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ public class PaymentProviderWorkflow : WorkflowType
4141

4242
#region WorkflowSettings
4343

44-
[Core.Attributes.Setting("Usage",
45-
Description = "Payment usage description",
46-
View = "TextField")]
47-
public string Usage { get; set; }
48-
4944
[Core.Attributes.Setting("Amount",
5045
Description = "Payment amount (without decimals)",
5146
View = "TextField")]
@@ -56,11 +51,6 @@ public class PaymentProviderWorkflow : WorkflowType
5651
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/currency.html")]
5752
public string Currency { get; set; }
5853

59-
[Core.Attributes.Setting("Supplier",
60-
Description = "Name of business supplier",
61-
View = "TextField")]
62-
public string Supplier { get; set; }
63-
6454
[Core.Attributes.Setting("CustomerDetails",
6555
Description = "Map customer details with form fields",
6656
View = "~/App_Plugins/UmbracoForms.Integrations/Commerce/eMerchantPay/customer-details-mapper.html")]
@@ -154,7 +144,7 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
154144
var payment = new PaymentDto
155145
{
156146
TransactionId = transactionId.ToString(),
157-
Usage = Usage,
147+
Usage = _paymentProviderSettings.Usage,
158148
NotificationUrl = $"{_paymentProviderSettings.UmbracoBaseUrl}/umbraco/api/paymentprovider/notifypayment?formId={formId}&recordUniqueId={recordUniqueId}&statusFieldId={statusKey}",
159149
ReturnSuccessUrl = _urlHelper.GetPageUrl(int.Parse(SuccessUrl)),
160150
ReturnFailureUrl = _urlHelper.GetPageUrl(int.Parse(FailureUrl)),
@@ -175,7 +165,7 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
175165
State = mappingBuilder.State,
176166
Country = mappingBuilder.Country
177167
},
178-
BusinessAttribute = new BusinessAttribute { NameOfTheSupplier = Supplier },
168+
BusinessAttribute = new BusinessAttribute { NameOfTheSupplier = _paymentProviderSettings.Supplier },
179169
TransactionTypes = new TransactionTypeDto
180170
{
181171
TransactionTypes = new List<TransactionTypeRecordDto>
@@ -220,26 +210,23 @@ public override List<Exception> ValidateSettings()
220210
{
221211
var list = new List<Exception>();
222212

223-
if(string.IsNullOrEmpty(Usage)) list.Add(new Exception("Usage value is not valid."));
213+
if (string.IsNullOrEmpty(Amount) || !int.TryParse(Amount, out _))
214+
list.Add(new Exception("Amount value is not valid."));
224215

225-
if(string.IsNullOrEmpty(Amount) || !int.TryParse(Amount, out _)) list.Add(new Exception("Amount value is not valid."));
216+
if (string.IsNullOrEmpty(Currency)) list.Add(new Exception("Currency field is required."));
226217

227-
if(string.IsNullOrEmpty(Currency)) list.Add(new Exception("Currency field is required."));
218+
if (!CustomerDetailsMappings.TryParseMappings(out _))
219+
list.Add(new Exception("Customer details mappings are required."));
228220

229-
if(!CustomerDetailsMappings.TryParseMappings(out _)) list.Add(new Exception("Customer details mappings are required."));
221+
if (!SuccessUrl.IsContentValid(nameof(SuccessUrl), out var successError))
222+
list.Add(new Exception(successError));
230223

231-
if (!SuccessUrl.IsContentValid(nameof(SuccessUrl), out var successError)) list.Add(new Exception(successError));
232-
233-
if (!FailureUrl.IsContentValid(nameof(FailureUrl), out var failureError)) list.Add(new Exception(failureError));
224+
if (!FailureUrl.IsContentValid(nameof(FailureUrl), out var failureError))
225+
list.Add(new Exception(failureError));
234226

235227
if (!CancelUrl.IsContentValid(nameof(CancelUrl), out var cancelError)) list.Add(new Exception(cancelError));
236228

237229
return list;
238230
}
239-
240-
public static string X()
241-
{
242-
return string.Empty;
243-
}
244231
}
245232
}

0 commit comments

Comments
 (0)