public async Task <string> CreateOrderAsync(PaymentDataRequestModel requestModel)
        {
            Customer customer = await _customerService.CreateAsync(new CustomerCreateOptions
            {
                Email  = requestModel.StripeEmail,
                Source = requestModel.StripeToken
            });

            decimal totalPrice = default(decimal);

            List <int>            printedEditionIds = requestModel.CartItemModels.Select(item => item.PrintedEditionId).ToList();
            List <PrintedEdition> printedEditions   = await _printedEditionRepository.GetRangeByIdAsync(printedEditionIds);

            if (!printedEditions.Any())
            {
                return("There is no any matching with printed edition Ids that you provided");
            }

            foreach (CartItemModel cartItem in requestModel.CartItemModels)
            {
                PrintedEdition printedEdition = printedEditions.FirstOrDefault(item => item.Id == cartItem.PrintedEditionId);
                if (!(printedEdition is null))
                {
                    totalPrice += printedEdition.Price * cartItem.Quantity;
                }
            }

            Charge charge = await _chargeService.CreateAsync(new ChargeCreateOptions
            {
                Amount      = ConvertToStripeAmount(totalPrice),
                Description = string.Format("Charge for {0} user", requestModel.StripeEmail),
                Currency    = StripeCurrencyUsd,
                CustomerId  = customer.Id
            });

            if (charge.StripeResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return("Stripe responded with status: " + charge.StripeResponse.StatusCode);
            }

            string[] namesList   = printedEditions.Select(item => item.Name).ToArray();
            string   description = "PrintedEditions :" + string.Join(", ", namesList);

            string          userName = _httpContextAccessor.HttpContext.User.Identity.Name;
            ApplicationUser user     = await _userManager.FindByNameAsync(userName);

            Order order = new Order()
            {
                ApplicationUserId = user.Id,
                Description       = description,
                OrderAmount       = totalPrice,
            };

            await _orderRepository.CreateAsync(order);

            return(string.Empty);
        }
Exemple #2
0
        public async Task <IActionResult> SendPaymentData([FromBody] PaymentDataRequestModel requestModel)
        {
            if (ModelState.IsValid)
            {
                string result = await _orderService.CreateOrderAsync(requestModel);

                if (!string.IsNullOrWhiteSpace(result))
                {
                    ModelState.AddModelError(string.Empty, result);

                    return(BadRequest(ModelState));
                }

                return(Ok());
            }

            return(BadRequest(ModelState));
        }