Repository navigation
Expand file tree
/
Copy pathOrderController.cs
More file actions
278 lines (228 loc) · 10.9 KB
/
Copy pathOrderController.cs
File metadata and controls
278 lines (228 loc) · 10.9 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
using Grand.Core;
using Grand.Core.Domain.Customers;
using Grand.Core.Domain.Orders;
using Grand.Core.Domain.Shipping;
using Grand.Core.Infrastructure;
using Grand.Framework.Controllers;
using Grand.Framework.Security;
using Grand.Services.Common;
using Grand.Services.Localization;
using Grand.Services.Orders;
using Grand.Services.Payments;
using Grand.Services.Shipping;
using Grand.Web.Models.Order;
using Grand.Web.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public partial class OrderController : BasePublicController
{
#region Fields
private readonly IOrderViewModelService _orderViewModelService;
private readonly IOrderService _orderService;
private readonly IWorkContext _workContext;
private readonly IOrderProcessingService _orderProcessingService;
private readonly IPaymentService _paymentService;
private readonly ILocalizationService _localizationService;
private readonly OrderSettings _orderSettings;
#endregion
#region Constructors
public OrderController(IOrderViewModelService orderViewModelService,
IOrderService orderService,
IWorkContext workContext,
IOrderProcessingService orderProcessingService,
IPaymentService paymentService,
ILocalizationService localizationService,
OrderSettings orderSettings)
{
this._orderViewModelService = orderViewModelService;
this._orderService = orderService;
this._workContext = workContext;
this._orderProcessingService = orderProcessingService;
this._paymentService = paymentService;
this._localizationService = localizationService;
this._orderSettings = orderSettings;
}
#endregion
#region Methods
//My account / Orders
public virtual async Task<IActionResult> CustomerOrders()
{
if (!_workContext.CurrentCustomer.IsRegistered())
return Challenge();
var model = await _orderViewModelService.PrepareCustomerOrderList();
return View(model);
}
//My account / Orders / Cancel recurring order
[HttpPost, ActionName("CustomerOrders")]
[PublicAntiForgery]
[FormValueRequired(FormValueRequirement.StartsWith, "cancelRecurringPayment")]
public virtual async Task<IActionResult> CancelRecurringPayment(IFormCollection form)
{
if (!_workContext.CurrentCustomer.IsRegistered())
return Challenge();
//get recurring payment identifier
string recurringPaymentId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("cancelRecurringPayment", StringComparison.OrdinalIgnoreCase))
recurringPaymentId = formValue.Substring("cancelRecurringPayment".Length);
var recurringPayment = await _orderService.GetRecurringPaymentById(recurringPaymentId);
if (recurringPayment == null)
{
return RedirectToRoute("CustomerOrders");
}
if (await _orderProcessingService.CanCancelRecurringPayment(_workContext.CurrentCustomer, recurringPayment))
{
var errors = await _orderProcessingService.CancelRecurringPayment(recurringPayment);
var model = await _orderViewModelService.PrepareCustomerOrderList();
model.CancelRecurringPaymentErrors = errors;
return View(model);
}
else
{
return RedirectToRoute("CustomerOrders");
}
}
//My account / Reward points
public virtual async Task<IActionResult> CustomerRewardPoints([FromServices] RewardPointsSettings rewardPointsSettings)
{
if (!_workContext.CurrentCustomer.IsRegistered())
return Challenge();
if (!rewardPointsSettings.Enabled)
return RedirectToRoute("CustomerInfo");
var customer = _workContext.CurrentCustomer;
var model = await _orderViewModelService.PrepareCustomerRewardPoints(customer);
return View(model);
}
//My account / Order details page
public virtual async Task<IActionResult> Details(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
var model = await _orderViewModelService.PrepareOrderDetails(order);
return View(model);
}
//My account / Order details page / Print
public virtual async Task<IActionResult> PrintOrderDetails(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
var model = await _orderViewModelService.PrepareOrderDetails(order);
model.PrintMode = true;
return View("Details", model);
}
//My account / Order details page / Cancel Unpaid Order
public virtual async Task<IActionResult> CancelOrder(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.PaymentStatus != Core.Domain.Payments.PaymentStatus.Pending
|| (order.ShippingStatus != ShippingStatus.ShippingNotRequired && order.ShippingStatus != ShippingStatus.NotYetShipped)
|| order.OrderStatus != OrderStatus.Pending
|| order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId
|| !_orderSettings.UserCanCancelUnpaidOrder)
return Challenge();
await _orderProcessingService.CancelOrder(order, true, true);
return RedirectToRoute("OrderDetails", new { orderId = orderId });
}
//My account / Order details page / PDF invoice
public virtual async Task<IActionResult> GetPdfInvoice(string orderId, [FromServices] IPdfService pdfService)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
var orders = new List<Order>();
orders.Add(order);
byte[] bytes;
using (var stream = new MemoryStream())
{
await pdfService.PrintOrdersToPdf(stream, orders, _workContext.WorkingLanguage.Id);
bytes = stream.ToArray();
}
return File(bytes, "application/pdf", string.Format("order_{0}.pdf", order.Id));
}
//My account / Order details page / Add order note
public virtual async Task<IActionResult> AddOrderNote(string orderId)
{
if (!_orderSettings.AllowCustomerToAddOrderNote)
return RedirectToRoute("HomePage");
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
var model = new AddOrderNoteModel();
model.OrderId = orderId;
return View("AddOrderNote", model);
}
//My account / Order details page / Add order note
[HttpPost]
[PublicAntiForgery]
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 (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
await _orderViewModelService.InsertOrderNote(model);
AddNotification(Framework.UI.NotifyType.Success, _localizationService.GetResource("OrderNote.Added"), true);
return RedirectToRoute("OrderDetails", model.OrderId);
}
//My account / Order details page / re-order
public virtual async Task<IActionResult> ReOrder(string orderId)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
await _orderProcessingService.ReOrder(order);
return RedirectToRoute("ShoppingCart");
}
//My account / Order details page / Complete payment
[HttpPost, ActionName("Details")]
[FormValueRequired("repost-payment")]
[PublicAntiForgery]
public virtual async Task<IActionResult> RePostPayment(string orderId, [FromServices] IWebHelper webHelper)
{
var order = await _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
if (!await _paymentService.CanRePostProcessPayment(order))
return RedirectToRoute("OrderDetails", new { orderId = orderId });
var postProcessPaymentRequest = new PostProcessPaymentRequest
{
Order = order
};
await _paymentService.PostProcessPayment(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(string shipmentId, [FromServices] IShipmentService shipmentService)
{
var shipment = await shipmentService.GetShipmentById(shipmentId);
if (shipment == null)
return Challenge();
var order = await _orderService.GetOrderById(shipment.OrderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return Challenge();
var model = await _orderViewModelService.PrepareShipmentDetails(shipment);
return View(model);
}
#endregion
}
}