Laravel 8 Stripe Subscription Tutorial Using Cashier Example

Hello Artisan,

In this Laravel laravel 8 stripe subscription tutorial, we are going to see how to create laravel subscription system using laravel cashier. In this laravel 8 cashier tutorial, I am going to create this tutorial step by step so that you can understand better and copy this code for your project.

You know that for laravel subscription management, Laravel gives us their laravel cashier and using that package we can manage easily the subscription system in Laravel. I will use a stripe payment gateway with Laravel cashier to create this laravel cashier stripe checkout page and laravel cashier subscription system.

Suppose we are going to create a web application like Laracats or any kind of web application that needs to be implemented subscription system, then we can use Laravel cashier to create this subscription system. We will use laravel Billing subscription using Stripe. we will make the bellow example for pay subscription payment method.

 

Step 1 : Download Laravel 

To create this laravel 8 stripe subscription tutorial, we need a fresh Laravel application. So download it by the following command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2 : Make Auth

We need users to create a subscription plan in Laravel. So we need auth. So make the below command to create auth system in Laravel.

composer require laravel/ui
php artisan ui vue --auth

 

We are going to use the Laravel collective form. So install it by the following command:

composer require laravelcollective/html

 

Step 3 : Install Cashier Packag

In this step, You need laravel official cashier package. you can install the cashier package using the bellow command So Let's open the terminal and run the bellow command:

composer require laravel/cashier

 

And then run 

php artisan migrate

 

If we need to overwrite the migrations that ship with the Cashier package, you can publish them using the vendor:publish Artisan command:

php artisan vendor:publish --tag="cashier-migrations"

 

Step 5 : Update User Model

In this step, before using Cashier, we need to add the Billable trait to User model like that:

app\Models\User.php

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
} 

 

After adding that Billable trait to the User model now Cashier assumes your Billable model will be the App\Models\User class that ships with Laravel. If you want to change this you can specify a different model in your .env file like:

.env

CASHIER_MODEL=App\Models\User

 

Step 6 : Create Stripe Account to Get Stripe API Key and SECRET

In this step, if you don't have a stripe account, we need it to get a stripe API key and secret key. So let's open and visit Stripe official website and get your client secret code.

.env

STRIPE_KEY=pk_test*****
STRIPE_SECRET=sk_test******

 

After getting that you need to create your all plans for your subscription plan system. So create your all plan.

 

Step 6 : Add Route

In this step, We will add a route in the route file so let's open the web.php file and add two routes first route is form and the second route is store stripe subscription.

routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SubscriptionController;

Route::get('/subscription/create', ['as'=>'home','uses'=>'SubscriptionController@index'])->name('subscription.create');
Route::post('order-post', ['as'=>'order-post','uses'=>'SubscriptionController@orderPost']);

 

Step 7 : Create Controlle

Now we need to create a SubscriptionController and has to be implemented above both methods. 

app/Http/Controllers/SubscriptionController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
Use App\Models\User;
use Stripe;
use Session;
use Exception;

class SubscriptionController extends Controller
{
    public function index()
    {
        return view('subscription.create');
    }

    public function orderPost(Request $request)
    {
            $user = auth()->user();
            $input = $request->all();
            $token =  $request->stripeToken;
            $paymentMethod = $request->paymentMethod;
            try {

                Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
                
                if (is_null($user->stripe_id)) {
                    $stripeCustomer = $user->createAsStripeCustomer();
                }

                \Stripe\Customer::createSource(
                    $user->stripe_id,
                    ['source' => $token]
                );

                $user->newSubscription('test',$input['plane'])
                    ->create($paymentMethod, [
                    'email' => $user->email,
                ]);

                return back()->with('success','Subscription is completed.');
            } catch (Exception $e) {
                return back()->with('success',$e->getMessage());
            }
            
    }
}

 

Step 8: Create View File

In the last step, You can create a view blade file first you can create a subscription directory then after create the blade file in the subscription directory.

resources/views/subscription/create.blade.php

 

Recommended: Laravel 8 Stripe Payment Gateway Integration Example

 

All are set to go. You can test it and let me know if any issues arise. Hope this Laravel 8 cashier tutorial with a subscription plan will help you.

 

#laravel #laravel-8x