Esempio n. 1
0
        public async Task <ActionResult> Complete(OrderModel order)
        {
            try
            {
                // TODO: Complete the payment processing via the gateway and update the order...
                NVPAPICaller gatewayCaller = new NVPAPICaller();

                string   token = "";
                string   finalPaymentAmount = "";
                NVPCodec decoder            = new NVPCodec();

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

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

                    using (var _db = new ProductContext())
                    {
                        // Get the current order id.
                        int currentOrderId = -1;
                        if (Session["currentOrderId"] != null && Session["currentOrderId"].ToString() != string.Empty)
                        {
                            currentOrderId = Convert.ToInt32(Session["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.
                            _db.SaveChanges();

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

                            if (myCurrentOrder.SMSOptIn)
                            {
                                await AzureQueueHelper.QueueSmsMessageRequest(myCurrentOrder);
                            }

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

                        // Clear shopping cart.
                        using (ShoppingCartActions usersShoppingCart =
                                   new ShoppingCartActions(cartId))
                        {
                            usersShoppingCart.EmptyCart();
                        }

                        // Clear order id.
                        Session["currentOrderId"] = string.Empty;
                    }
                }
                else
                {
                    var error = gatewayCaller.PopulateGatewayErrorModel(decoder);

                    // Report failed event to Application Insights.
                    Exception ex = new Exception(error.ToString())
                    {
                        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));
        }