Browse Source

Added unlinking of User billing customer where orphaned

tags/5.1.0
Teknikode 2 weeks ago
parent
commit
999cff2bd2
3 changed files with 43 additions and 17 deletions
  1. 2
    1
      BillingCore/BillingService.cs
  2. 17
    2
      BillingCore/StripeService.cs
  3. 24
    14
      BillingService/Program.cs

+ 2
- 1
BillingCore/BillingService.cs View File

@@ -18,7 +18,8 @@ namespace Teknik.BillingCore
Config = billingConfig;
}

public abstract object GetCustomer(string id);
public abstract List<Customer> GetCustomers();
public abstract Customer GetCustomer(string id);
public abstract string CreateCustomer(string username, string email);

public abstract List<Product> GetProductList();

+ 17
- 2
BillingCore/StripeService.cs View File

@@ -19,14 +19,29 @@ namespace Teknik.BillingCore
StripeConfiguration.ApiKey = config.StripeSecretApiKey;
}

public override string GetCustomer(string email)
public override List<Models.Customer> GetCustomers()
{
var customers = new List<Models.Customer>();
var service = new CustomerService();
var foundCustomers = service.List();
if (foundCustomers != null)
{
foreach (var customer in foundCustomers)
{
customers.Add(ConvertCustomer(customer));
}
}
return customers;
}

public override Models.Customer GetCustomer(string email)
{
if (!string.IsNullOrEmpty(email))
{
var service = new CustomerService();
var foundCustomer = service.Get(email);
if (foundCustomer != null)
return foundCustomer.Id;
return ConvertCustomer(foundCustomer);
}

return null;

+ 24
- 14
BillingService/Program.cs View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection;
using CommandLine;
using Microsoft.EntityFrameworkCore;
using Teknik.Areas.Billing;
using Teknik.Areas.Users.Models;
using Teknik.Areas.Users.Utility;
using Teknik.BillingCore;
@@ -76,8 +77,21 @@ namespace Teknik.BillingService
// Get Biling Service
var billingService = BillingFactory.GetBillingService(config.BillingConfig);

// Get all customers
var customers = billingService.GetCustomers();
if (customers != null)
{
// Find customers that aren't linked anymore
var unlinkedCustomers = db.Users.Select(u => u.BillingCustomer).Where(b => !customers.Exists(c => c.CustomerId == b.CustomerId));
foreach (var customer in unlinkedCustomers)
{
BillingHelper.RemoveCustomer(db, customer.CustomerId);
}
}

foreach (var user in db.Users)
{
// Only set/reset their limits if they have a subscription or have subscribed at some point
string email = UserHelper.GetUserEmailAddress(config, user.Username);
if (user.BillingCustomer != null)
{
@@ -86,26 +100,22 @@ namespace Teknik.BillingService
var uploadPrice = subscriptions.SelectMany(s => s.Prices).FirstOrDefault(p => p.ProductId == config.BillingConfig.UploadProductId);
if (uploadPrice != null)
{
// Process Upload Settings
user.UploadSettings.MaxUploadStorage = uploadPrice.Storage;
user.UploadSettings.MaxUploadFileSize = uploadPrice.FileSize;
BillingHelper.SetUploadLimits(db, user, uploadPrice.Storage, uploadPrice.FileSize);
}
else
{
BillingHelper.SetUploadLimits(db, user, config.UploadConfig.MaxStorage, config.UploadConfig.MaxUploadFileSize);
}
var emailPrice = subscriptions.SelectMany(s => s.Prices).FirstOrDefault(p => p.ProductId == config.BillingConfig.EmailProductId);
if (emailPrice != null)
{
UserHelper.EnableUserEmail(config, email);
UserHelper.EditUserEmailMaxSize(config, email, emailPrice.Storage);
BillingHelper.SetEmailLimits(config, user, emailPrice.Storage, true);
}
else
{
BillingHelper.SetEmailLimits(config, user, config.EmailConfig.MaxSize, false);
}
}
else
{
// No customer, so let's reset their info
user.UploadSettings.MaxUploadStorage = config.UploadConfig.MaxStorage;
user.UploadSettings.MaxUploadFileSize = config.UploadConfig.MaxUploadFileSize;
UserHelper.DisableUserEmail(config, email);
}
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
}


Loading…
Cancel
Save