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