Skip to content

Commit a8185f6

Browse files
committed
Refactored payment methods to be providers
1 parent 70b4342 commit a8185f6

30 files changed

Lines changed: 364 additions & 358 deletions

File tree

src/Libraries/SmartStore.Data/Migrations/201406262150229_Providers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ public void MigrateLocaleResources(LocaleResourcesBuilder builder)
151151
// ExternalAuth
152152
prefix = "Admin.Configuration.ExternalAuthenticationMethods.";
153153
builder.Delete(prefix + "Fields.DisplayOrder", prefix + "Fields.FriendlyName", prefix + "Fields.SystemName", prefix + "Fields.IsActive");
154+
// Payment
155+
prefix = "Admin.Configuration.Payment.Methods.";
156+
builder.Delete(prefix + "Fields.DisplayOrder", prefix + "Fields.FriendlyName", prefix + "Fields.SystemName", prefix + "Fields.IsActive");
154157
}
155158
}
156159
}

src/Libraries/SmartStore.Services/Messages/MessageTokenProvider.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
using SmartStore.Services.Seo;
3333
using SmartStore.Services.Topics;
3434
using SmartStore.Core.Domain.Directory;
35+
using SmartStore.Core.Plugins;
36+
using System.Linq.Expressions;
3537

3638
namespace SmartStore.Services.Messages
3739
{
@@ -756,7 +758,7 @@ public virtual void AddOrderTokens(IList<Token> tokens, Order order, int languag
756758
tokens.Add(new Token("Order.ShippingCountry", order.ShippingAddress != null && order.ShippingAddress.Country != null ? order.ShippingAddress.Country.GetLocalized(x => x.Name) : ""));
757759

758760
var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
759-
var paymentMethodName = paymentMethod != null ? paymentMethod.GetLocalizedValue(_localizationService, "FriendlyName", _workContext.WorkingLanguage.Id) : order.PaymentMethodSystemName;
761+
var paymentMethodName = paymentMethod != null ? GetLocalizedValue(paymentMethod.Metadata, "FriendlyName", x => x.FriendlyName) : order.PaymentMethodSystemName;
760762
tokens.Add(new Token("Order.PaymentMethod", paymentMethodName));
761763
tokens.Add(new Token("Order.VatNumber", order.VatNumber));
762764

@@ -785,6 +787,23 @@ public virtual void AddOrderTokens(IList<Token> tokens, Order order, int languag
785787
_eventPublisher.EntityTokensAdded(order, tokens);
786788
}
787789

790+
private string GetLocalizedValue(ProviderMetadata metadata, string propertyName, Expression<Func<ProviderMetadata, string>> fallback)
791+
{
792+
// TODO: (mc) this actually belongs to PluginMediator, but we simply cannot add a dependency to framework from here. Refactor later!
793+
794+
Guard.ArgumentNotNull(() => metadata);
795+
796+
string systemName = metadata.SystemName;
797+
var languageId = _workContext.WorkingLanguage.Id;
798+
var resourceName = metadata.ResourceKeyPattern.FormatInvariant(metadata.SystemName, propertyName);
799+
string result = _localizationService.GetResource(resourceName, languageId, false, "", true);
800+
801+
if (result.IsEmpty())
802+
result = fallback.Compile()(metadata);
803+
804+
return result;
805+
}
806+
788807
public virtual void AddShipmentTokens(IList<Token> tokens, Shipment shipment, int languageId)
789808
{
790809
tokens.Add(new Token("Shipment.ShipmentNumber", shipment.Id.ToString()));

src/Libraries/SmartStore.Services/Orders/OrderProcessingService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using SmartStore.Services.Shipping;
3232
using SmartStore.Services.Tax;
3333
using SmartStore.Services.Seo;
34+
using SmartStore.Core.Plugins;
3435

3536
namespace SmartStore.Services.Orders
3637
{
@@ -854,7 +855,7 @@ public virtual PlaceOrderResult PlaceOrder(ProcessPaymentRequest processPaymentR
854855
skipPaymentWorkflow = true;
855856

856857
//payment workflow
857-
IPaymentMethod paymentMethod = null;
858+
Provider<IPaymentMethod> paymentMethod = null;
858859
if (!skipPaymentWorkflow)
859860
{
860861
paymentMethod = _paymentService.LoadPaymentMethodBySystemName(processPaymentRequest.PaymentMethodSystemName);

src/Libraries/SmartStore.Services/Payments/IPaymentMethod.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SmartStore.Services.Payments
99
/// <summary>
1010
/// Provides an interface for creating payment gateways and methods
1111
/// </summary>
12-
public partial interface IPaymentMethod : IPlugin
12+
public partial interface IPaymentMethod : IProvider, IUserEditable
1313
{
1414
#region Methods
1515

@@ -82,14 +82,6 @@ public partial interface IPaymentMethod : IPlugin
8282
/// <returns>Result</returns>
8383
bool CanRePostProcessPayment(Order order);
8484

85-
/// <summary>
86-
/// Gets a route for provider configuration
87-
/// </summary>
88-
/// <param name="actionName">Action name</param>
89-
/// <param name="controllerName">Controller name</param>
90-
/// <param name="routeValues">Route values</param>
91-
void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues);
92-
9385
/// <summary>
9486
/// Gets a route for payment info
9587
/// </summary>

src/Libraries/SmartStore.Services/Payments/IPaymentService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using SmartStore.Core.Domain.Orders;
3+
using SmartStore.Core.Plugins;
34

45
namespace SmartStore.Services.Payments
56
{
@@ -14,21 +15,21 @@ public partial interface IPaymentService
1415
/// <param name="filterByCustomerId">Filter payment methods by customer; null to load all records</param>
1516
/// <param name="storeId">Load records allows only in specified store; pass 0 to load all records</param>
1617
/// <returns>Payment methods</returns>
17-
IList<IPaymentMethod> LoadActivePaymentMethods(int? filterByCustomerId = null, int storeId = 0);
18+
IEnumerable<Provider<IPaymentMethod>> LoadActivePaymentMethods(int? filterByCustomerId = null, int storeId = 0);
1819

1920
/// <summary>
2021
/// Load payment provider by system name
2122
/// </summary>
2223
/// <param name="systemName">System name</param>
2324
/// <returns>Found payment provider</returns>
24-
IPaymentMethod LoadPaymentMethodBySystemName(string systemName);
25+
Provider<IPaymentMethod> LoadPaymentMethodBySystemName(string systemName, bool onlyWhenActive = false, int storeId = 0);
2526

2627
/// <summary>
2728
/// Load all payment providers
2829
/// </summary>
2930
/// <param name="storeId">Load records allows only in specified store; pass 0 to load all records</param>
3031
/// <returns>Payment providers</returns>
31-
IList<IPaymentMethod> LoadAllPaymentMethods(int storeId = 0);
32+
IEnumerable<Provider<IPaymentMethod>> LoadAllPaymentMethods(int storeId = 0);
3233

3334

3435

src/Libraries/SmartStore.Services/Payments/PaymentExtentions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Linq;
23
using System.Collections.Generic;
34
using SmartStore.Core.Domain.Orders;
45
using SmartStore.Core.Domain.Payments;
6+
using SmartStore.Core.Plugins;
57
using SmartStore.Services.Orders;
68

79
namespace SmartStore.Services.Payments
@@ -14,7 +16,8 @@ public static class PaymentExtentions
1416
/// <param name="paymentMethod">Payment method</param>
1517
/// <param name="paymentSettings">Payment settings</param>
1618
/// <returns>Result</returns>
17-
public static bool IsPaymentMethodActive(this IPaymentMethod paymentMethod,
19+
public static bool IsPaymentMethodActive(
20+
this Provider<IPaymentMethod> paymentMethod,
1821
PaymentSettings paymentSettings)
1922
{
2023
if (paymentMethod == null)
@@ -25,10 +28,8 @@ public static bool IsPaymentMethodActive(this IPaymentMethod paymentMethod,
2528

2629
if (paymentSettings.ActivePaymentMethodSystemNames == null)
2730
return false;
28-
foreach (string activeMethodSystemName in paymentSettings.ActivePaymentMethodSystemNames)
29-
if (paymentMethod.PluginDescriptor.SystemName.Equals(activeMethodSystemName, StringComparison.InvariantCultureIgnoreCase))
30-
return true;
31-
return false;
31+
32+
return paymentSettings.ActivePaymentMethodSystemNames.Contains(paymentMethod.Metadata.SystemName, StringComparer.OrdinalIgnoreCase);
3233
}
3334

3435
/// <summary>
@@ -41,11 +42,14 @@ public static bool IsPaymentMethodActive(this IPaymentMethod paymentMethod,
4142
/// <param name="usePercentage">Is fee amount specified as percentage or fixed value?</param>
4243
/// <returns>Result</returns>
4344
public static decimal CalculateAdditionalFee(this IPaymentMethod paymentMethod,
44-
IOrderTotalCalculationService orderTotalCalculationService, IList<OrganizedShoppingCartItem> cart,
45-
decimal fee, bool usePercentage)
45+
IOrderTotalCalculationService orderTotalCalculationService,
46+
IList<OrganizedShoppingCartItem> cart,
47+
decimal fee,
48+
bool usePercentage)
4649
{
4750
if (paymentMethod == null)
4851
throw new ArgumentNullException("paymentMethod");
52+
4953
if (fee <= 0)
5054
return fee;
5155

src/Libraries/SmartStore.Services/Payments/PaymentMethodBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SmartStore.Services.Payments
1111
/// <summary>
1212
/// Base class for payment methods
1313
/// </summary>
14-
public abstract class PaymentMethodBase : BasePlugin, IPaymentMethod
14+
public abstract class PaymentMethodBase : BasePlugin, IPaymentMethod, IConfigurable
1515
{
1616

1717
#region Methods

0 commit comments

Comments
 (0)