-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
OrderController.cs
268 lines (219 loc) · 10.6 KB
/
OrderController.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
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
using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Localization;
using Nop.Services.Messages;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Services.Shipping;
using Nop.Web.Factories;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc.Filters;
namespace Nop.Web.Controllers;
[AutoValidateAntiforgeryToken]
public partial class OrderController : BasePublicController
{
#region Fields
protected readonly ICustomerService _customerService;
protected readonly ILocalizationService _localizationService;
protected readonly INotificationService _notificationService;
protected readonly IOrderModelFactory _orderModelFactory;
protected readonly IOrderProcessingService _orderProcessingService;
protected readonly IOrderService _orderService;
protected readonly IPaymentService _paymentService;
protected readonly IPdfService _pdfService;
protected readonly IShipmentService _shipmentService;
protected readonly IWebHelper _webHelper;
protected readonly IWorkContext _workContext;
protected readonly RewardPointsSettings _rewardPointsSettings;
#endregion
#region Ctor
public OrderController(ICustomerService customerService,
ILocalizationService localizationService,
INotificationService notificationService,
IOrderModelFactory orderModelFactory,
IOrderProcessingService orderProcessingService,
IOrderService orderService,
IPaymentService paymentService,
IPdfService pdfService,
IShipmentService shipmentService,
IWebHelper webHelper,
IWorkContext workContext,
RewardPointsSettings rewardPointsSettings)
{
_customerService = customerService;
_localizationService = localizationService;
_notificationService = notificationService;
_orderModelFactory = orderModelFactory;
_orderProcessingService = orderProcessingService;
_orderService = orderService;
_paymentService = paymentService;
_pdfService = pdfService;
_shipmentService = shipmentService;
_webHelper = webHelper;
_workContext = workContext;
_rewardPointsSettings = rewardPointsSettings;
}
#endregion
#region Methods
//My account / Orders
public virtual async Task<IActionResult> CustomerOrders()
{
if (!await _customerService.IsRegisteredAsync(await _workContext.GetCurrentCustomerAsync()))
return Challenge();
var model = await _orderModelFactory.PrepareCustomerOrderListModelAsync();
return View(model);
}
//My account / Orders / Cancel recurring order
[HttpPost, ActionName("CustomerOrders")]
[FormValueRequired(FormValueRequirement.StartsWith, "cancelRecurringPayment")]
public virtual async Task<IActionResult> CancelRecurringPayment(IFormCollection form)
{
var customer = await _workContext.GetCurrentCustomerAsync();
if (!await _customerService.IsRegisteredAsync(customer))
return Challenge();
//get recurring payment identifier
var recurringPaymentId = 0;
foreach (var formValue in form.Keys)
if (formValue.StartsWith("cancelRecurringPayment", StringComparison.InvariantCultureIgnoreCase))
recurringPaymentId = Convert.ToInt32(formValue["cancelRecurringPayment".Length..]);
var recurringPayment = await _orderService.GetRecurringPaymentByIdAsync(recurringPaymentId);
if (recurringPayment == null)
{
return RedirectToRoute("CustomerOrders");
}
if (await _orderProcessingService.CanCancelRecurringPaymentAsync(customer, recurringPayment))
{
var errors = await _orderProcessingService.CancelRecurringPaymentAsync(recurringPayment);
var model = await _orderModelFactory.PrepareCustomerOrderListModelAsync();
model.RecurringPaymentErrors = errors;
return View(model);
}
return RedirectToRoute("CustomerOrders");
}
//My account / Orders / Retry last recurring order
[HttpPost, ActionName("CustomerOrders")]
[FormValueRequired(FormValueRequirement.StartsWith, "retryLastPayment")]
public virtual async Task<IActionResult> RetryLastRecurringPayment(IFormCollection form)
{
var customer = await _workContext.GetCurrentCustomerAsync();
if (!await _customerService.IsRegisteredAsync(customer))
return Challenge();
//get recurring payment identifier
var recurringPaymentId = 0;
if (!form.Keys.Any(formValue => formValue.StartsWith("retryLastPayment", StringComparison.InvariantCultureIgnoreCase) &&
int.TryParse(formValue[(formValue.IndexOf('_') + 1)..], out recurringPaymentId)))
{
return RedirectToRoute("CustomerOrders");
}
var recurringPayment = await _orderService.GetRecurringPaymentByIdAsync(recurringPaymentId);
if (recurringPayment == null)
return RedirectToRoute("CustomerOrders");
if (!await _orderProcessingService.CanRetryLastRecurringPaymentAsync(customer, recurringPayment))
return RedirectToRoute("CustomerOrders");
var errors = await _orderProcessingService.ProcessNextRecurringPaymentAsync(recurringPayment);
var model = await _orderModelFactory.PrepareCustomerOrderListModelAsync();
model.RecurringPaymentErrors = errors.ToList();
return View(model);
}
//My account / Reward points
public virtual async Task<IActionResult> CustomerRewardPoints(int? pageNumber)
{
if (!await _customerService.IsRegisteredAsync(await _workContext.GetCurrentCustomerAsync()))
return Challenge();
if (!_rewardPointsSettings.Enabled)
return RedirectToRoute("CustomerInfo");
var model = await _orderModelFactory.PrepareCustomerRewardPointsAsync(pageNumber);
return View(model);
}
//My account / Order details page
public virtual async Task<IActionResult> Details(int orderId)
{
var order = await _orderService.GetOrderByIdAsync(orderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
var model = await _orderModelFactory.PrepareOrderDetailsModelAsync(order);
return View(model);
}
//My account / Order details page / Print
public virtual async Task<IActionResult> PrintOrderDetails(int orderId)
{
var order = await _orderService.GetOrderByIdAsync(orderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
var model = await _orderModelFactory.PrepareOrderDetailsModelAsync(order);
model.PrintMode = true;
return View("Details", model);
}
//My account / Order details page / PDF invoice
[CheckLanguageSeoCode(ignore: true)]
public virtual async Task<IActionResult> GetPdfInvoice(int orderId)
{
var order = await _orderService.GetOrderByIdAsync(orderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
byte[] bytes;
await using (var stream = new MemoryStream())
{
await _pdfService.PrintOrderToPdfAsync(stream, order, await _workContext.GetWorkingLanguageAsync());
bytes = stream.ToArray();
}
return File(bytes, MimeTypes.ApplicationPdf, string.Format(await _localizationService.GetResourceAsync("PDFInvoice.FileName"), order.CustomOrderNumber) + ".pdf");
}
//My account / Order details page / re-order
public virtual async Task<IActionResult> ReOrder(int orderId)
{
var order = await _orderService.GetOrderByIdAsync(orderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
var warnings = await _orderProcessingService.ReOrderAsync(order);
if (warnings.Any())
_notificationService.WarningNotification(await _localizationService.GetResourceAsync("ShoppingCart.ReorderWarning"));
return RedirectToRoute("ShoppingCart");
}
//My account / Order details page / Complete payment
[HttpPost, ActionName("Details")]
[FormValueRequired("repost-payment")]
public virtual async Task<IActionResult> RePostPayment(int orderId)
{
var order = await _orderService.GetOrderByIdAsync(orderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
if (!await _paymentService.CanRePostProcessPaymentAsync(order))
return RedirectToRoute("OrderDetails", new { orderId = orderId });
var postProcessPaymentRequest = new PostProcessPaymentRequest
{
Order = order
};
await _paymentService.PostProcessPaymentAsync(postProcessPaymentRequest);
if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
{
//redirection or POST has been done in PostProcessPayment
return Content("Redirected");
}
//if no redirection has been done (to a third-party payment page)
//theoretically it's not possible
return RedirectToRoute("OrderDetails", new { orderId = orderId });
}
//My account / Order details page / Shipment details page
public virtual async Task<IActionResult> ShipmentDetails(int shipmentId)
{
var shipment = await _shipmentService.GetShipmentByIdAsync(shipmentId);
if (shipment == null)
return Challenge();
var order = await _orderService.GetOrderByIdAsync(shipment.OrderId);
var customer = await _workContext.GetCurrentCustomerAsync();
if (order == null || order.Deleted || customer.Id != order.CustomerId)
return Challenge();
var model = await _orderModelFactory.PrepareShipmentDetailsModelAsync(shipment);
return View(model);
}
#endregion
}