Managing User Resources on Subscription Change

produkt

Feb 3rd, 2025 09:21 AM

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.

bobbyiliev

Feb 3rd, 2025 09:48 AM

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.

produkt

Feb 3rd, 2025 06:34 PM

OK so apparently that is just for new purchases. This change should be implemented inside the User.php file.

bobbyiliev

Feb 4th, 2025 12:08 AM

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.

produkt

Feb 4th, 2025 05:55 AM

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');
bobbyiliev

Feb 4th, 2025 06:29 AM

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.

produkt

Feb 4th, 2025 06:48 AM

Which part is a bug, that fulfill_checkout() is not being run when a subscription is updated? Or the user role resetting to “registered” when they cancel? I'm not 100% sure how you want it implemented.

produkt

Feb 4th, 2025 06:16 PM

It seems like I was wrong about certain things, I retract my PR, I need to do more testing

bobbyiliev

Feb 5th, 2025 02:17 AM

Ok sounds good, let me know how it goes.