Stripe Subscription Plan Creation

 Steps to Create Stripe Subscription Plan in Laravel



#1 Create plan in stripe

Goto to developer mode -> click on Product catalogue (by clicking on more in sidebar)

Then add plan by clicking on add product


#2 Get Plan Id 

After creation product get plan id in pricing section

Ex: price_1OFasVD3U1WRiWt8E76osjj1


#3 Install stripe package in project

composer require stripe/stripe-php


#4 Update .env file

STRIPE_KEY=pk_test_xxxxxx

STRIPE_SECRET=sk_test_xxxxxx


#5 Add route in api

Route::post('subscribe', [SubscriptionController::class, 'subscribe']);


#6 Add subscribe function in controller


  

 public function subscribe(Request $request)

   {

       // Set your Stripe secret key

       Stripe::setApiKey(env('STRIPE_SECRET'));


       // Create a customer

       $customer = Customer::create([

           'email' => $request->input('email'),

           'source' => $request->input('stripeToken'),

       ]);


       // Create a subscription

       $subscription = Subscription::create([

           'customer' => $customer->id,

           'items' => [

               ['plan' => "price_1OFasVD3U1WRiWt8E76osjj1"], // Replace with your actual plan ID

           ],

       ]);


       return sendResponse(200, "Subscribed Successfully", $subscription);


       // return redirect('/')->with('success', 'Subscription successful!');

   }




#Now its work



#In indian stripe account you must need to pass customer name and address to create customer in controller

Post a Comment

0 Comments

Visual Studio