How to create subscription using stripe API in PHP Laravel
Stripe is a most common and trusted payment gateway used by lots of businesses and companies and it provides lots of functionalities to make payment more smoother, faster and secure in no time. Stripe provides every service to fulfill your requirement for implementing payment gateway to your business.
We have already discussed about why to use stripe , how to create a customer , how to save a card, how to create product and prices and how to create a token and continuing this series , in this article we will see how to create or purchase a subscription over stripe using stripe api in Laravel.
If you are using some popular platforms like Netflix or Amazon , then you must be aware of how subscription works. To give a basic idea subscriptions are nothing but you are paying for some services repeatedly in a particular interval , it might be monthly or annually or any other interval.
Before creating a subscription, make sure you have these following data at your end to create a subscription.
- Customer id
- Default payment method
- Price id or unit amount
- Product id
- Quantity ( optional )
- Coupon code ( optional )
paymentController.php :-
Example of creating subscription with price_id ( if you don't know how to get price_id then , go checkout our article over how to create product and prices ).
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class paymentController extends Controller { public function createSubscription(Request $request){ try{ $customer_id = "cus_N0GwHfQdFmGKp9"; $price_id = "price_1MG03USJakTn1zSVhdnfenVQ"; $product_id = "prod_N003ZMKvxUySMg"; // connect with stripe account. $stripe = new \Stripe\StripeClient( Config('constants.stripe_keys.secret_key') ); // create subscription with price_id $subscription_detail = $stripe->subscriptions->create([ 'customer' => $customer_id, 'items' => [ ['price' => $price_id], ], ]); return $subscription_detail; }catch(\Exception $e){ return false; } } }
Example of creating subscription without price_id ( if you don't want to create price ,then you can use the following method to create your subscription ).
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class paymentController extends Controller { public function createSubscription(Request $request){ try{ $customer_id = "cus_N0GwHfQdFmGKp9"; $price_id = "price_1MG03USJakTn1zSVhdnfenVQ"; $product_id = "prod_N003ZMKvxUySMg"; // connect with stripe account. $stripe = new \Stripe\StripeClient( Config('constants.stripe_keys.secret_key') ); // create subscription with unit amount $subscription_detail = $stripe->subscriptions->create([ 'customer' => $customer_id, 'items' => [ [ 'price_data' => [ "currency"=>"inr", "product"=>$product_id, "recurring"=>[ "interval"=>"month" ], "unit_amount"=>100 ] ], ], ]); return $subscription_detail; }catch(\Exception $e){ return false; } } }
Example of creating subscription with price_id or unit amount and with quantity ( you can provide the quantity to create that much amount of subscription items ).
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class paymentController extends Controller { public function createSubscription(Request $request){ try{ $customer_id = "cus_N0GwHfQdFmGKp9"; $price_id = "price_1MG03USJakTn1zSVhdnfenVQ"; $product_id = "prod_N003ZMKvxUySMg"; // connect with stripe account. $stripe = new \Stripe\StripeClient( Config('constants.stripe_keys.secret_key') ); // create subscription with price_id and quantity $subscription_detail = $stripe->subscriptions->create([ 'customer' => $customer_id, 'items' => [ ['price' => $price_id,"quantity"=>2] ], ]); return $subscription_detail; }catch(\Exception $e){ return false; } } }
Example of creating subscription with coupon ( You can provide coupon your consumer to give them discount. If you want to know how to create coupon then go check it out here ).
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class paymentController extends Controller { public function createSubscription(Request $request){ try{ $customer_id = "cus_N0GwHfQdFmGKp9"; $price_id = "price_1MG03USJakTn1zSVhdnfenVQ"; $product_id = "prod_N003ZMKvxUySMg"; // connect with stripe account. $stripe = new \Stripe\StripeClient( Config('constants.stripe_keys.secret_key') ); // create subscription with price_id and quantity $subscription_detail = $stripe->subscriptions->create([ 'customer' => $customer_id, "coupon"=>"fHQHYFXs", 'items' => [ ['price' => $price_id,"quantity"=>2] ], ]); return $subscription_detail; }catch(\Exception $e){ return false; } } }
There are still lots of more parameters are there in stripe docs which you can use as per your business need.