示例#1
0
        public void ProceedPayment(PowerbankSessionModel session)
        {
            var secondsBetweenStartAndFinish = (session.Finish - session.Start).TotalSeconds;

            if (secondsBetweenStartAndFinish < 60)
            {
                return;
            }

            var client = new Client(shopId: Strings.YandexShopId, secretKey: Strings.YandexAPIKey);

            ICalculationStrategy strategy;

            switch (session.RentModel.RentStrategy)
            {
                case RentStrategy.Day:
                    strategy = new DayCalculationStrategy(99, false);
                    break;
                case RentStrategy.Hour:
                    strategy = new OneHourCalculationStrategy(49, 99, false);
                    break;
                case RentStrategy.FirstHourFree:
                    strategy = new OneHourCalculationStrategy(49, 99, true);
                    break;
                default:
                    return;
            }

            var amount = strategy.Calculate(session.Start, session.Finish);

            if (amount <= 0) return;

            client.CreatePayment(new NewPayment
            {
                Amount = new Amount { Currency = "RUB", Value = new decimal(amount) },
                PaymentMethodId = session.CardId,
                Capture = true,
                Description = $"{session.Costumer.Phone}, {session.Start} - {session.Finish}",
                Confirmation = new Confirmation
                {
                    Type = ConfirmationType.Redirect,
                    ReturnUrl = ""
                },
            });
        }
        public async Task <bool> ReleasePowerBank(string powerBankCode, string holderCode, int position)
        {
            var powerBank = await _appRepository.Powerbanks.Include(x => x.Sessions).ThenInclude(x => x.RentModel).Include(x => x.Holder).FirstOrDefaultAsync(x => x.Code == powerBankCode);

            if (powerBank == null)
            {
                return(false);
            }

            var session = powerBank.Sessions.FirstOrDefault(x => x.IsActive);

            if (session == null)
            {
                return(false);
            }

            session.Finish = DateTime.Now;
            _appRepository.Entry(session).Property(x => x.Finish).IsModified = true;
            var oldHolder = powerBank.Holder;
            var newHolder = await _appRepository.Holders.FirstOrDefaultAsync(x => x.Code == holderCode);

            var oldHolderPowerBanks = oldHolder.Powerbanks.ToList();

            oldHolderPowerBanks.Remove(powerBank);
            oldHolder.Powerbanks = oldHolderPowerBanks;
            var newHolderPowerBanks = newHolder.Powerbanks.ToList();

            newHolderPowerBanks.Add(powerBank);
            newHolder.Powerbanks = newHolderPowerBanks;
            _appRepository.Entry(oldHolder).Collection(x => x.Powerbanks).IsModified = true;
            _appRepository.Entry(newHolder).Collection(x => x.Powerbanks).IsModified = true;

            var card = await _appRepository.CardBidnings.FirstOrDefaultAsync(x => x.BindingId == session.CardId);

            card.IsLocked = false;
            _appRepository.Entry(card).Property(x => x.IsLocked).IsModified = true;

            ICalculationStrategy strategy;

            switch (session.RentModel.RentStrategy)
            {
            case RentStrategy.Day:
                strategy = new DayCalculationStrategy(99, false);
                break;

            case RentStrategy.Hour:
                strategy = new OneHourCalculationStrategy(49, 99, false);
                break;

            case RentStrategy.FirstHourFree:
                strategy = new OneHourCalculationStrategy(49, 99, true);
                break;

            default:
                strategy = null;
                break;
            }

            if (strategy != null)
            {
                var amount = strategy.Calculate(session.Start, session.Finish);
                session.Price = amount;
                _appRepository.Entry(session).Property(x => x.Price).IsModified = true;
            }

            await _appRepository.SaveChangesAsync();

            return(true);
        }