public async Task <ActionResult <ProcessOrderPaymentResult> > ProcessOrderPayment(string orderNumber, string paymentNumber, [FromBody][SwaggerOptional] BankCardInfo bankCardInfo) { //Need lock to prevent concurrent access to same order using (await AsyncLock.GetLockByKey(GetAsyncLockKey(orderNumber, WorkContext)).LockAsync()) { var orderDto = await GetOrderDtoByNumber(orderNumber); var paymentDto = orderDto.InPayments.FirstOrDefault(x => x.Number.EqualsInvariant(paymentNumber)); if (paymentDto == null) { throw new StorefrontException("payment " + paymentNumber + " not found"); } var processingResult = await _orderApi.ProcessOrderPaymentsAsync(orderDto.Id, paymentDto.Id, bankCardInfo.ToBankCardInfoDto()); var order = orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage); return(new ProcessOrderPaymentResult { OrderProcessingResult = processingResult.ToProcessPaymentResult(order), PaymentMethod = paymentDto.PaymentMethod.ToPaymentMethod(order) }); } }
private async Task <OrderCreatedInfo> CreateOrderFromCartAsync(ICartBuilder cartBuilder, BankCardInfo bankCardInfo, bool removeCart) { var orderDto = await _orderApi.CreateOrderFromCartAsync(cartBuilder.Cart.Id); var order = orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage); var taskList = new List <Task> { //Raise domain event asynchronously _publisher.Publish(new OrderPlacedEvent(WorkContext, orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage), cartBuilder.Cart)), //Remove the cart asynchronously removeCart ? cartBuilder.RemoveCartAsync() : Task.CompletedTask }; //Process order asynchronously var incomingPaymentDto = orderDto.InPayments?.FirstOrDefault(); Task <orderModel.ProcessPaymentRequestResult> processPaymentTask = null; if (incomingPaymentDto != null) { processPaymentTask = _orderApi.ProcessOrderPaymentsAsync(orderDto.Id, incomingPaymentDto.Id, bankCardInfo?.ToBankCardInfoDto()); taskList.Add(processPaymentTask); } await Task.WhenAll(taskList.ToArray()); return(new OrderCreatedInfo { Order = order, OrderProcessingResult = processPaymentTask != null ? (await processPaymentTask).ToProcessPaymentResult(order) : null, PaymentMethod = incomingPaymentDto?.PaymentMethod.ToPaymentMethod(order), }); }
public async Task <ActionResult <ProcessOrderPaymentResult> > ProcessOrderPayment(string orderNumber, string paymentNumber, [FromBody][SwaggerOptional] BankCardInfo bankCardInfo) { //Need lock to prevent concurrent access to same order using (await AsyncLock.GetLockByKey(GetAsyncLockKey(orderNumber, WorkContext)).LockAsync()) { var orderDto = await _orderApi.GetByNumberAsync(orderNumber, string.Empty); var authorizationResult = await _authorizationService.AuthorizeAsync(User, orderDto, CanAccessOrderAuthorizationRequirement.PolicyName); if (!authorizationResult.Succeeded) { return(Unauthorized()); } var paymentDto = orderDto.InPayments.FirstOrDefault(x => x.Number.EqualsInvariant(paymentNumber)); if (paymentDto == null) { throw new StorefrontException("payment " + paymentNumber + " not found"); } var processingResult = await _orderApi.ProcessOrderPaymentsAsync(orderDto.Id, paymentDto.Id, bankCardInfo.ToBankCardInfoDto()); if ((bool)processingResult.IsSuccess) { orderDto.Status = OrderStatuses.PaidOrderStatus; await _orderApi.UpdateOrderAsync(orderDto); } var order = orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage); return(new ProcessOrderPaymentResult { OrderProcessingResult = processingResult.ToProcessPaymentResult(order), PaymentMethod = paymentDto.PaymentMethod.ToPaymentMethod(order) }); } }
public async Task <ActionResult <OrderCreatedInfo> > CreateOrder([FromBody] BankCardInfo bankCardInfo) { EnsureCartExists(); //Need lock to prevent concurrent access to same cart using (await AsyncLock.GetLockByKey(WorkContext.CurrentCart.Value.GetCacheKey()).LockAsync()) { var cartBuilder = await LoadOrCreateCartAsync(); var orderDto = await _orderApi.CreateOrderFromCartAsync(cartBuilder.Cart.Id); var order = orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage); var taskList = new List <Task> { //Raise domain event asynchronously _publisher.Publish(new OrderPlacedEvent(WorkContext, orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage), cartBuilder.Cart)), //Remove the cart asynchronously cartBuilder.RemoveCartAsync() }; //Process order asynchronously var incomingPaymentDto = orderDto.InPayments?.FirstOrDefault(); Task <orderModel.ProcessPaymentResult> processPaymentTask = null; if (incomingPaymentDto != null) { processPaymentTask = _orderApi.ProcessOrderPaymentsAsync(orderDto.Id, incomingPaymentDto.Id, bankCardInfo.ToBankCardInfoDto()); taskList.Add(processPaymentTask); } await Task.WhenAll(taskList.ToArray()); return(new OrderCreatedInfo { Order = order, OrderProcessingResult = processPaymentTask != null ? (await processPaymentTask).ToProcessPaymentResult(order) : null, PaymentMethod = incomingPaymentDto?.PaymentMethod.ToPaymentMethod(order), }); } }