public async Task Complete()

in Hands-on lab/lab-files/src/Contoso Sports League/Contoso.Apps.SportsLeague.Web/Controllers/CheckoutController.cs [197:299]


        public async Task<ActionResult> Complete(OrderModel order)
        {
            try
            {
                // TODO: Complete the payment processing via the gateway and update the order...
                var gatewayCaller = new NVPAPICaller(_config, HttpContext);
                var finalPaymentAmount = string.Empty;
                var decoder = new NVPCodec();

                var token = HttpContext.Session.GetString("token");
                //PayerID = Session["payerId"].ToString();
                //finalPaymentAmount = Session["payment_amt"].ToString();
                finalPaymentAmount = order.Total.ToString("C2");

                var ret = gatewayCaller.DoCheckoutPayment(finalPaymentAmount, token, ref decoder);
                if (ret)
                {
                    // Retrieve PayPal confirmation value.
                    string PaymentConfirmation = decoder[NVPProperties.Properties.TRANSACTIONID].ToString();
                    order.PaymentTransactionId = PaymentConfirmation;

                    // Get the current order id.
                    int currentOrderId = -1;
                    if (HttpContext.Session.GetInt32("currentOrderId") != null && HttpContext.Session.GetInt32("currentOrderId")?.ToString() != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(HttpContext.Session.GetInt32("currentOrderId"));
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        // Get the order based on order id.
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        // Update the order to reflect payment has been completed.
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        // Save to DB.
                        await _db.SaveChangesAsync();

                        // Queue up a receipt generation request, asynchronously.
                        await new AzureQueueHelper(_config).QueueReceiptRequest(myCurrentOrder);

                        // Report successful event to Application Insights.
                        var eventProperties = new Dictionary<string, string>();
                        eventProperties.Add("CustomerEmail", myCurrentOrder.Email);
                        eventProperties.Add("OrderTotal", finalPaymentAmount);
                        eventProperties.Add("PaymentTransactionId", PaymentConfirmation);
                        TelemetryHelper.TrackEvent("OrderCompleted", eventProperties);
                    }

                    // Clear shopping cart.
                    var usersShoppingCart = new ShoppingCartActions(_db, CartId);
                    
                    await usersShoppingCart.EmptyCart();
                    

                    // Clear order id.
                    HttpContext.Session.Remove("currentOrderId");
                }
                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 Complete Action");

                var error = new CheckoutErrorModel
                {
                    ErrorCode = ex.Message
                };

                // Redirect to the checkout error view:
                return RedirectToAction("Error", error);
            }

            return View(order);
        }