Skip to content

Commit

Permalink
Save OrderPayment when needed for more situation (#469)
Browse files Browse the repository at this point in the history
* Avoid middle layer provider to a specific action.

* Save OrderPayment when needed for more situation
  • Loading branch information
infofromca authored Jul 28, 2024
1 parent 34ae004 commit 1944cdc
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.HelpfulLibraries.OrchardCore.DependencyInjection;
using Lombiq.HelpfulLibraries.OrchardCore.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
Expand Down Expand Up @@ -54,7 +54,7 @@ public ExactlyPaymentProvider(
H = services.HtmlLocalizer.Value;
}

public async Task<object> CreatePaymentProviderDataAsync(IPaymentViewModel model)
public async Task<object> CreatePaymentProviderDataAsync(IPaymentViewModel model, bool isPaymentRequest = false)
{
var settings = (await _siteService.GetSiteSettingsAsync())?.As<ExactlySettings>();
return string.IsNullOrEmpty(settings?.ApiKey) || string.IsNullOrEmpty(settings.ProjectId) ? null : new object();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using OrchardCore.Commerce.Abstractions.Abstractions;
using OrchardCore.Commerce.Payment.Abstractions;
using OrchardCore.Commerce.Payment.Controllers;
using OrchardCore.Commerce.Payment.Stripe.Abstractions;
using OrchardCore.Commerce.Payment.Stripe.Models;
using OrchardCore.ContentManagement;
Expand All @@ -20,7 +17,6 @@ public class StripePaymentProvider : IPaymentProvider
{
public const string ProviderName = "Stripe";

private readonly IHttpContextAccessor _hca;
private readonly IPaymentIntentPersistence _paymentIntentPersistence;
private readonly ISession _session;
private readonly ISiteService _siteService;
Expand All @@ -29,20 +25,18 @@ public class StripePaymentProvider : IPaymentProvider
public string Name => ProviderName;

public StripePaymentProvider(
IHttpContextAccessor hca,
IPaymentIntentPersistence paymentIntentPersistence,
ISession session,
ISiteService siteService,
IStripePaymentService stripePaymentService)
{
_hca = hca;
_paymentIntentPersistence = paymentIntentPersistence;
_session = session;
_siteService = siteService;
_stripePaymentService = stripePaymentService;
}

public async Task<object> CreatePaymentProviderDataAsync(IPaymentViewModel model)
public async Task<object> CreatePaymentProviderDataAsync(IPaymentViewModel model, bool isPaymentRequest = false)
{
PaymentIntent paymentIntent;

Expand All @@ -55,7 +49,7 @@ public async Task<object> CreatePaymentProviderDataAsync(IPaymentViewModel model
return null;
}

if (_hca.HttpContext?.GetRouteValue("action")?.ToString() == nameof(PaymentController.PaymentRequest))
if (isPaymentRequest)
{
await _session.SaveAsync(new OrderPayment
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Commerce.Abstractions.Abstractions;
using OrchardCore.Commerce.Abstractions.Constants;
using OrchardCore.Commerce.Payment.Controllers;
Expand Down Expand Up @@ -29,7 +29,7 @@ public interface IPaymentProvider
/// Arbitrary data which will be set as the value in <see cref="IPaymentViewModel.PaymentProviderData"/>. If it
/// returns <see langword="null"/> then the shape won't be displayed.
/// </returns>
Task<object?> CreatePaymentProviderDataAsync(IPaymentViewModel model);
Task<object?> CreatePaymentProviderDataAsync(IPaymentViewModel model, bool isPaymentRequest = false);

/// <summary>
/// Validates the data POSTed to the <see cref="PaymentController.Validate"/> action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task<IActionResult> PaymentRequest(string orderId)
}

var viewModel = new PaymentViewModel(orderPart, singleCurrencyTotal, singleCurrencyTotal);
await viewModel.WithProviderDataAsync(_paymentProviders);
await viewModel.WithProviderDataAsync(_paymentProviders, isPaymentRequest: true);

if (!viewModel.PaymentProviderData.Any())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Commerce.Abstractions.Abstractions;
using OrchardCore.Commerce.Payment.Abstractions;
Expand Down Expand Up @@ -33,7 +33,7 @@ public DummyPaymentProvider(
_shoppingCartHelpers = shoppingCartHelpers;
}

public Task<object?> CreatePaymentProviderDataAsync(IPaymentViewModel model) =>
public Task<object?> CreatePaymentProviderDataAsync(IPaymentViewModel model, bool isPaymentRequest = false) =>
// This provider doesn't have any special data, and it should only be displayed during development even if the
// feature is enabled. So if the condition is met a blank object is returned, otherwise null which will cause
// the provider to be skipped when used through the viewModel.WithProviderDataAsync(providers) method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.Commerce.Abstractions.Abstractions;
using OrchardCore.Commerce.Abstractions.Models;
using OrchardCore.Commerce.MoneyDataType;
Expand Down Expand Up @@ -27,11 +27,11 @@ public PaymentViewModel(OrderPart orderPart, Amount singleCurrencyTotal, Amount
NetTotal = netTotal;
}

public async Task WithProviderDataAsync(IEnumerable<IPaymentProvider> paymentProviders)
public async Task WithProviderDataAsync(IEnumerable<IPaymentProvider> paymentProviders, bool isPaymentRequest = false)
{
foreach (var provider in paymentProviders)
{
if (await provider.CreatePaymentProviderDataAsync(this) is { } data)
if (await provider.CreatePaymentProviderDataAsync(this, isPaymentRequest) is { } data)
{
PaymentProviderData[provider.Name] = data;
}
Expand Down

0 comments on commit 1944cdc

Please sign in to comment.