-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathOrderController.cs
More file actions
268 lines (222 loc) · 10.3 KB
/
OrderController.cs
File metadata and controls
268 lines (222 loc) · 10.3 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
using Grand.Business.Core.Commands.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Checkout.Payments;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Pdf;
using Grand.Domain.Orders;
using Grand.Domain.Payments;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
using Grand.Web.Commands.Models.Orders;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Page;
using Grand.Web.Events;
using Grand.Web.Extensions;
using Grand.Web.Features.Models.Orders;
using Grand.Web.Models.Orders;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Grand.Web.Common.Security.Authorization;
using Grand.Domain.Customers;
using MongoDB.Driver;
namespace Grand.Web.Controllers
{
[DenySystemAccount]
public class OrderController : BasePublicController
{
#region Fields
private readonly IOrderService _orderService;
private readonly IWorkContext _workContext;
private readonly IPaymentService _paymentService;
private readonly IPaymentTransactionService _paymentTransactionService;
private readonly ITranslationService _translationService;
private readonly IGroupService _groupService;
private readonly IMediator _mediator;
private readonly OrderSettings _orderSettings;
#endregion
#region Constructors
public OrderController(IOrderService orderService,
IWorkContext workContext,
IPaymentService paymentService,
IPaymentTransactionService paymentTransactionService,
ITranslationService translationService,
IGroupService groupService,
IMediator mediator,
OrderSettings orderSettings)
{
_orderService = orderService;
_workContext = workContext;
_paymentService = paymentService;
_paymentTransactionService = paymentTransactionService;
_translationService = translationService;
_groupService = groupService;
_mediator = mediator;
_orderSettings = orderSettings;
}
#endregion
#region Utilities
protected virtual bool IsRequestBeingRedirected {
get {
var response = HttpContext.Response;
return new List<int> { 301, 302 }.Contains(response.StatusCode);
}
}
protected virtual bool IsPostBeingDone {
get => HttpContext.Items["grand.IsPOSTBeingDone"] != null && Convert.ToBoolean(HttpContext.Items["grand.IsPOSTBeingDone"]);
set => HttpContext.Items["grand.IsPOSTBeingDone"] = value;
}
#endregion
#region Methods
[HttpPost]
public virtual async Task<IActionResult> Search( int orderNumber)
{
var order = await _orderService.GetOrderByNumber(orderNumber);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var model = await _mediator.Send(new GetOrderDetails { Order = order, Language = _workContext.WorkingLanguage });
return View("Details",model);
}
//My account / Orders
[HttpGet]
[CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> CustomerOrders(OrderPagingModel command)
{
var model = await _mediator.Send(new GetCustomerOrderList {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Command = command
});
return View(model);
}
//My account / Order details page
[HttpGet]
public virtual async Task<IActionResult> Details(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var model = await _mediator.Send(new GetOrderDetails { Order = order, Language = _workContext.WorkingLanguage });
return View(model);
}
//My account / Order details page / Cancel Unpaid Order
[HttpGet]
public virtual async Task<IActionResult> CancelOrder(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService)
|| order.PaymentStatusId != PaymentStatus.Pending
|| (order.ShippingStatusId != ShippingStatus.ShippingNotRequired && order.ShippingStatusId != ShippingStatus.Pending)
|| order.OrderStatusId != (int)OrderStatusSystem.Pending
|| !_orderSettings.UserCanCancelUnpaidOrder)
return Challenge();
await _mediator.Send(new CancelOrderCommand { Order = order, NotifyCustomer = true, NotifyStoreOwner = true });
return RedirectToRoute("OrderDetails", new { orderId });
}
//My account / Order details page / PDF invoice
[HttpGet]
public virtual async Task<IActionResult> GetPdfInvoice(string orderId, [FromServices] IPdfService pdfService)
{
var order = await _orderService.GetOrderById(orderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var orders = new List<Order> { order };
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintOrdersToPdf(stream, orders, _workContext.WorkingLanguage.Id);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", $"order_{order.Id}.pdf");
}
//My account / Order details page / Add order note
[HttpPost]
[AutoValidateAntiforgeryToken]
public virtual async Task<IActionResult> AddOrderNote(AddOrderNoteModel model)
{
if (!_orderSettings.AllowCustomerToAddOrderNote)
return RedirectToRoute("HomePage");
if (!ModelState.IsValid)
{
return View("AddOrderNote", model);
}
var order = await _orderService.GetOrderById(model.OrderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
await _mediator.Send(new InsertOrderNoteCommand { Order = order, OrderNote = model, Language = _workContext.WorkingLanguage });
//notification
await _mediator.Publish(new OrderNoteEvent(order, model));
Notification(NotifyType.Success, _translationService.GetResource("OrderNote.Added"), true);
return RedirectToRoute("OrderDetails", new { orderId = model.OrderId });
}
//My account / Order details page / re-order
[HttpGet]
public virtual async Task<IActionResult> ReOrder(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var warnings = await _mediator.Send(new ReOrderCommand { Order = order });
if (warnings.Any())
Notification(NotifyType.Error, string.Join(",", warnings), true);
return RedirectToRoute("ShoppingCart");
}
//My account / Order details page / Complete payment
[HttpPost]
[AutoValidateAntiforgeryToken]
public virtual async Task<IActionResult> RePostPayment(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var paymentTransaction = await _paymentTransactionService.GetOrderByGuid(order.OrderGuid);
if (paymentTransaction == null || !await _paymentService.CanRePostRedirectPayment(paymentTransaction))
return RedirectToRoute("OrderDetails", new { orderId });
await _paymentService.PostRedirectPayment(paymentTransaction);
if (IsRequestBeingRedirected || 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 });
}
//My account / Order details page / Shipment details page
[HttpGet]
public virtual async Task<IActionResult> ShipmentDetails(string shipmentId, [FromServices] IShipmentService shipmentService)
{
var shipment = await shipmentService.GetShipmentById(shipmentId);
if (shipment == null)
return Challenge();
var order = await _orderService.GetOrderById(shipment.OrderId);
if (!await order.Access(_workContext.CurrentCustomer, _groupService))
return Challenge();
var model = await _mediator.Send(new GetShipmentDetails {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
Order = order,
Shipment = shipment
});
return View(model);
}
//My account / Loyalty points
[HttpGet]
[CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]
public virtual async Task<IActionResult> CustomerLoyaltyPoints([FromServices] LoyaltyPointsSettings loyaltyPointsSettings)
{
if (!loyaltyPointsSettings.Enabled)
return RedirectToRoute("CustomerInfo");
var model = await _mediator.Send(new GetCustomerLoyaltyPoints {
Customer = _workContext.CurrentCustomer,
Store = _workContext.CurrentStore,
Currency = _workContext.WorkingCurrency
});
return View(model);
}
#endregion
}
}