Stripe Payment Gateway Integrate In Laravel 5.7 App

How to Integrate Stripe Payment Gateway in Laravel 5.7 application. In this tutorial, we’ll discuss step by step how to intergrate stripe payment gateway in our laravel 5.7. Stripe is most popular payment gateway which is integrated in many websites, Stripe payment is easy to integrate and use. Stripe is very simple and most powerful and flexible tools.

Content

  • Install Laravel 5.7 Fresh Setup
  • Install stripe Package
  • Set Secret Credential
  • Generate Migration and Create Model
  • Make Route
  • Create Controller
  • Create Blade View file

Install Laravel 5.7 Fresh Project

We need to install Laravel 5.7 fresh application using below command, Open your command prompt and run the below command :

composer create-project --prefer-dist laravel/laravel StripeLaravel

Install Stripe package

composer require stripe/stripe-php

Once the installation of Stripe finishes, update the package.json with the following command :

"cartalyst/stripe-laravel": "9.0.*"

After update package.json file, we need to register provider and aliase. Go to the app/config/app.php and put the below lines here :


‘providers’ => [
 ..........
Cartalyst\Stripe\Laravel\StripeServiceProvider::class,
 
],
 
‘aliases’ => [
 ..........
‘Stripe’ => Cartalyst\Stripe\Laravel\Facades\Stripe::class,
 
],

Set Secret Credential

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

STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxxx 
STRIPE_SECRET=sk_test_xxxxxxxxxxxxxx

Make Route

 <?php 

Route::get('stripe', 'StripeController@stripe');
Route::post('payment', 'StripeController@payStripe');

Create Contoller

Create the controller name StripeController using the below command.

php artisan make:controller StripeController

Go to app/http/Controller/StripeController and put the below code :

<?php


namespace App\Http\Controllers;

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

class StripeController extends Controller
{


/**
 * Redirect the user to the Payment Gateway.
 *
 * @return Response
 */
public function stripe()
{
    return view('stripe');
}


/**
 * Redirect the user to the Payment Gateway.
 *
 * @return Response
 */
public function payStripe(Request $request)
{
    $this->validate($request, [
        'card_no' => 'required',
        'expiry_month' => 'required',
        'expiry_year' => 'required',
        'cvv' => 'required',
    ]);

$stripe = Stripe::make('sk_test_K0ThsZWtj2g1AorbBQDcV2h5');
try {
    $token = $stripe->tokens()->create([
        'card' => [
            'number'    => $request->get('card_no'),
            'exp_month' => $request->get('expiry_month'),
            'exp_year'  => $request->get('expiry_year'),
            'cvc'       => $request->get('cvv'),
        ],
    ]);
    if (!isset($token['id'])) {
        return Redirect::to('strips')->with('Token is not generate correct');
    }
    $charge = $stripe->charges()->create([
        'card' => $token['id'],
        'currency' => 'USD',
        'amount'   => 20,
        'description' => 'Register Event',
    ]);
    $charge = Charge::create(array(
        'amount' => 20,
        "source" => $token,
        'currency' => 'usd'
    ));

     return 'Payment Success';
	} catch (\Exception $ex) {
	    return $ex->getMessage();
	}

  }
}

Create Blade View file

We need to create blade views file, Go to app/resources/views/ and create one file name 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>

Start Development Server

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

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/stripe

Or direct hit in your browser

http://localhost/StripeLaravel/public/stripe

Testing Card Credential

Card No : 4242424242424242
Month : any future month
Year : any future Year
CVV : 123

Conclusion

In this tutorial, We have successfully integrate stripe payment gateway in laravel 5.7 Application. Our examples run quickly.

You may like

  1. Laravel 6 Razorpay Payment Gateway Integration Tutorial E.g
  2. Laravel 6 Instamojo Payment Gateway Integration Example
  3. Angular 8 – Stripe Payment Gateway Example

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *