Beispiel #1
0
 public void Configure(string name, TariffService options)
 {
     Configure(options);
     options.QuotaService      = QuotaService.Get(name);
     options.TenantService     = TenantService.Get(name);
     options.LazyCoreDbContext = new Lazy <CoreDbContext>(() => CoreDbContextManager.Get(name));
 }
Beispiel #2
0
        public void SetTariff(int tenantId, Tariff tariff)
        {
            if (tariff == null)
            {
                throw new ArgumentNullException("tariff");
            }

            var q = QuotaService.GetTenantQuota(tariff.QuotaId);

            if (q == null)
            {
                return;
            }
            SaveBillingInfo(tenantId, Tuple.Create(tariff.QuotaId, tariff.DueDate));
            if (q.Trial)
            {
                // reset trial date
                var tenant = TenantService.GetTenant(tenantId);
                if (tenant != null)
                {
                    tenant.VersionChanged = DateTime.UtcNow;
                    TenantService.SaveTenant(CoreSettings, tenant);
                }
            }

            ClearCache(tenantId);
        }
Beispiel #3
0
        public Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true)
        {
            //single tariff for all portals
            if (CoreBaseSettings.Standalone)
            {
                tenantId = -1;
            }

            var key    = GetTariffCacheKey(tenantId);
            var tariff = Cache.Get <Tariff>(key);

            if (tariff == null)
            {
                tariff = Tariff.CreateDefault();

                var cached = GetBillingInfo(tenantId);
                if (cached != null)
                {
                    tariff.QuotaId = cached.Item1;
                    tariff.DueDate = cached.Item2;
                }

                tariff = CalculateTariff(tenantId, tariff);
                Cache.Insert(key, tariff, DateTime.UtcNow.Add(GetCacheExpiration()));

                if (billingConfigured && withRequestToPaymentSystem)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            using var client = GetBillingClient();
                            var p            = client.GetLastPayment(GetPortalId(tenantId));
                            var quota        = QuotaService.GetTenantQuotas().SingleOrDefault(q => q.AvangateId == p.ProductId);
                            if (quota == null)
                            {
                                throw new InvalidOperationException(string.Format("Quota with id {0} not found for portal {1}.", p.ProductId, GetPortalId(tenantId)));
                            }
                            var asynctariff         = Tariff.CreateDefault();
                            asynctariff.QuotaId     = quota.Id;
                            asynctariff.Autorenewal = p.Autorenewal;
                            asynctariff.DueDate     = 9999 <= p.EndDate.Year ? DateTime.MaxValue : p.EndDate;

                            if (SaveBillingInfo(tenantId, Tuple.Create(asynctariff.QuotaId, asynctariff.DueDate), false))
                            {
                                asynctariff = CalculateTariff(tenantId, asynctariff);
                                ClearCache(tenantId);
                                Cache.Insert(key, asynctariff, DateTime.UtcNow.Add(GetCacheExpiration()));
                            }
                        }
                        catch (Exception error)
                        {
                            LogError(error);
                        }
                    });
                }
            }

            return(tariff);
        }
Beispiel #4
0
 public void Configure(string name, TariffService options)
 {
     Configure(options);
     options.QuotaService  = QuotaService.Get(name);
     options.TenantService = TenantService.Get(name);
     options.CoreDbContext = CoreDbContextManager.Get(name);
 }
Beispiel #5
0
        public Uri GetShoppingUri(int?tenant, int quotaId, string affiliateId, string currency = null, string language = null, string customerId = null)
        {
            var quota = QuotaService.GetTenantQuota(quotaId);

            if (quota == null)
            {
                return(null);
            }

            var key = tenant.HasValue
                          ? GetBillingUrlCacheKey(tenant.Value)
                          : string.Format("notenant{0}", !string.IsNullOrEmpty(affiliateId) ? "_" + affiliateId : "");

            key += quota.Visible ? "" : "0";
            if (!(Cache.Get <Dictionary <string, Tuple <Uri, Uri> > >(key) is IDictionary <string, Tuple <Uri, Uri> > urls))
            {
                urls = new Dictionary <string, Tuple <Uri, Uri> >();
                if (billingConfigured)
                {
                    try
                    {
                        var products = QuotaService.GetTenantQuotas()
                                       .Where(q => !string.IsNullOrEmpty(q.AvangateId) && q.Visible == quota.Visible)
                                       .Select(q => q.AvangateId)
                                       .ToArray();

                        using var client = GetBillingClient();
                        urls             = tenant.HasValue ?
                                           client.GetPaymentUrls(GetPortalId(tenant.Value), products, GetAffiliateId(tenant.Value), GetCampaign(tenant.Value), "__Currency__", "__Language__", "__CustomerID__") :
                                           client.GetPaymentUrls(null, products, !string.IsNullOrEmpty(affiliateId) ? affiliateId : null, null, "__Currency__", "__Language__", "__CustomerID__");
                    }
                    catch (Exception error)
                    {
                        Log.Error(error);
                    }
                }
                Cache.Insert(key, urls, DateTime.UtcNow.Add(TimeSpan.FromMinutes(10)));
            }

            ResetCacheExpiration();

            if (!string.IsNullOrEmpty(quota.AvangateId) && urls.TryGetValue(quota.AvangateId, out var tuple))
            {
                var result = tuple.Item2;

                var tariff = tenant.HasValue ? GetTariff(tenant.Value) : null;
                if (result == null || tariff == null || tariff.QuotaId == quotaId || tariff.State >= TariffState.Delay)
                {
                    result = tuple.Item1;
                }

                result = new Uri(result.ToString()
                                 .Replace("__Currency__", currency ?? "")
                                 .Replace("__Language__", (language ?? "").ToLower())
                                 .Replace("__CustomerID__", customerId ?? ""));
                return(result);
            }
            return(null);
        }
Beispiel #6
0
        public void Configure(string name, TenantManager options)
        {
            Configure(options);

            options.TenantService = TenantService.Get(name);
            options.QuotaService  = QuotaService.Get(name);
            options.TariffService = TariffService.Get(name);
        }
Beispiel #7
0
        public void SetTariff(int tenant, bool paid)
        {
            var quota = QuotaService.GetTenantQuotas().FirstOrDefault(q => paid ? q.NonProfit : q.Trial);

            if (quota != null)
            {
                TariffService.SetTariff(tenant, new Tariff {
                    QuotaId = quota.Id, DueDate = DateTime.MaxValue,
                });
            }
        }
Beispiel #8
0
 public void Configure(string name, HostedSolution hostedSolution)
 {
     Configure(hostedSolution);
     hostedSolution.Region              = name;
     hostedSolution.TenantService       = TenantService.Get(name);
     hostedSolution.UserService         = UserService.Get(name);
     hostedSolution.QuotaService        = QuotaService.Get(name);
     hostedSolution.TariffService       = TariffService.Get(name);
     hostedSolution.ClientTenantManager = TenantManager.Get(name);
     hostedSolution.TenantUtil          = TenantUtil.Get(name);
     hostedSolution.SettingsManager     = DbSettingsManager.Get(name);
     hostedSolution.CoreSettings        = CoreSettings.Get(name);
 }
Beispiel #9
0
        public IEnumerable <PaymentInfo> GetPayments(int tenantId, DateTime from, DateTime to)
        {
            from = from.Date;
            to   = to.Date.AddTicks(TimeSpan.TicksPerDay - 1);
            var key      = GetBillingPaymentCacheKey(tenantId, from, to);
            var payments = Cache.Get <List <PaymentInfo> >(key);

            if (payments == null)
            {
                payments = new List <PaymentInfo>();
                if (billingConfigured)
                {
                    try
                    {
                        var quotas = QuotaService.GetTenantQuotas();
                        using var client = GetBillingClient();
                        foreach (var pi in client.GetPayments(GetPortalId(tenantId), from, to))
                        {
                            var quota = quotas.SingleOrDefault(q => q.AvangateId == pi.ProductId);
                            if (quota != null)
                            {
                                pi.QuotaId = quota.Id;
                            }
                            payments.Add(pi);
                        }
                    }
                    catch (Exception error)
                    {
                        LogError(error);
                    }
                }

                Cache.Insert(key, payments, DateTime.UtcNow.Add(TimeSpan.FromMinutes(10)));
            }

            return(payments);
        }
Beispiel #10
0
 public QuotasController(QuotaService quotaService)
 {
     _quotaService = quotaService;
 }
Beispiel #11
0
        private Tariff CalculateTariff(int tenantId, Tariff tariff)
        {
            tariff.State = TariffState.Paid;
            var q = QuotaService.GetTenantQuota(tariff.QuotaId);

            if (q == null || q.GetFeature("old"))
            {
                tariff.QuotaId = Tenant.DEFAULT_TENANT;
                q = QuotaService.GetTenantQuota(tariff.QuotaId);
            }

            var delay = 0;

            if (q != null && q.Trial)
            {
                tariff.State = TariffState.Trial;
                if (tariff.DueDate == DateTime.MinValue || tariff.DueDate == DateTime.MaxValue)
                {
                    var tenant = TenantService.GetTenant(tenantId);
                    if (tenant != null)
                    {
                        var fromDate    = tenant.CreatedDateTime < tenant.VersionChanged ? tenant.VersionChanged : tenant.CreatedDateTime;
                        var trialPeriod = GetPeriod("TrialPeriod", DEFAULT_TRIAL_PERIOD);
                        if (fromDate == DateTime.MinValue)
                        {
                            fromDate = DateTime.UtcNow.Date;
                        }
                        tariff.DueDate = trialPeriod != default ? fromDate.Date.AddDays(trialPeriod) : DateTime.MaxValue;
                    }
                    else
                    {
                        tariff.DueDate = DateTime.MaxValue;
                    }
                }
            }
            else
            {
                delay = PaymentDelay;
            }

            if (tariff.DueDate != DateTime.MinValue && tariff.DueDate.Date < DateTime.Today && delay > 0)
            {
                tariff.State = TariffState.Delay;

                tariff.DelayDueDate = tariff.DueDate.Date.AddDays(delay);
            }

            if (tariff.DueDate == DateTime.MinValue ||
                tariff.DueDate != DateTime.MaxValue && tariff.DueDate.Date.AddDays(delay) < DateTime.Today)
            {
                tariff.State = TariffState.NotPaid;

                if (CoreBaseSettings.Standalone)
                {
                    if (q != null)
                    {
                        var defaultQuota = QuotaService.GetTenantQuota(Tenant.DEFAULT_TENANT);
                        defaultQuota.Name = "overdue";

                        defaultQuota.Features = q.Features;
                        defaultQuota.Support  = false;

                        QuotaService.SaveTenantQuota(defaultQuota);
                    }

                    var unlimTariff = Tariff.CreateDefault();
                    unlimTariff.LicenseDate = tariff.DueDate;

                    tariff = unlimTariff;
                }
            }

            tariff.Prolongable = tariff.DueDate == DateTime.MinValue || tariff.DueDate == DateTime.MaxValue ||
                                 tariff.State == TariffState.Trial ||
                                 new DateTime(tariff.DueDate.Year, tariff.DueDate.Month, 1) <= new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1).AddMonths(1);

            return(tariff);
        }