/// <summary> /// Populates a new CheckoutErrorModel object from the passed in return data from the gateway. /// </summary> /// <param name="decoder"></param> /// <returns></returns> public CheckoutErrorModel PopulateGatewayErrorModel(NVPCodec decoder) { var errorVM = new CheckoutErrorModel { ErrorCode = decoder[NVPProperties.Properties.ERRORCODE], LongMessage = decoder[NVPProperties.Properties.LONGMESSAGE], ShortMessage = decoder[NVPProperties.Properties.SHORTMESSAGE] }; return(errorVM); }
public async Task <ActionResult> Review(CheckoutModel data) { if (ModelState.IsValid) { try { NVPAPICaller gatewayCaller = new NVPAPICaller(); string token = ""; NVPCodec decoder = new NVPCodec(); // Call the gateway payment authorization API: bool ret = true;// 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. await SqlDbHelper.SaveOrder(myOrder); // Get the shopping cart items and process them. using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions(cartId, items, categories)) { 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.OrderDetailId = i; myOrderDetail.OrderId = myOrder.OrderId; myOrderDetail.ProductId = myOrderList[i].ItemId; myOrderDetail.Quantity = myOrderList[i].Quantity; myOrderDetail.UnitPrice = myOrderList[i].Product.UnitPrice; myOrderDetail.Email = myOrder.Email; // Add OrderDetail to DB. await SqlDbHelper.SaveOrderDetails(myOrderDetail); } // Set OrderId. Session["currentOrderId"] = myOrder.OrderId; Session["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.Movies.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)); }
public ActionResult Error(CheckoutErrorModel error) { return(View(error)); }
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 = true;// gatewayCaller.DoCheckoutPayment(finalPaymentAmount, token, ref decoder); if (ret) { //// Retrieve PayPal confirmation value. string PaymentConfirmation = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10);// decoder[NVPProperties.Properties.TRANSACTIONID].ToString(); order.PaymentTransactionId = PaymentConfirmation; // 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) { myCurrentOrder = await SqlDbHelper.GetOrder(currentOrderId); myCurrentOrder.PaymentTransactionId = PaymentConfirmation; await SqlDbHelper.SaveOrder(myCurrentOrder); // Queue up a receipt generation request, asynchronously. await new AzureQueueHelper().QueueReceiptRequest(myCurrentOrder); // Report successful event to Application Insights. var eventProperties = new Dictionary <string, string>(); eventProperties.Add("CustomerEmail", order.Email); eventProperties.Add("OrderTotal", finalPaymentAmount); eventProperties.Add("PaymentTransactionId", PaymentConfirmation); TelemetryHelper.TrackEvent("OrderCompleted", eventProperties); } // Clear shopping cart. using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions(cartId, items, categories)) { await 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()); ex.Source = "Contoso.Apps.Movies.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)); }
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); }