Stripe is a popular payment gateway that allows you to accept payments online. Integrating Stripe in Laravel application is a straightforward process, and this step-by-step guide will walk you through the entire process.
Prerequisites
Before you start integrating Stripe with your Laravel application, you need to ensure that you have the following prerequisites:
- A working Laravel installation.
- A Stripe account.
- Composer installed on your local machine.
Step 1: Install Stripe Package
To begin, you need to install the Stripe PHP library in your Laravel application. You can install it using Composer by running the following command in your terminal:
composer require stripe/stripe-php
Step 2: Set Up Stripe API Keys
To use Stripe, you need to set up your API keys. You can find your API keys in the Stripe Dashboard. Navigate to the Developers > API Keys section, where you will find your Publishable Key and Secret Key.
Next, create a new .env
file in the root directory of your Laravel application and add the following lines to it:
STRIPE_KEY=your_publishable_keySTRIPE_SECRET=your_secret_key
Make sure to replace your_publishable_key
and your_secret_key
with your actual Stripe API keys.
Step 3: Create a Payment Form
To create a payment form, you need to create a new route and controller method in your Laravel application. For example, let’s create a route at /checkout
and a checkout
method in our PaymentController
.
// routes/web.phpRoute::get('/checkout', [PaymentController::class, 'checkout'])->name('checkout');
// app/Http/Controllers/PaymentController.phpuse Illuminate\Http\Request;class PaymentController extends Controller{ public function checkout(Request $request) { return view('checkout'); }}
Next, create a new Blade view at resources/views/checkout.blade.php
and add the following HTML code to it:
<form action="{{ route('checkout') }}" method="POST"> @csrf <input type="text" name="name" placeholder="Name"> <input type="email" name="email" placeholder="Email"> <input type="number" name="amount" placeholder="Amount"> <button type="submit">Pay Now</button></form>
This form will collect the user’s name, email, and payment amount.
Step 4: Process the Payment
To process the payment, you need to handle the form submission in your checkout
method. First, you need to validate the form data using Laravel’s built-in validation rules. Then, you can create a new charge using the Stripe PHP library.
// app/Http/Controllers/PaymentController.phpuse Illuminate\Http\Request;use Stripe\Charge;use Stripe\Customer;use Stripe\Stripe;class PaymentController extends Controller{ public function checkout(Request $request) { // Validate the form data $request->validate([ 'name' => 'required|string', 'email' => 'required|email', 'amount' => 'required|numeric|min:1', ]); // Set your secret key Stripe::setApiKey(config('services.stripe.secret')); // Create a new customer $customer = Customer::create([ 'email' => $request->email, 'name' => $request->name, ]); // Charge the customer $charge = Charge::create([ 'customer' => $customer->id, 'amount' => $request->amount * 100, 'currency' => 'usd', 'description' => 'Payment for ' . $request->name, ]); // Return a success message return 'Payment processed successfully!';}
In the code above, we first validate the form data using Laravel’s built-in validation rules. Then, we set our Stripe secret key and create a new customer object using the customer’s email and name. Finally, we create a new charge object using the customer’s ID, the payment amount (in cents), the currency, and a description.
Once the charge has been processed, we return a success message.
Step 5: Test the Payment
Now that you have integrated Stripe with your Laravel application, it’s time to test the payment. Start your Laravel server by running the following command in your terminal:
php artisan serve
Then, navigate to http://localhost:8000/checkout
in your web browser. Fill in the form with test data and submit it. If everything was set up correctly, you should see a success message.
Conclusion
In this tutorial, you learned how to integrate Stripe with your Laravel application. You learned how to install the Stripe PHP library, set up your API keys, create a payment form, process the payment, and test the payment. With this knowledge, you can now easily accept payments online using Stripe and Laravel.
Comments
Post a Comment