Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

How to integrate stripe & Cashier Subscription payment system on laravel 6

I'm getting that error when i post a payment from my app. I'm using stripe payment system and cashier.

My controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SubscriptionController extends Controller
{
    public function index()
    {
        return view('subscribe');
    }
    public function store(Request $request)
    {
    
        $token =$request->stripeToken;
     
        auth()->user()->newSubscription('model','monthly')->create($token);   
        auth()->user()->assignRole('subscriber');
        return redirect('/plan');
    }
}

My View

@extends('layouts.app')

@section('post-js')
<div class="container">
    <div class="row">
            <div class="col-md-6 col-md-offset-3">
                
                <h3>Subscribe to monthly plan</h3>
                
                <form action="/subscribe" method="POST">
                    {{csrf_field()}}

                   <!-- <input type="text" name="coupon"> -->

                  <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_PUYJMeO6wE9Y5iK8ca28ZJGS00Z6OhAZdo"
                    data-amount="0"
                    data-name="Exclusive Fans"
                    data-description="Subscribe to XclusiveFans"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-label="Subscribe Now"
                    data-email="{{ auth()->check()?auth()->user()->email: null}}"
                    data-panel-label="Pay Monthly"
                    data-locale="auto">
                  </script>
                </form>

            </div>
    </div>
</div>
@endsection

My route

Route::get('/subscribe','SubscriptionController@index');
Route::post('/subscribe','SubscriptionController@store');
0 likes
13 replies
tomasosho's avatar

The stripe token is the payment method, please assist me on what i'm to do.

tomasosho's avatar

When i do this, i get a 404 error|Not Found

public function store(Request $request)
    {
    
        $plan = Plan::findOrFail($request->plan);

    if ($request->user()->subscribedToPlan($plan->stripe_plan, 'main')) {
        return redirect('/')->with('error', 'Unauthorised operation');
    }

    $request->user()->newSubscription('main', $plan->stripe_plan)->create($request->payment_method_nonce);

        auth()->user()->assignRole('subscriber');
        return redirect('/plan');
    }
tomasosho's avatar

@sinnbeck I added $token =$request->paymentMethod; to the controller and i added data-paymentMethod="pm_1FU2bgBF6ERF9jhEQvwnA7sX" to the views. It's giving me this error "Stripe\Exception\InvalidRequestException "No such plan: month"

tomasosho's avatar

@nakov I'm getting a 404 error with that... It's my first time working with subscriptions. There's no recent tutorial on it, and it's really frustrating. I'm almost done with the project, it's just the subscription part that i've been on for more than 2 weeks now.

Nakov's avatar
Nakov
Best Answer
Level 73

@tomasosho I know that the documentation for Cashier is not a step by step guide on how to integrate everything with Stripe, but following both the Stripe documentation and Cashier will get you very far.

So first you need to add a Payment Method on Stripe for the user, here is a project that I created to showcase on how to do that, it was done for another question here on Laracasts:

https://github.com/nakov0301/cashier-v10

Then as the documentation says, you need to create a subscription using that payment method:

https://laravel.com/docs/master/billing#subscriptions

But first the subscription plan has to exist in your Stripe account as well, for that checkout this documentation

https://stripe.com/docs/billing/subscriptions/products-and-plans#using-the-dashboard

There is a video for this here on Laracasts as well

https://laracasts.com/lessons/painless-subscriptions-with-laravel-and-stripe

It is for an older version, but you can get very far following the video and also reading the documentation.

So I know it is going back and forward a lot, but that's the job of a programmer :)

I hope this was helpful, let me know if you get any step closer with what I've shown you above.

tomasosho's avatar

I tried this, its registering new subscription on both database and on my stripe dashboard. The problem is on my user database, my card brand and card last four are not registering, and i'm getting Stripe\Exception\UnexpectedValueException Could not determine which URL to request: Stripe\PaymentMethod instance has invalid ID: error.

Here is my a copy of my project on github, Please help me out. https://github.com/Thomasosho/laravel-social-network-with-subscription.git

@nakov @sinnbeck

 public function store(Request $request)
    {
        $paymentMethod = auth()->user()->defaultPaymentMethod();
        auth()->user()->newSubscription('model','plan_GKxQ5awzJGPxc6')->create($paymentMethod);  
        auth()->user()->addPaymentMethod($paymentMethod);  
        auth()->user()->assignRole('subscriber');
          
        return redirect('/plan');
    }
tomasosho's avatar

I just need to see how to implement payment methods.

am0rphis's avatar

I've had the same issue, and here's the solution that worked for me.

Form

('intent' is created in the controller, like so: $intent = \Auth::user()->createSetupIntent(), and then passed over to view)

<form action="{{ route('subscription.create') }}" method="post" id="payment-form">
  <input type="hidden" name="plan" value="{{ $plan->id }}">
  @csrf
  <div class="form-group">
    <div class="card-header">
      <label for="card-element">
        Enter your credit card information
      </label>
    </div>
    <div class="card-body">
      <div id="card-element">
        <!-- A Stripe Element will be inserted here. -->
      </div>
      <!-- Used to display form errors. -->
      <div id="card-errors" role="alert"></div>

    </div>
  </div>
  <div class="card-footer">
    <button id="card-button" class="btn btn-dark" type="button"
      data-secret="{{ $intent->client_secret }}">Subscribe</button>
  </div>
</form>

JS in View

<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('{{ env("STRIPE_KEY") }}');
var elements = stripe.elements();
var card = elements.create('card');
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.on('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

const cardButton = document.getElementById('card-button');
const clientSecret = cardButton.dataset.secret;

cardButton.addEventListener('click', async (e) => {
    const { setupIntent, error } = await stripe.confirmCardSetup(
        clientSecret, {
            payment_method: {
                card: card
            }
        }
    );

    if (error) {
        var errorElement = document.getElementById('card-errors');
        errorElement.textContent = error.message;
    } else {
        // Send the token to your server.
        stripeTokenHandler(setupIntent.payment_method);
    }
});

// Submit the form with the token.
function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}
</script>

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Plan;

class SubscriptionController extends Controller
{
    public function create(Request $request, Plan $plan)
    {
        $plan = Plan::findOrFail($request->get('plan'));

        if($request->user()->subscribedToPlan($plan->stripe_plan, $plan->name)) {
            
            $notification = [
                'type' => 'error',
                'message' => 'You are already subscribed to this plan!',
            ];
        } else {
        
            $request->user()
                ->newSubscription($plan->name, $plan->stripe_plan)
                ->create($request->stripeToken);

            $notification = [
                'type' => 'success',
                'message' => 'Successfully subscribed to ' . $plan->name . ' plan!',
            ];
	}

        return redirect(route('plans.index'))->with($notification);
    }
}

Please or to participate in this conversation.