Any business project cannot be said to be complete until you are able to sell it. To make the sale, you should be able to receive the payment. 

There are two types of payment that are mostly used in any business project:

  • Single payment: A single payment is what needs to be made only once. For example, making a payment to purchase through an ecommerce website. 
  • Recurring payment: A recurring payment is that which you have to make more than once in a particular period of time. For example, subscribing to web hosting services for a website.

In this blog, I will tell you how you can implement the recurring payment process in laravel. The blog will focus on implementing the stripe payment.

If you are into web development, this blog will be a very handy guide. You might also want to check out how you can set up stripe connect marketplace account

Let's get started. 

Implementing Recurring Payments with Stripe

Step 1: Install Laravel Fresh Project

To begin, use the command below to install Laravel as a new application, then open your command prompt and run it:

Composer create-project –prefer-dist laravel/laravel stripeRecurringPayment

Step 2: Install the Stripe package

composer requires catalyst/stripe-laravel 

After that, you need to register the provider and aliases. Go to the app/config/app.php and enter the lines below:

‘providers’ => [

 ……….

 CartalystStripeLaravelStripeServiceProvider::class 

],

‘aliases’ => [

 ……….

 ‘Stripe' => CartalystStripeLaravelFacadesStripe::class 

],

Step 3: Set Secret Credential

Now open the .env file and set the secret credential provided by a stripe payment gateway.

STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxxx

STRIPE_SECRET=sk_test_xxxxxxxxxxxxxx

In the next step, you need to set up the Stripe API key. To do this, open or create the config/services.php file and add or update the ‘stripe' array:

1

2

3

‘stripe' => [

     ‘secret' => env(‘STRIPE_SECRET'),

 ],

Step 4: Make Route

 Route::get(‘stripe', ‘StripeController@index');

 Route::post(‘store', ‘StripeController@store');

Step 5: Create Controller

In this step, you need to create the controller name StripeController using the below command.

php artisan make: controller StripeController

Go to app/Http/Controller/StripeController and enter the below code:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use Session;

use Stripe;

class StripeController extends Controller

{

  /**

   * success response method.

   *

   * @return IlluminateHttpResponse

   */

  public function stripe()

  {

    return view(‘stripe');

  }

  /**

   * success response method.

   *

   * @return IlluminateHttpResponse

   */

  public function stripePost(Request $request)

  {

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

    //create customer

    $customer = StripeCustomer::create([

      ‘name' => ‘your name',

      ‘address' => [

        ‘line1' => ‘your address',

        ‘postal_code' => ‘your postal code',

        ‘city' => ‘Your City,

        ‘state' => ‘Your State Code',

        ‘country' => ‘Your Country Code',

      ],

      'email' => ‘your email',

      ‘source' => $request->stripeToken

    ]);

    $stripe = new StripeStripeClient(

      env(‘STRIPE_SECRET')

    );

    $customer_id = $customer->id;

    //create product

    $product = $stripe->products->create([

      ‘name' => ‘Diamond',

      ‘id'   => ‘123',

      ‘metadata' => [

        ‘name' => “silver”,

        ‘last-date' => '30-7-2021′

      ]

    ]);

    $product_id = $product->id;

    //define product price and recurring interval

    $price = $stripe->prices->create([

      ‘unit_amount' => 2000,

      ‘currency' => ‘usd',

      ‘recurring' => [‘interval' => ‘month'],

      ‘product' => $product_id,

    ]);

    $price_id = $price->id;

    //CREATE SUBSCRIPTION

    $subscription = $stripe->subscriptions->create([

      ‘customer' => $customer_id,

      ‘items' => [

        [‘price' => $price_id],

      ],

      ‘metadata' => [

        ‘start_date' => '30-7-2021′,

        ‘total_months' => '11',

        ‘end_date' => '30-5-2022′

      ]

    ]);

    Session::flash(‘success', ‘Payment successful!');

    return back();

  }

}

In the above example, you can see that there are basically three things that are done with the stripe code.

  • Creating customers
  • Creating a recurring product
  • Creating a subscription and assigning that subscription to the above-created customer with the above-created product. 

How to Create a Blade View File?

To create a frontend view, you can simply use the Stripe code, which shows the popup form and a form to store all the required information. 

To create a blade views file, go to app/resources/views/ and create one file named stripe.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset=”UTF-8″>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

<meta http-equiv=”X-UA-Compatible” content=”ie=edge”>

<meta name=”csrf-token” content=”{{ csrf_token() }}”>

<title>Stripe Payment Gateway Integrate – Tutsmake.com</title>

<link href=”https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css” rel=”stylesheet”>

<style>

.mt40{

margin-top: 40px;

}

</style>

</head>

<body>

<div class=”container”>

<div class=”row”>

<div class=”col-lg-12 mt40″>

<div class=”text-center”>

<h2>Pay for Event</h2>

<br>

</div>

</div>

</div>

@if ($errors->any())

<div class=”alert alert-danger”>

<strong>Whoops!</strong> Something went wrong<br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif

<div class=”row”> 

<div class=”col-md-3″>

</div>

<div class=”col-md-6″>

<form accept-charset=”UTF-8″ action=”{{url(‘payment')}}” class=”require-validation”

data-cc-on-file=”false”

data-stripe-publishable-key=”test_public_key”

id=”payment-stripe” method=”post”>

{{ csrf_field() }}

<div class='row'>

<div class='col-xs-12 form-group'>

<label class='control-label'>Name on Card</label> <input

class='form-control' size='4′ type='text'>

</div>

</div>

<div class='row'>

<div class='col-xs-12 form-group'>

<label class='control-label'>Card Number</label> <input

autocomplete='off' class='form-control' size='20'

type='text' name=”card_no”>

</div>

</div>

<div class='row'>

<div class='col-xs-4 form-group'>

<label class='control-label'>CVC</label> <input autocomplete='off'

class='form-control' placeholder='ex. 311′ size='3′

type='text' name=”ccv”>

</div>

<div class='col-xs-4 form-group'>

<label class='control-label'>Expiration</label> <input

class='form-control' placeholder='MM' size='2′

type='text' name=”expiry_month”>

</div>

<div class='col-xs-4 form-group'>

<label class='control-label'> </label> <input

class='form-control' placeholder='YYYY' size='4′

type='text' name=”expiry_year”>

</div>

</div>

<div class='row'>

<div class='col-md-12′>

<div class='form-control total btn btn-info'>

Total: <span class='amount'>$20</span>

</div>

</div>

</div>

<div class='row'>

<div class='col-md-12 form-group'>

<button class='form-control btn btn-primary submit-button'

type='submit' style=”margin-top: 10px;”>Pay »</button>

</div>

</div>

</form>

</div>

</div>

</body>

</html>

How to Start Development Server?

In this step, you can use the php artisan serve command. It will start your server locally.

php artisan serve

Testing Card Credential

Card No: 4242424242424242

Month: any future month

Year: any future Year

CVV: 123

And there you have it. Use your demo stripe keys to test the payment process.

Now, you can use these simple steps and implement this recurring payment process on your own. 

To access the full code you can download it from my git repo.

https://github.com/iqbaldhillon116/laravelStripePayment.git

Wrapping Up

In this blog, I showed you how to make recurring deposits with Stripe. I believe the steps mentioned above are clear and will assist you in achieving your objectives. 

Still, if you require assistance with website development or the creation of a professional website, Contact Us!

                                                                                                                            Editor: Amrutha

Frequently Asked Questions

To set up recurring payments on Stripe:

  • Log in to your Stripe dashboard.
  • Create or select a product or subscription plan.
  • Configure the subscription details.
  • Integrate Stripe's API into your website or app.
  • Use webhooks for event handling.
  • Test the setup thoroughly.
  • Launch and monitor recurring payments.

Yes, Stripe supports recurring payments and subscription billing for businesses.

Recurring payments can be set up for various types, including subscriptions, membership fees, rent, loan repayments, utility bills, and more, depending on the business's needs.

The choice between Stripe and Square depends on your specific business needs. Both are reputable payment processors, but Stripe is known for its flexibility and developer-friendly features, while Square offers a more integrated point-of-sale (POS) system. Evaluate which aligns better with your business requirements.