private void AttachAccountExtension(SellModel sell)
        {
            sell.BuyerAccountExtension.LastUsedDate = sell.SellDate;
            AccountExtensionModel accountExtension = sell.BuyerAccountExtension.Id == 0 ?
                                                     AccountExtensionRepo.Create(sell.BuyerAccountExtension) :
                                                     AccountExtensionRepo.Update(sell.BuyerAccountExtension.Id, sell.BuyerAccountExtension);

            Mapper.Map <AccountExtensionModel, AccountExtensionModel>(accountExtension, sell.BuyerAccountExtension);
        }
        private SellModel BuildOrderFromManufacturer(string orderId, AccountExtensionModel accountExtension, IEnumerable <SellPositionDto> sellPositions)
        {
            SellModel sell = new SellModel(accountExtension);

            IEnumerable <ConcretePartModel> source = ConcretePartRepo.GetStored();
            IEnumerable <ConcretePartModel> order  = GetOrderFromSource(sellPositions, source, "part ");

            foreach (ConcretePartModel concretePart in order)
            {
                float             price        = concretePart.Part.Price.Value * concretePart.SelectedMaterial.PriceCoefficient.Value;
                SellPositionModel sellPosition = new SellPositionModel(price, concretePart);
                sell.SellPositions.Add(sellPosition);
            }

            return(sell);
        }
        public PaymentInfo CreateManufacturerTradePromise(AddManufacturerSellDto manufacturerSellDto, string callbackEndpoint)
        {
            if (manufacturerSellDto.NewAccountExtension == null && manufacturerSellDto.ExisitingAccountExtensionId == null)
            {
                throw new NotFoundException("nor old neither new account extension");
            }

            SessionService.CheckSession(manufacturerSellDto.Session);

            if (manufacturerSellDto.NewAccountExtension != null && manufacturerSellDto.NewAccountExtension.AccountId != manufacturerSellDto.Session.UserId)
            {
                throw new ForbiddenException("account owner");
            }

            AccountExtensionModel accountExtension = manufacturerSellDto.ExisitingAccountExtensionId == null ||
                                                     !manufacturerSellDto.ExisitingAccountExtensionId.HasValue ?
                                                     Mapper.Map <AccountExtensionDto, AccountExtensionModel>(manufacturerSellDto.NewAccountExtension) :
                                                     AccountExtensionRepo.Get(manufacturerSellDto.ExisitingAccountExtensionId.Value);

            if (accountExtension == null)
            {
                throw new NotFoundException("account extension");
            }

            if (accountExtension.AccountId != manufacturerSellDto.Session.UserId)
            {
                throw new ForbiddenException("account owner");
            }

            string    orderId   = Guid.NewGuid().ToString();
            SellModel sellModel = BuildOrderFromManufacturer(orderId, accountExtension, manufacturerSellDto.Positions);

            Sell sell = new Sell(orderId, sellModel);

            PendingOrders.Add(orderId, sell);

            PaymentPrepareModel paymentPrepare = new PaymentPrepareModel(orderId, sellModel.TotalPrice);

            paymentPrepare.CallbackUrl = callbackEndpoint;
            paymentPrepare.Expired     = sell.Expires;

            PaymentInfo payment = PaymentService.CreateFromUserPayment(paymentPrepare);

            sell.PaymentInfo = payment;

            return(payment);
        }