Stripe is a powerful payment service designed for developers, but it can be difficult to use for newcomers even if the documentation is really good. Let’s make an example for a simple payment with a credit card on a Symfony app.
There are several ways to work with Stripe and achieve a single payment with a credit card, in this article we will tokenize the card details and then charge this card.
Set up the Stripe.js script
First things first, you need to import the Stripe.js library and instantiate the stripe object, which can easily be done with the following code:
Stripe recommends you not only to import the script on the payment pages of your site, but on every page, as it packages an advanced fraud functionality.
Create our payment form
Now that we have prepared the script, we need a form to let the user put his card details. We will use the Elements to create our form, so we can use the following:
For now if you try to display the page, all you have is an empty form without any input. We need to instantiate the Element in our script to fill the form with a Stripe form for the card:
We will now retrieve a set of inputs allowing the user to fill its card details. Those inputs come with a nice UX and error management so you can concentrate on more important things.
Create the token
Everything is ready, so now let’s get on with the source creation.
The first thing to do with our form is to create a token with the payment details.
The token creation ensures that you do not pass any sensitive data to your server.
Thanks to the card Element, this is easy, all we need to do is to call the method
Add some error management and use the promise sent back to obtain the following:
Now that we have our token, we need to send it to our server, and we can do it thanks to a hidden input:
Please note that the Element’s fields are not sent to the server with this code, meaning that you are operating in a PCI compliant way. This way, we can send all the data we had in our form with the token and your server never receives sensitive card details from your request (card number, expiracy date, cvc).
To use Stripe in our backend Application, we need to install the stripe-php library. All you need to do is to run the following command:
For starters, we need to save our API keys. Let’s add our key in the
app/config/parameters.yml file, like so:
This way, we can specify for each instance the keys to use, test keys for the test instance and the live keys for our production environment.
Now we can create our service. This service will need the secret key to initialize the Stripe library, so it will look like this:
And now we can register our service in the
app/config/services.yml config file:
Now that we have a functional service for the Stripe library, let’s make our first payment.
Now let’s implement the charge mechanism in our Stripe service:
With this simple code, we can process a payment of 42.00€ (passed in cents to the API), with the card corresponding to the token given. The description is a text that will appear in your Stripe dashboard and on the client bank receipt. Now all we have to do is use this method in our controller :
This action is pretty straightforward, we retrieve the token, charge the card, and display a message. Clean and simple.
But what if there is a problem and the card cannot be charged? The charge method of the Stripe library throws an exception that will help you manage those errors, so you can do something like this:
The message of the exception is readable by your customers (if they are english speakers).
If you want to provide internationalization, you can rely on the Stripe error code, that you can get with
So that’s pretty much all you need to know to process payments with Stripe. The front part is almost done by Stripe itself and the back is easy to use, and everything is secure, so it’s worth a try. If you need an example of this test case, we got a github repository to help you test that!
If you have other questions related to Stripe, don’t hesitate to comment down below, and we’ll try our best to answer quickly and if you are interested in other articles about Stripe be sure to tell us!