mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
Added handling of customer deletion
This commit is contained in:
parent
5b4eb2c578
commit
ed9a94cba1
@ -41,5 +41,6 @@ namespace Teknik.BillingCore
|
||||
public abstract Task<Event> ParseEvent(HttpRequest request, string apiKey);
|
||||
public abstract CheckoutSession ProcessCheckoutCompletedEvent(Event e);
|
||||
public abstract Subscription ProcessSubscriptionEvent(Event e);
|
||||
public abstract Customer ProcessCustomerEvent(Event e);
|
||||
}
|
||||
}
|
||||
|
@ -310,6 +310,14 @@ namespace Teknik.BillingCore
|
||||
return ConvertSubscription(subscription);
|
||||
}
|
||||
|
||||
public override Models.Customer ProcessCustomerEvent(Models.Event ev)
|
||||
{
|
||||
// Handle the checkout.session.completed event
|
||||
var customer = ev.Data as Stripe.Customer;
|
||||
|
||||
return ConvertCustomer(customer);
|
||||
}
|
||||
|
||||
public override PortalSession CreatePortalSession(string customerId, string returnUrl)
|
||||
{
|
||||
var portalService = new Stripe.BillingPortal.SessionService();
|
||||
@ -469,7 +477,8 @@ namespace Teknik.BillingCore
|
||||
CustomerId = customer.Id
|
||||
};
|
||||
|
||||
if (customer.Subscriptions.Any())
|
||||
if (customer.Subscriptions != null &&
|
||||
customer.Subscriptions.Any())
|
||||
returnCust.Subscriptions = customer.Subscriptions.Select(s => ConvertSubscription(s)).ToList();
|
||||
|
||||
return returnCust;
|
||||
|
@ -45,7 +45,7 @@ namespace Teknik.Areas.API.V1.Controllers
|
||||
{
|
||||
var billingService = BillingFactory.GetBillingService(_config.BillingConfig);
|
||||
|
||||
var billingEvent = await billingService.ParseEvent(Request, _config.BillingConfig.StripeCustomerWebhookSecret);
|
||||
var billingEvent = await billingService.ParseEvent(Request, _config.BillingConfig.StripeSubscriptionWebhookSecret);
|
||||
|
||||
if (billingEvent == null)
|
||||
return BadRequest();
|
||||
@ -58,5 +58,23 @@ namespace Teknik.Areas.API.V1.Controllers
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> HandleCustomerDeletion()
|
||||
{
|
||||
var billingService = BillingFactory.GetBillingService(_config.BillingConfig);
|
||||
|
||||
var billingEvent = await billingService.ParseEvent(Request, _config.BillingConfig.StripeCustomerWebhookSecret);
|
||||
|
||||
if (billingEvent == null)
|
||||
return BadRequest();
|
||||
|
||||
var customerEvent = billingService.ProcessCustomerEvent(billingEvent);
|
||||
if (customerEvent == null)
|
||||
return BadRequest();
|
||||
|
||||
BillingHelper.RemoveCustomer(_dbContext, customerEvent.CustomerId);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,41 @@ namespace Teknik.Areas.Billing
|
||||
{
|
||||
public static class BillingHelper
|
||||
{
|
||||
public static Models.Customer CreateCustomer(TeknikEntities db, User user, string customerId)
|
||||
{
|
||||
var customer = new Models.Customer()
|
||||
{
|
||||
CustomerId = customerId,
|
||||
User = user
|
||||
};
|
||||
db.Customers.Add(customer);
|
||||
user.BillingCustomer = customer;
|
||||
db.Entry(user).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
|
||||
return customer;
|
||||
}
|
||||
|
||||
public static void RemoveCustomer(TeknikEntities db, string customerId)
|
||||
{
|
||||
// They have paid, so let's get their subscription and update their user settings
|
||||
var user = db.Users.FirstOrDefault(u => u.BillingCustomer != null &&
|
||||
u.BillingCustomer.CustomerId == customerId);
|
||||
if (user != null &&
|
||||
user.BillingCustomer != null)
|
||||
{
|
||||
db.Customers.Remove(user.BillingCustomer);
|
||||
user.BillingCustomer = null;
|
||||
db.Entry(user).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ProcessSubscription(Config config, TeknikEntities db, string customerId, Subscription subscription)
|
||||
{
|
||||
// They have paid, so let's get their subscription and update their user settings
|
||||
var user = db.Users.FirstOrDefault(u => u.BillingCustomer != null &&
|
||||
u.BillingCustomer.CustomerId == customerId);
|
||||
u.BillingCustomer.CustomerId == customerId);
|
||||
if (user != null)
|
||||
{
|
||||
var isActive = subscription.Status == SubscriptionStatus.Active;
|
||||
@ -31,25 +61,38 @@ namespace Teknik.Areas.Billing
|
||||
// What type of subscription is this
|
||||
if (config.BillingConfig.UploadProductId == price.ProductId)
|
||||
{
|
||||
// Process Upload Settings
|
||||
user.UploadSettings.MaxUploadStorage = active ? price.Storage : config.UploadConfig.MaxStorage;
|
||||
user.UploadSettings.MaxUploadFileSize = active ? price.FileSize : config.UploadConfig.MaxUploadFileSize;
|
||||
db.Entry(user).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
SetUploadLimits(db,
|
||||
user,
|
||||
active ? price.Storage : config.UploadConfig.MaxStorage,
|
||||
active ? price.FileSize : config.UploadConfig.MaxUploadFileSize);
|
||||
}
|
||||
else if (config.BillingConfig.EmailProductId == price.ProductId)
|
||||
{
|
||||
// Process an email subscription
|
||||
string email = UserHelper.GetUserEmailAddress(config, user.Username);
|
||||
if (active)
|
||||
{
|
||||
UserHelper.EnableUserEmail(config, email);
|
||||
UserHelper.EditUserEmailMaxSize(config, email, price.Storage);
|
||||
}
|
||||
else
|
||||
{
|
||||
UserHelper.DisableUserEmail(config, email);
|
||||
}
|
||||
SetEmailLimits(config, user, price.Storage, active);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetUploadLimits(TeknikEntities db, User user, long storage, long fileSize)
|
||||
{
|
||||
// Process Upload Settings
|
||||
user.UploadSettings.MaxUploadStorage = storage;
|
||||
user.UploadSettings.MaxUploadFileSize = fileSize;
|
||||
db.Entry(user).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public static void SetEmailLimits(Config config, User user, long storage, bool active)
|
||||
{
|
||||
// Process an email subscription
|
||||
string email = UserHelper.GetUserEmailAddress(config, user.Username);
|
||||
if (active)
|
||||
{
|
||||
UserHelper.EnableUserEmail(config, email);
|
||||
UserHelper.EditUserEmailMaxSize(config, email, storage);
|
||||
}
|
||||
else
|
||||
{
|
||||
UserHelper.DisableUserEmail(config, email);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user