Skip to content

Commit

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

[ActionName("Details")]
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Details_PAY_NOW()
{
OrderVM.OrderHeader = _unitOfWork.OrderHeader.GetFirstOrDefault(u => u.Id == OrderVM.OrderHeader.Id, includeProperties: "ApplicationUser");
OrderVM.OrderDetail = _unitOfWork.OrderDetail.GetAll(u => u.OrderId == OrderVM.OrderHeader.Id, includeProperties: "Product");

//stripe settings
var domain = "https://localhost:44300/";
var options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = new List<SessionLineItemOptions>(),
Mode = "payment",
SuccessUrl = domain + $"admin/order/PaymentConfirmation?orderHeaderid={OrderVM.OrderHeader.Id}",
CancelUrl = domain + $"admin/order/details?orderId={OrderVM.OrderHeader.Id}",
};

foreach (var item in OrderVM.OrderDetail)
{

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

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

}

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

public IActionResult PaymentConfirmation(int orderHeaderid)
{
OrderHeader orderHeader = _unitOfWork.OrderHeader.GetFirstOrDefault(u => u.Id == orderHeaderid);
if (orderHeader.PaymentStatus == SD.PaymentStatusDelayedPayment)
{
var service = new SessionService();
Session session = service.Get(orderHeader.SessionId);
//check the stripe status
if (session.PaymentStatus.ToLower() == "paid")
{
_unitOfWork.OrderHeader.UpdateStatus(orderHeaderid, orderHeader.OrderStatus, SD.PaymentStatusApproved);
_unitOfWork.Save();
}
}
return View(orderHeaderid);
}

[HttpPost]
[Authorize(Roles = SD.Role_Admin + "," + SD.Role_Employee)]
[ValidateAntiForgeryToken]
Expand Down
7 changes: 5 additions & 2 deletions BulkyBookWeb/Areas/Admin/Views/Order/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
<br />
<div class="container">
<div class="card">
<div class="card-header bg-dark text-light ml-0 row container">
<div class="card-header bg-dark text-light ml-0">
<div class="container row">
<div class="col-12 d-none d-md-block col-md-6 pb-1">
<i class="fas fa-shopping-cart"></i> &nbsp; Order Summary
</div>
<div class="col-12 col-md-4 offset-md-2 text-right">
<a asp-action="Index" class="btn btn-outline-info form-control btn-sm">Back to Orders</a>
</div>
</div>
</div>
<div class="card-body">
<div class="container rounded p-2">
Expand Down Expand Up @@ -249,7 +251,8 @@
<input type="submit" class="btn btn-success form-control my-1" value="Pay Now" />
}
@if (Model.OrderHeader.OrderStatus != SD.StatusRefunded &&
Model.OrderHeader.OrderStatus != SD.StatusCancelled)
Model.OrderHeader.OrderStatus != SD.StatusCancelled &&
Model.OrderHeader.OrderStatus!=SD.StatusShipped)
{
<input type="submit" asp-action="CancelOrder" class="btn btn-danger form-control my-1" value="Cancel Order" />
}
Expand Down
16 changes: 16 additions & 0 deletions BulkyBookWeb/Areas/Admin/Views/Order/PaymentConfirmation.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@model int

<div class="container row pt-4">
<div class="col-12 text-center">
<h1 class="text-primary text-center">Payment Successfully!</h1>
Order Number : @Model <br /><br />
<img src="~/images/lawn_leaf.jpg" width="65%" />
</div>
<div class="col-12 text-center" style="color:maroon">
<br />
Payment has been applied successfully! <br />
<a asp-area="Admin" asp-controller="Order" asp-action="Details" asp-route-orderId="@Model" class="btn btn-primary">Back to Order Details</a>

</div>
</div>

0 comments on commit 3d867f1

Please sign in to comment.