generated from Kentico/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckoutService.cs
196 lines (152 loc) · 8.23 KB
/
CheckoutService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using CMS.Globalization;
using CMS.Websites;
using DancingGoat.Models;
using Kentico.Content.Web.Mvc;
using Kentico.Content.Web.Mvc.Routing;
using Kentico.Xperience.K13Ecommerce.Countries;
using Kentico.Xperience.K13Ecommerce.Customers;
using Kentico.Xperience.K13Ecommerce.ShoppingCart;
using Kentico.Xperience.K13Ecommerce.StoreApi;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace DancingGoat;
public class CheckoutService : ICheckoutService
{
private readonly IShoppingService shoppingService;
private readonly ICountryService countryService;
private readonly ICustomerService customerService;
private readonly IWebPageDataContextRetriever webPageDataContextRetriever;
private readonly IWebPageUrlRetriever webPageUrlRetriever;
private readonly CheckoutPageRepository checkoutPageRepository;
private readonly IPreferredLanguageRetriever preferredLanguageRetriever;
public CheckoutService(IShoppingService shoppingService,
ICountryService countryService,
ICustomerService customerService,
IWebPageDataContextRetriever webPageDataContextRetriever,
IWebPageUrlRetriever webPageUrlRetriever,
CheckoutPageRepository checkoutPageRepository,
IPreferredLanguageRetriever preferredLanguageRetriever
)
{
this.shoppingService = shoppingService;
this.countryService = countryService;
this.customerService = customerService;
this.webPageDataContextRetriever = webPageDataContextRetriever;
this.webPageUrlRetriever = webPageUrlRetriever;
this.checkoutPageRepository = checkoutPageRepository;
this.preferredLanguageRetriever = preferredLanguageRetriever;
}
public async Task<CartContentViewModel> PrepareCartViewModel()
{
var cart = await shoppingService.GetCurrentShoppingCartContent();
return new CartContentViewModel(cart, await checkoutPageRepository.GetProductImages(cart, preferredLanguageRetriever.Get()));
}
public async Task<DeliveryDetailsViewModel> PrepareDeliveryDetailsViewModel(CustomerViewModel customer = null,
BillingAddressViewModel billingAddress = null, ShippingOptionViewModel shippingOption = null,
KShoppingCartDetails cartDetails = null)
{
cartDetails ??= await shoppingService.GetCurrentShoppingCartDetails();
var currency = (await shoppingService.GetCurrentShoppingCartContent()).Currency;
var countries = await CreateCountryList();
var shippingOptions =
CreateShippingOptionList(cartDetails.AvailableShippingOptions, currency.CurrencyFormatString);
customer ??= new CustomerViewModel(await shoppingService.GetCustomerOrCreateFromAuthenticatedUser(cartDetails.Customer));
var addresses = (cartDetails.Customer != null)
? await customerService.GetCurrentCustomerAddresses()
: Enumerable.Empty<KAddress>();
var billingAddresses = new SelectList(addresses, nameof(KAddress.AddressId), nameof(KAddress.AddressName));
billingAddress ??= await BillingAddressViewModel.GetModel(cartDetails.BillingAddress, countries,
countryService, billingAddresses);
var selectedShippingOption =
cartDetails.AvailableShippingOptions!.FirstOrDefault(s =>
s.ShippingOptionId == cartDetails.ShippingOptionId);
shippingOption ??=
new ShippingOptionViewModel(selectedShippingOption, shippingOptions, cartDetails.IsShippingNeeded);
billingAddress.BillingAddressCountryStateSelector.Countries ??= countries;
billingAddress.BillingAddressSelector ??= new AddressSelectorViewModel { Addresses = billingAddresses };
shippingOption.ShippingOptions = shippingOptions;
var pageContext = webPageDataContextRetriever.Retrieve().WebPage;
var currentStepPage = await checkoutPageRepository.GetCartStepPage<CartDeliveryDetails>(
pageContext.WebPageItemID,
pageContext.LanguageName, pageContext.ContentTypeName);
var viewModel = new DeliveryDetailsViewModel
{
Customer = customer,
BillingAddress = billingAddress,
ShippingOption = shippingOption,
CartPreviousStepUrl =
await GetNextOrPreviousStepUrl<CartDeliveryDetails>(s => s.CartPreviousStep.First().WebPageGuid)
};
return viewModel;
}
public async Task<bool> IsCountryValid(int countryId) => await countryService.GetCountry(countryId) != null;
public async Task<bool> IsStateValid(int countryId, int? stateId)
{
var states = (await countryService.GetCountryStates(countryId)).ToList();
return (states.Count < 1) || states.Exists(s => s.StateID == stateId);
}
public async Task<KAddress> GetAddress(int customerId, int addressId) =>
customerId > 0 && addressId > 0
? (await customerService.GetCurrentCustomerAddresses()).FirstOrDefault(a => a.AddressId == addressId)
: null;
public async Task<string> GetNextOrPreviousStepUrl<TCurrentStep>(Func<TCurrentStep, Guid> nextOrPreviousStepFunc)
where TCurrentStep : IWebPageFieldsSource, new()
{
var pageContext = webPageDataContextRetriever.Retrieve().WebPage;
var currentStepPage = await checkoutPageRepository.GetCartStepPage<TCurrentStep>(pageContext.WebPageItemID,
pageContext.LanguageName, pageContext.ContentTypeName);
string nextStepUrl =
(await webPageUrlRetriever.Retrieve(nextOrPreviousStepFunc(currentStepPage), pageContext.LanguageName))
.RelativePath;
return nextStepUrl;
}
/// <summary>
/// Creates view model for checkout preview step.
/// </summary>
/// <param name="paymentMethod">Payment method selected on preview step</param>
public async Task<SummaryViewModel> PreparePreviewViewModel(PaymentMethodViewModel paymentMethod = null)
{
var cart = await shoppingService.GetCurrentShoppingCartSummary();
var billingAddress = cart.CartDetails.BillingAddress;
var shippingOption = cart.CartDetails.ShippingOption;
var paymentMethods = new SelectList(cart.CartDetails.AvailablePaymentOptions,
nameof(KPaymentOption.PaymentOptionId), nameof(KPaymentOption.PaymentOptionDisplayName));
var paymentOption = cart.CartDetails.PaymentOption;
paymentMethod ??= new PaymentMethodViewModel(paymentOption, paymentMethods);
// PaymentMethods are excluded from automatic binding and must be recreated manually after post action
paymentMethod.PaymentMethods ??= paymentMethods;
var deliveryDetailsModel = new DeliveryDetailsViewModel
{
Customer = new CustomerViewModel(cart.CartDetails.Customer),
BillingAddress = await BillingAddressViewModel.GetModel(billingAddress, null, countryService),
ShippingOption = new ShippingOptionViewModel(shippingOption, null, cart.CartDetails.IsShippingNeeded)
};
var cartModel = new CartContentViewModel(cart.CartContent, await checkoutPageRepository.GetProductImages(cart.CartContent, preferredLanguageRetriever.Get()));
var viewModel = new SummaryViewModel
{
CartModel = cartModel,
DeliveryDetails = deliveryDetailsModel,
ShippingName = shippingOption?.ShippingOptionDisplayName ?? "",
PaymentMethod = paymentMethod,
CartPreviousStepUrl = await GetNextOrPreviousStepUrl<CartSummary>(s => s.CartPreviousStep.First().WebPageGuid)
};
return viewModel;
}
private async Task<SelectList> CreateCountryList()
{
var allCountries = await countryService.GetAllCountries();
return new SelectList(allCountries, nameof(CountryInfo.CountryID), nameof(CountryInfo.CountryDisplayName));
}
private SelectList CreateShippingOptionList(IEnumerable<KShippingOption> shippingOptions,
string currencyFormatString)
{
var selectList = shippingOptions.Select(s =>
{
return new SelectListItem
{
Value = s.ShippingOptionId.ToString(),
Text = $"{s.ShippingOptionDisplayName} ({string.Format(currencyFormatString, s.ShippingOptionPrice)})"
};
});
return new SelectList(selectList, "Value", "Text");
}
}