Exemplo n.º 1
0
        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
                {
                    //what are the api keys stripe private key
                    ApiKey = _config["StripePrivateKey"]
                };
                var chargeOptions = new ChargeCreateOptions()

                {
                    //required
                    Amount   = (int)(order.OrderTotal * 100),
                    Currency = "usd",
                    //here we cretae a stripe payment
                    Source = order.StripeToken,
                    //optional
                    Description = string.Format("Order Payment {0}", order.UserName),
                    //receipt should go to the username
                    ReceiptEmail = order.UserName,
                };

                //using this charge options
                var chargeService = new ChargeService();



                Charge stripeCharge = null;
                try
                {
                    stripeCharge = chargeService.Create(chargeOptions, options);
                    _logger.LogDebug("Stripe charge object creation" + stripeCharge.StripeResponse.ToString());
                }
                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));
            }
        }
        public async Task <IActionResult> Create(Order frmorder)
        {
            if (ModelState.IsValid)
            {
                var   user  = _identityService.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()
                {
                    //required
                    Amount   = (int)(order.OrderTotal * 100),
                    Currency = "usd",
                    Source   = order.StripeToken,
                    //optional
                    Description  = string.Format("Order payment {0}", order.UserName),
                    ReceiptEmail = user.UserName,
                };

                var    chargeService = new ChargeService();
                Charge stripeCharge  = null;
                try
                {
                    stripeCharge = chargeService.Create(chargeOptions, options);
                    _logger.LogDebug("Stripe charge object creation" + stripeCharge.StripeResponse.ToString());
                }
                catch (StripeException stripeException)
                {
                    _logger.LogDebug("Stripe Exception" + stripeException.Message);
                    ModelState.AddModelError(string.Empty, stripeException.Message);
                    return(View(frmorder));
                }
                try
                {
                    if (stripeCharge.Id != null)
                    {
                        order.PaymentAuthCode = stripeCharge.Id;
                        int orderId = await _orderService.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 new order, please try later on.(Business Msg due to Circuit Break)");
                    return(View(frmorder));
                }
            }
            else
            {
                return(View(frmorder));
            }
        }