Exemplo n.º 1
0
 public HttpResponseMessage BuyFromManufacturer([FromBody] AddManufacturerSellDto manufacturerSellDto)
 {
     return(Request.ExecuteProtectedAndWrapResult <AddManufacturerSellDto, PaymentInfo>(
                dto => TradeService.CreateManufacturerTradePromise(dto, $"http://{Request.RequestUri.Authority}/api/Trade/ConfirmOrderFromManufacturer"),
                ModelState, manufacturerSellDto
                ));
 }
Exemplo n.º 2
0
        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);
        }