Laravel with Packages | Laravel 9 Stripe Payment Gateway Integration Tutorial

Step 1: Install Laravel 9

composer create-project laravel/laravel example-app

Step 2: Install stripe-PHP Package

STRIPE_KEY=pk_test_reFxwbsm9cdCKASdTfxAR
STRIPE_SECRET=sk_test_oQMFWteJiPd4wj4AtgApY

Step 4: Create Controller File

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Session;
use Stripe;

class StripePaymentController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripe()
{
return view('stripe');
}

/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripePost(Request $request)
{
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

Stripe\Charge::create ([
"amount" => 100 * 100,
"currency" => "usd",
"source" => $request->stripeToken,
"description" => "Test payment from raviyatechnical"
]);

Session::flash('success', 'Payment successful!');

return back();
}
}
/*** success response method.** @return \Illuminate\Http\Response*/public function stripePost(Request $request){Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));$customer = Stripe\Customer::create(array("address" => ["line1" => "Virani Chowk","postal_code" => "360001","city" => "Rajkot","state" => "GJ","country" => "IN",],"email" => "demo@gmail.com","name" => "Hardik Savani","source" => $request->stripeToken));Stripe\Charge::create (["amount" => 100 * 100,"currency" => "usd","customer" => $customer->id,"description" => "Test payment from raviyatechnical.","shipping" => ["name" => "Jenny Rosen","address" => ["line1" => "510 Townsend St","postal_code" => "98140","city" => "San Francisco","state" => "CA","country" => "US",],]]);Session::flash('success', 'Payment successful!');return back();}

Step 5: Create Routes

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\StripePaymentController;


Route::controller(StripePaymentController::class)->group(function(){
Route::get('stripe', 'stripe');
Route::post('stripe', 'stripePost')->name('stripe.post');
});

Step 6: Create Blade File

<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 - Stripe Payment Gateway Integration Example - raviyatechnical</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

<div class="container">

<h1>Laravel 9 - Stripe Payment Gateway Integration Example <br/> raviyatechnical</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default credit-card-box">
<div class="panel-heading display-table" >
<h3 class="panel-title" >Payment Details</h3>
</div>
<div class="panel-body">

@if (Session::has('success'))
<div class="alert alert-success text-center">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<p>{{ Session::get('success') }}</p>
</div>
@endif

<form
role="form"
action="{{ route('stripe.post') }}"
method="post"
class="require-validation"
data-cc-on-file="false"
data-stripe-publishable-key="{{ env('STRIPE_KEY') }}"
id="payment-form">
@csrf

<div class='form-row row'>
<div class='col-xs-12 form-group required'>
<label class='control-label'>Name on Card</label> <input
class='form-control' size='4' type='text'>
</div>
</div>

<div class='form-row row'>
<div class='col-xs-12 form-group card required'>
<label class='control-label'>Card Number</label> <input
autocomplete='off' class='form-control card-number' size='20'
type='text'>
</div>
</div>

<div class='form-row row'>
<div class='col-xs-12 col-md-4 form-group cvc required'>
<label class='control-label'>CVC</label> <input autocomplete='off'
class='form-control card-cvc' placeholder='ex. 311' size='4'
type='text'>
</div>
<div class='col-xs-12 col-md-4 form-group expiration required'>
<label class='control-label'>Expiration Month</label> <input
class='form-control card-expiry-month' placeholder='MM' size='2'
type='text'>
</div>
<div class='col-xs-12 col-md-4 form-group expiration required'>
<label class='control-label'>Expiration Year</label> <input
class='form-control card-expiry-year' placeholder='YYYY' size='4'
type='text'>
</div>
</div>

<div class='form-row row'>
<div class='col-md-12 error form-group hide'>
<div class='alert-danger alert'>Please correct the errors and try
again.</div>
</div>
</div>

<div class="row">
<div class="col-xs-12">
<button class="btn btn-primary btn-lg btn-block" type="submit">Pay Now ($100)</button>
</div>
</div>

</form>
</div>
</div>
</div>
</div>

</div>

</body>

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">

$(function() {

/*------------------------------------------
--------------------------------------------
Stripe Payment Code
--------------------------------------------
--------------------------------------------*/

var $form = $(".require-validation");

$('form.require-validation').bind('submit', function(e) {
var $form = $(".require-validation"),
inputSelector = ['input[type=email]', 'input[type=password]',
'input[type=text]', 'input[type=file]',
'textarea'].join(', '),
$inputs = $form.find('.required').find(inputSelector),
$errorMessage = $form.find('div.error'),
valid = true;
$errorMessage.addClass('hide');

$('.has-error').removeClass('has-error');
$inputs.each(function(i, el) {
var $input = $(el);
if ($input.val() === '') {
$input.parent().addClass('has-error');
$errorMessage.removeClass('hide');
e.preventDefault();
}
});

if (!$form.data('cc-on-file')) {
e.preventDefault();
Stripe.setPublishableKey($form.data('stripe-publishable-key'));
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
}

});

/*------------------------------------------
--------------------------------------------
Stripe Response Handler
--------------------------------------------
--------------------------------------------*/
function stripeResponseHandler(status, response) {
if (response.error) {
$('.error')
.removeClass('hide')
.find('.alert')
.text(response.error.message);
} else {
/* token contains id, last4, and card type */
var token = response['id'];

$form.find('input[type=text]').empty();
$form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
$form.get(0).submit();
}
}

});
</script>
</html>
php artisan serve
http://localhost:8000/stripe

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store