|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq;
|
3 |
| -using Umbraco.Commerce.Core.Models; |
4 |
| -using Umbraco.Commerce.Core.Api; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Umbraco.Cms.Core; |
5 | 7 | using Umbraco.Cms.Core.Cache;
|
6 | 8 | using Umbraco.Cms.Core.Events;
|
7 | 9 | using Umbraco.Cms.Core.Models.PublishedContent;
|
8 | 10 | using Umbraco.Cms.Core.Notifications;
|
9 |
| -using Umbraco.Cms.Core.Sync; |
| 11 | +using Umbraco.Cms.Core.PublishedCache; |
10 | 12 | using Umbraco.Cms.Core.Services.Changes;
|
| 13 | +using Umbraco.Cms.Core.Services.Navigation; |
| 14 | +using Umbraco.Cms.Core.Sync; |
11 | 15 | using Umbraco.Cms.Core.Web;
|
12 |
| -using Umbraco.Extensions; |
13 | 16 | using Umbraco.Commerce.Checkout.Web;
|
| 17 | +using Umbraco.Commerce.Core.Api; |
| 18 | +using Umbraco.Commerce.Core.Models; |
| 19 | +using Umbraco.Commerce.Extensions; |
| 20 | +using Umbraco.Extensions; |
14 | 21 |
|
15 | 22 | namespace Umbraco.Commerce.Checkout.Events
|
16 | 23 | {
|
17 |
| - public class SyncZeroValuePaymentProviderContinueUrl : INotificationHandler<ContentCacheRefresherNotification> |
| 24 | + public class SyncZeroValuePaymentProviderContinueUrl : INotificationAsyncHandler<ContentCacheRefresherNotification> |
18 | 25 | {
|
19 |
| - public void Handle(ContentCacheRefresherNotification notification) |
| 26 | + private readonly IUmbracoContextFactory _umbracoContextFactory; |
| 27 | + private readonly IUmbracoCommerceApi _commerceApi; |
| 28 | + private readonly IPublishedContentTypeCache _publishedContentTypeCacheService; |
| 29 | + private readonly IDocumentNavigationQueryService _documentNavigationQueryService; |
| 30 | + |
| 31 | + public SyncZeroValuePaymentProviderContinueUrl( |
| 32 | + IUmbracoContextFactory umbracoContextFactory, |
| 33 | + IUmbracoCommerceApi commerceApi, |
| 34 | + IPublishedContentTypeCache publishedContentTypeCacheService, |
| 35 | + IDocumentNavigationQueryService documentNavigationQueryService) |
20 | 36 | {
|
21 |
| - Handle(notification.MessageObject, notification.MessageType); |
| 37 | + _umbracoContextFactory = umbracoContextFactory; |
| 38 | + _commerceApi = commerceApi; |
| 39 | + _publishedContentTypeCacheService = publishedContentTypeCacheService; |
| 40 | + _documentNavigationQueryService = documentNavigationQueryService; |
22 | 41 | }
|
23 | 42 |
|
24 |
| - private IUmbracoContextFactory _umbracoContextFactory; |
25 |
| - private IUmbracoCommerceApi _commerceApi; |
26 |
| - |
27 |
| - public SyncZeroValuePaymentProviderContinueUrl(IUmbracoContextFactory umbracoContextFactory, |
28 |
| - IUmbracoCommerceApi commerceApi) |
| 43 | + public Task HandleAsync(ContentCacheRefresherNotification notification, CancellationToken cancellationToken) |
29 | 44 | {
|
30 |
| - _umbracoContextFactory = umbracoContextFactory; |
31 |
| - _commerceApi = commerceApi; |
| 45 | + ArgumentNullException.ThrowIfNull(notification, nameof(notification)); |
| 46 | + return HandleAsync(notification.MessageObject, notification.MessageType); |
32 | 47 | }
|
33 | 48 |
|
34 |
| - public void Handle(object messageObject, MessageType messageType) |
| 49 | + public async Task HandleAsync(object messageObject, MessageType messageType) |
35 | 50 | {
|
36 | 51 | if (messageType != MessageType.RefreshByPayload)
|
| 52 | + { |
37 | 53 | throw new NotSupportedException();
|
| 54 | + } |
38 | 55 |
|
39 |
| - var payloads = messageObject as ContentCacheRefresher.JsonPayload[]; |
40 |
| - if (payloads == null) |
| 56 | + if (messageObject is not ContentCacheRefresher.JsonPayload[] payloads) |
| 57 | + { |
41 | 58 | return;
|
| 59 | + } |
42 | 60 |
|
43 |
| - using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) |
| 61 | + using (UmbracoContextReference ctx = _umbracoContextFactory.EnsureUmbracoContext()) |
44 | 62 | {
|
45 |
| - foreach (var payload in payloads) |
| 63 | + foreach (ContentCacheRefresher.JsonPayload payload in payloads) |
46 | 64 | {
|
47 | 65 | if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshNode))
|
48 | 66 | {
|
49 | 67 | // Single node refresh
|
50 |
| - var node = ctx.UmbracoContext.Content.GetById(payload.Id); |
| 68 | + IPublishedContent? node = ctx.UmbracoContext.Content.GetById(payload.Id); |
51 | 69 | if (node != null && IsConfirmationPageType(node))
|
52 | 70 | {
|
53 |
| - DoSyncZeroValuePaymentProviderContinueUrl(node); |
| 71 | + await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
54 | 72 | }
|
55 | 73 | }
|
56 | 74 | else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
|
57 | 75 | {
|
58 | 76 | // Branch refresh
|
59 |
| - var rootNode = ctx.UmbracoContext.Content.GetById(payload.Id); |
| 77 | + IPublishedContent? rootNode = ctx.UmbracoContext.Content.GetById(payload.Id); |
60 | 78 | if (rootNode != null)
|
61 | 79 | {
|
62 |
| - var nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
63 |
| - if (nodeType == null) |
| 80 | + IPublishedContentType checkoutStepPageDocType = _publishedContentTypeCacheService.Get(PublishedItemType.Content, UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
| 81 | + // TODO - Dinh: remove this line |
| 82 | + //IPublishedContentType? nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
| 83 | + if (checkoutStepPageDocType == null) |
| 84 | + { |
64 | 85 | continue;
|
| 86 | + } |
65 | 87 |
|
66 |
| - var nodes = ctx.UmbracoContext.Content.GetByContentType(nodeType); |
67 |
| - |
68 |
| - foreach (var node in nodes?.Where(x => IsConfirmationPageType(x) && x.Path.StartsWith(rootNode.Path))) |
| 88 | + // TODO: fix the obsolete warning. Try using INavigationQueryService |
| 89 | + IEnumerable<IPublishedContent> nodes = ctx.UmbracoContext.Content.GetByContentType(checkoutStepPageDocType); |
| 90 | + IEnumerable<IPublishedContent> confimationPages = nodes?.Where(x => IsConfirmationPageType(x) && x.Path.StartsWith(rootNode.Path, StringComparison.Ordinal)) ?? []; |
| 91 | + foreach (IPublishedContent? node in confimationPages) |
69 | 92 | {
|
70 |
| - DoSyncZeroValuePaymentProviderContinueUrl(node); |
| 93 | + await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
71 | 94 | }
|
72 | 95 | }
|
73 | 96 | }
|
74 | 97 | else if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
|
75 | 98 | {
|
76 | 99 | // All refresh
|
77 |
| - var nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
78 |
| - if (nodeType == null) |
| 100 | + // TODO - Dinh: remove this line |
| 101 | + //IPublishedContentType? nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
| 102 | + IPublishedContentType checkoutStepPageDocType = _publishedContentTypeCacheService.Get(PublishedItemType.Content, UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage); |
| 103 | + if (checkoutStepPageDocType == null) |
| 104 | + { |
79 | 105 | continue;
|
| 106 | + } |
80 | 107 |
|
81 |
| - var nodes = ctx.UmbracoContext.Content.GetByContentType(nodeType); |
82 |
| - |
83 |
| - foreach (var node in nodes?.Where(x => IsConfirmationPageType(x))) |
| 108 | + // TODO: fix the obsolete warning. Try using INavigationQueryService |
| 109 | + IEnumerable<IPublishedContent> nodes = ctx.UmbracoContext.Content.GetByContentType(checkoutStepPageDocType); |
| 110 | + IEnumerable<IPublishedContent> confimationPages = nodes?.Where(IsConfirmationPageType) ?? []; |
| 111 | + foreach (IPublishedContent? node in confimationPages) |
84 | 112 | {
|
85 |
| - DoSyncZeroValuePaymentProviderContinueUrl(node); |
| 113 | + await DoSyncZeroValuePaymentProviderContinueUrlAsync(node); |
86 | 114 | }
|
87 | 115 | }
|
88 | 116 | }
|
89 | 117 | }
|
90 | 118 | }
|
91 | 119 |
|
92 |
| - private bool IsConfirmationPageType(IPublishedContent node) |
| 120 | + private static bool IsConfirmationPageType(IPublishedContent node) |
93 | 121 | {
|
94 | 122 | if (node == null || node.ContentType == null || !node.HasProperty("uccStepType"))
|
| 123 | + { |
95 | 124 | return false;
|
| 125 | + } |
96 | 126 |
|
97 | 127 | return node.ContentType.Alias == UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage && node.Value<string>("uccStepType") == "Confirmation";
|
98 | 128 | }
|
99 | 129 |
|
100 |
| - private void DoSyncZeroValuePaymentProviderContinueUrl(IPublishedContent confirmationNode) |
| 130 | + private async Task DoSyncZeroValuePaymentProviderContinueUrlAsync(IPublishedContent confirmationNode) |
101 | 131 | {
|
102 | 132 | if (confirmationNode == null)
|
103 | 133 | {
|
104 | 134 | return;
|
105 | 135 | }
|
106 | 136 |
|
107 |
| - StoreReadOnly? store = confirmationNode.GetStore(); |
108 |
| - if (store == null) |
109 |
| - { |
110 |
| - return; |
111 |
| - } |
112 |
| - |
113 |
| - PaymentMethodReadOnly paymentMethod = _commerceApi.GetPaymentMethod(store.Id, UmbracoCommerceCheckoutConstants.PaymentMethods.Aliases.ZeroValue); |
| 137 | + StoreReadOnly store = confirmationNode.GetStore(); |
| 138 | + PaymentMethodReadOnly paymentMethod = await _commerceApi.GetPaymentMethodAsync(store.Id, UmbracoCommerceCheckoutConstants.PaymentMethods.Aliases.ZeroValue); |
114 | 139 | if (paymentMethod == null)
|
115 | 140 | {
|
116 | 141 | return;
|
117 | 142 | }
|
118 | 143 |
|
119 |
| - _commerceApi.Uow.Execute(uow => |
| 144 | + await _commerceApi.Uow.ExecuteAsync(async uow => |
120 | 145 | {
|
121 |
| - PaymentMethod writable = paymentMethod.AsWritable(uow) |
122 |
| - .SetSetting("ContinueUrl", confirmationNode.Url()); |
| 146 | + PaymentMethod writable = await paymentMethod.AsWritableAsync(uow) |
| 147 | + .SetSettingAsync("ContinueUrl", confirmationNode.Url()); |
123 | 148 |
|
124 |
| - _commerceApi.SavePaymentMethod(writable); |
| 149 | + await _commerceApi.SavePaymentMethodAsync(writable); |
125 | 150 |
|
126 | 151 | uow.Complete();
|
127 | 152 | });
|
|
0 commit comments