Stripe API subscribing new and existing Stripe customers

David Carr

Stripe API Nova Framework Tutorials

This post will cover how to subscribe new customers and existing customers to a subscription plan.

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

To setup a Stribe button create a form and set its action to post to a page that will handle the processing of the subscription. 

Include checkout.js as the script src, followed by the data attributes. add data-email if you have the users email address. The data-key is the public API key set from Stripe. The amount ideally should come from a plan directly to ensure there is no mistake from what you have setup on Stripe.

<form action='{{ site_url('account/subscribepayment') }}' method='POST'>
        src='' class='stripe-button'
        data-key='{{ Config::get('stripe.apiPublicKey') }}'
        data-email='{{ Auth::user()->email }}'
        data-name='{{ Config::get('') }}'
        data-description='{{ $plan->name }}'
        data-amount='{{ $plan->amount }}'
        data-label='{{ __d('users', 'Activate Plan') }}'

The above will generate a button with the label ‘Activate Plan’ when pressed a popup will appear allowing the customer to enter their credit card details, this will be sent directly to stripe for validation and never touch your server.

As long as the request validated a post request is created and sent to the action of the form, in this case, the

Process the request

To add the user to a new subscription use Customer::create pass in an array containing the email, the source both comes from the post request. The stripeToken represents the user's credit card, the last option is the plan. In the below example the plan is coming from the user's record.

Once the customer is recorded collect the customer id and store it against the user for future use, also I’ve chosen to store the current_period_end which is a date of when the subscription will end/renew and the status.

    //create stripe customer and subscribe them
    $customer = Customer::create([
        'email'  => $input['stripeEmail'],
        'source' => $input['stripeToken'],
        'plan'   => Auth::user()->plan_id

    //store customer id from stripe
    $customer_id = $customer->id;

    $user                     = User::find(Auth::id());
    $user->stripe_customer_id = $customer_id;
    $user->current_period_end = date('Y-m-d H:i:s', $customer->subscriptions->data[0]->current_period_end);
    $user->status             = $customer->subscriptions->data[0]->status;

} catch(Exception $e) {
    dd("unable to sign up customer: ".Input::get('stripeEmail').", error: " . $e->getMessage());

Subscribe existing customer

If the customer has already been created in Stripe use Subscription::create and pass in the customer id from the database along with the plan to be set, notice this is an array within an array.

$subscription = Subscription::create([
    'customer' => Auth::user()->stripe_customer_id,
    'items' => [['plan' => Auth::user()->plan_id]]


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.