stripe easy integration with laravel
Stripe is the best way to accept payments online and in mobile apps. We handle billions of dollars every year for forward-thinking businesses around the world., (*1)
First, add the Cashier package for Stripe to your composer.json file and run the composer update command:, (*2)
"laravel/cashier": "~6.0"
Next, register the Laravel\Cashier\CashierServiceProvider service provider in your app configuration file., (*3)
Laravel\Cashier\CashierServiceProvider::class,
Before using Cashier, we'll also need to prepare the database. We need to add several columns to your users table and create a new subscriptions table to hold all of our customer's subscriptions:, (*4)
Schema::table('users', function ($table) { $table->string('stripe_id')->nullable(); $table->string('card_brand')->nullable(); $table->string('card_last_four')->nullable(); $table->timestamp('trial_ends_at')->nullable(); }); Schema::create('subscriptions', function ($table) { $table->increments('id'); $table->integer('user_id'); $table->string('name'); $table->string('stripe_id'); $table->string('stripe_plan'); $table->integer('quantity'); $table->timestamp('trial_ends_at')->nullable(); $table->timestamp('ends_at')->nullable(); $table->timestamps(); });
Once the migrations have been created, simply run the migrate Artisan command., (*5)
Next, add the Billable trait to your model definition:, (*6)
use Laravel\Cashier\Billable; class User extends Authenticatable { use Billable; }
Next, you should configure your Stripe key in your services.php configuration file:, (*7)
'stripe' => [ 'model' => App\User::class, 'secret' => env('STRIPE_SECRET'), ],
First, add the udaykumar77/stripe package to your composer.json file and run the composer update command:, (*8)
"udaykumar77/stripe": "v1.0"
Next, register the UdayKumar77\Stripe\StripeServiceProvider service provider in your app configuration file., (*9)
UdayKumar77\Stripe\StripeServiceProvider::class,
Next, register the UdayKumar77\Stripe\Facade\StripeController facade in your app configuration file aliases., (*10)
'Stripe' => UdayKumar77\Stripe\Facade\StripeController::class,
Run the following command from your terminal., (*11)
composer dump-autoload
Add the following in your .env file, (*12)
STRIPE_SECRET=************************** STRIPE_PUBLISHABLE_SECRET=*********************
Stripe::generateCardToken(array $params) $params = ["number" => "4242424242424242", "exp_month" => "9", "exp_year" => "2017", "cvc" => "456"]
use the name space in your controller like this, (*13)
<?php namespace App\Http\Controllers\Stripe; use Stripe; public function generateToken() { $stripeToken = Stripe::generateCardToken( ["number" => "4242424242424242", "exp_month" => "9", "exp_year" => "2017", "cvc" => "456"]); return response()->json($stripeToken); }
MIT License (MIT) @ Uday Kumar Gudivada, (*14)