How to Integrate Laravel with Stripe

Juang Salaz Prabowo
Mar 6 · 3 min read
Laravel + Stripe

Hello, today I want to share about how to integrating our Laravel App with Stripe. We know that stripe is the best way to create a payment gateway system. Before we start this tutorial, we need to create a Stripe account first, because we will use a Stripe secreet key to call the API.

We’ll doing four step in this tutorial:

  1. Create Stripe account
  2. Create new Laravel project
  3. Implement the Stripe docs
  4. Testing

Step 1 — Create Stripe account

In the first step, let’s create Stripe account with enter on this page url https://dashboard.stripe.com/register. Just following the step of registration, and you’ll success to have the Stripe account.

Step 2 — Create new Laravel project

Second step, we will create new project with Laravel, enter to terminal, and we can create new project like this, for example

composer create-project — prefer-dist laravel/larave laravelstripe

After that, we need to enter into the main directory of our new project.

cd laravelstripe

Step 3 — Implement the Stripe docs

In this step, we need to add stripe package into our composer file, with typing in terminal like this

composer require stripe/stripe-php

Add stripe php package into composer file

After that, we need to go to routes/api.php file and set a new route like this

New route setup to call Stripe API

Then, we need to create new controller, called PaymentController, we can type in our terminal, like this to create new controller

Create new controller, called PaymentController

Now, open the controller, and create new function in there, say “paymentProcess”. Your controller should look like this

Don’t forget to change the “Replace with your secreet key” with your test secreet key from your Stripe dashboard.

For the Stripe payment button, we’ll put in the welcome view for an example, now open resources/views/welcome.blade.php file, and now your welcome view should be like this

Welcome view

Now, we can try to running our app, go to terminal and type

php artisan serve

Start development server

Please try to access http://127.0.0.1:8000 in the browser, and we should look like this

Step 4 — Testing

Now, we can try to click the Stripe payment button, and will show a popup like this

Stripe payment form

Try to fill all of form, fill email form with our email address, and card number form with “4242 4242 4242 4242”, then fill the month and year and then fill the CVC with any 3 digit numbers. Then click “Pay”. After that, we can check into our Stripe dashboard, and if the payment is successful, we will look at our dashboard like this

Payment report in Stripe dashboard

That mean, our payment integration between Laravel and Stripe are successful.

Thanks for read this tutorial, hopefully this tutorial help you all guys :)

Thanks

Juang Salaz Prabowo

Written by

Senior PHP Developer | https://github.com/juangsalaz