Stripe Payment Gateway Laravel

Laravel 9 Stripe Payment Gateway Integrate Example

Laravel 9 Stripe Payment Gateway Integrate Example

In this post we will give you Laravel 9 Stripe Payment Gateway Integrate Example, hear for Laravel 9 Stripe Payment Gateway Integrate Example we will give you details about it.

Stripe is a secure payment method and it’s work-based using API. It provides two environments for the user like a sandbox(Test) and live. so you can learn laravel 8 stripe integration using the following steps.

The Stripe payment gateway is a popular payment gateway method and it is easily used for our project. so many developers prefer that payment gateway method.

Step 1: Install Laravel 9

Step 2: Install Package

Step 3: Get and Set Stripe API Key and SECRET

Step 4: Create Route

Step 5: Create Controller

Step 6: Create Blade File

Step 1: Install Laravel 9

We are going to install laravel 9, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

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

Step 2: Install Package

Now, we are going to install the stripe package using the below command.

composer require stripe/stripe-php

Step 3: Get and Set Stripe API Key and SECRET

Now, we have to need the API Key and SECRET Key. so we will go to the official Stripe site and after then login get API Key and SECRET Key after then we will set in .env file.

STRIPE_KEY=pk_test_fgfl5fgfg5f8g5fg8fg5 
STRIPE_SECRET=sk_test_gfl5fgfg5f8g5fdcd

Step 4: Create Route

Add the following route code in the “routes/web.php” file.

<?php
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::get('/', function () {
    return view('welcome');
});
 
Route::get('stripe', 'StripeController@stripe');
Route::post('payment', 'StripeController@payStripe');
 
?>

Step 5: Create Controller

Now, We will create the controller using the below command and paste the below code in this controller.

php artisan make:controller StripeController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe;

class StripeController extends Controller
{
    public function stripe()
    {
        return view('stripe');
    }
    public function payStripe(Request $request)
    {
        $this->validate($request, [
            'card_no' => 'required',
            'expiry_month' => 'required',
            'expiry_year' => 'required',
            'cvv' => 'required',
        ]);

        $stripe = Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
        try {
            $response = \Stripe\Token::create(array(
                "card" => array(
                    "number"    => $request->input('card_no'),
                    "exp_month" => $request->input('expiry_month'),
                    "exp_year"  => $request->input('expiry_year'),
                    "cvc"       => $request->input('cvv')
                )));
            if (!isset($response['id'])) {
                return redirect()->route('addmoney.paymentstripe');
            }
            $charge = \Stripe\Charge::create([
                'card' => $response['id'],
                'currency' => 'USD',
                'amount' =>  100 * 100,
                'description' => 'wallet',
            ]);

            if($charge['status'] == 'succeeded') {
                return redirect('stripe')->with('success', 'Payment Success!');

            } else {
                return redirect('stripe')->with('error', 'something went to wrong.');
            }

        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }
}
?>

Step 6: Create Blade File

So finally, we will create the stripe.tpl file in “resources/views/” directory.

<html lang="en">
<head>
    <title>Laravel 9 Stripe Payment Gateway</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0https://www.onlinecode.org/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <div class="text-center">
                <h2>Pay for Event</h2>
                <br>
            </div>
        </div>
    </div>

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    @if(session()->has('error'))
        <div class="alert alert-danger">
            {{ session()->get('error') }}
        </div>
    @endif

    @if(session()->has('success'))
        <div class="alert alert-success">
            {{ session()->get('success') }}
        </div>
    @endif
    <div class="row">
        <div class="col-lg-3">

        </div>
        <div class="col-lg-6">
            <form  action="{{url('payment')}}"  data-cc-on-file="false" data-stripe-publishable-key="pk_test_fgfl5fgfg5f8g5fg8fg5" name="frmStripe" id="frmstripe" method="post">
                {{ csrf_field() }}
                <div class="row">
                    <div class="col-lg-12 form-group">
                        <label>Name on Card</label>
                        <input class="form-control" size="4" type="text">
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12 form-group">
                        <label>Number of Card</label>
                        <input autocomplete="off" class="form-control" size="20" type="text" name="card_no">
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-4 form-group">
                        <label>CVC</label>
                        <input autocomplete="off" class="form-control" placeholder="ex. 311" size="3" type="text" name="cvv">
                    </div>
                    <div class="col-lg-4 form-group">
                        <label>Expiration month</label>
                        <input class="form-control" placeholder="MM" size="2" type="text" name="expiry_month">
                    </div>
                    <div class="col-lg-4 form-group">
                        <label>Expiration year</label>
                        <input class="form-control" placeholder="YYYY" size="4" type="text" name="expiry_year">
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-control total btn btn-primary">
                            Total: <span class="amount">$35</span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12 form-group">
                        <button class="form-control btn btn-success submit-button" type="submit" style="margin-top: 10px;">Pay Now»</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>
</html>

Hope this code and post will helped you for implement Laravel 9 Stripe Payment Gateway Integrate Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs https://onlinecode.org