Skip to content

Commit d51e39c

Browse files
After payment, need to delete the shopping cart in session and cookies (#568)
* After payment, need to delete the shopping cart in session and cookies * Fix error CS1574: XML comment has cref attribute 'ShoppingCartPersistenceBase' that could not be resolved * Update src/Libraries/OrchardCore.Commerce.Abstractions/Abstractions/IShoppingCartPersistence.cs Co-authored-by: Sára El-Saig <sara.el-saig@lombiq.com> * Update test/OrchardCore.Commerce.Tests/Fakes/FakeCartStorage.cs Co-authored-by: Sára El-Saig <sara.el-saig@lombiq.com> * After Review * Update src/Modules/OrchardCore.Commerce/Services/ShoppingCartPersistenceBase.cs * Update src/Libraries/OrchardCore.Commerce.Abstractions/Abstractions/IShoppingCartPersistence.cs --------- Co-authored-by: Sára El-Saig <sara.el-saig@lombiq.com>
1 parent 3678212 commit d51e39c

5 files changed

Lines changed: 53 additions & 9 deletions

File tree

src/Modules/OrchardCore.Commerce/Abstractions/IShoppingCartPersistence.cs renamed to src/Libraries/OrchardCore.Commerce.Abstractions/Abstractions/IShoppingCartPersistence.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using OrchardCore.Commerce.Abstractions.Models;
2-
using OrchardCore.Commerce.Services;
32
using System.Threading.Tasks;
43

54
namespace OrchardCore.Commerce.Abstractions;
@@ -8,8 +7,8 @@ namespace OrchardCore.Commerce.Abstractions;
87
/// Service that provides a way to retain shopping cart information.
98
/// </summary>
109
/// <remarks><para>
11-
/// When deriving a custom implementation, please inherit from <see cref="ShoppingCartPersistenceBase"/> to retain event
12-
/// handling and dependency injection scope level caching.
10+
/// When deriving a custom implementation, please inherit from <c>ShoppingCartPersistenceBase</c> in the
11+
/// <c>OrchardCore.Commerce</c> project to retain event handling and dependency injection scope level caching.
1312
/// </para></remarks>
1413
public interface IShoppingCartPersistence
1514
{
@@ -22,9 +21,17 @@ public interface IShoppingCartPersistence
2221
Task<ShoppingCart> RetrieveAsync(string shoppingCartId);
2322

2423
/// <summary>
25-
/// Saves a shopping cart by a given ID.
24+
/// Saves the provided <see cref="ShoppingCart"/> <paramref name="items"/>.
2625
/// </summary>
2726
Task StoreAsync(ShoppingCart items);
27+
28+
/// <summary>
29+
/// Remove a <see cref="ShoppingCart"/> identified by <paramref name="shoppingCartId"/>.
30+
/// </summary>
31+
/// <param name="shoppingCartId">
32+
/// The name used to identify the shopping cart. <see langword="null"/> refers to the default shopping cart.
33+
/// </param>
34+
Task RemoveAsync(string shoppingCartId);
2835
}
2936

3037
public static class ShoppingCartPersistenceExtensions

src/Modules/OrchardCore.Commerce.Payment/Services/PaymentService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Identity;
66
using Microsoft.AspNetCore.Mvc.Localization;
77
using Microsoft.Extensions.Logging;
8+
using OrchardCore.Commerce.Abstractions;
89
using OrchardCore.Commerce.Abstractions.Abstractions;
910
using OrchardCore.Commerce.Abstractions.Constants;
1011
using OrchardCore.Commerce.Abstractions.Models;
@@ -33,6 +34,7 @@ namespace OrchardCore.Commerce.Payment.Services;
3334

3435
public class PaymentService : IPaymentService
3536
{
37+
private readonly IShoppingCartPersistence _shoppingCartPersistence;
3638
private readonly IFieldsOnlyDisplayManager _fieldsOnlyDisplayManager;
3739
private readonly IContentManager _contentManager;
3840
private readonly IShoppingCartHelpers _shoppingCartHelpers;
@@ -51,6 +53,7 @@ public class PaymentService : IPaymentService
5153
// We need all of them.
5254
#pragma warning disable S107 // Methods should not have too many parameters
5355
public PaymentService(
56+
IShoppingCartPersistence shoppingCartPersistence,
5457
IFieldsOnlyDisplayManager fieldsOnlyDisplayManager,
5558
IOrchardServices<PaymentService> services,
5659
IShoppingCartHelpers shoppingCartHelpers,
@@ -64,6 +67,7 @@ public PaymentService(
6467
IMoneyService moneyService)
6568
#pragma warning restore S107 // Methods should not have too many parameters
6669
{
70+
_shoppingCartPersistence = shoppingCartPersistence;
6771
_fieldsOnlyDisplayManager = fieldsOnlyDisplayManager;
6872
_contentManager = services.ContentManager.Value;
6973
_shoppingCartHelpers = shoppingCartHelpers;
@@ -177,11 +181,7 @@ public async Task FinalModificationOfOrderAsync(ContentItem order, string? shopp
177181
await _orderEvents.AwaitEachAsync(orderEvent =>
178182
orderEvent.FinalizeAsync(order, shoppingCartId, paymentProviderName));
179183

180-
await _shoppingCartHelpers.UpdateAsync(shoppingCartId, cart =>
181-
{
182-
cart.Items?.Clear();
183-
return Task.CompletedTask;
184-
});
184+
await _shoppingCartPersistence.RemoveAsync(shoppingCartId);
185185

186186
if (!string.IsNullOrEmpty(paymentProviderName))
187187
{

src/Modules/OrchardCore.Commerce/Services/SessionShoppingCartPersistence.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ protected override async Task<bool> StoreInnerAsync(string key, ShoppingCart ite
4545

4646
return true;
4747
}
48+
49+
protected override Task<bool> RemoveInnerAsync(string key)
50+
{
51+
try
52+
{
53+
Session.Clear();
54+
_httpContextAccessor.HttpContext?.Response.Cookies.Delete(key);
55+
}
56+
catch
57+
{
58+
return Task.FromResult(false);
59+
}
60+
61+
return Task.FromResult(true);
62+
}
4863
}

src/Modules/OrchardCore.Commerce/Services/ShoppingCartPersistenceBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public async Task StoreAsync(ShoppingCart items)
4949
}
5050
}
5151

52+
public async Task RemoveAsync(string shoppingCartId)
53+
{
54+
var key = GetCacheId(shoppingCartId);
55+
56+
if (await RemoveInnerAsync(key))
57+
{
58+
_scopeCache.Remove(key, out _);
59+
}
60+
}
61+
5262
/// <summary>
5363
/// Retrieves the items using the <see cref="ShoppingCartPersistenceBase"/>-specific <paramref name="key"/>.
5464
/// </summary>
@@ -68,6 +78,12 @@ public async Task StoreAsync(ShoppingCart items)
6878
/// </returns>
6979
protected abstract Task<bool> StoreInnerAsync(string key, ShoppingCart items);
7080

81+
/// <summary>
82+
/// Remove the items using the <see cref="ShoppingCartPersistenceBase"/>-specific <paramref name="key"/>.
83+
/// </summary>
84+
/// <param name="key">A prefix and (if set) the shopping cart ID combined.</param>
85+
/// <returns>A value indicating whether the removal was successful.</returns>
86+
protected abstract Task<bool> RemoveInnerAsync(string key);
7187
protected string GetCacheId(string shoppingCartId) =>
7288
string.IsNullOrEmpty(shoppingCartId) ? ShoppingCartPrefix : $"{ShoppingCartPrefix}_{shoppingCartId}";
7389
}

test/OrchardCore.Commerce.Tests/Fakes/FakeCartStorage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public Task StoreAsync(ShoppingCart items)
3030
_carts[items.Id ?? string.Empty] = new ShoppingCart(items.Items);
3131
return Task.CompletedTask;
3232
}
33+
34+
public Task RemoveAsync(string shoppingCartId)
35+
{
36+
_carts.Remove(shoppingCartId);
37+
return Task.CompletedTask;
38+
}
3339
}

0 commit comments

Comments
 (0)