In this article, we will see how to quickly and easily implement a basic payment gateway with Stripe using PHP.

Stripe is the best software platform to run a business on the Internet. It operates with billions of dollars annually from the most innovative business models around the world. This is the official description.

This tool makes internet payment processing for online businesses fast and secure.

Stripe has a very powerful payment API that powers commerce for online businesses of any size, including fraud prevention and subscription management. It is ideal for accepting and processing payments online as an easy-to-use commerce solution.

The fastest way to start earning with Stripe is to use Stripe Checkout, a direct checkout flow designed to drive conversion, whether it’s to sell unique products or subscriptions. It integrates in minutes and allows you to safely accept card payments.

Stripe Checkout quickly integrates into your PHP– based website to provide users with an optimized, mobile-ready checkout experience.

Although most content managers and e-commerce platforms already have a plugin or module with this implementation, it can always be interesting to make a custom payment gateway without using any content manager or e-commerce platform. If this is what you need, continue with this tutorial.

In Stripe it is possible to use the documentation to use Checkout and PHP.

In the working example, we are showing a button that when pressed opens a modal window that allows you to indicate an email, a card number, the expiration date, and the security code.

The working example is a page that allows you to send a donation of € 1 to thank this tutorial that I hope you find useful.


The minimum development environment to work with Stripe is:

  • PHP> = 5.3.3
  • Library Stripe for PHP
  • Account Stripe is free.

Payment gateway: Stripe installation

The PHP library to use Stripe on your website can be installed via Composer by executing the following line in your computer’s terminal, previously placing yourself in the directory where your project is:

composer require stripe / stripe - php

If you don’t have it yet, before executing this line on the command line, download and install the Composer program on your computer that you can find on the official website.

After executing this statement via Composer, something should appear on the console:

Where it is indicating that the necessary package to use payments through Stripe is already available to use in the path of your project.

Payment gateway: Configuration data

In the root of your project create the file config.php   and add the following code:

<? php 
require_once ( 'vendor / autoload.php' ) ;
$ stripe  =  array ( 
    'secret_key'  =>  'YOUR SECRET KEY HERE' , 
    'publishable_key'  =>  'YOUR PUBLIC KEY HERE' , 
) ;
\ Stripe \ Stripe :: setApiKey ( $ stripe [ 'secret_key' ] ) ; 

The first line refers to the Stripe library for PHP that you have previously installed through Composer.

Next, we create an array with the secret key and the public key of your Stripe account.

Change the secret and public keys to those of your Stripe account. You can find it in the control panel of your Stripe account, just in Developers – API Keys.


Inside the body of the page or inside the <body> tag, wherever you want you can add the form for Stripe Checkout . You can basically add something like the following:

<? php  require_once ( 'config.php' ) ;  ?>
<form action = "charge.php" method = "post">
    <script src = "" class = "stripe-button"
        data-key = " <? php  echo  $ stripe [ 'publishable_key' ] ;  ?> "
        data-description = "Send a symbolic donation of € 1"
        data-amount = "100"
        data-currency = "eur"
        data-locale = "es"> </script>

Where we are including the config.php file that allows you to connect to your Stripe account.

Next, we are putting together the payment form through Stripe that Checkout uses. A built-in HTML form that will take care of data validation, error handling, and secure submission of credit card numbers to Stripe.

If you look a bit at the code, you can see that the <script> tag contains some special attributes that perhaps you have never seen before. They are like parameters that you can send to the Stripe platform to generate the payment with dynamic data.

In the case of the working example, we are passing the API public key, description, amount, currency type, and language.

Payment processing with PHP

As you can see in the action attribute of the form, the form data is sent to the server.

The charge.php file is the file that receives the form data. This file may contain the following code:

<? php 
require_once ( 'config.php' ) ;
$ token  =  $ _POST [ 'stripeToken' ] ; 
$ email  =  $ _POST [ 'stripeEmail' ] ;
$ customer  = \ Stripe \ Customer :: create ( [ 
  'email'  =>  $ email , 
  'source'   =>  $ token , 
] ) ;
$ charge  = \ Stripe \ Charge :: create ( [ 
  'customer'  =>  $ customer -> id , 
  'amount'    =>  100 , 
  'currency'  =>  'eur' , 
] ) ;
echo  '<h1> Successfully charged € 1! </h1>' ; 

Where we include the config.php file to connect with the Stripe account. We receive the token and the email of the form to create the client and the payment. That simple

See the Stripe Checkout PHP documentation for more information.


The payment appears reflected in the Stripe account:

Leave a Reply

Your email address will not be published. Required fields are marked *