Exemple #1
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);
        }
Exemple #2
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);
            }

            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(int) ? fromDate.Date.AddDays(trialPeriod) : DateTime.MaxValue;
                    }
                    else
                    {
                        tariff.DueDate = DateTime.MaxValue;
                    }
                }
            }
            if (tariff.DueDate.Date < DateTime.UtcNow.Date)
            {
                tariff.State = TariffState.NotPaid;

                if (config.Standalone)
                {
                    tariff = Tariff.CreateDefault();
                }
            }

            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);
        }
Exemple #3
0
        public Tariff GetTariff(int tenantId)
        {
            var key    = "tariff/" + tenantId;
            var tariff = cache.Get(key) as Tariff;

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

                try
                {
                    using (var client = new BillingClient())
                    {
                        var xelement = client.GetLastPayment(tenantId);

                        var productid = xelement.Element("product-id").Value;
                        var quota     = quotaService.GetTenantQuotas().SingleOrDefault(q => q.AvangateId == productid);
                        tariff.QuotaId = quota.Id;

                        var enddate = xelement.Element("end-date");
                        tariff.DueDate = DateTime.ParseExact(enddate.Value, "yyyy-MM-dd HH:mm:ss", null);

                        SaveBillingInfo(tenantId, Tuple.Create(tariff.QuotaId, tariff.DueDate));
                    }
                }
                catch (Exception error)
                {
                    log.Error(error);

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

                CalculateState(tenantId, tariff);

                cache.Insert(key, tariff, DateTime.UtcNow.Add(CacheExpiration));
            }

            return(tariff);
        }
Exemple #4
0
        private Tariff GetBillingInfo(int tenant)
        {
            var q = new SqlQuery("tenants_tariff")
                    .Select("tariff", "stamp", "quantity")
                    .Where("tenant", tenant)
                    .OrderBy("id", false)
                    .SetMaxResults(1);

            return(ExecList(q)
                   .ConvertAll(r =>
            {
                var tariff = Tariff.CreateDefault();
                tariff.QuotaId = Convert.ToInt32(r[0]);
                tariff.DueDate = ((DateTime)r[1]).Year < 9999 ? (DateTime)r[1] : DateTime.MaxValue;
                tariff.Quantity = Convert.ToInt32(r[2]);
                return tariff;
            })
                   .SingleOrDefault()
                   ?? Tariff.CreateDefault());
        }
Exemple #5
0
        public Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true)
        {
            var key    = "tariff/" + tenantId;
            var tariff = cache.Get(key) as Tariff;

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

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

                if (withRequestToPaymentSystem)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            using (var client = GetBillingClient())
                            {
                                try
                                {
                                    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)))
                                    {
                                        asynctariff = CalculateTariff(tenantId, asynctariff);
                                        ClearCache(tenantId);
                                        cache.Insert(key, asynctariff, DateTime.UtcNow.Add(GetCacheExpiration()));
                                    }
                                }
                                finally
                                {
                                    if (config.Standalone)
                                    {
                                        var po = client.GetPaymentOffice(GetPortalId(tenantId));
                                        if (!string.IsNullOrEmpty(po.Key2) && !Equals(config.SKey, po.Key2))
                                        {
                                            config.SKey = po.Key2;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception error)
                        {
                            LogError(error);
                        }
                    });
                }

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

            return(tariff);
        }
Exemple #6
0
        public Tariff GetTariff(int tenantId, bool withRequestToPaymentSystem = true)
        {
            //single tariff for all portals
            if (CoreContext.Configuration.Standalone)
            {
                tenantId = -1;
            }

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

            if (tariff == null)
            {
                tariff = GetBillingInfo(tenantId);

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

                if (BillingClient.Configured && withRequestToPaymentSystem)
                {
                    Task.Run(() =>
                    {
                        try
                        {
                            var client      = GetBillingClient();
                            var lastPayment = client.GetLastPayment(GetPortalId(tenantId));

                            var quota = quotaService.GetTenantQuotas().SingleOrDefault(q => q.AvangateId == lastPayment.ProductId);
                            if (quota == null)
                            {
                                throw new InvalidOperationException(string.Format("Quota with id {0} not found for portal {1}.", lastPayment.ProductId, GetPortalId(tenantId)));
                            }

                            var asynctariff         = Tariff.CreateDefault();
                            asynctariff.QuotaId     = quota.Id;
                            asynctariff.Autorenewal = lastPayment.Autorenewal;
                            asynctariff.DueDate     = 9999 <= lastPayment.EndDate.Year ? DateTime.MaxValue : lastPayment.EndDate;

                            if (quota.ActiveUsers == -1 &&
                                lastPayment.Quantity < ACTIVE_USERS_MIN)
                            {
                                throw new BillingException(string.Format("The portal {0} is paid for {1} users", tenantId, lastPayment.Quantity));
                            }
                            asynctariff.Quantity = lastPayment.Quantity;

                            if (SaveBillingInfo(tenantId, asynctariff, false))
                            {
                                asynctariff = CalculateTariff(tenantId, asynctariff);
                                ClearCache(tenantId);
                                cache.Insert(key, asynctariff, DateTime.UtcNow.Add(GetCacheExpiration()));
                            }
                        }
                        catch (BillingNotFoundException)
                        {
                            var q = quotaService.GetTenantQuota(tariff.QuotaId);

                            if (q != null &&
                                !q.Trial &&
                                !q.Free &&
                                !q.NonProfit &&
                                !q.Open &&
                                !q.Custom)
                            {
                                var asynctariff         = Tariff.CreateDefault();
                                asynctariff.DueDate     = DateTime.Today.AddDays(-1);
                                asynctariff.Prolongable = false;
                                asynctariff.Autorenewal = false;
                                asynctariff.State       = TariffState.NotPaid;

                                if (SaveBillingInfo(tenantId, asynctariff))
                                {
                                    asynctariff = CalculateTariff(tenantId, asynctariff);
                                    ClearCache(tenantId);
                                    cache.Insert(key, asynctariff, DateTime.UtcNow.Add(GetCacheExpiration()));
                                }
                            }
                        }
                        catch (Exception error)
                        {
                            LogError(error, tenantId.ToString());
                        }
                    });
                }
            }

            return(tariff);
        }
Exemple #7
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(int) ? 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 (config.Standalone)
                {
                    if (q != null)
                    {
                        var defaultQuota = quotaService.GetTenantQuota(Tenant.DEFAULT_TENANT);
                        defaultQuota.Name = "overdue";

                        defaultQuota.Features = q.Features;

                        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);
        }