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