in src/Relecloud.Web.CallCenter/Controllers/CartController.cs [162:220]
public async Task<IActionResult> CheckoutConfirmed(CheckoutViewModel model)
{
try
{
if (model == null || model.PaymentDetails is null)
{
ModelState.AddModelError(string.Empty, "Invalid form state data");
return View(nameof(Checkout), model);
}
if (ModelState.IsValid)
{
var cartData = await GetCartAsync();
var serializableDictionary = MapToSerializableDictionary(cartData.Concerts);
var purchaseResult = await this.ticketPurchaseService.PurchaseTicketAsync(new PurchaseTicketsRequest
{
ConcertIdsAndTicketCounts = serializableDictionary,
PaymentDetails = model.PaymentDetails,
UserId = User.GetUniqueId(),
});
if (purchaseResult.Status == PurchaseTicketsResultStatus.Success)
{
// Remove all items from the cart.
SetCartData(new Dictionary<int, int>());
// Most custom telemetry should go through OpenTelemetry APIs,
// but Azure Monitor's OpenTelemetry SDK does not support custom events yet.
// https://learn.microsoft.com/azure/azure-monitor/app/opentelemetry-add-modify#send-custom-telemetry-using-the-application-insights-classic-api
// https://learn.microsoft.com/azure/azure-monitor/app/opentelemetry-add-modify#whats-the-current-release-state-of-features-within-the-azure-monitor-opentelemetry-distro
this.telemetryClient.TrackEvent("CheckoutCart");
// An alternative which wouldn't require AppInsights SDK usage is to log traces.
// Note that these will appear as traces in Application Insights rather than true
// separately queryable custom events. But they may work for some scenarios.
this.logger.LogInformation("Cart checked out");
return RedirectToAction(nameof(Index), "Ticket");
}
if (purchaseResult.ErrorMessages is null)
{
ModelState.AddModelError(string.Empty, "We're sorry but the purchasing service is unavailable at this time.");
}
else
{
ModelState.AddErrorMessages(purchaseResult.ErrorMessages);
}
}
}
catch (Exception ex)
{
this.logger.LogError(ex, $"Unable to perform checkout for ${User.Identity!.Name}");
ModelState.AddModelError(string.Empty, "We're sorry for the iconvenience but there was an error while trying to process your order. Please try again later.");
}
model.Cart = await GetCartAsync();
return View(nameof(Checkout), model);
}