Start to end Billing using Stripe, Cashier and Laravel 5.1
Start to end Billing and Laravel
As usual the Laravel docs rock but sometimes it is nice to see this from start to end.
With that said read those docs first and then come back here. Also the public repo can be seen here
Setup your Stripe
Yup this is easy. Note the test/live dash board. Super nice to see the different processes.
Turn it to test and make your plans. Keep in mind you will have to make the same plans for Live after the fact.
Go to Accounts -> Settings and get your API keys.
Plug them into the .env file (try this super cool .env deployer tool to make life easier)
So now your .env has the right settings, your config/app.php has the right env
calls
'stripe' => [
'model' => 'App\User',
'key' => env('STRIPE_PUBLIC'),
'secret' => env('STRIPE_API_SECRET'),
],
You are ready to code.
Add this to your composer.json if you are using linux
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"php -r \"exec('chmod -R +x vendor/laravel/cashier/src/Laravel/Cashier/bin');\"",
],
Or just remember you need to do this to print
Like that docs say go ahead and install it and setup your app.php for the Provider
Routes
My main route file looked like this when I was done
<?php
Route::get('/', function() {
return Redirect::to('/sponsor');
});
require __DIR__ . '/routes.sponsor.php';
require __DIR__ . '/routes.profile.php';
require __DIR__ . '/routes.auth.php';
Which is not much help :)
The profile one is this
<?php
Route::group(['prefix' => 'profile', 'middleware' => 'auth'], function () {
Route::get('/', 'ProfileController@getUser');
Route::post('edit', 'ProfileController@postEdit');
Route::get('invoice/{invoice}', 'ProfileController@getPrintInvoice');
Route::get('cancel', 'ProfileController@getCancel');
});
And the sponsor one is
<?php
Route::group(['prefix' => 'sponsor'], function() {
Route::get('/', 'SubscribeController@getSponsorPage');
Route::post('1show', 'SubscribeController@post1Show');
Route::post('2show', 'SubscribeController@post2Show');
Route::post('fan', 'SubscribeController@postFan');
});
Stripe Controller
Pretty simple, which is my goal. Basically take the requests, check the plans and setup the user.
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\Plans;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
class SubscribeController extends Controller
{
public function registerUser($input, $level)
{
if($user = User::where("email", $input['stripeEmail'])->first())
{
if($user->subscribed())
{
$user->subscription($level)->swap();
}
else
{
$user->subscription($level)->create($input['stripeToken']);
}
}
else
{
$user = User::create(
[
'email' => $input['stripeEmail'],
'password' => Hash::make(Str::random())
]
);
$user->subscription($level)->create($input['stripeToken']);
}
return $user;
}
public function getSponsorPage()
{
$public_key = env('STRIPE_PUBLIC');
return view('stripe.subscribe', compact('public_key'));
}
public function post1Show()
{
$input = Input::all();
if(empty($input['stripeToken']))
return Redirect::back();
$user = $this->registerUser($input, Plans::$ONE_SHOW_A_MONTH);
Auth::login($user);
return Redirect::to('profile')->with("message", "Thanks!");
}
public function post2Show()
{
$input = Input::all();
if(empty($input['stripeToken']))
return Redirect::back();
$user = $this->registerUser($input, Plans::$TWO_SHOWS_A_MONTH);
Auth::login($user);
return Redirect::to('profile')->with("message", "Thanks!");
}
public function postFan()
{
$input = Input::all();
if(empty($input['stripeToken']))
return Redirect::back();
$user = $this->registerUser($input, Plans::$FAN);
Auth::login($user);
return Redirect::to('profile')->with("message", "Thanks!");
}
}
We will hit the UI now
Stripe Ui
I ended up with these view files and a theme file from WrapBootstrap which I can not include of course but just use default bootstrap and it will all work.
You can see them all here
So at this point you could have something like this
Now how do they manage all this?
Profile Controller
This controller takes on the profile route to manage their subscriptions and print. You can see those files for the view about in the image and the gist link provided above.
When all that is in place they can see this
comments powered by Disqus