public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _bskrepo.GetBasketAsync(basketId); var orderItems = new List <OrderItem>(); foreach (var item in basket.Items) { var product = await _uow.Repository <Product>().GetByIdAsync(item.Id); var productItemOrdered = new ProductItemOrdered(product.Id, product.Name, product.PhotoUrl); orderItems.Add(new OrderItem(productItemOrdered, product.Price, item.Quantity)); } var order = new Order(orderItems, buyerEmail, orderItems.Sum(x => x.Price * x.Quantity), shippingAddress, await _uow.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId)); _uow.Repository <Order>().Add(order); var result = await _uow.Complete(); if (result <= 0) { return(null); } await _bskrepo.DeleteBasketAsync(basketId); return(order); }
public async Task <CustomerBasket> CreateOrUpdatePaymentIntent(string basketId) { StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"]; var basket = await _basketRepo.GetBasketAsync(basketId); if (basket is null) { return(null); } var shippingPrice = 0m; if (basket.DeliveryMethodId != null) { shippingPrice = (await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync((int)basket.DeliveryMethodId)).Price; } foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Core.Entities.Product>().GetByIdAsync(item.Id); if (item.Price != productItem.Price) { item.Price = productItem.Price; } } var service = new PaymentIntentService(); PaymentIntent intent; if (string.IsNullOrEmpty(basket.PaymentIntentId)) { var options = new PaymentIntentCreateOptions { Amount = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100, Currency = "usd", PaymentMethodTypes = new List <string> { "card" }, }; intent = await service.CreateAsync(options); basket.PaymentIntentId = intent.Id; basket.ClientSecret = intent.ClientSecret; } else { var options = new PaymentIntentUpdateOptions { Amount = (long)basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long)shippingPrice * 100, }; await service.UpdateAsync(basket.PaymentIntentId, options); } await _basketRepo.UpdateBasketAsync(basket); return(basket); }
public async Task <CustomerBasket> CreateOrUpdatePayementIntent(string basketId) { StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"]; var basket = await _bsktRepo.GetBasketAsync(basketId); if (basket == null) { return(null); } var shippingCharge = 0m; if (basket.DeliveryMethodId.HasValue) { var deliveryMethod = await _uow.Repository <DeliveryMethod>().GetByIdAsync(basket.DeliveryMethodId.Value); if (deliveryMethod != null) { shippingCharge = deliveryMethod.Price; } } foreach (var item in basket.Items) { var product = await _uow.Repository <Core.Entities.Product>().GetByIdAsync(item.Id); item.Price = product.Price; } var paymentIntentService = new PaymentIntentService(); if (string.IsNullOrEmpty(basket.PaymentIntentId)) { var options = new PaymentIntentCreateOptions { Amount = (long)((basket.Items.Sum(x => x.Price * x.Quantity) + shippingCharge)) * 100, Currency = "usd", PaymentMethodTypes = new List <string> { "card" } }; var payment = await paymentIntentService.CreateAsync(options); basket.ClientSecret = payment.ClientSecret; basket.PaymentIntentId = payment.Id; } else { var options = new PaymentIntentUpdateOptions { Amount = (long)((basket.Items.Sum(x => x.Price * x.Quantity) + shippingCharge)) * 100 }; await paymentIntentService.UpdateAsync(basket.PaymentIntentId, options); } await _bsktRepo.CreateOrUpdateBasketAsync(basket); return(basket); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethod, string basketId, Address shippingAddress) { // get basket var basket = await _basketRepo.GetBasketAsync(basketId); // get products var items = new List <OrderItem>(); foreach (var item in basket.Items) { var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id); var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.PictureUrl); var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity); items.Add(orderItem); } // get delivery method var delivery = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethod); // calc subtotal decimal subtotal = items.Sum(item => item.Quantity * item.Price); // check if order with the same paymentIntentId exist var spec = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId); var orderToCheck = await _unitOfWork.Repository <Order>().GetBySpecification(spec); if (!(orderToCheck is null)) { _unitOfWork.Repository <Order>().Delete(orderToCheck); // update to make sure order is accurate await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId); } // create order var order = new Order(items, buyerEmail, shippingAddress, delivery, subtotal, basket.PaymentIntentId); _unitOfWork.Repository <Order>().Add(order); // save to db var result = await _unitOfWork.Complete(); if (result <= 0) { return(null); } // delete basket // await _basketRepo.DeleteBasketAsync(basketId); return(order); }
public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId, Address shippingAddress) { var basket = await _bskrepo.GetBasketAsync(basketId); var orderItems = new List <OrderItem> (); foreach (var item in basket.Items) { var product = await _uow.Repository <Product> ().GetByIdAsync(item.Id); var productItemOrdered = new ProductItemOrdered(product.Id, product.Name, product.PhotoUrl); orderItems.Add(new OrderItem(productItemOrdered, product.Price, item.Quantity)); } var spec = new OrderWithPaymentIntentIdSpec(basket.PaymentIntentId); var existingOrder = await _uow.Repository <Order> ().GetEntityBySpecAsync(spec); if (existingOrder != null) { _uow.Repository <Order> ().Delete(existingOrder); await _service.CreateOrUpdatePayementIntent(basket.PaymentIntentId); } var order = new Order(orderItems, buyerEmail, orderItems.Sum(x => x.Price * x.Quantity), shippingAddress, await _uow.Repository <DeliveryMethod> ().GetByIdAsync(deliveryMethodId), basket.PaymentIntentId); _uow.Repository <Order> ().Add(order); var result = await _uow.Complete(); if (result <= 0) { return(null); } //await _bskrepo.DeleteBasketAsync (basketId); return(order); }
public async Task <ActionResult <CustomerBasket> > GetBasketAsync(string id) { var basket = await _repo.GetBasketAsync(id); return(Ok(basket ?? new CustomerBasket(id))); }
public async Task <IActionResult> GetBasket([FromQuery] string id) { var basket = await _basketRepo.GetBasketAsync(id); return(Ok(basket ?? new CustomerBasket(id))); }
/// <inheritdoc/> public async Task <Basket> GetAsync(Guid userId) { await InitializeBasketAsync(userId); return(await _basketRepository.GetBasketAsync(userId)); }