Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ await IsVatExempt(calculateTaxRequest.Address, calculateTaxRequest.Customer))
else
{
foreach (var error in calculateTaxResult.Errors)
_logger.LogError("{FriendlyName} - {Error}", activeTaxProvider.FriendlyName, error);
_logger.LogError("{SystemName} - {Error}", activeTaxProvider.SystemName, error);
}

return (taxRate, isTaxable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public virtual async Task<GetShippingOptionResponse> GetShippingOptions(Customer
foreach (var error in getShippingOptionResponse.Errors)
{
result.AddError(error);
_logger.LogWarning("Shipping ({FriendlyName}) {Error}", shippingRateMethod.FriendlyName, error);
_logger.LogWarning("Shipping ({SystemName}) {Error}", shippingRateMethod.SystemName, error);
}

//clear the shipping options in this case
Expand Down
26 changes: 26 additions & 0 deletions src/Business/Grand.Business.Core/Extensions/PaymentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Checkout.Payments;
using Grand.Domain.Orders;
using Grand.Domain.Payments;

namespace Grand.Business.Core.Extensions;
Expand All @@ -24,4 +25,29 @@ public static bool IsPaymentMethodActive(this IPaymentProvider paymentMethod,
paymentSettings.ActivePaymentProviderSystemNames.Any(activeMethodSystemName =>
paymentMethod.SystemName.Equals(activeMethodSystemName, StringComparison.OrdinalIgnoreCase));
}

/// <summary>
/// Gets the name of the order's payment method in the order's language, for text that does not follow
/// the current request (e-mails, PDF). Falls back to the friendly name when the order has no language,
/// and to the system name when the provider is missing or gives no name.
/// </summary>
/// <param name="paymentMethod">Payment method, or null when it is no longer installed</param>
/// <param name="order">Order</param>
/// <returns>Payment method name</returns>
public static string GetPaymentMethodName(this IPaymentProvider paymentMethod, Order order)
{
ArgumentNullException.ThrowIfNull(order);

if (paymentMethod == null)
return order.PaymentMethodSystemName;

var name = string.IsNullOrEmpty(order.CustomerLanguageId)
? paymentMethod.FriendlyName
: paymentMethod.GetFriendlyName(order.CustomerLanguageId);

if (string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(order.CustomerLanguageId))
name = paymentMethod.FriendlyName;

return string.IsNullOrEmpty(name) ? order.PaymentMethodSystemName : name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public interface IPaymentProvider : IProvider
/// </summary>
Task<string> GetControllerRouteName();

/// <summary>
/// Gets a friendly name in the given language, for text that does not follow the current request
/// (e.g. order e-mails). Defaults to <see cref="IProvider.FriendlyName" />.
/// </summary>
/// <param name="languageId">Language identifier</param>
string GetFriendlyName(string languageId) => FriendlyName;

#endregion

#region Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ public async Task<LiquidOrder> Handle(GetOrderTokensCommand request, Cancellatio
await _addressAttributeParser.FormatAttributes(language, request.Order.ShippingAddress?.Attributes);

var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(request.Order.PaymentMethodSystemName);
liquidOrder.PaymentMethod =
paymentMethod != null ? paymentMethod.FriendlyName : request.Order.PaymentMethodSystemName;
liquidOrder.PaymentMethod = paymentMethod.GetPaymentMethodName(request.Order);

var dict = new Dictionary<string, string>();
foreach (var item in request.Order.OrderTaxes)
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/Payments.BrainTree/BrainTreePaymentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ public async Task<bool> SupportVoid()

public string FriendlyName => _translationService.GetResource(BrainTreeDefaults.FriendlyName);

public string GetFriendlyName(string languageId) =>
_translationService.GetResource(BrainTreeDefaults.FriendlyName, languageId, returnEmptyIfNotFound: true);

public int Priority => _brainTreePaymentSettings.DisplayOrder;

public IList<string> LimitedToStores => new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public CashOnDeliveryPaymentProvider(

public string FriendlyName => _translationService.GetResource(CashOnDeliveryPaymentDefaults.FriendlyName);

public string GetFriendlyName(string languageId) =>
_translationService.GetResource(CashOnDeliveryPaymentDefaults.FriendlyName, languageId, returnEmptyIfNotFound: true);

public int Priority => _cashOnDeliveryPaymentSettings.DisplayOrder;

public IList<string> LimitedToStores => new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public StripeCheckoutPaymentProvider(

public string FriendlyName => _translationService.GetResource(StripeCheckoutDefaults.FriendlyName);

public string GetFriendlyName(string languageId) =>
_translationService.GetResource(StripeCheckoutDefaults.FriendlyName, languageId, returnEmptyIfNotFound: true);

public int Priority => _stripeCheckoutPaymentSettings.DisplayOrder;

public IList<string> LimitedToStores => new List<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Checkout.Payments;
using Grand.Domain.Orders;
using Grand.Domain.Payments;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand Down Expand Up @@ -42,4 +43,48 @@ public void IsPaymentMethodActive_NullProvider_ThrowException()
var settings = new PaymentSettings();
Assert.ThrowsExactly<ArgumentNullException>(() => provider.IsPaymentMethodActive(settings));
}
}

[TestMethod]
public void GetPaymentMethodName_OrderLanguage_ReturnNameInOrderLanguage()
{
_providerMock.Setup(c => c.FriendlyName).Returns("Request language");
_providerMock.Setup(c => c.GetFriendlyName("1")).Returns("Order language");
var order = new Order { CustomerLanguageId = "1", PaymentMethodSystemName = "Payments.Test" };
Assert.AreEqual("Order language", _providerMock.Object.GetPaymentMethodName(order));
}

[TestMethod]
public void GetPaymentMethodName_NoOrderLanguage_ReturnFriendlyName()
{
_providerMock.Setup(c => c.FriendlyName).Returns("Request language");
var order = new Order { CustomerLanguageId = null, PaymentMethodSystemName = "Payments.Test" };
Assert.AreEqual("Request language", _providerMock.Object.GetPaymentMethodName(order));
_providerMock.Verify(c => c.GetFriendlyName(It.IsAny<string>()), Times.Never);
}

[TestMethod]
public void GetPaymentMethodName_NoTranslation_ReturnFriendlyName()
{
_providerMock.Setup(c => c.FriendlyName).Returns("Request language");
_providerMock.Setup(c => c.GetFriendlyName("1")).Returns("");
var order = new Order { CustomerLanguageId = "1", PaymentMethodSystemName = "Payments.Test" };
Assert.AreEqual("Request language", _providerMock.Object.GetPaymentMethodName(order));
}

[TestMethod]
public void GetPaymentMethodName_NoName_ReturnSystemName()
{
_providerMock.Setup(c => c.FriendlyName).Returns("");
_providerMock.Setup(c => c.GetFriendlyName("1")).Returns("");
var order = new Order { CustomerLanguageId = "1", PaymentMethodSystemName = "Payments.Test" };
Assert.AreEqual("Payments.Test", _providerMock.Object.GetPaymentMethodName(order));
}

[TestMethod]
public void GetPaymentMethodName_NullProvider_ReturnSystemName()
{
IPaymentProvider provider = null;
var order = new Order { PaymentMethodSystemName = "Payments.Test" };
Assert.AreEqual("Payments.Test", provider.GetPaymentMethodName(order));
Comment thread
KrzysztofPajak marked this conversation as resolved.
Dismissed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class GetOrderTokensCommandHandlerTests
{
private readonly Currency _orderCurrency = new() { CurrencyCode = "EUR" };
private GetOrderTokensCommandHandler _handler;
private Mock<IPaymentService> _paymentServiceMock;
private Mock<IPriceFormatter> _priceFormatterMock;

[TestInitialize]
Expand All @@ -41,10 +42,12 @@ public void Init()
_priceFormatterMock.Setup(x => x.FormatPrice(It.IsAny<double>(), It.IsAny<Currency>()))
.Returns((double price, Currency currency) => $"{currency?.CurrencyCode}{price}");

_paymentServiceMock = new Mock<IPaymentService>();

_handler = new GetOrderTokensCommandHandler(languageService.Object, currencyService.Object,
new Mock<IProductService>().Object, new Mock<IVendorService>().Object, _priceFormatterMock.Object,
new Mock<ICountryService>().Object, new Mock<IAddressAttributeParser>().Object,
new Mock<IPaymentService>().Object, giftVoucherService.Object, new TaxSettings());
_paymentServiceMock.Object, giftVoucherService.Object, new TaxSettings());
}

[TestMethod]
Expand All @@ -62,4 +65,26 @@ public async Task Handle_FormatsTaxRatesInTheOrderCurrency()
//Assert
Assert.AreEqual("EUR10", result.TaxRates["23"]);
}

[TestMethod]
public async Task Handle_PaymentMethodNameInTheCustomerLanguage()
{
//Arrange
var paymentProvider = new Mock<IPaymentProvider>();
paymentProvider.Setup(x => x.FriendlyName).Returns("Request language");
paymentProvider.Setup(x => x.GetFriendlyName("1")).Returns("Customer language");
_paymentServiceMock.Setup(x => x.LoadPaymentMethodBySystemName("Payments.Test"))
.Returns(paymentProvider.Object);
var order = new Order {
CustomerCurrencyCode = "EUR", CustomerLanguageId = "1", PaymentMethodSystemName = "Payments.Test"
};

//Act
var result = await _handler.Handle(new GetOrderTokensCommand {
Order = order, Customer = new Customer(), Store = new Store(), Host = new DomainHost { Url = "https://localhost/" }
}, CancellationToken.None);

//Assert
Assert.AreEqual("Customer language", result.PaymentMethod);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@using Grand.Business.Core.Extensions
@using Grand.Business.Core.Interfaces.Catalog.Products
@using Grand.Business.Core.Interfaces.Checkout.GiftVouchers
@using Grand.Business.Core.Interfaces.Checkout.Payments
Expand Down Expand Up @@ -152,7 +153,7 @@
@((await _countryService.GetCountryById(order.BillingAddress.CountryId))?.Name)<br/>
<br/>
var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
var paymentMethodStr = paymentMethod != null ? paymentMethod.FriendlyName : order.PaymentMethodSystemName;
var paymentMethodStr = paymentMethod.GetPaymentMethodName(order);
<b>@(string.Format(_translationService.GetResource("PDFInvoice.PaymentMethod", order.CustomerLanguageId), paymentMethodStr))</b>
<br/>
@await Component.InvokeAsync("Widget", new { widgetZone = "pdf_payment_method_below", additionalData = order.Id })
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Grand.Web/Views/PdfTemplates/OrderPdfTemplate.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@using Grand.Business.Core.Extensions
@using Grand.Business.Core.Interfaces.Catalog.Products
@using Grand.Business.Core.Interfaces.Checkout.GiftVouchers
@using Grand.Business.Core.Interfaces.Checkout.Payments
Expand Down Expand Up @@ -152,7 +153,7 @@
@((await _countryService.GetCountryById(order.BillingAddress.CountryId))?.Name)<br/>
<br/>
var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
var paymentMethodStr = paymentMethod != null ? paymentMethod.FriendlyName : order.PaymentMethodSystemName;
var paymentMethodStr = paymentMethod.GetPaymentMethodName(order);
<b>@(string.Format(_translationService.GetResource("PDFInvoice.PaymentMethod", order.CustomerLanguageId), paymentMethodStr))</b>
<br/>
@await Component.InvokeAsync("Widget", new { widgetZone = "pdf_payment_method_below", additionalData = order.Id })
Expand Down
Loading