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 = 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);
        }
Exemplo n.º 2
0
        public IEnumerable <TenantQuota> GetTenantQuotas()
        {
            var quotas = cache.Get <IEnumerable <TenantQuota> >(KEY_QUOTA);

            if (quotas == null)
            {
                cache.Insert(KEY_QUOTA, quotas = service.GetTenantQuotas(), DateTime.UtcNow.Add(CacheExpiration));
            }
            return(quotas);
        }
Exemplo n.º 3
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,
                });
            }
        }
Exemplo n.º 4
0
        private Hashtable GetTenantQuotasInernal()
        {
            var key   = "quota";
            var store = cache.Get(key) as Hashtable;

            if (store == null)
            {
                store = Hashtable.Synchronized(new Hashtable());
                foreach (var q in service.GetTenantQuotas())
                {
                    store[q.Id] = q;
                }
                cache.Insert(key, store, DateTime.UtcNow.Add(CacheExpiration));
            }
            return(store);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
 public IEnumerable <TenantQuota> GetTenantQuotas(bool all)
 {
     return(quotaService.GetTenantQuotas().Where(q => q.Id < 0 && (all || q.Visible)).OrderByDescending(q => q.Id).ToList());
 }
Exemplo n.º 8
0
 public IEnumerable <TenantQuota> GetTenantQuotas()
 {
     return(quotaService.GetTenantQuotas().Where(q => q.Id < 0).OrderByDescending(q => q.Id).ToList());
 }