Tutorial ~ Stripe Checkout Integration in PHP

15

Stripe is the most popular payment gateway to accept credit card payment in the web application. The Stripe API provides and secure and powerful solution to integrate the checkout system in the website. You can allow the user to make payment directly from your website using credit or debit cards.

The Stripe payment gateway can be easily integrated into the website to provide a user-friendly payment experience. Using Strip checkout API and PHP, you can allow the user to make payment with credit cards without page refresh (leaving the website). In this tutorial, we will show you how to integrate Stripe checkout payment gateway and collect payment online using JavaScript API and PHP.

In the example code, we will integrate custom process that allows you to trigger Checkout with any JavaScript event or HTML element. The following functionality will be implemented to demonstrate the Strip checkout process in PHP using JavaScript.

  • Display product details with a Buy button.
  • Generate Stripe token and passes it to a JavaScript callback.
  • Send the token to the server-side and charge the card.
  • Verify the payment and insert transaction data in the database.

Stripe Test API Keys

Before making the Stripe payment gateway live, the checkout process need to be tested. Stripe Test API Keys Data allows checking the checkout with test transaction. Follow the below steps to generate test API keys in the Stripe account.

  • Login to your Stripe account and go to the Developers » API keys page.
  • Under the TEST DATA section, you’ll see the Standard API keys (Publishable key and Secret key) are listed. To view the Secret key, click on Reveal test key token button.

Copy the Publishable key and Secret key for later use in the code.

Before getting started, take a look at the files structure of Stripe Checkout in PHP.

Create Database Table

To store the transaction data, a table needs to be created in the database. The following SQL creates an orders table in the MySQL database.

Database Configuration (dbConfig.php)

The dbConfig.php file is used to connect and select the database. Specify the database host ($dbHost), username ($dbUsername), password ($dbPassword), and name ($dbName) as per your database credentials.

Stripe Checkout (index.php)

Initially, the product details will be displayed with a Buy button. Once the button is clicked, Stripe dialog will open for checkout.

PHP Code:
At the beginning of the script, specify some useful variables for product and Stripe API.

  • Specify the product details.
  • Specify the Stripe API Publishable key.

JavaScript Code:
The jQuery library is not required to use Stripe, it’s used only for this example.

Include the Stripe checkout JS library.

The following JavaScript code handles the checkout process on single page without page refresh.

  • Stripe JS library sends the payment details to Stripe from the browser after pass the basic validation.
  • Stripe generates a token and returns it to Checkout.
  • Checkout stores the token in a hidden element named stripeToken.
  • Checkout posts the form data to the server-side code.
  • The server-side code uses the stripeToken to charge the card.

HTML Code:
The following code display product details on the web page with custom Stripe checkout button.

  • By clicking the Buy button, Stripe Checkout dialog will appear.
  • Card details are posted to the server-side code using jQuery and Ajax.
  • If checkout is successful, the order details will be shown, otherwise, an error message will be shown.

Stripe PHP Library

The Stripe PHP library is used to process the card payment. You don’t need to download this library, all the required library files are included in our source code. Also, you don’t need to use composer, this Stripe PHP Library will work without composer.

Process Card Payment (stripe_charge.php)

In this file, the posted card details are validated and the charge is processed using Stripe PHP library.

  • Get token, card and item details from the POST data.
  • Include the Stripe PHP library.
  • Set Stripe API Secret key which generated in Stripe Test API Data section.
  • Add customer to Stripe with the user’s email and Stripe token.
  • Specify item details and create a charge to the credit card or debit card.
  • Retrieve the charge details from the created charge.
  • If the payment is successful, the order and transaction details will be inserted into the MySQL database.
  • The charge response will be returned in JSON format to the success method of Ajax request.

Make Stripe Payment Gateway Live

Once you statisfied with Stripe checkout payment process, you can make the Stripe Payment gateway live to accept payment online from real customer.

  • Login to your Stripe account and go to the Developers » API keys page.
  • Collect the API keys (Publishable key and Secret key) from the Live Data section.
  • Replace the Test API keys (Publishable key and Secret key) with the Live API keys (Publishable key and Secret key) in the script.

Conclusion

If you want to accept payment on your web application, Stripe is the best option for credit card payment. You can allow the customer to make payment using their credit or debit card with Stripe checkout API. Our example code will help you to integrate single page checkout using Stripe and PHP.

Leave A Reply

Your email address will not be published.