Skip to content

Commit

Permalink
Merge pull request #4 from nixtus/nopCommerce-440
Browse files Browse the repository at this point in the history
feat: upgrade to nopCommerce 4.40
  • Loading branch information
mewajda authored Oct 27, 2021
2 parents 87bc764 + e76b520 commit 4f22c65
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 200 deletions.
36 changes: 23 additions & 13 deletions Nixtus.Plugin.Payments.Nmi/Components/NmiViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand Down Expand Up @@ -36,19 +37,28 @@ public NmiViewComponent(IWorkContext workContext, ILogger logger, NmiPaymentSett
_genericAttributeService = genericAttributeService;
}

public IViewComponentResult Invoke()
/// <summary>
/// Invoke view component
/// </summary>
/// <param name="widgetZone">Widget zone name</param>
/// <param name="additionalData">Additional data</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the view component result
/// </returns>
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{
var model = new PaymentInfoModel
{
IsGuest = _customerService.IsGuest(_workContext.CurrentCustomer),
IsGuest = await _customerService.IsGuestAsync(await _workContext.GetCurrentCustomerAsync()),
AllowCustomerToSaveCards = _nmiPaymentSettings.AllowCustomerToSaveCards
};

if (_nmiPaymentSettings.AllowCustomerToSaveCards)
{
if (!string.IsNullOrEmpty(_genericAttributeService.GetAttribute<string>(_workContext.CurrentCustomer, Constants.CustomerVaultIdKey)))
if (!string.IsNullOrEmpty(await _genericAttributeService.GetAttributeAsync<string>(await _workContext.GetCurrentCustomerAsync(), Constants.CustomerVaultIdKey)))
{
PopulateStoredCards(model);
await PopulateStoredCards(model);
}

model.StoredCards.Insert(0, new SelectListItem { Text = "Select a card...", Value = "0" });
Expand All @@ -57,7 +67,7 @@ public IViewComponentResult Invoke()
return View("~/Plugins/Payments.Nmi/Views/PaymentInfo.cshtml", model);
}

private void PopulateStoredCards(PaymentInfoModel model)
private async Task PopulateStoredCards(PaymentInfoModel model)
{
try
{
Expand All @@ -66,16 +76,16 @@ private void PopulateStoredCards(PaymentInfoModel model)
{ "username", _nmiPaymentSettings.Username },
{ "password", _nmiPaymentSettings.Password },
{ "report_type", "customer_vault" },
{ "customer_vault_id", _genericAttributeService.GetAttribute<string>(_workContext.CurrentCustomer, Constants.CustomerVaultIdKey) },
{ "customer_vault_id", await _genericAttributeService.GetAttributeAsync<string>(await _workContext.GetCurrentCustomerAsync(), Constants.CustomerVaultIdKey) },
// this is an undocumented variable which will make the API return multiple billings (aka credit cards)
{ "ver", "2" }
};

var response = _httpClient.PostAsync(NMI_QUERY_URL, new FormUrlEncodedContent(values)).Result;
var response = await _httpClient.PostAsync(NMI_QUERY_URL, new FormUrlEncodedContent(values));
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
var nmiQueryResponse = DeserializeXml(content);
var content = await response.Content.ReadAsStringAsync();
var nmiQueryResponse = await DeserializeXml(content);

if (nmiQueryResponse?.CustomerVault != null)
{
Expand All @@ -92,18 +102,18 @@ private void PopulateStoredCards(PaymentInfoModel model)
}
else
{
_logger.Warning($"No saved cards where found in the response from NMI, Response: {content}");
await _logger.WarningAsync($"No saved cards where found in the response from NMI, Response: {content}");
}
}

}
catch (Exception exception)
{
_logger.Error("NMI Error querying customer vault records", exception);
await _logger.ErrorAsync("NMI Error querying customer vault records", exception);
}
}

private NmiQueryResponse DeserializeXml(string xml)
private async Task<NmiQueryResponse> DeserializeXml(string xml)
{
try
{
Expand All @@ -116,7 +126,7 @@ private NmiQueryResponse DeserializeXml(string xml)
}
catch (Exception e)
{
_logger.Error("Error parsing xml", e);
await _logger.ErrorAsync("Error parsing xml", e);
return null;
}
}
Expand Down
70 changes: 35 additions & 35 deletions Nixtus.Plugin.Payments.Nmi/Controllers/PaymentNmiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Nixtus.Plugin.Payments.Nmi.Models;
using Nop.Core;
Expand All @@ -14,6 +15,9 @@

namespace Nixtus.Plugin.Payments.Nmi.Controllers
{
[AuthorizeAdmin]
[Area(AreaNames.Admin)]
[AutoValidateAntiforgeryToken]
public class PaymentNmiController : BasePaymentController
{
private readonly ILocalizationService _localizationService;
Expand All @@ -39,16 +43,14 @@ public PaymentNmiController(ILocalizationService localizationService,
_notificationService = notificationService;
}

[AuthorizeAdmin]
[Area(AreaNames.Admin)]
public IActionResult Configure()
public async Task<IActionResult> Configure()
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManagePaymentMethods))
if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManagePaymentMethods))
return AccessDeniedView();

//load settings for a chosen store scope
var storeScope = _storeContext.ActiveStoreScopeConfiguration;
var nmiPaymentSettings = _settingService.LoadSetting<NmiPaymentSettings>(storeScope);
var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();
var nmiPaymentSettings = await _settingService.LoadSettingAsync<NmiPaymentSettings>(storeScope);

var model = new ConfigurationModel
{
Expand All @@ -59,42 +61,40 @@ public IActionResult Configure()
SecurityKey = nmiPaymentSettings.SecurityKey,
CollectJsTokenizationKey = nmiPaymentSettings.CollectJsTokenizationKey,
TransactModeId = Convert.ToInt32(nmiPaymentSettings.TransactMode),
TransactModeValues = nmiPaymentSettings.TransactMode.ToSelectList(),
TransactModeValues = await nmiPaymentSettings.TransactMode.ToSelectListAsync(),
AdditionalFee = nmiPaymentSettings.AdditionalFee,
AdditionalFeePercentage = nmiPaymentSettings.AdditionalFeePercentage,
ActiveStoreScopeConfiguration = storeScope
};

if (storeScope > 0)
{
model.Username_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.Username, storeScope);
model.Password_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.Password, storeScope);
model.UseUsernamePassword_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.UseUsernamePassword, storeScope);
model.AllowCustomerToSaveCards_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.AllowCustomerToSaveCards, storeScope);
model.SecurityKey_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.SecurityKey, storeScope);
model.CollectJsTokenizationKey_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.CollectJsTokenizationKey, storeScope);
model.TransactModeId_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.TransactMode, storeScope);
model.AdditionalFee_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.AdditionalFee, storeScope);
model.AdditionalFeePercentage_OverrideForStore = _settingService.SettingExists(nmiPaymentSettings, x => x.AdditionalFeePercentage, storeScope);
model.Username_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.Username, storeScope);
model.Password_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.Password, storeScope);
model.UseUsernamePassword_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.UseUsernamePassword, storeScope);
model.AllowCustomerToSaveCards_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.AllowCustomerToSaveCards, storeScope);
model.SecurityKey_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.SecurityKey, storeScope);
model.CollectJsTokenizationKey_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.CollectJsTokenizationKey, storeScope);
model.TransactModeId_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.TransactMode, storeScope);
model.AdditionalFee_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.AdditionalFee, storeScope);
model.AdditionalFeePercentage_OverrideForStore = await _settingService.SettingExistsAsync(nmiPaymentSettings, x => x.AdditionalFeePercentage, storeScope);
}

return View("~/Plugins/Payments.Nmi/Views/Configure.cshtml", model);
}

[HttpPost]
[AuthorizeAdmin]
[Area(AreaNames.Admin)]
public IActionResult Configure(ConfigurationModel model)
public async Task<IActionResult> Configure(ConfigurationModel model)
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManagePaymentMethods))
if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManagePaymentMethods))
return AccessDeniedView();

if (!ModelState.IsValid)
return Configure();
return await Configure();

//load settings for a chosen store scope
var storeScope = _storeContext.ActiveStoreScopeConfiguration;
var nmiPaymentSettings = _settingService.LoadSetting<NmiPaymentSettings>(storeScope);
var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();
var nmiPaymentSettings = await _settingService.LoadSettingAsync<NmiPaymentSettings>(storeScope);

//save settings
nmiPaymentSettings.Username = model.Username;
Expand All @@ -110,22 +110,22 @@ public IActionResult Configure(ConfigurationModel model)
/* We do not clear cache after each setting update.
* This behavior can increase performance because cached settings will not be cleared
* and loaded from database after each update */
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.Password, model.Password_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.Username, model.Username_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.SecurityKey, model.SecurityKey_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.CollectJsTokenizationKey, model.CollectJsTokenizationKey_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.AdditionalFee, model.AdditionalFee_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.AdditionalFeePercentage, model.AdditionalFeePercentage_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.TransactMode, model.TransactModeId_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.UseUsernamePassword, model.UseUsernamePassword_OverrideForStore, storeScope, false);
_settingService.SaveSettingOverridablePerStore(nmiPaymentSettings, x => x.AllowCustomerToSaveCards, model.AllowCustomerToSaveCards_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.Password, model.Password_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.Username, model.Username_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.SecurityKey, model.SecurityKey_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.CollectJsTokenizationKey, model.CollectJsTokenizationKey_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.AdditionalFee, model.AdditionalFee_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.AdditionalFeePercentage, model.AdditionalFeePercentage_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.TransactMode, model.TransactModeId_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.UseUsernamePassword, model.UseUsernamePassword_OverrideForStore, storeScope, false);
await _settingService.SaveSettingOverridablePerStoreAsync(nmiPaymentSettings, x => x.AllowCustomerToSaveCards, model.AllowCustomerToSaveCards_OverrideForStore, storeScope, false);

//now clear settings cache
_settingService.ClearCache();
await _settingService.ClearCacheAsync();

_notificationService.SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));
_notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Plugins.Saved"));

return Configure();
return await Configure();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<Copyright>Copyright © Nixtus, LLC</Copyright>
<Company>Nixtus, LLC</Company>
<Authors>Nixtus, LLC</Authors>
Expand Down
Loading

0 comments on commit 4f22c65

Please sign in to comment.