Implementing Laravel Cashier

This time we will discuss how to implement Stripe’s billing service using Laravel Cashier. Laravel Cashier provides an expressive and fluent interface for Stripe. It handles almost all the code we need for  subscription management, Cashier can handle coupons, swapping subscription, subscription “quantities”, cancellation grace periods, and even generate invoice PDFs. We will go step by step in the implementation process.

Configuration

Composer

First, add the Cashier and Stripe package to your composer.json file.

Screen Shot 2016-03-31 at 9.02.04 AM

And run this command inside your terminal.

composer update

Service provider

Add the package to your application service providers in config/app.php file.

Screen Shot 2016-03-31 at 9.04.19 AM

Migration

Before using Cashier, we’ll need to add several columns to your database. Don’t worry, you can use the cashier:table Artisan command to create a migration to add the necessary column.

For example, to add the column to the users table runs the command:

php artisan cashier:table users

It’s create a following column in users table

Screen Shot 2016-03-31 at 9.05.26 AM

Once the migration has been created, simply run the migrate command

php artisan migrate

Model 

Include Billable trait and also implement Billable contract inside your User model. Adding the columns to your model’s $dates property will instruct Eloquent to return the columns as Carbon / DateTime instances instead of raw strings

Screen Shot 2016-03-31 at 9.06.22 AM

Stripe Key

Finally, set your Stripe key in your config/services.php configuration file:

Screen Shot 2016-03-31 at 9.07.09 AM

Add key to .env

Screen Shot 2016-03-31 at 9.07.45 AM

Stripe Account Create

Now Sign-up the stripe account

1

After creating an account, you can use test or live mode. Now I drive in test mode.

You have an API key in stripe Account Setting -> API Keys. Its show Test Secret Key as well as Live Secret Keys.

Now we will see the following process

  1. Setup Stripe
  2. Create a Plan
  3. Subscribe the user
  4. Create a coupon
  5. Subscribe user with coupon
  6. User Status
  7. Changes the plan
  8. Subscription status
  9. Subscription Quantity
  10. Subscription cancel
  11. Subscription resuming
  12. Invoices
  1. Set API Keys

StripeStripe::setApiKey(env(‘STRIPE_SECRET’));

2.Create a Plan

Create a basic plan for subscriptions. Now we create plan for subscriber.

Screen Shot 2016-03-31 at 9.09.10 AM

Plan intervals are either day, week, month or year. Currency is 3-letter ISO code. ID is unique for plan.

3.Subscribe the user

Now we create a customer with cards details and get token to subscription.

Screen Shot 2016-03-31 at 9.10.04 AM

 

4.Create a Coupon

Create a coupon for subscriptions.

Screen Shot 2016-03-31 at 9.13.11 AM

Coupon percent of offer, duration have the three type of values. Forever,  once and repeating. Duration in a month is numerical values. ID is unique for coupon.

5.Subscribe the user with a coupon

Customer subscription is with a coupon.

Screen Shot 2016-03-31 at 9.16.10 AM

6.User Status

Customer Status

Screen Shot 2016-03-31 at 9.17.34 AM

7.Change the Plan

Subscription Plan changes

Stripe API

Screen Shot 2016-03-31 at 9.18.44 AM

Cashier

Screen Shot 2016-03-31 at 9.19.29 AM

If user want to subscribe the prorate basis use

Stripe API

Screen Shot 2016-03-31 at 9.21.01 AM

Cashier API

Screen Shot 2016-03-31 at 9.21.35 AM

User immediate invoice

Screen Shot 2016-03-31 at 9.22.32 AM

8.User Subscription Status

Customer Subscription Status

Stripe API

Screen Shot 2016-03-31 at 9.23.27 AM

Cashier API

Screen Shot 2016-03-31 at 9.24.04 AM

9.Subscription Quantity

Increase or decrease the quantity.

Stripe API

Screen Shot 2016-03-31 at 9.24.54 AM

10.Subscription Cancel

Strip API

Screen Shot 2016-03-31 at 9.25.33 AM

Cashier API

Screen Shot 2016-03-31 at 9.26.37 AM

11.Subscription Resume

Strip API

Screen Shot 2016-03-31 at 9.27.21 AM

Cashier API

Screen Shot 2016-03-31 at 9.27.55 AM

12.Invoices

Strip API

Screen Shot 2016-03-31 at 9.28.38 AM

Cashier API

Screen Shot 2016-03-31 at 9.28.44 AM

Hope this blog might have helped you in implementing the feature very clearly. We will try to discuss on the implementation of using an Indian Online payment gateway in the next coming blog.

Balasubramani,
PHP Senior Developer,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *