|
| 1 | +#nullable enable |
| 2 | + |
1 | 3 | using Microsoft.AspNetCore.Http; |
2 | 4 | using OrchardCore.Commerce.Payment.Stripe.Abstractions; |
| 5 | +using OrchardCore.Commerce.Payment.Stripe.Models; |
| 6 | +using System.Text.Json; |
3 | 7 | using System.Threading.Tasks; |
4 | 8 |
|
5 | 9 | namespace OrchardCore.Commerce.Payment.Stripe.Services; |
6 | 10 |
|
7 | 11 | public class PaymentIntentPersistence : IPaymentIntentPersistence |
8 | 12 | { |
9 | 13 | // Using _ as a separator to avoid separator character conflicts. |
10 | | - private const string PaymentIntentKeyPrefix = "OrchardCore_Commerce_PaymentIntent"; |
| 14 | + private const string PaymentIntentKeyPrefix = "OrchardCore_Commerce_" + nameof(PaymentIntentPersistenceInfo); |
11 | 15 |
|
12 | 16 | private readonly IHttpContextAccessor _httpContextAccessor; |
13 | | - private ISession Session => _httpContextAccessor.HttpContext?.Session; |
| 17 | + |
| 18 | + private ISession? Session => _httpContextAccessor.HttpContext?.Session; |
| 19 | + private HttpRequest? Request => _httpContextAccessor.HttpContext?.Request; |
14 | 20 |
|
15 | 21 | public PaymentIntentPersistence(IHttpContextAccessor httpContextAccessor) => _httpContextAccessor = httpContextAccessor; |
16 | 22 |
|
17 | | - public Task<string> RetrieveAsync(string shoppingCartId = null) |
| 23 | + public Task<PaymentIntentPersistenceInfo?> RetrieveAsync(string? shoppingCartId) |
18 | 24 | { |
19 | 25 | var key = GetCacheId(shoppingCartId); |
20 | | - var serialized = Session.GetString(key); |
21 | | - if (serialized == null && _httpContextAccessor.HttpContext != null) |
| 26 | + |
| 27 | + if (Session?.GetString(key)?.Trim() is { Length: > 0 } serializedFromSession && |
| 28 | + TryParse(serializedFromSession, out var sessionResult)) |
22 | 29 | { |
23 | | - _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue(key, out var serializedCart); |
24 | | - return Task.FromResult(serializedCart); |
| 30 | + return Task.FromResult(sessionResult); |
25 | 31 | } |
26 | 32 |
|
27 | | - return Task.FromResult(serialized); |
| 33 | + if (Request != null && |
| 34 | + Request.Cookies.TryGetValue(key, out var serializedFromCookie) && |
| 35 | + TryParse(serializedFromCookie, out var cookieResult)) |
| 36 | + { |
| 37 | + return Task.FromResult(cookieResult); |
| 38 | + } |
| 39 | + |
| 40 | + return Task.FromResult<PaymentIntentPersistenceInfo?>(null); |
28 | 41 | } |
29 | 42 |
|
30 | | - public Task StoreAsync(string paymentIntentId, string shoppingCartId = null) |
| 43 | + public Task StoreAsync(string? shoppingCartId, PaymentIntentPersistenceInfo info) |
31 | 44 | { |
32 | 45 | var key = GetCacheId(shoppingCartId); |
33 | | - if (Session.GetString(key) == paymentIntentId) return Task.CompletedTask; |
| 46 | + var serialized = JsonSerializer.Serialize(info); |
34 | 47 |
|
35 | | - Session.SetString(key, paymentIntentId); |
36 | | - _httpContextAccessor.SetCookieForever(key, paymentIntentId); |
| 48 | + Session?.SetString(key, serialized); |
| 49 | + _httpContextAccessor.SetCookieForever(key, serialized); |
37 | 50 |
|
38 | 51 | return Task.CompletedTask; |
39 | 52 | } |
40 | 53 |
|
41 | | - public Task RemoveAsync(string shoppingCartId = null) |
| 54 | + public Task RemoveAsync(string? shoppingCartId) |
42 | 55 | { |
43 | 56 | var key = GetCacheId(shoppingCartId); |
44 | | - Session.Remove(key); |
| 57 | + Session?.Remove(key); |
45 | 58 | _httpContextAccessor.HttpContext?.Response.Cookies.Delete(key); |
46 | 59 |
|
47 | 60 | return Task.CompletedTask; |
48 | 61 | } |
49 | 62 |
|
50 | | - protected string GetCacheId(string shoppingCartId) => |
| 63 | + protected string GetCacheId(string? shoppingCartId) => |
51 | 64 | string.IsNullOrEmpty(shoppingCartId) ? PaymentIntentKeyPrefix : $"{PaymentIntentKeyPrefix}_{shoppingCartId}"; |
| 65 | + |
| 66 | + private static bool TryParse(string serialized, out PaymentIntentPersistenceInfo? result) |
| 67 | + { |
| 68 | + result = null; |
| 69 | + if (string.IsNullOrWhiteSpace(serialized)) return false; |
| 70 | + |
| 71 | + try |
| 72 | + { |
| 73 | + result = JsonSerializer.Deserialize<PaymentIntentPersistenceInfo?>(serialized); |
| 74 | + return !string.IsNullOrWhiteSpace(result?.PaymentIntentId); |
| 75 | + } |
| 76 | + catch |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
52 | 81 | } |
0 commit comments