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

lamarlaing's avatar

Where is the best place assign setup intent for Cashier 10 subscription payments with Laravel 6?

Hi Laravel developers,

I am using Laravel Cashier to take stripe subscription payments from users.

Where is the best place assign setup intent? In route, controller or view?

Here Are My Routes:

Route::get('/plan/{plan}', 'PlanController@show')->name('plans.show');
Route::post('/subscription', 'SubscriptionController@create')->name('subscription.create') ;

Here is my Plan Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Plan;

class PlanController extends Controller {


public function index() {


    $plans = Plan::all();
    return view('plans.index', compact('plans'));
}

public function show(Plan $plan, Request $request) {


    return view('plans.show', compact('plan'));
}
}

Here is my Subscription Controller:

// SubscriptionController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Cashier\Billable;
use App\Plan;
use App\User;

class SubscriptionController extends Controller
{
public function __construct() {
    Stripe::setApiKey(env('STRIPE_SECRET'));
}
public function create(Request $request, Plan $plan)
{
    $plan = Plan::findOrFail($request->get('plan'));
    $user = $request->user();
    $paymentMethod = $request->paymentMethod;


    $paymentMethod = $request->get('stripePaymentMethod'); $plan = 'Your plan in a cae of 
subcription'; $user->newSubscription('main', $plan)->create($paymentMethod);



    return redirect()->route('home')->with('status', 'Your plan subscribed successfully');
}
}

Here is the form in my view:

<form action="{{ route('subscription.create') }}" method="post" id="payment-form">
@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 style="width: 30em" id="card-element">
        <!-- A Stripe Element will be inserted here. -->
        </div>
        <!-- Used to display form errors. -->
        <div id="card-errors" role="alert"></div>
        <input type="hidden" name="plan" value="{{ $plan->id }}" />
    </div>
</div>
<div class="card-footer">
    <button data-secret="{{ $intent->client_secret }}" class="btn btn-dark" type="submit">Pay</button>
</div>
0 likes
14 replies
fylzero's avatar
fylzero
Best Answer
Level 67

@lamarlaing Just pass it to whatever view the form is on like so...

class SubscriptionController extends Controller
{

    public function setupPayment()
    {
        return view('pages.subscribe', [
            'intent' => auth()->user()->createSetupIntent()
        ]);
    }

}

lamarlaing's avatar

@fylzero now i'm getting this error

 Method 'createSetupIntent' not found in \Illuminate\Contracts\Auth\Authenticatable|null
lamarlaing's avatar

@fylzero I ran this command

 npm -v laravel/cashier 

and it returned

6.10.3

Does this mean i'm running the old version of cashier?

fylzero's avatar

@lamarlaing No. Cashier is a Composer package, not a Node package. That is your npm version number. It is ignoring the laravel/cashier part of that command.

Follow the documentation... it's all in there...

composer require laravel/cashier

Make sure to add use Billable to your User model or it will not have access to the intent method.

lamarlaing's avatar

Ok, I am Using version ^10.5 for laravel/cashier

Here is my user model

 <?php

 namespace App;

 use Illuminate\Notifications\Notifiable;
 use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
 use Jenssegers\Mongodb\Auth\User as Authenticatable;
 use Laravel\Cashier\Billable;
 use Illuminate\Foundation\Auth;

 class User extends Authenticatable
 {

     use  Billable, Notifiable;


     protected $connection = 'mongodb';


     /**
      * The attributes that are mass assignable.
      *
      * @var array
      */
     protected $fillable = [
         'name', 'email', 'username', 'password', 'phone'
     ];
 /**
 * The collection name
 *
 * @var array
 * . /
 protected $collection = 'users';



 / .  **
 * The attributes that are mass assignable.
 *
 * @var array
 */
 protected $dates = ['deleted_at'];
     /**
      * The attributes that should be hidden for arrays.
      *
      * @var array
      */
     protected $hidden = [
         'password', 'remember_token',
     ];

     /**
     * The attributes that should be cast to native types.
      *
      * @var array
      */
     protected $casts = [
         'email_verified_at' => 'datetime',
     ];

}

lamarlaing's avatar

@fylzero Ok Here is controller

        <?php
        
        // SubscriptionController.php
        
        namespace App\Http\Controllers;
        
        use Illuminate\Http\Request;
        use App\Plan;
        use App\User;
        use Laravel\Cashier\Billable;
        
        class SubscriptionController extends Controller
        {
        
            public function setupPayment()
            {
        
                return view('plans.show', [
                    'intent' => auth()->user()->createSetupIntent()
                ]);
            }
        
        
        public function create(Request $request, Plan $plan)
            {
                $user = User::find(1);
                $user->newSubscription('main', 'monthly')->create($paymentMethod);
                $user->createAsStripeCustomer();
                return redirect()->route('home')->with('success', 'Your plan subscribed successfully');
            }
        }
lamarlaing's avatar

@fylzero here are my errors

find() returns error:

 Method 'find' not found in \App\User 

Create setupintent() returns error:

  Method 'createSetupIntent' not found in \Illuminate\Contracts\Auth\Authenticatable|null 

createAsStripeCustomer returns error:

 Method 'createAsStripeCustomer' not found in 

newSubscription returns error:

 Method 'newSubscription' not found in
fylzero's avatar

@lamarlaing You don't need the Billable trait in your controller.

Do you actually have the view pages/subscribe.blade php? I put that as an example of where to put the setup intent, you don't need to copy that exactly.

Are you still seeing the same error?

lamarlaing's avatar

@fylzero I removed Billable trait in controller.

Although I made changes I still get the errors. I do have plans.show here is my route. This is the page the user in their card information.

   Route::get('/plan/{plan}', 'PlanController@show')->name('plans.show');

When I submit test payment in browser. This is the error that comes up:

 Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)

Call to a member function newSubscription() on null

fylzero's avatar

@lamarlaing This is a different error... It seems like you're hitting issue after issue... and I'm not planning to code this app for you on here.

Your original question has been answered... I would suggest trying to troubleshoot this and ask more narrowed down questions here as needed.

lamarlaing's avatar

@fylzero Yes I figured it would be a bit overwhelming with me listing every error that popped up in my controller. If you are available to help out on this project. Let's figure out how we can work that out. Either way thanks for your help @fylzero.

Please or to participate in this conversation.