add stripe payment method in Laravel

How to add (backend) stripe payment method in Laravel?

0 / 0 4 Mins Admin

December 18, 2022

10 Comments

In this complete tutorial, we will learn how to integrate Laravel stripe payments. We will look at a Laravel payment gateway integration example.

To add the Stripe payment method in Laravel, you can use the Stripe PHP library.

  1. First, you will need to install the Stripe PHP library in your Laravel project. You can do this using Composer by running the following command:
composer require stripe/stripe-php
  1. Next, you will need to sign up for a Stripe account and obtain your Stripe API keys. You can find your API keys in the Stripe dashboard under the “Developers” tab.
  2. In your Laravel application, you will need to configure the Stripe PHP library with your API keys. You can do this by adding the following code to your .env file:
STRIPE_SECRET=sk_test_0000000000000000
STRIPE_PUBLISHABLE=pk_test_00000000000
  1. To accept payment using Stripe, you will need to create a form that includes a credit card input field and a submit button. You can use Stripe Elements to create a customizable credit card form that is compatible with Stripe’s payment processing.
  2. When the form is submitted, you can use the Stripe PHP library to create a charge using the \Stripe\Charge::create() method. This method takes an array of parameters as an argument, including the amount to be charged, the credit card details, and the currency.

Here is a sample code that demonstrates how to accept a payment using Stripe in Laravel:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\Charge;
use Stripe\Stripe;

class PaymentController extends Controller
{
    public function charge(Request $request)
    {
        // Set your secret key: remember to change this to your live secret key in production
        // See your keys here: https://dashboard.stripe.com/account/apikeys
        Stripe::setApiKey(env('STRIPE_SECRET'));

        // Token is created using Checkout or Elements!
        // Get the payment token ID submitted by the form:
        $token = $request->stripeToken;

        // Charge the user's card:
        $charge = Charge::create([
            'amount' => 1000,
            'currency' => 'usd',
            'description' => 'Example charge',
            'source' => $token,
        ]);

        return redirect()->route('home')->with('success', 'Payment successful!');
    }
}

Card numbers to test payment integration

You can use the following card data for testing purposes and ensure that your integration works as expected.

NumberBrandCVCDate
4242424242424242VisaAny 3 digitsAny future date
4000056655665556Visa (debit)Any 3 digitsAny future date
5555555555554444MastercardAny 3 digitsAny future date
2223003122003222Mastercard (2-series)Any 3 digitsAny future date
5200828282828210Mastercard (debit)Any 3 digitsAny future date
5105105105105100Mastercard (prepaid)Any 3 digitsAny future date
378282246310005American ExpressAny 4 digitsAny future date
371449635398431American ExpressAny 4 digitsAny future date
6011111111111117DiscoverAny 3 digitsAny future date
6011000990139424DiscoverAny 3 digitsAny future date
3056930009020004Diners ClubAny 3 digitsAny future date
36227206271667Diners Club (14 digit card)Any 3 digitsAny future date
3566002020360505JCBAny 3 digitsAny future date
6200000000000005UnionPayAny 3 digitsAny future date


Finally, we completed this basic Laravel with Stripe Payment Gateway. If you like my hard work, please share this tutorial with others. I would appreciate it.