Home > Web Development > Laravel Tutorials > Complete guide to integrating Stripe into Laravel easily

Complete guide to integrating Stripe into Laravel easily

Diego Cortés
Diego Cortés
January 20, 2025
Complete guide to integrating Stripe into Laravel easily

The integration of payment methods in web applications has become a fundamental necessity for any developer. Stripe is one of the most popular and effective platforms for managing payments, and combining it with Laravel can be straightforward if you follow the right steps. Below, I present a detailed guide to integrating Stripe into your Laravel application.

What is Stripe and Why Use It?

Stripe is an online payment system that enables businesses to accept and process payments quickly and securely. Its popularity lies in its user-friendly interface, as well as the wide range of features it offers, such as subscriptions, one-time payments, and fraud management. For this reason, it is an excellent choice for developers working with Laravel.

Prerequisites

Before starting the integration, make sure you have the following:

  • A Laravel project already set up and running.
  • A Stripe account (you can create one for free on their official website).
  • Basic knowledge of PHP and Laravel.

Installing the Stripe Library

To begin, you need to install the Stripe library in your Laravel project. You can do this using Composer. Open the terminal and run the following command:

composer require stripe/stripe-php

This command will install the latest version of the Stripe library, which is essential for making calls to the Stripe API.

Configuring the API Key

Once the library is installed, the next step is to configure the Stripe API keys. Go to your .env file and add the following lines:

STRIPE_KEY=your_public_key
STRIPE_SECRET=your_secret_key

Replace your_public_key and your_secret_key with the keys you obtained from your Stripe account in the developers' section.

Creating a Payment Controller

Create a controller to handle payments. You can generate a new one using Artisan with the following command:

php artisan make:controller PaymentController

After creating the controller, inside the PaymentController.php file, import the Stripe library and set up the necessary methods.

use Stripe\Stripe;
use Stripe\Charge;

public function createCharge(Request $request)
{
    Stripe::setApiKey(env('STRIPE_SECRET'));

    $charge = Charge::create([
        'amount' => $request->amount,
        'currency' => 'usd',
        'source' => $request->source,
        'description' => 'Test payment',
    ]);

    return view('charge.success');
}

Creating the Payment Form

Create a simple form in your view so that users can enter their card details. Below is basic code for a payment form:

<form action="/charge" method="POST" id="payment-form">
    @csrf
    <label for="card-element">
        Credit or debit card
    </label>
    <div id="card-element"></div>
    <button type="submit">Pay</button>
</form>

Handling the Response

Once the payment has been processed, it is important to handle the response and provide feedback to the user. You can do this by adding a method that shows a success or error message based on the processing result.

public function success()
{
    return view('charge.success');
}

public function error()
{
    return view('charge.error');
}

Testing and Tracking

To ensure everything works correctly, use the tools provided by Stripe to conduct tests. Set up your environment to receive notifications and track transactions from your Stripe dashboard.

Conclusion

Integrating Stripe into your Laravel application may seem complicated, but by following these steps, it can become a straightforward process. By properly setting up the library, managing the API keys, and establishing the controllers and views, you will be ready to accept payments in no time.

If you are interested in topics like this, I invite you to continue exploring more news and guides on my blog. There’s always something new to learn!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 24.17 ms