Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Stripe API subscribing new and existing Stripe customers

David Carr

Stripe API Tutorials Nova Framework

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]]


Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

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


My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

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