So you want to handle payment on the web? You brave, foolish soul :).

Nah, it's fine - these days, handling credit card payments is a blast. Especially with Stripe - an awesome service we've used on KnpU for years.

But let's be real: you're dealing with people's money, so don't muck it up! If you screw up or do something insecure, there could be real consequences. You will at least have an angry customer. So I guess this tutorial is all about accepting money and having happy customers.

So let's build a real-life, robust payment system so that when things go wrong - because they will - we fail gracefully, avoid surprises and make happy customers.

Code Along with Me. Do it!

As always, I beg you, I implore you, to code along with me! To do that, download the course code on this page, unzip it, and move into the start/ directory. That will give you the same code I have here.

This is a Symfony project, but we'll avoid going too deep into that stuff because I want to focus on Stripe. Inside, open the file and follow the setup details to get the project running. The last step is to open your favorite console app, move into the directory, and run:

php bin/console server:run

to start up the built-in web server.

Our Great Idea: Sheep Shear Club

But before you start collecting any money, you need to come up with that next, huge idea. And here at KnpUniversity, we're convinced we've uncovered the next tech unicorn.

Ready to find out what it is? Open your browser, and go to:


That's right: welcome to The Sheep Shear Club, your one-stop shop for artisanal shearing accessories for the most dapper sheep. Purchase cutting-edge individual products - like one of our After-Shear scents - or have products delivered directly to your farm with a monthly subscription.

Gosh, it's shear luck that we got to this idea first. Once we finish coding the checkout, our competition will be feeling sheepish.

But the site is pretty simple: we have a login page - the password is breakingbaad. After you login, you can add items to your cart and they'll show up on the checkout page. But notice, there is no checkout form yet. That's our job.

Getting to Know your Stripe Dashboard

The first step towards that is to sign up with a fancy new account on Stripe. Once you're in, you'll see this: your new e-commerce best friend: the Stripe dashboard.

There is a lot here, but right now I want you to notice that there are two environments: "test" and "live". These are like two totally separate databases full of orders, customers and more, and you can just switch between them to see your data.

Also, once you login, when you read the Stripe documentation, it will actually pre-fill your account's API keys into code examples.

Let's use those docs to put in our checkout form!

Leave a comment!

  • 2017-10-18 Diego Aguiar

    Hey RAJAONA Ywoumé

    If you want to install a specific version, there are many options, but the easiest one (at least to me) is like this:

     composer require vendor/package:version

    You can find more info here:

    Have a nice day

  • 2017-10-18 RAJAONA Ywoumé

    hi, my twig is versions : * v2.4.4 .... idont know how to fix it to 2.0 do i put in the composer of symfony ? where i do composer require twig/twig i have the same probleme... i've change in vender/symfony and do composer install but there is a same pbm, i've doing composer update but the same pbm... idont know how to do

  • 2017-10-18 Victor Bocharsky

    Hey RAJAONA Ywoumé ,

    What Twig version do you use? You can check it with "$ composer info twig/twig". You can try to upgrade dependencies, most probably it'll fix the problem. Have you tried it?


  • 2017-10-18 RAJAONA Ywoumé

    Do have resolve your pbm ?

  • 2017-10-18 RAJAONA Ywoumé

    Twig_Error_Runtime in ExtensionSet.php line 82:
    The "core" extension is not enabled.

    in the project i dont know ho to enable it

  • 2017-09-19 Diego Aguiar

    Hey Nina

    Can you tell me which version of Symfony / Twig you are installing? I believe this is a version problem between Symfony and Twig


  • 2017-09-19 Nina

    I have error
    Some parameters are missing. Please provide them.
    database_host (
    database_port (null):
    database_name (stripe):
    database_user (root):
    database_password (null):
    mailer_transport (smtp):
    mailer_host (
    mailer_user (null):
    mailer_password (null):
    secret (ThisTokenIsNotSoSecretChangeIt):
    > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
    > Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

    The "core" extension is not enabled.

    Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

    An error occurred when executing the ""cache:clear --no-warmup"" command:

    The "core" extension is not enabled.

    can you help me fix this, please?

  • 2017-06-02 Diego Aguiar

    I'm happy to help you, also, we have fixed our problem in our base code, if for any reason you have to download it again, you won't face this problem :)

    Que tengas un buen día!

  • 2017-05-31 Céline Ollagnier

    Good to "see" you gain Diego Aguiar
    I need to learn Symfony + Stripe so... Thanks for the congif it's working :)
    ¡ Gracias todavía por tu ayuda Diego! Merci encore pour ton aide Diego !

  • 2017-05-31 Diego Aguiar

    Hey Céline Ollagnier, it's good to see you again! :)

    Look's like FOSUserBundle has made some changes since this tutorial was released, but it's easy to fix :)
    Just add this to your config.yml:

    # config.yml
    address: '%mailer_sender_address%' # whatever email you want to use for this tutorial
    sender_name: '%mailer_sender_name%'


  • 2017-05-31 Céline Ollagnier

    Hi KNP' teachs!

    I've done the installation as said and can see the site mehhh. Composer told me that the lock file is outdated so I need updates after. Why not? But at the end of the update I've got...

    The child node "from_email" at path "fos_user" must be configured.

    And I can't access the site, my sheeps are in the wildddddd.
    So... can I work without the updates for the course? If I start again with the "old" files it will work but if you've got an answer with the updated one I'm also OK with it.


  • 2017-05-17 Victor Bocharsky

    Hey Danny,

    Yeah, non-standard MySQL port is another common mistake. Glad you got it working!


  • 2017-05-15 Danny Avery

    Hey Victor,

    I'm under a different account now. Ok, everything is working fine now. I replaced database_host: "localhost" with "" and added my MAMP MySQL database_port: "8889". Thanks for the help!


  • 2017-05-15 Victor Bocharsky

    Hey Danny,

    Do you have installed MySQL? Are that DB credentials in parameters.yml valid for your MySQL, i.e. your db user is root and its password is root too? Also, try to replace "localhost" with IP "" in database_host parameter. Let me know if you still have this error.


  • 2017-05-15 Victor Bocharsky

    Hey Danny,

    "Could not open input file: composer.phar" error means that you don't have downloaded composer.phar file in the directory where you run this command. If you already have Composer installed globally - it makes sense. So you can replace all the commands in our screencasts which starts with "php composer.phar ..." with simple "composer ...", so you just need to run "composer install" in this case.


  • 2017-05-12 DannyandAndrea Avery

    I'm also getting the following errors in the terminal:

    An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory

    SQLSTATE[HY000] [2002] No such file or directory

    SQLSTATE[HY000] [2002] No such file or directory

  • 2017-05-12 DannyandAndrea Avery

    When I run "php composer.phar install" I get the following error "Could not open input file: composer.phar". However, if I run "composer" I have it installed globally. So, I ran "composer install" in the directory. Not sure how to move forward. I have the following in my "parameters.yml":

    database_host: localhost
    database_port: null
    database_name: knpu_stripe
    database_user: root
    database_password: root
    mailer_transport: smtp
    mailer_user: null
    mailer_password: null
    secret: ThisTokenIsNotSoSecretChangeIt


  • 2017-03-22 Victor Bocharsky

    Actually, yes, exactly like a PHP native. But with Symfony you probably want to declare this cart as a service to use everywhere in your code base - it allows you always use a single instance of your cart and inject its service into other services. And you have to use Symfony's built-in `session` service to work with sessions inside your shopping cart service, i.e. you have to inject '@session' in it. I mean don't use low-level $_SESSION.

  • 2017-03-22 Henri Tompodung

    Thank you Victor Bocharsky ... So just like a PHP native?

  • 2017-03-15 Victor Bocharsky

    Hey Henri,

    Well, it's simple Symfony service, which works with session, i.e. you hold order items in session. For that you probably want to create a model for order items, which will hold information about products which user want to add to the cart. You don't have to store this order items in the DB yet, just hold them in session. You also need a controller with simple CRUD operations to be able to add/edit/delete orders in your shopping card. So when a user do checkout - you will convert stored order items into real products user want to buy and add them to an order entity which in turn should be persisted in DB. Also, you have to assign the current user to this order entity to know who was made this order.

    Probably you can take a look at Open Source SyliusOrderBundle which is a part of popular E-Commerce platform.


  • 2017-03-14 Henri Tompodung

    Hi Ryan..
    Don't you mind if you explain step by step how to create a shopping cart in Symfony?