Introduction
In today’s world, integrating payment options into web applications is a necessity for businesses. Stripe is one of the leading payment gateways that helps businesses accept payments smoothly. Symfony, a robust PHP framework, is a preferred choice for many developers due to its flexibility and reusable components. Integrating Stripe with Symfony can be a daunting task, but with this step-by-step guide, you can easily add Stripe payment to your Symfony project.
Prerequisites
- PHP 7.3 or higher
- Composer
- Stripe Account
- Basic knowledge of Symfony framework
Step-by-Step Instruction
Step 1: Getting Stripe API Keys
Before integrating Stripe into Symfony, you need to set up a Stripe account. Go to the Stripe website, and sign up or log in to your existing account. Navigate to the Dashboard, where you can find your API keys. You’ll need these later to configure the Stripe client within your Symfony project.
Step 2: Installing Stripe PHP Library
Run the following command in the root of your Symfony project to install the official Stripe PHP library via Composer:
composer require stripe/stripe-php
Step 3: Configuring Stripe
Add the Stripe API keys to your Symfony environment. Open up the .env file in your project directory and add the following:
STRIPE_SECRET_KEY=your_secret_key
STRIPE_PUBLIC_KEY=your_public_key
You can now access these variables using the Symfony getenv()
method or the $_ENV
superglobal.
Step 4: Creating the PaymentController
Create a new controller class called PaymentController. Inside, you’ll create two routes — one to display the payment form and another to handle payment processing.
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
class PaymentController extends AbstractController
{
/**
* @Route("/payment", name="payment_form")
*/
public function showPaymentForm(): Response
{
return $this->render('payment/payment_form.html.twig', [
'stripe_public_key' => $_ENV['STRIPE_PUBLIC_KEY']
]);
}
/**
* @Route("/process_payment", name="process_payment")
*/
public function handlePayment(): Response
{
// Stripe logic goes here
return new Response('Payment successful.');
}
}
Step 5: Creating the Payment Form
In your Symfony templates directory, create a new Twig template named payment_form.html.twig
and paste the following code:
{# templates/payment/payment_form.html.twig #}
Submit Payment {# Include Stripe.js script #}
Step 6: Processing Payments
In your handlePayment()
method within the PaymentController, add the logic to process the payment with Stripe:
// Additional namespace imports
use Symfony\Component\HttpFoundation\Request;
use Stripe\Stripe;
// Add this method inside PaymentController class
public function handlePayment(Request $request): Response
{
Stripe::setApiKey($_ENV['STRIPE_SECRET_KEY']);
$token = $request->request->get('stripeToken');
try {
// Use Stripe's library to make requests...
$charge = \Stripe\Charge::create([
'amount' => 999, // Amount in cents
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token,
]);
// Check charge status
if($charge->status == 'succeeded') {
// Payment was successful
return new Response('Payment successful.');
} else {
// Payment failed
return new Response('Payment failed.');
}
} catch(\Exception $e) {
// Catch any errors for debugging
return new Response($e->getMessage());
}
}
Step 7: Finalizing and Testing the Integration
Now that you’ve added the server-side processing and client-side forms, you should thoroughly test your payment system. Stripe provides a set of test credit card numbers you can use to simulate different scenarios.
Final Words
Integrating Stripe into a Symfony project involves installing the Stripe PHP library, configuring your API keys, creating a payment controller and form, and then implementing the charge logic. Always remember to test your payment system before going live. With this guide, you’re now equipped to add Stripe payment to your Symfony app, enhancing your application’s e-commerce capabilities.