how to integrate stripe payment gateway in php?

Today, We want to share with you how to integrate stripe payment gateway in php.In this post we will show you payment gateway in php and mysql, hear for Steps to integrate the Stripe payment gateway we will give you demo and example for implement.In this post, we will learn about Stripe Payment gateway integration in PHP with DEMO Examples with an example.

Stripe Payment Gateway Integration in PHP

first of all make an account in Stripe and generate the required keys. you can Generate Stripe API Keys and then GO to Go to > Developers > API Keys

Create Stripe Payment Form

Let’s make a basic html from to receive payment or make the transaction. You need from fields for card number, card expiry month, card expiry year and CVV. You need to add data-stripe tag to each from fields to our JavaScript can read the form value and validate the form without reloading the page.

Date stripe value for

  • – Card Number is data-stripe=”number”
  • – Expiry Month is data-stripe=”exp_month”
  • – Expiry Year is data-stripe=”exp_year”
Read Also:  die in php

Example 1:

<form action="" method="post" name="cardgateway" id="gateway-form">
<div class="dsp form-group">
<label class="form-label" for="name">Card Holder Name</label>
<input name="holdername" id="name" class="form-input" type="text"  required />
</div>               
<div class="dsp form-group">
<label class="form-label" for="email">Email</label>
<input name="email" id="email" class="form-input" type="email" required />
</div>         
<div class="dsp form-group">
<label class="form-label" for="card">Card Number</label>
<input name="cardnumber" id="card" class="form-input" type="text" maxlength="16" data-stripe="number" required />
</div>
<div class="dsp form-group2">
<label class="form-label" for="password">Expiry Month / Year & CVV</label>
<select name="month" id="month" class="form-input2" data-stripe="exp_month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>     
<select name="year" id="year" class="form-input2" data-stripe="exp_year">
<option value="19">2019</option>
<option value="20">2020</option>
<option value="21">2021</option>
<option value="22">2022</option>
<option value="23">2023</option>
<option value="24">2024</option>
<option value="25">2025</option>
<option value="26">2026</option>
<option value="27">2027</option>
<option value="28">2028</option>
<option value="29">2029</option>
<option value="30">2030</option>
</select>
<input name="cvv" id="cvv" class="form-input2" type="text" placeholder="CVV" data-stripe="cvc" required />
</div>
<div class="dsp form-group">
<div class="gateway-errors"></div>
</div>
<div class="button-style">
<button class="button login submit">Paynow ($1.00)</button>
</div>
</form>

Validate card through Stripe JavaScript library

We need to include JavaScript file provide by stripe to validate card information.

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('YOUR PUBLIC KEY HERE');
$(function() {
   var $form = $('#gateway-form');
   $form.submit(function(event) {

   $form.find('.submit').prop('disabled', true);
    

   Stripe.card.createToken($form, stripeResponseHandler);
    

   return false;
   });
 });
function stripeResponseHandler(status, response) {

  var $form = $('#gateway-form');
    
  if (response.error) { // Problem!

    $form.find('.gateway-errors').text(response.error.message);
    $form.find('.submit').prop('disabled', false); // Re-enable submission
  }else { // Token was created! here

    var token = response.id;

    $form.append($('<input type="hidden" name="stripeToken">').val(token));

    $form.get(0).submit();
  }
};
</script>

Make a payment now

In this demo i made the transaction amount fixed value as $1.00, you can create it any value, But keep it in your mind that Stripe only accept value in some of the dollar cents. If you want a transaction of $1.00 then $amount = 100; If you want a transaction of $1500 then $amount=150000; Also you required to define the currency and USD or CAD etc.

Read Also:  how to get serialized array data in php?

<?php
error_reporting(0);
session_start();
require 'stripe/Stripe.php';
$publishable_key     = "YOUR Stripe Payment Gateway KEY HERE";
$secret_key            = "YOUR Stripe Payment Gateway KEY HERE";

if(isset($_POST['stripeToken'])){
Stripe::setApiKey($secret_key);
$description     = "Invoice #".rand(99999,999999999);
$amount_cents     = 100;
$tokenid        = $_POST['stripeToken'];

	try {
		$charge         = Stripe_Charge::create(array( 
		"amount"         => $amount_cents,
		"currency"         => "usd",
		"source"         => $tokenid,
		"description"     => $description)              
		);
		        
		$id            = $charge['id'];
		$amount     = $charge['amount'];
		$balance_transaction = $charge['balance_transaction'];
		$currency     = $charge['currency'];
		$status     = $charge['status'];
		$date     = date("Y-m-d H:i:s");
		        
		$result = "succeeded";
		        

		header("location:index.php?id=".$id);
		exit;
	}catch(Stripe_CardError $e) {            
		$error = $e->getMessage();
		$result = "declined";
	}
}
?>

Test Card Details

You can use any list of all the card from the bellow as well as for CVV any 3 digit number as well as Expiry date any month with year from the current active month and year.

  • 4242424242424242 => Visa card information
  • 4000056655665556 => Visa (debit) card information
  • 5555555555554444 => Mastercard card information
  • 5200828282828210 => Mastercard (debit) card information
  • 378282246310005 => American Express card information
  • 6011111111111117 => Discover card information

I hope you get an idea about debit card payment gateway integration in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.