public async Task <IActionResult> Create(Order frmOrder) { if (ModelState.IsValid) { var user = _identitySvc.Get(HttpContext.User); Order order = frmOrder; order.UserName = user.Email; order.BuyerId = user.Email; var options = new RequestOptions { ApiKey = _config["StripePrivateKey"] }; var chargeOptions = new ChargeCreateOptions() { Amount = (int)(order.OrderTotal * 100), Currency = "usd", Source = order.StripeToken, Description = string.Format("Order Payment {0}", order.UserName), ReceiptEmail = order.UserName, }; var chargeService = new ChargeService(); Charge stripeCharge = null; try { stripeCharge = chargeService.Create(chargeOptions, options); } catch (StripeException stripeException) { ModelState.AddModelError(string.Empty, stripeException.Message); return(View(frmOrder)); } try { if (stripeCharge.Id != null) { order.PaymentAuthCode = stripeCharge.Id; int orderId = await _orderSvc.CreateOrder(order); return(RedirectToAction("Complete", new { id = orderId, userName = user.UserName })); } else { ViewData["message"] = "Payment cannot be processed, try again"; return(View(frmOrder)); } } catch (BrokenCircuitException) { ModelState.AddModelError("Error", "It was not possible to create a new order, please try later on. (Business Msg Due to Circuit-Breaker)"); return(View(frmOrder)); } } else { return(View(frmOrder)); } }
public async Task <IActionResult> Create(WebMVC.Models.OrderModels.Order frmOrder) { if (ModelState.IsValid) { var user = _identitySvc.Get(HttpContext.User); WebMVC.Models.OrderModels.Order order = frmOrder; order.UserName = user.Email; order.BuyerId = user.Id; var chargeOptions = new StripeChargeCreateOptions() { //required Amount = (int)(order.OrderTotal * 100), Currency = "usd", SourceTokenOrExistingSourceId = order.StripeToken, //optional Description = string.Format("Order Payment {0}", order.UserName), ReceiptEmail = order.UserName, }; var chargeService = new StripeChargeService(); chargeService.ApiKey = _config["StripePrivateKey"]; StripeCharge stripeCharge = null; try { stripeCharge = chargeService.Create(chargeOptions); _logger.LogDebug("Stripe charge object creation" + stripeCharge.StripeResponse.ObjectJson); } catch (StripeException stripeException) { _logger.LogDebug("Stripe exception " + stripeException.Message); ModelState.AddModelError(string.Empty, stripeException.Message); return(View(frmOrder)); } try { if (stripeCharge.Id != null) { //_logger.LogDebug("TransferID :" + stripeCharge.Id); order.PaymentAuthCode = stripeCharge.Id; //_logger.LogDebug("User {userName} started order processing", user.UserName); int orderId = await _orderSvc.CreateOrder(order); //_logger.LogDebug("User {userName} finished order processing of {orderId}.", order.UserName, order.OrderId); //await _cartSvc.ClearCart(user); return(RedirectToAction("Complete", new { id = orderId, userName = user.UserName })); } else { ViewData["message"] = "Payment cannot be processed, try again"; return(View(frmOrder)); } } catch (BrokenCircuitException) { ModelState.AddModelError("Error", "It was not possible to create a new order, please try later on. (Business Msg Due to Circuit-Breaker)"); return(View(frmOrder)); } } else { return(View(frmOrder)); } }