How to Integrate Stripe Payment Gateway in PHP

0 171

Integrating payments with Stripe is easy. You would need a quick registration on, after which you can use the test version of the Stripe API until your web app is deployed and the live version afterwards. Stripe is responsible for processing and keeping clients’ credit/debit card data so no information of essence would be stored on your server and you would not have to comply with all the rules that come with storing credit/debit cards.

Let’s start integration:

Step 1.

Create account on

Step 2.

Now login to your account and go to Account Settings -> API Keys.

Step 3.

Edit charge.php file and replace with your Secret Key

Edit index.php file and replace with your Publishable Key

The test version does not entail actual transfer of funds and you can teste transfer if you have set up everything necessary to charge a customer by entering the following credit card number: 4242424242424242.

The only thing you would have to worry is Man-in-the-middle attacks and that is why Stripe highly recommends using HTTPS but no data about a card will be stored in your server.

First, we create a basic static web page and create a form that includes a script from Stripe (Checkout.js).



This script will create a button which when clicked would urge the users to enter an email, credit/debit card number and optionally choose to remember them.

We also set several variables.

Data-key would hold your publishable key, data-image would hold a link to your company’s logo, data-name would hold the company’s name and data-description – the description of the product being bought. Data-image, data-name and data-description will be shown when users click on the button created by Checkout.js and will make the modal feel like customers are in your site (they are optional but highly recommended).

Here is an image of the modal that this code displays:

When the users fill out the modal and click on “Pay …” they will be redirected to the action attribute of the form, which is “charge.php” in this case. Thus, in charge.php we will actually retrieve 15$ from the client’s card. When redirecting to the form’s action attribute you would have two variables that you could use -> $_POST[‘stripeEmail’] (you could use it to send an email to the customer after a purchase or whatever) and $_POST[‘stripeToken’](which is used for retrieving funds from the customer in the script displayed after form submission). Of course, this is considering our form’s method attribute is set to POST.

Note that the last two digits of data-amount are actually the cents. 1500 is equal to $15.00 and 15000 is equal to $150.00.

In the script responsible for changing, we load the Stripe library which is available in download code or it could be downloaded at: set up using Composer:

Afterwards we set our secret API key using the static method setApiKey of the Stripe class:

Afterwards, we actually charge the customer 15.00 bucks:

We set up a try and catch block. In the try block we attempt to charge the customer:

We tell Stripe the currency we want the 15.00 bucks to be in, we give the token received from the Checkout.js script when the user entered his data and provide a description of the charge.

Now, we are done and could execute any code that we want executed after a purchase has been made,we just place it below the $charge variable, it will be executed only if the payment has been successful. (no exceptions were thrown when charging)

Here is the whole charge.php file:

Here is what happens when a payment has been successful:

Leave A Reply

Your email address will not be published.