Skip to content

Commit

Permalink
Section 11 - Cancel Order
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 27, 2021
1 parent d014357 commit 9143413
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions BulkyBookWeb/Areas/Admin/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BulkyBook.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Stripe;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -39,6 +40,7 @@ public IActionResult Details(int orderId)
}

[HttpPost]
[Authorize(Roles = SD.Role_Admin + "," + SD.Role_Employee)]
[ValidateAntiForgeryToken]
public IActionResult UpdateOrderDetail()
{
Expand All @@ -64,6 +66,7 @@ public IActionResult UpdateOrderDetail()
}

[HttpPost]
[Authorize(Roles = SD.Role_Admin + "," + SD.Role_Employee)]
[ValidateAntiForgeryToken]
public IActionResult StartProcessing()
{
Expand All @@ -74,6 +77,7 @@ public IActionResult StartProcessing()
}

[HttpPost]
[Authorize(Roles = SD.Role_Admin + "," + SD.Role_Employee)]
[ValidateAntiForgeryToken]
public IActionResult ShipOrder()
{
Expand All @@ -82,12 +86,45 @@ public IActionResult ShipOrder()
orderHeader.Carrier = OrderVM.OrderHeader.Carrier;
orderHeader.OrderStatus = SD.StatusShipped;
orderHeader.ShippingDate = DateTime.Now;
if (orderHeader.PaymentStatus == SD.PaymentStatusDelayedPayment)
{
orderHeader.PaymentDueDate = DateTime.Now.AddDays(30);
}
_unitOfWork.OrderHeader.Update(orderHeader);
_unitOfWork.Save();
TempData["Success"] = "Order Shipped Successfully.";
return RedirectToAction("Details", "Order", new { orderId = OrderVM.OrderHeader.Id });
}

[HttpPost]
[Authorize(Roles = SD.Role_Admin + "," + SD.Role_Employee)]
[ValidateAntiForgeryToken]
public IActionResult CancelOrder()
{
var orderHeader = _unitOfWork.OrderHeader.GetFirstOrDefault(u => u.Id == OrderVM.OrderHeader.Id, tracked: false);
if (orderHeader.PaymentStatus == SD.PaymentStatusApproved)
{
var options = new RefundCreateOptions
{
Reason = RefundReasons.RequestedByCustomer,
PaymentIntent = orderHeader.PaymentIntentId
};

var service = new RefundService();
Refund refund = service.Create(options);

_unitOfWork.OrderHeader.UpdateStatus(orderHeader.Id, SD.StatusCancelled, SD.StatusRefunded);
}
else
{
_unitOfWork.OrderHeader.UpdateStatus(orderHeader.Id, SD.StatusCancelled, SD.StatusCancelled);
}
_unitOfWork.Save();

TempData["Success"] = "Order Cancelled Successfully.";
return RedirectToAction("Details", "Order", new { orderId = OrderVM.OrderHeader.Id });
}

#region API CALLS
[HttpGet]
public IActionResult GetAll(string status)
Expand Down
4 changes: 2 additions & 2 deletions BulkyBookWeb/Areas/Admin/Views/Order/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@
{
<input type="submit" class="btn btn-success form-control my-1" value="Pay Now" />
}
@if (Model.OrderHeader.OrderStatus != SD.StatusRefunded ||
@if (Model.OrderHeader.OrderStatus != SD.StatusRefunded &&
Model.OrderHeader.OrderStatus != SD.StatusCancelled)
{
<input type="submit" class="btn btn-danger form-control my-1" value="Cancel Order" />
<input type="submit" asp-action="CancelOrder" class="btn btn-danger form-control my-1" value="Cancel Order" />
}
}
</div>
Expand Down

0 comments on commit 9143413

Please sign in to comment.