Just make sure you got the directory name right, you might be having a different release than “stripe-php-4.9.1”.

Great, now if we navigate to your project in the browser http://127.0.0.1/stripe-payments/ you should probably see a blank screen. If so, then you’re at it, otherwise if you get a 500 error page or something then make sure you have the correct files required. Debugging is always necessary so let’s add some code to the top of our index.php and charge.php files ( this is totally optional and you should not leave debugging mode on when in production):

Now if you have any errors they’ll be printed wide and clear in the browser, in order for you to debug them properly.

Basic configuration

Let’s now place some constants into the top of our index.php in order to configure the process properly.

STRIPE_PUBLIC_KEY: Your API public key which can be obtained from the dashboard dashboard.stripe.com

STRIPE_SECRET_KEY: Your API secret, can be obtained from the same dashboard

STRIPE_PRICE: The price we are charging here. It should be an integer and in cents (pennies) so multiply it by 100 ($50 becomes 5000 and £0.5 becomes 50)

CURRENCY_CODE: The ISO code for the currency you are charging for. USD for the US dollar $, EUR for euro €, GBP for £ the British pound, you can find a list of currency codes and their symbols online.

USER_EMAIL: Optional, if you already have a user signed in or you only allow logged in users to make payments, then place here the current user’s email address otherwise the user will be free to choose an email address in order to make a payment.

So we’ll add them as constants into our index.php file (from now on, index.php will handle every request, even calling charge.php when form data has been passed):

Using Stripe Checkout tool

What’s amazing about Stripe is it saves you all the trouble of coding the payment forms, validating credit card numbers and form fields, and everything! Stripe Checkout gives you the opportunity to place a simple button which the user can click to get a neat popup where they fill in their card information to make the payment. Once the information have been submitted successfully, Stripe Checkout will submit then the form to your server (charge.php in this tutorial) and from there you’ll use Stripe API to make actual payments (because no payment has been made yet).

We can then with no further due add a form with their checkout JS to our index page and actually start displaying content!

Go ahead and click the pay button, you’ll see the popup containing the credit card credentials fields, and the icon, title, description and button text along with other generic data can be customized in the script tag within the form, for instance loose the cat and add a real icon image to your card by specifying the image URL through data-image

data-image="http://lorempixel.com/150/100/cats/"

Cool! To do some test payment, Stripe has some sandbox card which number is 4242 4242 4242 4242, the expiration can be any future date in format MM/YY (10/19 for October 2019), and CVC can be a random 3 digits number so 123.

The remember button will basically let Stripe remember the card details for the current site and fill them automatically upon the next request securly, this is not something we’d be worried about for the moment, nor collecting the card details as well, we don’t need them.

We’re not going to submit the card yet, let’s make sure charge.php file is involved when doing so, so as to verify payments:

Registering the customer and charging them

So far so good. Now if you submit the payment for with dummy details:

Card number: 4242 4242 4242 4242

Expiration: (some future month)/(some future year): 10/19

CVC: 123

We’ll now have checkout.js submit the form automatically and we will see an array of data passed, among them stripeToken which will be the token Stripe PHP SDK requires to create the customer and make the payment:

Now usually this is the step before las. It can either be successful creating a customer or throw a warning of some exception, so we’ll wrap it in try/catch block to make sure we handle the request properly.

Now usually this should throw errors when something is not well, so we will again use the try/catch bloc and not worry about the different exceptions that Stripe throws because in the end all of them are pointing to the fact that an error occured and no payment has been made.

In the catch bloc you might want to log the request, send you an email as the site admin, and tell the user to hang in there until you inspect the issue. This is rare to happen unless of course we have a mis-configured something or the user uses the same form data many times etc. (The code comes after)

We can then charge the user and show them custom success notices, but we are also supposed to be doing this with care and make sure their membership is upgraded as paid or some other action is taken, a good thing worth mentioning is sending them a custom notice to their inbox to let them know and keep track of this transaction.

And of course, if you visit your Stripe dashboard you’ll notice you have made some earnings in the transactions graph. I have $2 in the screenshot because I actually forgot to take one until I made already 2 transactions for this tutorial.

Wrapping up

To wrap up, here’s the final code for both files index.php and charge.php

Going Live

Before going live, in addition to disabling debug mode (removing the error_reporting and ini_set to the top of index.php file), you are required to have a valid SSL certificate on your site. It makes sense to enable SSL when you have a user authentication site for security, now it makes even more sense to secure your payments requests.

If you are on shared hosting and cannot afford an SSL, you can always use CloudFlare’s free SSL which is included along with the other free services they have in their free plan. Otherwise if you can afford one, I recommend Godaddy or NameCheap (that’s my referral link).

If you already have a dedicated server or a VPS then problem solved with LetsEncrypt’s powerful and free SSL certificates.