To integrate the official Stripe Payment Gateway in your Laravel application, you just have to follow these easy steps:
Installation
Starting by making a Laravel installation , and if you've already settled it then you can follow the next steps.
composer create-project laravel/laravel stripe-checkout
checkout.blade.php
In this step , you just have to set a button in your blade template in order redirect to stripe checkout page .
In this I will use this example :
<button id="checkout-button" type="button">Proceed to Checkout</button>
Installing Stripe-Php library via Composer
Following it with installing the stripe-php library via composer :
composer require stripe/stripe-php
Creating the Checkout Session
In the checkout blade template that we had settled ,it is required to create the checkout session in it :
<?php
require 'vendor/autoload.php';
// This is your test secret API key.
\Stripe\Stripe::setApiKey('sk_test_51JX1BnCwSqIoxRpKte18TcaGSEegx1UX1NKYaKGDCwFkbyDWeYiQW4cKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'T-shirt',
],
'unit_amount' => 2000,
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
]);
?>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test....') //Your Publishable key.
const btn = document.getElementById('checkout-button');
btn.addEventListener("click", function()
{
stripe.redirectToCheckout({
$sessionId: "<?php echo $session->id ?> "
})
}
</script>
Now run your application.
In case you had the require(vendor/autoload.php)
error :
Instead of <?php require 'vendor/autoload.php';
Add this
<?php require_once __DIR__. '/../../../vendor/autoload.php';
Now run your application !
Connecting MySQL Database and Stripe Checkout details
- In your VerifyCsrfToken.php add this following line:
protected $except = [ 'webhook'] ;
- Now install expose via composer:(https://expose.dev/docs/getting-started/installation)
composer global require beyondcode/expose
- Activate your tokken :
expose tokken xxxxxxxxxxx //Create an account in expose.dev and you will get the tokken
expose share http://localhost
- You will get a global link for your laravel application only temporary. The reason for this, is to link webhook events to an existing website. Copy your laravel application exposed link *Visit stripe dashboard(make sure you are in the **Test Mode),Then clicking on Webhooks, in the **Endpoint URL* add your website link followed by /webhook.Then select your events(charge.succeeded to see if the payment was successful)
- In your web.php:
Route::post('webhook/payment/succeeded', [App\Http\Controllers\StripePaymentController::class, 'stripePost']) ;
- In StripePaymentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Payment;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Stripe;
class StripePaymentController extends Controller
{
public function stripePost(Request $request)
{
if($request->type === "charge.succeeded"){
try{
Payment::create([
'stripe_id' => $request->data['object']['id'],
'amount' => $request->data['object']['amount'],
'email' => $request->data['object']['billing_details']['email'],
'name' => $request->data['object']['billing_details']['name']
]);
} catch (\Exception $e) {
return $e->getMessage();
}
}
}
}
- In Payment.php Model:
protected $guarded = [] ;
- In create-payments.php migration :
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->string('stripe_id'); // stripe payment id
$table->integer('amount');
$table->string('email');
$table->string('name');
$table->timestamps();
});
}
- Then Migrate the payments table .Then try the checkout and see the details after a successful checkout.
Final words
This was an easy tutorial , to integrate stripe payment gateway in your laravel application.
For more details watch my Youtube video to get the full view of the integration:https://www.youtube.com/watch?v=c1YgWrgsSQk&ab_channel=Mithrandir
Top comments (1)
Nice, I wanted to be able to select between different payment processors, so I added a mechanism to be able to select between multiple payment providers: Implementing Contextual Binding at Compile Time for Payment Processing in Laravel 11