Managing User Resources on Subscription Change
Let's say for the different tiers of subscriptions I have, you are allotted different number of resources. Basic tier gets 1 widget, Premium tier gets 2, Pro tier gets unlimited. When a user downgrades their subscription from a higher tier to a lower tier, I also need to adjust the amount of widgets they have and remove entries above their allotment. Where is the best place to put this logic? If using Stripe, is it within the StripeWebhook.php > fulfill_checkout()
method? I searched the project for syncRoles
and I just want to be sure this makes sense.
Hi there,
Yes, sounds like the best place to handle this is within StripeWebhook.php > fulfill_checkout()
, where you process subscription changes. After updating the user's role based on the new subscription tier, you can adjust their widget count accordingly.
Just be cautious when removing excess widgets—notify users or provide a grace period if needed or use soft deletes for example.
Not 100% sure I understand the question here. The switch plan method that you've linked to is just updating the user roles in the database itself.
It turns out that fulfill_checkout()
only runs when the customer is performing a new purchase. It does not run when a user is changing plans, so re-assigning allowed resources in that block never runs if they're already a subscriber, instead, in the if($event->type == 'customer.subscription.updated'){
block of StripeWebhook.php
is where a subscription change is executed. In my case, I want user resources to be reassigned any time that a user's role changes. Whether their subscription changes or whether I change their role from the admin dashboard. Therefore, within the switch plan method of the User model would be a good place to reassign resources if you need resources to be synced with their role.
Furthermore, when the user cancels their subscription, their role does not change, just their subscription status. That may be suitable for other use cases, but in my use case, I want the user's role to change back to registered and lose their privileges. In this case, I have added the following code to the block if(is_null($stripeSubscription->cancel_at)){
just a few lines below this area.
if(is_null($stripeSubscription->cancel_at)){
$subscription->ends_at = NULL;
// I added these lines below
$subscription->user->syncRoles([]);
$subscription->user->assignRole('registered');
Oh yes, that sounds like a bug in the webhook controller though rather than the user model itself:
wave/src/Http/Controllers/Billing/Webhooks/StripeWebhook.php#L44-L67
Feel free to submit a PR if you have the time!
Otherwise, I will look into patching this in the next few days.
Ok sounds good, let me know how it goes.