Buy

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 README.md 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:

http://localhost:8000

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-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
    fos_user:
    ...
    from_email:
    address: '%mailer_sender_address%' # whatever email you want to use for this tutorial
    sender_name: '%mailer_sender_name%'

    Cheers!

  • 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...

    [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
    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.

    Thanks!

  • 2017-05-17 Victor Bocharsky

    Hey Danny,

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

    Cheers!

  • 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 "127.0.0.1" and added my MAMP MySQL database_port: "8889". Thanks for the help!

    Danny

  • 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 "127.0.0.1" in database_host parameter. Let me know if you still have this error.

    Cheers!

  • 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.

    Cheers!

  • 2017-05-12 DannyandAndrea Avery

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

    [Doctrine\DBAL\Exception\ConnectionException]
    An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory

    [Doctrine\DBAL\Driver\PDOException]
    SQLSTATE[HY000] [2002] No such file or directory

    [PDOException]
    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":

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

    Thanks!

  • 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.

    Cheers!

  • 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?