1

I have a website built with the Laravel framework that uses Stripe to process subscription payments. When a user subscribes to a plan through my website, this row appears in the "subscriptions" database table:

enter image description here

When a subscription is canceled through my website, the ends_at column is populated with a date the current subscription period ends and will not be renewed:

enter image description here

This is the desired behavior, however, when a payment method is invalid, Stripe automatically cancels the subscription outside of the website. Since the subscription was canceled automatically on Stripe's end, my website doesn't reflect the changes and the ends_at database column will indefinitely have a value of NULL, allowing the user to continue using premium features of the website for free.

This is how I currently check if a user has an active subscription plan on my website:

public function index()
{
    $user = auth()->user();

    if (!$user->subscribed('plan') && $user->posts->count() > 0) {
        foreach ($user->posts as $post) {
            $post->update(['status' => 'unpublished']);
        }
    }

    return view('dashboard');
}

This is how a user cancels their subscription on my website:

public function cancel()
{
    $user = auth()->user();

    $user->subscription('plan')->cancel();

    return redirect()->back()->with("success", "Subscription canceled.");
}

How do I fix this issue so my website will automatically check Stripe for a customer's subscription status?

Jake Scervino
  • 581
  • 4
  • 9
  • 26
  • that's why you can configure your webhooks for requests from stripe to your server when changes happens (renew subscription/ cancel subscription) https://stripe.com/docs/webhooks you can secure those webhook with IP verification, there is a list of stripe servers IPs in the docs (sandbox and live) – N69S Nov 15 '21 at 21:59

0 Answers0