|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 |
| -using System.Threading; |
4 | 3 | using System.Threading.Tasks;
|
5 |
| -using Umbraco.Cms.Core.Cache; |
6 |
| -using Umbraco.Cms.Core.Events; |
7 |
| -using Umbraco.Cms.Core.Models.PublishedContent; |
8 |
| -using Umbraco.Cms.Core.Notifications; |
9 |
| -using Umbraco.Cms.Core.PublishedCache; |
10 |
| -using Umbraco.Cms.Core.Services.Changes; |
| 4 | +using Umbraco.Cms.Core.Models; |
| 5 | +using Umbraco.Cms.Core.Routing; |
| 6 | +using Umbraco.Cms.Core.Services; |
11 | 7 | using Umbraco.Cms.Core.Services.Navigation;
|
12 | 8 | using Umbraco.Cms.Core.Sync;
|
13 |
| -using Umbraco.Commerce.Checkout.Web; |
14 | 9 | using Umbraco.Commerce.Core.Api;
|
15 | 10 | using Umbraco.Commerce.Core.Models;
|
16 | 11 | using Umbraco.Commerce.Extensions;
|
17 |
| -using Umbraco.Extensions; |
| 12 | +using UmbracoCommerceConstants = Umbraco.Commerce.Cms.Constants; |
18 | 13 |
|
19 | 14 | namespace Umbraco.Commerce.Checkout.Events
|
20 | 15 | {
|
21 |
| - public class SyncZeroValuePaymentProviderContinueUrl : INotificationAsyncHandler<ContentCacheRefresherNotification> |
| 16 | + public class SyncZeroValuePaymentProviderContinueUrl( |
| 17 | + IDocumentNavigationQueryService documentNavigationQueryService, |
| 18 | + IContentService contentService, |
| 19 | + IIdKeyMap keyMap, |
| 20 | + IServerRoleAccessor serverRoleAccessor, |
| 21 | + IUmbracoCommerceApi commerceApi, |
| 22 | + IPublishedUrlProvider publishedUrlProvider) |
| 23 | + : ContentOfTypeCacheRefresherNotificationHandlerBase(documentNavigationQueryService, contentService, keyMap, serverRoleAccessor) |
22 | 24 | {
|
23 |
| - private readonly IUmbracoCommerceApi _commerceApi; |
24 |
| - private readonly IDocumentNavigationQueryService _documentNavigationQueryService; |
25 |
| - private readonly IPublishedContentCache _publishedContentCache; |
| 25 | + private readonly IDocumentNavigationQueryService _documentNavigationQueryService = documentNavigationQueryService; |
| 26 | + private readonly IContentService _contentService = contentService; |
26 | 27 |
|
27 |
| - public SyncZeroValuePaymentProviderContinueUrl( |
28 |
| - IUmbracoCommerceApi commerceApi, |
29 |
| - IDocumentNavigationQueryService documentNavigationQueryService, |
30 |
| - IPublishedContentCache publishedContentCache) |
| 28 | + protected override string ContentTypeAlias => UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage; |
| 29 | + |
| 30 | + protected override async Task HandleContentOfTypeAsync(IContent content) |
31 | 31 | {
|
32 |
| - _commerceApi = commerceApi; |
33 |
| - _documentNavigationQueryService = documentNavigationQueryService; |
34 |
| - _publishedContentCache = publishedContentCache; |
| 32 | + if (IsConfirmationPageType(content)) |
| 33 | + { |
| 34 | + await DoSyncZeroValuePaymentProviderContinueUrlAsync(content); |
| 35 | + } |
35 | 36 | }
|
36 | 37 |
|
37 |
| - public async Task HandleAsync(ContentCacheRefresherNotification notification, CancellationToken cancellationToken) |
| 38 | + private static bool IsConfirmationPageType(IContent content) |
38 | 39 | {
|
39 |
| - ArgumentNullException.ThrowIfNull(notification, nameof(notification)); |
40 |
| - |
41 |
| - if (notification.MessageType != MessageType.RefreshByPayload) |
| 40 | + if (!content.HasProperty("uccStepType")) |
42 | 41 | {
|
43 |
| - throw new NotSupportedException(); |
| 42 | + return false; |
44 | 43 | }
|
45 | 44 |
|
46 |
| - if (notification.MessageObject is not ContentCacheRefresher.JsonPayload[] payloads) |
| 45 | + return content.ContentType.Alias == UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage && content.GetValue<string>("uccStepType") == "Confirmation"; |
| 46 | + } |
| 47 | + |
| 48 | + private async Task DoSyncZeroValuePaymentProviderContinueUrlAsync(IContent content) |
| 49 | + { |
| 50 | + if (!content.Published) |
47 | 51 | {
|
| 52 | + // Don't do anything if the content is not published |
48 | 53 | return;
|
49 | 54 | }
|
50 | 55 |
|
51 |
| - foreach (ContentCacheRefresher.JsonPayload payload in payloads) |
| 56 | + // Get the store ID from the store picker in the contents ancestors |
| 57 | + Guid? storeId = null; |
| 58 | + |
| 59 | + if (content.HasProperty(UmbracoCommerceConstants.Properties.StorePropertyAlias)) |
52 | 60 | {
|
53 |
| - if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshNode)) |
54 |
| - { |
55 |
| - // Single node refresh |
56 |
| - IPublishedContent? node = _publishedContentCache.GetById(payload.Id); |
57 |
| - if (node != null && IsConfirmationPageType(node)) |
58 |
| - { |
59 |
| - await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
60 |
| - } |
61 |
| - } |
62 |
| - else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch)) |
63 |
| - { |
64 |
| - // Branch refresh |
65 |
| - IPublishedContent? rootNode = _publishedContentCache.GetById(payload.Id); |
66 |
| - if (rootNode != null && _documentNavigationQueryService.TryGetDescendantsKeysOfType(rootNode.Key, UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage, out IEnumerable<Guid> keys)) |
67 |
| - { |
68 |
| - foreach (Guid key in keys) |
69 |
| - { |
70 |
| - IPublishedContent? node = _publishedContentCache.GetById(key); |
71 |
| - if (node != null && IsConfirmationPageType(node)) |
72 |
| - { |
73 |
| - await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
74 |
| - } |
75 |
| - } |
76 |
| - } |
77 |
| - } |
78 |
| - else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll)) |
| 61 | + storeId = content.GetValue<Guid>(UmbracoCommerceConstants.Properties.StorePropertyAlias); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + if (_documentNavigationQueryService.TryGetAncestorsKeys(content.Key, out IEnumerable<Guid> ancestorsKeys)) |
79 | 66 | {
|
80 |
| - if (_documentNavigationQueryService.TryGetRootKeys(out IEnumerable<Guid> rootKeys)) |
| 67 | + foreach (Guid ancestorKey in ancestorsKeys) |
81 | 68 | {
|
82 |
| - foreach (Guid rootKey in rootKeys) |
| 69 | + IContent? content2 = _contentService.GetById(ancestorKey); |
| 70 | + if (content2 != null && content2.HasProperty(UmbracoCommerceConstants.Properties.StorePropertyAlias)) |
83 | 71 | {
|
84 |
| - if (_documentNavigationQueryService.TryGetDescendantsKeysOfType(rootKey, UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage, out IEnumerable<Guid> keys)) |
85 |
| - { |
86 |
| - foreach (Guid key in keys) |
87 |
| - { |
88 |
| - IPublishedContent? node = _publishedContentCache.GetById(key); |
89 |
| - if (node != null && IsConfirmationPageType(node)) |
90 |
| - { |
91 |
| - await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
92 |
| - } |
93 |
| - } |
94 |
| - } |
| 72 | + storeId = content2.GetValue<Guid>(UmbracoCommerceConstants.Properties.StorePropertyAlias); |
| 73 | + break; |
95 | 74 | }
|
96 | 75 | }
|
97 | 76 | }
|
98 | 77 | }
|
99 |
| - } |
100 | 78 |
|
101 |
| - private static bool IsConfirmationPageType(IPublishedContent node) |
102 |
| - { |
103 |
| - if (!node.HasProperty("uccStepType")) |
| 79 | + if (!storeId.HasValue) |
104 | 80 | {
|
105 |
| - return false; |
| 81 | + return; |
106 | 82 | }
|
107 | 83 |
|
108 |
| - return node.ContentType.Alias == UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage && node.Value<string>("uccStepType") == "Confirmation"; |
109 |
| - } |
110 |
| - |
111 |
| - private async Task DoSyncZeroValuePaymentProviderContinueUrlAsync(IPublishedContent confirmationNode) |
112 |
| - { |
113 |
| - StoreReadOnly store = confirmationNode.GetStore(); |
114 |
| - PaymentMethodReadOnly paymentMethod = await _commerceApi.GetPaymentMethodAsync(store.Id, UmbracoCommerceCheckoutConstants.PaymentMethods.Aliases.ZeroValue); |
| 84 | + // Get the payment method and set the continue URL |
| 85 | + PaymentMethodReadOnly paymentMethod = await commerceApi.GetPaymentMethodAsync(storeId.Value, UmbracoCommerceCheckoutConstants.PaymentMethods.Aliases.ZeroValue); |
115 | 86 |
|
116 | 87 | if (paymentMethod == null)
|
117 | 88 | {
|
118 | 89 | return;
|
119 | 90 | }
|
120 | 91 |
|
121 |
| - await _commerceApi.Uow.ExecuteAsync(async uow => |
| 92 | + await commerceApi.Uow.ExecuteAsync(async uow => |
122 | 93 | {
|
123 | 94 | PaymentMethod writable = await paymentMethod.AsWritableAsync(uow)
|
124 |
| - .SetSettingAsync("ContinueUrl", confirmationNode.Url()); |
| 95 | + .SetSettingAsync("ContinueUrl", publishedUrlProvider.GetUrl(content.Key)); |
125 | 96 |
|
126 |
| - await _commerceApi.SavePaymentMethodAsync(writable); |
| 97 | + await commerceApi.SavePaymentMethodAsync(writable); |
127 | 98 |
|
128 | 99 | uow.Complete();
|
129 | 100 | });
|
|
0 commit comments