Laravel 5.7 – Stripe Payment Gateway Integration Example
In this post we will give you information about Laravel 5.7 – Stripe Payment Gateway Integration Example. Hear we will give you detail about Laravel 5.7 – Stripe Payment Gateway Integration ExampleAnd how to use it also give you demo for it if it is necessary.
Today, i want to share with you how to integrate stripe payment gateway in laravel 5.7. we will use stripe/stripe-php composer library for stripe payment gateway. we will make $100 charge using stripe payment gateway.
Stripe is a very popular and secure internet payment gateway company which helps to accept payment worldwide. Stripe provide really nice development interface to start and you don’t have to pay subscription charges to learn it provides free developer account, before starting to code in your app.
I will give you example from scratch to implement stripe payment gateway in laravel 5.7 application. You just need to follow few step to get full example to pay.
Preview:
Step 1: Install Laravel 5.7
I am going to explain step by step from scratch so, we need to get fresh Laravel 5.7 application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install stripe-php Package
In this step we need to install stripe-php via the Composer package manager, so one your terminal and fire bellow command:
composer require stripe/stripe-php
Step 3: Set Stripe API Key and SECRET
Now, we need to set stripe key and secret. so first you can go on Stripe website and create development stripe account key and secret and add bellow:
.env
STRIPE_KEY=pk_test_reFxwbsm9cdCKASdTfxAR
STRIPE_SECRET=sk_test_oQMFWteJiPd4wj4AtgApY
Step 4: Create Routes
In this step, we will create two routes for get request and another for post request. So, let’s add new route on that file.
routes/api.php
<?php
Route::get('stripe', 'StripePaymentController@stripe');
Route::post('stripe', 'StripePaymentController@stripePost')->name('stripe.post');
Step 5: Create Controller File
in next step, now we have create new controller as StripePaymentController and write both method on it like as bellow, So let’s create both controller:
app/Http/Controllers/StripePaymentController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Session;
use Stripe;
class StripePaymentController extends Controller
{
/**
* success response method.
*
* @return IlluminateHttpResponse
*/
public function stripe()
{
return view('stripe');
}
/**
* success response method.
*
* @return IlluminateHttpResponse
*/
public function stripePost(Request $request)
{
StripeStripe::setApiKey(env('STRIPE_SECRET'));
StripeCharge::create ([
"amount" => 100 * 100,
"currency" => "usd",
"source" => $request->stripeToken,
"description" => "Test payment from onlinecode."
]);
Session::flash('success', 'Payment successful!');
return back();
}
}
Step 6: Create Blade File
In Last step, let’s create stripe.blade.php(resources/views/stripe.blade.php) for layout and write code of jquery to get token from stripe here and put following code:
resources/views/stripe.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5 - Stripe Payment Gateway Integration Example - ItSolutionStuff.com</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>
<style type="text/css">
.panel-title {
display: inline;
font-weight: bold;
}
.display-table {
display: table;
}
.display-tr {
display: table-row;
}
.display-td {
display: table-cell;
vertical-align: middle;
width: 61%;
}
</style>
</head>
<body>
<div >
<h1>Laravel 5 - Stripe Payment Gateway Integration Example <br/> ItSolutionStuff.com</h1>
<div >
<div >
<div >
<div >
<div >
<h3 >Payment Details</h3>
<div >
<img src="http://i76.imgup.net/accepted_c22e0.png">
</div>
</div>
</div>
<div >
@if (Session::has('success'))
<div >
<a href="#" data-dismiss="alert" aria-label="close">×</a>
<p>{{ Session::get('success') }}</p>
</div>
@endif
<form role="form" action="{{ route('stripe.post') }}" method="post"
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 >
<div >
<button 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() {
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);
}
});
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'];
// insert the token into the form so it gets submitted to the server
$form.find('input[type=text]').empty();
$form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
$form.get(0).submit();
}
}
});
</script>
</html>
Now you can check with following card details:
Name: Test
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 12
Expiration Year: 2024
I hope it can help you…
Hope this code and post will helped you for implement Laravel 5.7 – Stripe Payment Gateway Integration 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 us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs