How to Integrate Stripe API in Laravel for Secure Transactions

16 views 9:34 am 0 Comments February 22, 2025

Integrating Stripe API in Laravel allows you to process secure transactions effortlessly. This guide will walk you through the setup and implementation of Stripe payments in a Laravel application.

Prerequisites

  • Laravel 8 or later installed
  • A Stripe account (Sign up here)
  • Composer installed
  • Basic knowledge of Laravel and PHP

Step 1: Install Stripe Package

Run the following command to install the Stripe PHP SDK:

composer require stripe/stripe-php

Step 2: Configure Stripe API Keys

Obtain your API keys from the Stripe Dashboard.

In your .env file, add:

STRIPE_KEY=your_publishable_key
STRIPE_SECRET=your_secret_key

Update config/services.php:

return [
    'stripe' => [
        'key' => env('STRIPE_KEY'),
        'secret' => env('STRIPE_SECRET'),
    ],
];

Step 3: Create Payment Controller

Generate a new controller:

php artisan make:controller PaymentController

In app/Http/Controllers/PaymentController.php, add:

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

class PaymentController extends Controller
{
    public function charge(Request $request)
    {
        Stripe::setApiKey(config('services.stripe.secret'));
        
        try {
            $charge = Charge::create([
                'amount' => $request->amount * 100,
                'currency' => 'usd',
                'source' => $request->stripeToken,
                'description' => 'Payment for Order #'.rand(),
            ]);
            
            return response()->json(['success' => true, 'message' => 'Payment successful!', 'charge' => $charge]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => $e->getMessage()]);
        }
    }
}

Step 4: Set Up Routes

In routes/web.php, add:

use App\Http\Controllers\PaymentController;

Route::post('/charge', [PaymentController::class, 'charge'])->name('payment.charge');

Step 5: Create Payment Form

In your Blade template (resources/views/payment.blade.php):

<form action="{{ route('payment.charge') }}" method="POST">
    @csrf
    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="{{ config('services.stripe.key') }}"
            data-amount="1000"
            data-name="Laravel Stripe Payment"
            data-description="Secure Payment Processing"
            data-currency="usd">
    </script>
</form>

Step 6: Test the Integration

Run your Laravel application:

php artisan serve

Go to your payment page and complete a test transaction using Stripe test card details (Find test cards here).

Conclusion

By following these steps, you have successfully integrated Stripe API into your Laravel application for secure payments. You can further enhance this by handling webhooks for automatic payment confirmation and refund processing.

For more details, check out the official Stripe documentation.

Leave a Reply

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