in Hands-on lab/lab-files/src/Contoso Sports League/Contoso.Apps.SportsLeague.Web/Controllers/CheckoutController.cs [80:192]
public async Task<ActionResult> Review(CheckoutModel data)
{
if (ModelState.IsValid)
{
try
{
var gatewayCaller = new NVPAPICaller(_config, HttpContext);
var token = string.Empty;
var decoder = new NVPCodec();
// Call the gateway payment authorization API:
bool ret = gatewayCaller.DoCheckoutAuth(data.Order, ref token, ref decoder);
// If authorizaton is successful:
if (ret)
{
// Hydrate a new Order model from our OrderModel.
var myOrder = _mapper.Map<Data.Models.Order>(data.Order);
// Timestamp with a UTC date.
myOrder.OrderDate = DateTime.UtcNow;
// Add order to DB.
_db.Orders.Add(myOrder);
await _db.SaveChangesAsync();
// Get the shopping cart items and process them.
var usersShoppingCart = new ShoppingCartActions(_db, CartId);
List<CartItem> myOrderList = usersShoppingCart.GetCartItems();
// Add OrderDetail information to the DB for each product purchased.
for (int i = 0; i < myOrderList.Count; i++)
{
// Create a new OrderDetail object.
var myOrderDetail = new OrderDetail();
myOrderDetail.OrderId = myOrder.OrderId;
myOrderDetail.ProductId = myOrderList[i].ProductId;
myOrderDetail.Quantity = myOrderList[i].Quantity;
myOrderDetail.UnitPrice = myOrderList[i].Product.UnitPrice;
// Add OrderDetail to DB.
_db.OrderDetails.Add(myOrderDetail);
_db.SaveChanges();
}
// Set OrderId.
HttpContext.Session.SetInt32("currentOrderId", myOrder.OrderId);
HttpContext.Session.SetString("Token", token);
// Report successful event to Application Insights.
var eventProperties = new Dictionary<string, string>();
eventProperties.Add("CustomerEmail", data.Order.Email);
eventProperties.Add("NumberOfItems", myOrderList.Count.ToString());
eventProperties.Add("OrderTotal", data.Order.Total.ToString("C2"));
eventProperties.Add("OrderId", myOrder.OrderId.ToString());
TelemetryHelper.TrackEvent("SuccessfulPaymentAuth", eventProperties);
data.Order.OrderId = myOrder.OrderId;
if (data.Order.CreditCardNumber.Length > 4)
{
// Only show the last 4 digits of the credit card number:
data.Order.CreditCardNumber = "xxxxxxxxxxx" + data.Order.CreditCardNumber.Substring(data.Order.CreditCardNumber.Length - 4);
}
}
else
{
var error = gatewayCaller.PopulateGatewayErrorModel(decoder);
// Report failed event to Application Insights.
Exception ex = new Exception(error.ToString());
ex.Source = "Contoso.Apps.SportsLeague.Web.CheckoutController.cs";
TelemetryHelper.TrackException(ex);
// Redirect to the checkout error view:
return RedirectToAction("Error", error);
}
}
catch (WebException wex)
{
ExceptionUtility.LogException(wex, "CheckoutController.cs Complete Action");
var error = new CheckoutErrorModel
{
ErrorCode = wex.Message
};
if (wex.Response != null && wex.Response.GetType() == typeof(HttpWebResponse))
{
// Extract the response body from the WebException's HttpWebResponse:
error.LongMessage = ((HttpWebResponse)wex.Response).StatusDescription;
}
// Redirect to the checkout error view:
return RedirectToAction("Error", error);
}
catch (Exception ex)
{
ExceptionUtility.LogException(ex, "CheckoutController.cs Review Action");
var error = new CheckoutErrorModel
{
ErrorCode = ex.Message
};
// Redirect to the checkout error view:
return RedirectToAction("Error", error);
}
}
return View(data);
}