Beispiel #1
0
        private void ValidateCreatePaymentLevelModel(EditPaymentLevel model, Brand.Interface.Data.Brand brand)
        {
            if (brand == null)
            {
                throw new RegoException("app:common.invalidBrand");
            }

            var currency = brand.BrandCurrencies.SingleOrDefault(c => c.CurrencyCode == model.Currency);

            if (currency == null)
            {
                throw new RegoException("app:payment.invalidCurrency");
            }

            var paymentLevels = _paymentQueries.GetPaymentLevels(brand.Id);

            if (paymentLevels.Any(pl => pl.Name == model.Name && pl.BrandId == brand.Id && pl.Id != model.Id))
            {
                throw new RegoException("app:payment.levelNameUnique");
            }

            if (paymentLevels.Any(pl => pl.Code == model.Code && pl.BrandId == brand.Id && pl.Id != model.Id))
            {
                throw new RegoException("app:payment.levelCodeUnique");
            }

            if (model.IsDefault &&
                paymentLevels.Any(
                    pl => pl.Id != model.Id && pl.BrandId == model.Brand && pl.CurrencyCode == model.Currency && pl.IsDefault))
            {
                throw new RegoException("Default payment level for the brand and currency combination already exists.");
            }
        }
Beispiel #2
0
        public PaymentLevelSaveResult Save(EditPaymentLevel model)
        {
            Brand.Interface.Data.Brand brand = null;

            if (model.Brand.HasValue)
            {
                brand = _brandRepository.Brands.Include(b => b.BrandCurrencies).SingleOrDefault(b => b.Id == model.Brand);
            }

            ValidateCreatePaymentLevelModel(model, brand);

            var currency     = brand.BrandCurrencies.Single(c => c.CurrencyCode == model.Currency);
            var currencyCode = currency.CurrencyCode;

            var bankAccounts = GetBankAccounts(model);

            FillBankReceipts(model, GetBankAccounts(model));

            var paymentGatewaySettings = GetPaymentGatewaySettings(model);

            var now = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId);

            var paymentLevel = new PaymentLevel
            {
                Id                   = model.Id ?? Guid.NewGuid(),
                BrandId              = brand.Id,
                CurrencyCode         = currencyCode,
                Status               = PaymentLevelStatus.Active,
                CreatedBy            = _actorInfoProvider.Actor.UserName,
                DateCreated          = now,
                ActivatedBy          = _actorInfoProvider.Actor.UserName,
                DateActivated        = now,
                Code                 = model.Code,
                Name                 = model.Name,
                EnableOfflineDeposit = model.EnableOfflineDeposit,
                EnableOnlineDeposit  = model.EnableOnlineDeposit,
                BankFeeRatio         = model.BankFeeRatio,
                MaxBankFee           = model.MaxBankFee
            };

            _repository.PaymentLevels.Add(paymentLevel);

            if (model.IsDefault)
            {
                _brandCommands.MakePaymentLevelDefault(paymentLevel.Id, paymentLevel.BrandId, currencyCode);
            }

            paymentLevel.BankAccounts = new List <BankAccount>();

            if (bankAccounts != null)
            {
                foreach (var bankAccount in bankAccounts.Where(x => x.Status == BankAccountStatus.Active))
                {
                    paymentLevel.BankAccounts.Add(bankAccount);
                }
            }

            paymentLevel.PaymentGatewaySettings = new List <PaymentGatewaySettings>();
            if (paymentGatewaySettings != null)
            {
                foreach (var settings in paymentGatewaySettings)
                {
                    paymentLevel.PaymentGatewaySettings.Add(settings);
                }
            }

            _repository.SaveChanges();

            _eventBus.Publish(new PaymentLevelAdded(
                                  paymentLevel.Id,
                                  paymentLevel.Code,
                                  paymentLevel.Name,
                                  paymentLevel.BrandId,
                                  paymentLevel.CurrencyCode,
                                  paymentLevel.Status,
                                  paymentLevel.CreatedBy,
                                  paymentLevel.DateCreated)
            {
                EventCreated = DateTimeOffset.Now.ToBrandOffset(brand.TimezoneId),
            });

            return(new PaymentLevelSaveResult
            {
                Message = "app:payment.levelCreated",
                PaymentLevelId = paymentLevel.Id
            });
        }