Stripe API change subscription plan

David Carr

Stripe API Nova Framework Tutorials

Changing a user’s subscription plan is a simple process, this post will cover the steps needed.

I’m using Nova Framework not setup with Nova? please read Getting Stripe API setup with Nova Framework

Much like resuming a plan passing the plan id to the subscription instance is what’s needed.

First get the customers information, if you don’t have this method please read Stripe API get customer information

From the $customer the subscription can be retrieved. Set the plan id to the attribute plan to change the subscription.

From here it's a good idea to update any information you are storing such as the end dates and status.

$customer = $this->getCustomer();

if ($customer != null) {

    if (isset($customer->subscriptions->data[0])) {
        $subscription_id = $customer->subscriptions->data[0]->id;

        //get plan from stripe or redirect back
        try {
            Plan::retrieve(Input::get('plan_id'));
        } catch (Exception $e) {
            return Redirect::back();
        }

        //change plan
        $stripe = $customer->subscriptions->retrieve($subscription_id);
        $stripe->plan = Input::get('plan_id');
        $result = $stripe->save();

        //update database user
        $user                       = User::find(Auth::id());
        $user->plan_id              = Input::get('plan_id');
        $user->current_period_end   = date('Y-m-d H:i:s', $result->current_period_end);
        $user->canceled_at          = null;
        $user->cancel_at_period_end = ($result->cancel_at_period_end) ? 'true' : 'false';
        $user->status               = $result->status;
        $user->save();

    }

}

 

Fathom Analytics $10 discount on your first invoice using this link

David Carr - Laravel Developer

Hi, I’m David Carr

A Senior Developer at Vivedia
I love to use the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Laravel Livewire)

I enjoy writing tutorials and working on Open Source packages.

I also write books. I'm writing a new book Laravel Testing Cookbook, This book focuses on testing coving both PestPHP and PHPUnit.

Sponsor me on GitHub

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Laravel Testing Cookbook

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.