Symfony 5 – Integrating Stripe Payment Gateway Tutorial

Symfony 5 – Integrating Stripe Payment Gateway Tutorial

Avatar photoPosted by

Today, I will be showing you how to integrate Stripe payment gateway in Symfony 5. Payment gateway is one of the most important part of a e-commerce site or any apps that requires payment processing. One of the most popular payment processing platform is stripe. Stripe has been one of the most chosen payment platform for its simple integration and fast account setup.

I will be showing how to create a Charge in Stripe API using a token. We will now start the integration of Stripe to Symfony 5.

Step 1: Install Symfony 5

First, select a folder that you want Symfony to be installed then execute this command on Terminal or CMD to install:

Install via composer:

composer create-project symfony/website-skeleton symfony-5-stripe-payment-gateway

Install via Symfony CLI:

symfony new symfony-5-stripe-payment-gateway --full

Step 2: Set Database Configuration

We must configure our database to avoid errors. Open the .env file and set database configuration. We will be using MySQL on this tutorial. Uncomment the DATABASE_URL variable for MySQL and updates its configs. Make sure you commented out the other DATABASE_URL variables.


# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
# Real environment variables win over .env files.
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
###> symfony/framework-bundle ###
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:db_password@"
###< doctrine/doctrine-bundle ###

Step 3: Install Stripe Package

We will now install the stripe package.

Execute this command:

composer require stripe/stripe-php

Step 4: Set Up Stripe Configurations

Add stripe api keys on the .env file. the API keys are found on your Stripe Dashboard under Developer API keys.



You can find you API keys on you stripe account dashboard, just navigate to Developers > API Keys.

Step 5: Create Controller

Run this command to create a controller:

php bin/console make:controller Stripe

After creating a controller, add these line of codes to the src\Controller\StripeController.php file:



namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Stripe;

class StripeController extends AbstractController
     * @Route("/stripe", name="stripe")
    public function index(): Response
        return $this->render('stripe/index.html.twig', [
            'stripe_key' => $_ENV["STRIPE_KEY"],

     * @Route("/stripe/create-charge", name="stripe_charge", methods={"POST"})
    public function createCharge(Request $request)
        Stripe\Charge::create ([
                "amount" => 5 * 100,
                "currency" => "usd",
                "source" => $request->request->get('stripeToken'),
                "description" => "Binaryboxtuts Payment Test"
            'Payment Successful!'
        return $this->redirectToRoute('stripe', [], Response::HTTP_SEE_OTHER);

Step 6: Create View File

Let’s update the view file that was also created when we create the controller:


<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Stripe Payment Gateway</title>
    <link href="" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-4">
                <div class="card">
                    <div class="card-body">
                        {% for message in app.flashes('success') %}
                                style="color: green;
                                    border: 2px green solid;
                                    text-align: center;
                                    padding: 5px;margin-bottom: 10px;">
                                {{ message }}
                        {% endfor %}

                        <form id='checkout-form' method='post' action="{{ path('stripe_charge') }}">             
                            <input type='hidden' name='stripeToken' id='stripe-token-id'>                              
                            <label for="card-element" class="mb-5">Checkout Forms</label>
                            <div id="card-element" class="form-control" ></div>
                                class="btn btn-success mt-3"
                                style="margin-top: 20px; width: 100%;padding: 7px;"
                                onclick="createToken()">PAY $5
    <script src="" ></script>
        var stripe = Stripe("{{stripe_key}}");
        var elements = stripe.elements();
        var cardElement = elements.create('card');
        function createToken() {
            document.getElementById("pay-btn").disabled = true;
            stripe.createToken(cardElement).then(function(result) {
                if(typeof result.error != 'undefined') {
                    document.getElementById("pay-btn").disabled = false;
                // creating token success
                if(typeof result.token != 'undefined') {
                    document.getElementById("stripe-token-id").value =;

Step 7: Run The Application

After finishing the steps above, you can now run your application by executing the command below:

symfony server:start

Open this URL:



Before Payment Transaction:

Symfony 5 – Integrating Stripe Payment Gateway Tutorial

After Payment Transaction:

Symfony 5 – Integrating Stripe Payment Gateway Tutorial