Skip to content

Commit

Permalink
Section 10 - Place Order for Company User
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 26, 2021
1 parent b93a6f4 commit cd232c6
Showing 1 changed file with 60 additions and 38 deletions.
98 changes: 60 additions & 38 deletions BulkyBookWeb/Areas/Customer/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public IActionResult SummaryPOST()
ShoppingCartVM.ListCart = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == claim.Value,
includeProperties: "Product");

ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusPending;
ShoppingCartVM.OrderHeader.OrderStatus = SD.StatusPending;

ShoppingCartVM.OrderHeader.OrderDate = System.DateTime.Now;
ShoppingCartVM.OrderHeader.ApplicationUserId = claim.Value;

Expand All @@ -97,7 +96,18 @@ public IActionResult SummaryPOST()
cart.Product.Price50, cart.Product.Price100);
ShoppingCartVM.OrderHeader.OrderTotal += (cart.Price * cart.Count);
}
ApplicationUser applicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(u => u.Id == claim.Value);

if (applicationUser.CompanyId.GetValueOrDefault() == 0)
{
ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusPending;
ShoppingCartVM.OrderHeader.OrderStatus = SD.StatusPending;
}
else
{
ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusDelayedPayment;
ShoppingCartVM.OrderHeader.OrderStatus = SD.StatusApproved;
}

_unitOfWork.OrderHeader.Add(ShoppingCartVM.OrderHeader);
_unitOfWork.Save();
Expand All @@ -114,48 +124,56 @@ public IActionResult SummaryPOST()
_unitOfWork.Save();
}

//stripe settings
var domain = "https://localhost:44300/";
var options = new SessionCreateOptions

if (applicationUser.CompanyId.GetValueOrDefault() == 0)
{
PaymentMethodTypes = new List<string>
//stripe settings
var domain = "https://localhost:44300/";
var options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = new List<SessionLineItemOptions>(),
Mode = "payment",
SuccessUrl = domain+$"customer/cart/OrderConfirmation?id={ShoppingCartVM.OrderHeader.Id}",
CancelUrl = domain+$"customer/cart/index",
};

foreach(var item in ShoppingCartVM.ListCart)
{
LineItems = new List<SessionLineItemOptions>(),
Mode = "payment",
SuccessUrl = domain + $"customer/cart/OrderConfirmation?id={ShoppingCartVM.OrderHeader.Id}",
CancelUrl = domain + $"customer/cart/index",
};

var sessionLineItem = new SessionLineItemOptions
foreach (var item in ShoppingCartVM.ListCart)
{
PriceData = new SessionLineItemPriceDataOptions

var sessionLineItem = new SessionLineItemOptions
{
UnitAmount = (long)(item.Price * 100),//20.00 -> 2000
Currency = "usd",
ProductData = new SessionLineItemPriceDataProductDataOptions
PriceData = new SessionLineItemPriceDataOptions
{
Name = item.Product.Title
UnitAmount = (long)(item.Price * 100),//20.00 -> 2000
Currency = "usd",
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Name = item.Product.Title
},

},
Quantity = item.Count,
};
options.LineItems.Add(sessionLineItem);

},
Quantity = item.Count,
};
options.LineItems.Add(sessionLineItem);

}
}

var service = new SessionService();
Session session = service.Create(options);
_unitOfWork.OrderHeader.UpdateStripePaymentID(ShoppingCartVM.OrderHeader.Id,session.Id,session.PaymentIntentId);
_unitOfWork.Save();
Response.Headers.Add("Location", session.Url);
return new StatusCodeResult(303);
var service = new SessionService();
Session session = service.Create(options);
_unitOfWork.OrderHeader.UpdateStripePaymentID(ShoppingCartVM.OrderHeader.Id, session.Id, session.PaymentIntentId);
_unitOfWork.Save();
Response.Headers.Add("Location", session.Url);
return new StatusCodeResult(303);
}

else
{
return RedirectToAction("OrderConfirmation", "Cart", new { id = ShoppingCartVM.OrderHeader.Id });
}

//_unitOfWork.ShoppingCart.RemoveRange(ShoppingCartVM.ListCart);
//_unitOfWork.Save();
Expand All @@ -165,14 +183,18 @@ public IActionResult SummaryPOST()
public IActionResult OrderConfirmation(int id)
{
OrderHeader orderHeader = _unitOfWork.OrderHeader.GetFirstOrDefault(u => u.Id == id);
var service = new SessionService();
Session session = service.Get(orderHeader.SessionId);
//check the stripe status
if (session.PaymentStatus.ToLower() == "paid")
if (orderHeader.PaymentStatus != SD.PaymentStatusDelayedPayment)
{
_unitOfWork.OrderHeader.UpdateStatus(id, SD.StatusApproved, SD.PaymentStatusApproved);
_unitOfWork.Save();
var service = new SessionService();
Session session = service.Get(orderHeader.SessionId);
//check the stripe status
if (session.PaymentStatus.ToLower() == "paid")
{
_unitOfWork.OrderHeader.UpdateStatus(id, SD.StatusApproved, SD.PaymentStatusApproved);
_unitOfWork.Save();
}
}

List<ShoppingCart> shoppingCarts = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId ==
orderHeader.ApplicationUserId).ToList();
_unitOfWork.ShoppingCart.RemoveRange(shoppingCarts);
Expand Down

0 comments on commit cd232c6

Please sign in to comment.