Ejemplo n.º 1
0
        public SessionModel CreateCheckoutSession(OrderModel order)
        {
            _validator.ValidateOrder(order);
            bool          isExisting = order.Id is not null;
            PaymentEntity payment    = isExisting ? new PaymentEntity {
                Id = order.PaymentId
            } : null;

            if (!isExisting)
            {
                order.Status = Enums.OrderStatusType.Unpaid;
                order.Date   = DateTime.UtcNow;
                payment      = new PaymentEntity
                {
                    TransactionId = string.Empty
                };
                _paymentRepository.Insert(payment);
            }
            var dbOrder = _mapper.Map <OrderEntity>(order);

            if (!isExisting)
            {
                dbOrder.PaymentId = payment.Id;
                dbOrder.Total     = order.CurrentItems.Sum(item => item.Price);
                _orderRepository.Insert(dbOrder);
            }
            var items         = new List <SessionLineItemOptions>();
            var editionIds    = order.CurrentItems.Select(item => item.PrintingEditionId).ToList();
            var editionFilter = new PrintingEditionFilterModel {
                EditionIds = editionIds
            };
            var printingEditions = _printingEditionService.GetPrintingEditionsRange(editionFilter);
            var dbItems          = _mapper.Map <List <OrderItemEntity> >(order.CurrentItems);

            dbItems.ForEach(item => item.OrderId = dbOrder.Id);
            foreach (var item in dbItems)
            {
                var printingEdition = printingEditions.Where(edition => edition.Id == item.PrintingEditionId).FirstOrDefault();
                var lineItem        = new SessionLineItemOptions
                {
                    PriceData = new SessionLineItemPriceDataOptions
                    {
                        UnitAmountDecimal = printingEdition.Price * Constants.CENTMULTIPLIER,
                        Currency          = item.Currency.ToString(),
                        ProductData       = new SessionLineItemPriceDataProductDataOptions
                        {
                            Name        = printingEdition.Title,
                            Description = printingEdition.Description
                        }
                    },
                    Quantity = item.Amount
                };
                items.Add(lineItem);
            }
            if (!isExisting)
            {
                _itemRepository.InsertRange(dbItems);
            }
            string successUrl = new UriBuilder
            {
                Scheme = _urlConfig.Scheme,
                Port   = _urlConfig.Port,
                Host   = _urlConfig.Host,
                Path   = Constants.STRIPESUCCESSPATH
            }.ToString();
            string cancelUrl = new UriBuilder
            {
                Scheme = _urlConfig.Scheme,
                Port   = _urlConfig.Port,
                Host   = _urlConfig.Host,
                Path   = Constants.STRIPECANCELPATH
            }.ToString();
            var options = new SessionCreateOptions
            {
                PaymentMethodTypes = new List <string>
                {
                    Constants.DEFAULTPAYMENTMETHOD
                },
                LineItems  = items,
                Mode       = Constants.DEFAULTPAYMENTMODE,
                SuccessUrl = successUrl,
                CancelUrl  = cancelUrl
            };
            var     service = new SessionService();
            Session session = service.Create(options);

            payment.TransactionId = session.PaymentIntentId;
            _paymentRepository.Update(payment);
            return(new SessionModel
            {
                Id = session.Id,
                PaymentIntentId = session.PaymentIntentId
            });
        }