Stripe API, Tutorials, Nova Framework

Stripe API subscribing new and existing Stripe customers

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


David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.