-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDancingGoatCheckoutController.cs
More file actions
353 lines (289 loc) · 18.1 KB
/
Copy pathDancingGoatCheckoutController.cs
File metadata and controls
353 lines (289 loc) · 18.1 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CMS.Commerce;
using CMS.ContentEngine;
using CMS.DataEngine;
using CMS.Membership;
using DancingGoat;
using DancingGoat.Commerce;
using DancingGoat.Helpers;
using DancingGoat.Models;
using DancingGoat.Services;
using Kentico.Commerce.Web.Mvc;
using Kentico.Content.Web.Mvc.Routing;
using Kentico.Membership;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
[assembly: RegisterWebPageRoute(Checkout.CONTENT_TYPE_NAME, typeof(DancingGoatCheckoutController), WebsiteChannelNames = new[] { DancingGoatConstants.WEBSITE_CHANNEL_NAME })]
namespace DancingGoat.Commerce;
/// <summary>
/// Controller for managing the checkout process.
/// </summary>
public sealed class DancingGoatCheckoutController : Controller
{
private readonly CountryStateRepository countryStateRepository;
private readonly WebPageUrlProvider webPageUrlProvider;
private readonly ICurrentShoppingCartRetriever currentShoppingCartRetriever;
private readonly ICurrentShoppingCartDiscardHandler currentShoppingCartDiscardHandler;
private readonly UserManager<ApplicationUser> userManager;
private readonly CustomerDataRetriever customerDataRetriever;
private readonly IPreferredLanguageRetriever currentLanguageRetriever;
private readonly OrderService orderService;
private readonly IStringLocalizer<SharedResources> localizer;
private readonly ProductNameProvider productNameProvider;
private readonly ProductRepository productRepository;
private readonly PaymentRepository paymentRepository;
private readonly ShippingRepository shippingRepository;
private readonly CalculationService calculationService;
private readonly IInfoProvider<OrderInfo> orderInfoProvider;
public DancingGoatCheckoutController(
CountryStateRepository countryStateRepository,
WebPageUrlProvider webPageUrlProvider,
ICurrentShoppingCartRetriever currentShoppingCartRetriever,
ICurrentShoppingCartDiscardHandler currentShoppingCartDiscardHandler,
UserManager<ApplicationUser> userManager,
CustomerDataRetriever customerDataRetriever,
IPreferredLanguageRetriever currentLanguageRetriever,
OrderService orderService,
IStringLocalizer<SharedResources> localizer,
ProductNameProvider productNameProvider,
ProductRepository productRepository,
PaymentRepository paymentRepository,
ShippingRepository shippingRepository,
CalculationService calculationService,
IInfoProvider<OrderInfo> orderInfoProvider)
{
this.countryStateRepository = countryStateRepository;
this.webPageUrlProvider = webPageUrlProvider;
this.currentShoppingCartRetriever = currentShoppingCartRetriever;
this.currentShoppingCartDiscardHandler = currentShoppingCartDiscardHandler;
this.userManager = userManager;
this.customerDataRetriever = customerDataRetriever;
this.currentLanguageRetriever = currentLanguageRetriever;
this.orderService = orderService;
this.localizer = localizer;
this.productNameProvider = productNameProvider;
this.productRepository = productRepository;
this.paymentRepository = paymentRepository;
this.shippingRepository = shippingRepository;
this.calculationService = calculationService;
this.orderInfoProvider = orderInfoProvider;
}
[HttpGet]
public async Task<IActionResult> Index(CancellationToken cancellationToken)
{
return View(await GetCheckoutViewModel(CheckoutStep.CheckoutCustomer, null, null, null, null, null, 0, cancellationToken));
}
[HttpPost]
public async Task<IActionResult> Index(CustomerViewModel customer, CustomerAddressViewModel billingAddress, ShippingAddressViewModel shippingAddress, PaymentShippingViewModel paymentShipping, CheckoutStep checkoutStep, CancellationToken cancellationToken)
{
if (!await IsValid(billingAddress, shippingAddress, cancellationToken) || checkoutStep == CheckoutStep.CheckoutCustomer)
{
return View(await GetCheckoutViewModel(CheckoutStep.CheckoutCustomer, customer, billingAddress, shippingAddress, null, paymentShipping, 0, cancellationToken));
}
var shoppingCart = await currentShoppingCartRetriever.Get(cancellationToken);
if (shoppingCart == null)
{
return View(await GetCheckoutViewModel(CheckoutStep.OrderConfirmation, customer, billingAddress, shippingAddress,
new ShoppingCartViewModel(Enumerable.Empty<ShoppingCartItemViewModel>(), 0, 0, 0, 0, null, Enumerable.Empty<CouponCodeViewModel>()), paymentShipping, 0, cancellationToken));
}
var shoppingCartData = shoppingCart.GetShoppingCartDataModel();
int.TryParse(paymentShipping.ShippingMethodId, out var shippingMethodId);
int.TryParse(paymentShipping.PaymentMethodId, out var paymentMethodId);
var calculationResult = await calculationService.Calculate(shoppingCartData, PriceCalculationMode.Checkout, shippingMethodId, paymentMethodId, billingAddress, cancellationToken);
var checkoutViewModel = await GetCheckoutViewModel(CheckoutStep.OrderConfirmation, customer, billingAddress, shippingAddress, null, paymentShipping, calculationResult.ShippingPrice, cancellationToken);
checkoutViewModel = checkoutViewModel with
{
ShoppingCart = await GetShoppingCartViewModel(calculationResult, shoppingCartData, cancellationToken)
};
return View(checkoutViewModel);
}
[HttpPost]
[Route("/Checkout/GetStates")]
public async Task<IEnumerable<SelectListItem>> GetStates(int countryId, CancellationToken cancellationToken)
{
if (countryId > 0)
{
var states = await countryStateRepository.GetStates(countryId, cancellationToken);
return states.Select(x => new SelectListItem()
{
Text = x.StateDisplayName,
Value = x.StateID.ToString(),
}).ToList();
}
return new List<SelectListItem>();
}
[HttpPost]
[Route("{languageName}/OrderConfirmation/ConfirmOrder")]
public async Task<IActionResult> ConfirmOrder(CustomerViewModel customer, CustomerAddressViewModel billingAddress, ShippingAddressViewModel shippingAddress, string languageName, PaymentShippingViewModel paymentShipping, CancellationToken cancellationToken)
{
// Add the current language to the route values in order to tell XbyK what the current language is
// since this route is not handled by the XbyK content-tree-based routing
HttpContext.Request.RouteValues.Add(WebPageRoutingOptions.LANGUAGE_ROUTE_VALUE_KEY, languageName);
if (!ModelState.IsValid)
{
Redirect(await webPageUrlProvider.CheckoutPageUrl(languageName, cancellationToken: cancellationToken));
}
var user = await GetAuthenticatedUser();
var shoppingCart = await currentShoppingCartRetriever.Get(cancellationToken);
if (shoppingCart == null)
{
return Content(localizer["Order not created. The shopping cart could not be found."]);
}
var shoppingCartData = shoppingCart.GetShoppingCartDataModel();
int.TryParse(paymentShipping.PaymentMethodId, out var paymentMethodId);
int.TryParse(paymentShipping.ShippingMethodId, out var shippingMethodId);
var orderId = await orderService.CreateOrder(shoppingCartData, customer, billingAddress, shippingAddress, user?.Id ?? 0, languageName, paymentMethodId, shippingMethodId, paymentShipping.ShippingPrice, cancellationToken);
await currentShoppingCartDiscardHandler.Discard(cancellationToken);
var orderNumber = await OrderIdToOrderNumber(orderId, cancellationToken);
return View(new ConfirmOrderViewModel(orderNumber));
}
private async Task<string> OrderIdToOrderNumber(int orderId, CancellationToken cancellationToken)
{
var orderInfo = await orderInfoProvider.GetAsync(orderId, cancellationToken);
if (orderInfo == null)
{
throw new InvalidOperationException($"Order with ID {orderId} does not exist.");
}
return orderInfo.OrderNumber;
}
private async Task<bool> IsValid(CustomerAddressViewModel billingAddress, ShippingAddressViewModel shippingAddress, CancellationToken cancellationToken)
{
// Validate state selection based on the selected country for billing address
int.TryParse(billingAddress.CountryId, out int billingCountryId);
var billingCountryStates = await countryStateRepository.GetStates(billingCountryId, cancellationToken);
bool selectedStateValidationResult = !billingCountryStates.Any() || !string.IsNullOrEmpty(billingAddress.StateId);
if (!selectedStateValidationResult)
{
ModelState.AddModelError($"{nameof(billingAddress)}.{nameof(CustomerAddressViewModel.StateId)}", CheckoutFormConstants.REQUIRED_FIELD_ERROR_MESSAGE);
}
if (shippingAddress.IsSameAsBilling)
{
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.Line1)}");
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.Line2)}");
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.City)}");
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.PostalCode)}");
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.CountryId)}");
ModelState.Remove($"{nameof(shippingAddress)}.{nameof(shippingAddress.StateId)}");
}
else
{
// Validate state selection based on the selected country for shipping address
int.TryParse(shippingAddress.CountryId, out int shippingCountryId);
var shippingCountryStates = billingCountryId == shippingCountryId ? billingCountryStates : await countryStateRepository.GetStates(shippingCountryId, cancellationToken);
bool billingStateValidationResult = !shippingCountryStates.Any() || !string.IsNullOrEmpty(shippingAddress.StateId);
if (!billingStateValidationResult)
{
ModelState.AddModelError($"{nameof(shippingAddress)}.{nameof(ShippingAddressViewModel.StateId)}", CheckoutFormConstants.REQUIRED_FIELD_ERROR_MESSAGE);
}
}
return ModelState.IsValid;
}
private async Task<CheckoutViewModel> GetCheckoutViewModel(CheckoutStep step, CustomerViewModel customerViewModel, CustomerAddressViewModel billingAddressViewModel, ShippingAddressViewModel shippingAddressViewModel,
ShoppingCartViewModel shoppingCartViewModel, PaymentShippingViewModel paymentShippingViewModel, decimal shippingPrice, CancellationToken cancellationToken)
{
var user = await GetAuthenticatedUser();
// No model data is provided => try to retrieve data from the registered member/customer
if (user != null && customerViewModel == null)
{
// Retrieve email information for the registered member
customerViewModel = new CustomerViewModel()
{
Email = user.Email,
};
// The registered member already has a customer account
var customer = await customerDataRetriever.GetCustomerForMember(user.Id, cancellationToken);
if (customer != null)
{
customerViewModel.FirstName = customer.CustomerFirstName;
customerViewModel.LastName = customer.CustomerLastName;
customerViewModel.Email = customer.CustomerEmail;
customerViewModel.PhoneNumber = customer.CustomerPhone;
var customerAddress = await customerDataRetriever.GetCustomerAddress(customer.CustomerID, cancellationToken);
if (customerAddress != null)
{
customerViewModel.Company = customerAddress.CustomerAddressCompany;
billingAddressViewModel ??= new CustomerAddressViewModel();
billingAddressViewModel.Line1 = customerAddress.CustomerAddressLine1;
billingAddressViewModel.Line2 = customerAddress.CustomerAddressLine2;
billingAddressViewModel.City = customerAddress.CustomerAddressCity;
billingAddressViewModel.PostalCode = customerAddress.CustomerAddressZip;
billingAddressViewModel.CountryId = customerAddress.CustomerAddressCountryID.ToString();
billingAddressViewModel.StateId = customerAddress.CustomerAddressStateID.ToString();
}
}
}
customerViewModel ??= new CustomerViewModel();
billingAddressViewModel ??= new CustomerAddressViewModel();
shippingAddressViewModel ??= new ShippingAddressViewModel();
paymentShippingViewModel ??= new PaymentShippingViewModel();
int.TryParse(billingAddressViewModel.CountryId, out var billingCountryId);
int.TryParse(billingAddressViewModel.StateId, out var billingStateId);
var countries = await countryStateRepository.GetCountries(cancellationToken);
var billingStates = await countryStateRepository.GetStates(billingCountryId, cancellationToken);
var countriesSelectList = countries.Select(x => new SelectListItem() { Text = x.CountryDisplayName, Value = x.CountryID.ToString() });
billingAddressViewModel.Countries = countriesSelectList;
billingAddressViewModel.Country = countriesSelectList.FirstOrDefault(country => country.Value == billingCountryId.ToString())?.Text;
billingAddressViewModel.States = billingStates.Select(x => new SelectListItem() { Text = x.StateDisplayName, Value = x.StateID.ToString() }).ToList();
billingAddressViewModel.State = billingStates.FirstOrDefault(state => state.StateID == billingStateId)?.StateDisplayName;
int.TryParse(shippingAddressViewModel.CountryId, out var shippingCountryId);
int.TryParse(shippingAddressViewModel.StateId, out var shippingStateId);
var shippingStates = billingCountryId == shippingCountryId ? billingStates : await countryStateRepository.GetStates(shippingCountryId, cancellationToken);
shippingAddressViewModel.Countries = countriesSelectList;
shippingAddressViewModel.Country = countriesSelectList.FirstOrDefault(country => country.Value == shippingCountryId.ToString())?.Text;
shippingAddressViewModel.States = shippingStates.Select(x => new SelectListItem() { Text = x.StateDisplayName, Value = x.StateID.ToString() }).ToList();
shippingAddressViewModel.State = shippingStates.FirstOrDefault(state => state.StateID == shippingStateId)?.StateDisplayName;
var payment = await paymentRepository.GetPayments(cancellationToken);
var shipping = await shippingRepository.GetShipping(cancellationToken);
paymentShippingViewModel.Payments = payment.Select(x => new SelectListItem() { Text = x.PaymentMethodDisplayName, Value = x.PaymentMethodID.ToString() });
paymentShippingViewModel.Shippings = shipping.Select(x => new SelectListItem() { Text = x.ShippingMethodDisplayName, Value = x.ShippingMethodID.ToString() });
paymentShippingViewModel.ShippingPrice = shippingPrice;
return new CheckoutViewModel(step, customerViewModel, billingAddressViewModel, shippingAddressViewModel, shoppingCartViewModel, paymentShippingViewModel);
}
private async Task<ShoppingCartViewModel> GetShoppingCartViewModel(DancingGoatPriceCalculationResult calculationResult, ShoppingCartDataModel shoppingCartData, CancellationToken cancellationToken)
{
var languageName = currentLanguageRetriever.Get();
var products = await productRepository.GetProductsByIds(shoppingCartData.Items.Select(item => item.ProductIdentifier.Identifier), cancellationToken);
var productPageUrls = await productRepository.GetProductPageUrls(products.Cast<IContentItemFieldsSource>().Select(p => p.SystemFields.ContentItemID), cancellationToken);
var totalWithoutShippingAndTax = PriceCalculationTotalsCalculator.GetTotalWithoutShippingAndTax(calculationResult);
return new ShoppingCartViewModel(
shoppingCartData.Items.Select(item =>
{
var product = products.FirstOrDefault(product => (product as IContentItemFieldsSource)?.SystemFields.ContentItemID == item.ProductIdentifier.Identifier);
productPageUrls.TryGetValue(item.ProductIdentifier.Identifier, out var pageUrl);
var productName = productNameProvider.GetProductName(product, item.ProductIdentifier.VariantIdentifier);
var calculationItem = calculationResult.Items.FirstOrDefault(i => i.ProductIdentifier == item.ProductIdentifier);
return ((product == null) || (calculationItem == null))
? null
: new ShoppingCartItemViewModel(
item.ProductIdentifier.Identifier,
productName,
product.ProductFieldImage.FirstOrDefault()?.ImageFile.Url,
pageUrl,
item.Quantity,
product.ProductFieldPrice,
calculationItem.LineSubtotalAfterLineDiscount,
item.Quantity * product.ProductFieldPrice,
calculationItem.PromotionData.CatalogPromotionCandidates.FirstOrDefault(c => c.Applied)?.PromotionCandidate as DancingGoatCatalogPromotionCandidate,
item.ProductIdentifier.VariantIdentifier);
})
.Where(x => x != null)
.ToList(),
calculationResult.GrandTotal,
totalWithoutShippingAndTax,
calculationResult.TotalTax,
0,
null,
Enumerable.Empty<CouponCodeViewModel>());
}
/// <summary>
/// Retrieves an authenticated live site user.
/// </summary>
/// <seealso cref="MemberInfo"/>"/>
private async Task<ApplicationUser> GetAuthenticatedUser() => await userManager.GetUserAsync(User);
}