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 Task<Event> ParseEvent(HttpRequest request, string apiKey);
|
||||||
public abstract CheckoutSession ProcessCheckoutCompletedEvent(Event e);
|
public abstract CheckoutSession ProcessCheckoutCompletedEvent(Event e);
|
||||||
public abstract Subscription ProcessSubscriptionEvent(Event e);
|
public abstract Subscription ProcessSubscriptionEvent(Event e);
|
||||||
|
public abstract Customer ProcessCustomerEvent(Event e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,6 +310,14 @@ namespace Teknik.BillingCore
|
|||||||
return ConvertSubscription(subscription);
|
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)
|
public override PortalSession CreatePortalSession(string customerId, string returnUrl)
|
||||||
{
|
{
|
||||||
var portalService = new Stripe.BillingPortal.SessionService();
|
var portalService = new Stripe.BillingPortal.SessionService();
|
||||||
@ -469,7 +477,8 @@ namespace Teknik.BillingCore
|
|||||||
CustomerId = customer.Id
|
CustomerId = customer.Id
|
||||||
};
|
};
|
||||||
|
|
||||||
if (customer.Subscriptions.Any())
|
if (customer.Subscriptions != null &&
|
||||||
|
customer.Subscriptions.Any())
|
||||||
returnCust.Subscriptions = customer.Subscriptions.Select(s => ConvertSubscription(s)).ToList();
|
returnCust.Subscriptions = customer.Subscriptions.Select(s => ConvertSubscription(s)).ToList();
|
||||||
|
|
||||||
return returnCust;
|
return returnCust;
|
||||||
|
@ -45,7 +45,7 @@ namespace Teknik.Areas.API.V1.Controllers
|
|||||||
{
|
{
|
||||||
var billingService = BillingFactory.GetBillingService(_config.BillingConfig);
|
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)
|
if (billingEvent == null)
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
@ -58,5 +58,23 @@ namespace Teknik.Areas.API.V1.Controllers
|
|||||||
|
|
||||||
return Ok();
|
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,6 +11,36 @@ namespace Teknik.Areas.Billing
|
|||||||
{
|
{
|
||||||
public static class BillingHelper
|
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)
|
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
|
// They have paid, so let's get their subscription and update their user settings
|
||||||
@ -30,21 +60,35 @@ namespace Teknik.Areas.Billing
|
|||||||
{
|
{
|
||||||
// What type of subscription is this
|
// What type of subscription is this
|
||||||
if (config.BillingConfig.UploadProductId == price.ProductId)
|
if (config.BillingConfig.UploadProductId == price.ProductId)
|
||||||
|
{
|
||||||
|
SetUploadLimits(db,
|
||||||
|
user,
|
||||||
|
active ? price.Storage : config.UploadConfig.MaxStorage,
|
||||||
|
active ? price.FileSize : config.UploadConfig.MaxUploadFileSize);
|
||||||
|
}
|
||||||
|
else if (config.BillingConfig.EmailProductId == price.ProductId)
|
||||||
|
{
|
||||||
|
SetEmailLimits(config, user, price.Storage, active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetUploadLimits(TeknikEntities db, User user, long storage, long fileSize)
|
||||||
{
|
{
|
||||||
// Process Upload Settings
|
// Process Upload Settings
|
||||||
user.UploadSettings.MaxUploadStorage = active ? price.Storage : config.UploadConfig.MaxStorage;
|
user.UploadSettings.MaxUploadStorage = storage;
|
||||||
user.UploadSettings.MaxUploadFileSize = active ? price.FileSize : config.UploadConfig.MaxUploadFileSize;
|
user.UploadSettings.MaxUploadFileSize = fileSize;
|
||||||
db.Entry(user).State = EntityState.Modified;
|
db.Entry(user).State = EntityState.Modified;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
else if (config.BillingConfig.EmailProductId == price.ProductId)
|
|
||||||
|
public static void SetEmailLimits(Config config, User user, long storage, bool active)
|
||||||
{
|
{
|
||||||
// Process an email subscription
|
// Process an email subscription
|
||||||
string email = UserHelper.GetUserEmailAddress(config, user.Username);
|
string email = UserHelper.GetUserEmailAddress(config, user.Username);
|
||||||
if (active)
|
if (active)
|
||||||
{
|
{
|
||||||
UserHelper.EnableUserEmail(config, email);
|
UserHelper.EnableUserEmail(config, email);
|
||||||
UserHelper.EditUserEmailMaxSize(config, email, price.Storage);
|
UserHelper.EditUserEmailMaxSize(config, email, storage);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -53,4 +97,3 @@ namespace Teknik.Areas.Billing
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user