Closed colynb closed 3 years ago
So what happens if you do these things in a single HTTP request is that a user's subscription collection is loaded to prevent n+1 problems. If you then perform changes to those subscriptions (like the cancelation you described) those won't be reflected in the loaded subscription's collection of the user. You'll need to refresh a user model to refresh its collection as well.
I've written a test for your scenario. Notice that I refresh
the model when I perform the subscribed
checks. If you leave those out you'll get the issue you experience from above.
https://github.com/laravel/cashier-stripe/commit/fed057d0bb8b1a113ef2971262215ea544b77c97
It's unlikely that you'll ever cancel and immediately subscribe a user in a single HTTP request in a real app which is why this doesn't pops up often.
Description:
I'm testing out canceled subscriptions in my app and I keep running to odd behavior when I check the subscription status. When I cancel a subscription, as expected, the ends_at column gets a timestamp (30 days from now if on a monthly plan). I wanted to test what happens when the system reaches that time so I set the timestamp to yesterday. As expected, the subscription check returns false saying I am not subscribed. Excellent. Next, now that I am unsubscribed, when I subscribe again, it creates a new row in the subscriptions table, superseding the one with the expiration. I think that is ok but when I use the subscribed method again, this time it's still saying I am not subscribed even though I am. Digging into the ManagesSubscription class, I see that the subscribed method grabs the FIRST subscription with a matching name. In this case, the first one is expired and therefore responds that I am not subscribed.
I can't tell if this a bug or I am doing something wrong.
Steps To Reproduce: