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.
First, add the Cashier and Stripe package to your composer.json file.
And run this command inside your terminal.
Add the package to your application service providers in config/app.php file.
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
Once the migration has been created, simply run the migrate command
php artisan migrate
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
Finally, set your Stripe key in your config/services.php configuration file:
Add key to .env
Stripe Account Create
Now Sign-up the stripe account
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
- Setup Stripe
- Create a Plan
- Subscribe the user
- Create a coupon
- Subscribe user with coupon
- User Status
- Changes the plan
- Subscription status
- Subscription Quantity
- Subscription cancel
- Subscription resuming
- Set API Keys
2.Create a Plan
Create a basic plan for subscriptions. Now we create plan for subscriber.
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.
4.Create a Coupon
Create a coupon for subscriptions.
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.
7.Change the Plan
Subscription Plan changes
If user want to subscribe the prorate basis use
User immediate invoice
8.User Subscription Status
Customer Subscription Status
Increase or decrease the quantity.
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.