We are going to use Stripe as an example to show us how to do payment gateway integration in PHP. With Stripe’s payment gateway, you can easily accept credit cards directly on your website by integrating a checkout system and enabling payment.
To integrate Stripe payment gateway in your PHP-based website, you need to implement the following functionalities.
Test Stripe API Keys Data
Before getting your Stripe payment gateway integration live, you need to test it thoroughly. To test the credit card payment process, you need the TEST API Keys to generate in your Stripe account.
Collect the Publishable key and Secret key to later use in the script.
You probably need to look at the files structures before you proceeding any further:
Create Database Table
Next, you will need to create a table in the database to save the transaction details. What follows is an SQL creating the orders table in the MySQL database.
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`card_num` bigint(20) NOT NULL,
`card_cvc` int(5) NOT NULL,
`card_exp_month` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`card_exp_year` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`item_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`item_number` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`item_price` float(10,2) NOT NULL,
`item_price_currency` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'usd',
`paid_amount` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`paid_amount_currency` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`txn_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`payment_status` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Database Configuration (dbConfig.php)
You will need the dbConfig.php file to connect and select the database. Specify the database host ($dbHost), username ($dbUsername), password ($dbPassword), and name ($dbName) after your database credentials.
<?php
//Database credentials
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '*****';
$dbName = 'codexworld';
//Connect with the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
//Display error if failed to connect
if ($db->connect_errno) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
Stripe Checkout Form (index.php)
JavaScript
Include the Stripe JavaScript library to securely send sensitive information to Stripe directly from the browser.
<!-- Stripe JavaScript library -->
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
The jQuery library is not necessary to use Stripe; it is used only for this example.
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
In the JavaScript code, set your publishable API key that identifies your website to Stripe. The stripeResponseHandler() function creates a single-use token and inserts a token field in the payment form HTML.
<script type="text/javascript">
//set your publishable key
Stripe.setPublishableKey('Your_API_Publishable_Key');
//callback to handle the response from stripe
function stripeResponseHandler(status, response) {
if (response.error) {
//enable the submit button
$('#payBtn').removeAttr("disabled");
//display the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#paymentFrm");
//get token id
var token = response['id'];
//insert the token into the form
form$.append("<input type='hidden' name='stripeToken' value='"
+ token + "' />");
//submit form to the server
form$.get(0).submit();
}
}
$(document).ready(function() {
//on form submit
$("#paymentFrm").submit(function(event) {
//disable the submit button to prevent repeated clicks
$('#payBtn').attr("disabled", "disabled");
//create single-use token to charge the user
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
//submit from callback
return false;
});
});
</script>
HTML
The following HTML form collects the user information (name and email) and card details (Card Number, Expiration Date, and CVC No.). For further card payment processing, the form is submitted to the PHP script (submit.php).
<h1>Charge $55 with Stripe</h1>
<!-- display errors returned by createToken -->
<span class="payment-errors"></span>
<!-- stripe payment form -->
<form action="submit.php" method="POST" id="paymentFrm">
<p>
<label>Name</label>
<input type="text" name="name" size="50" />
</p>
<p>
<label>Email</label>
<input type="text" name="email" size="50" />
</p>
<p>
<label>Card Number</label>
<input type="text" name="card_num" size="20" autocomplete="off"
class="card-number" />
</p>
<p>
<label>CVC</label>
<input type="text" name="cvc" size="4" autocomplete="off" class="card-cvc" />
</p>
<p>
<label>Expiration (MM/YYYY)</label>
<input type="text" name="exp_month" size="2" class="card-expiry-month"/>
<span> / </span>
<input type="text" name="exp_year" size="4" class="card-expiry-year"/>
</p>
<button type="submit" id="payBtn">Submit Payment</button>
</form>
Stripe PHP Library
Next, we’re going to use the Stripe PHP library to process the card payment. The library is available here.
Validate and Process Payment (submit.php)
In this file, the submitted card details are validated, and the charge is processed using the Stripe PHP library.
//check whether stripe token is not empty
if(!empty($_POST['stripeToken'])){
//get token, card and user info from the form
$token = $_POST['stripeToken'];
$name = $_POST['name'];
$email = $_POST['email'];
$card_num = $_POST['card_num'];
$card_cvc = $_POST['cvc'];
$card_exp_month = $_POST['exp_month'];
$card_exp_year = $_POST['exp_year'];
//include Stripe PHP library
require_once('stripe-php/init.php');
//set api key
$stripe = array(
"secret_key" => "Your_API_Secret_Key",
"publishable_key" => "Your_API_Publishable_Key"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
//add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));
//item information
$itemName = "Premium Script CodexWorld";
$itemNumber = "PS123456";
$itemPrice = 55;
$currency = "usd";
$orderID = "SKA92712382139";
//charge a credit or a debit card
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $itemPrice,
'currency' => $currency,
'description' => $itemName,
'metadata' => array(
'order_id' => $orderID
)
));
//retrieve charge details
$chargeJson = $charge->jsonSerialize();
<h4>Order ID: {$last_insert_id}</h4>";
}else{
$statusMsg = "Transaction has been failed";
}
}else{
$statusMsg = "Transaction has been failed";
}
}else{
$statusMsg = "Form submission error.......";
}
Test Card Details
To test the payment process, you need test card details. Use any of the following test card numbers, a valid, future expiration date, and any random CVC number to test Stripe payment gateway integration in PHP.
Launch Stripe Payment Gateway Live
Once testing is done and the payment process is working properly, follow the steps below to have Stripe payment gateway go live.