Today, We want to share with you stripe payment gateway integration in php.
In this post we will show you stripe login, stripe integration, hear for stripe custom checkout example we will give you demo and example for implement.
In this post, we will learn about php – How to generate token in stripe payment gateway with an example.
stripe payment gateway integration in php
There are the Following The simple About stripe payment gateway integration in php Full Information With Example and source code.
As I will cover this Post with live Working example to develop Stripe PHP simple example, so the some major files and Directory structures for this example is following below as well as more example Like stripe login, stripe integration, stripe sage integration, stripe custom checkout example, stripe checkout custom fields, stripe checkout demo, stripe checkout wordpress.
- config.php
- DBController.php
- index.php
- style.css
Stripe Payment Gateway Integration in PHP
Stripe Simple means Online Getway payment processing for Data internet businesses
Step 1 : PHP Stripe Payment Config File
config.php
<?php define("STRIPE_SECRET_KEY", "YOUR API PUT HERE SECRET KEY"); define("STRIPE_PUBLISHABLE_KEY", "YOUR API PUT Here KEY"); ?>
Step 2 : Payment Database Structure
— Table structure for table `tbl_stripe_payments`
CREATE TABLE `tbl_stripe_payments` ( `id` int(11) NOT NULL, `email` varchar(255) NOT NULL, `item_number` varchar(255) NOT NULL, `amount` double(10,2) NOT NULL, `currency_code` varchar(55) NOT NULL, `txn_id` varchar(255) NOT NULL, `payment_status` varchar(255) NOT NULL, `payment_response` text NOT NULL, `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Indexes for table `tbl_stripe_payments` -- ALTER TABLE `tbl_stripe_payments` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for table `tbl_stripe_payments` -- ALTER TABLE `tbl_stripe_payments` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; COMMIT;
Step 3 : Get Stripe Payment API Keys
Make a Stripe account PHP devloper Stripe account and fetch API keys
HTML Stripe Payment Form
index.php
This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php.
<?php require_once "config.php"; ?> <html> <head> <link href="style.css" rel="stylesheet" type="text/css" / > <title>Client-Side Stripe Payment Form Card Validation and Response & Request Handler with Stripe JavaScript Library</title> </head> <body> <form id="livePaymentFrmStripe" action="sendPayment.php" method="post"> <div class="infinity-row"> <label>Enetr Card Holder Name</label> <span id="card-holder-name-details" class="details"></span><br> <input type="text" id="name" name="name" class="lblInpBox"> </div> <div class="infinity-row"> <label>Email</label> <span id="email-details" class="details"></span><br> <input type="text" id="email" name="email" class="lblInpBox"> </div> <div class="infinity-row"> <label>Card Number</label> <span id="card-number-details" class="details"></span><br> <input type="text" id="card-number" name="card-number" class="lblInpBox"> </div> <div class="infinity-row"> <div class="myref-row column-right"> <label>Expiry Month / Year</label> <span id="userEmail-details" class="details"></span><br> <select name="month" id="month" class="liveCustomSelect"> <option value="08">08</option> <option value="09">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> <select name="year" id="year" class="liveCustomSelect"> <option value="18">2118</option> <option value="19">2119</option> <option value="20">2120</option> <option value="21">2121</option> <option value="22">2122</option> <option value="23">2123</option> <option value="24">2124</option> <option value="25">2125</option> <option value="26">2126</option> <option value="27">2127</option> <option value="28">2128</option> <option value="29">2129</option> <option value="30">2130</option> </select> </div> <div class="myref-row cvv-box"> <label>CVC</label> <span id="cvv-details" class="details"></span><br> <input type="text" name="cvc" id="cvc" class="lblInpBox lbl-cvv-card"> </div> </div> <div> <input type="submit" name="pay_now" value="Submit" id="submit-btn" class="lblBtnCard" onClick="stripePay(event);"> </div> <div id="preview-error-status"></div> <input type='hidden' name='amount' value='0.5'> <input type='hidden' name='currency_code' value='USD'> <input type='hidden' name='item_name' value='Test Product'> <input type='hidden' name='item_number' value='PHPPOTEG#1'> </form> <div class="display-none" id="data_loader"> <img alt="data_loader" src="https://www.pakainfo.com/stripe-payment-gateway-integration-php/LoaderIcon.gif"> </div> <script type="text/javascript" src="https://js.stripe.com/v2/"></script> <script src="vendor/jquery/jquery-3.2.1.min.js" type="text/javascript"></script> <script> function checkCardValid () { var valid = true; var name = $('#name').val(); var email = $('#email').val(); var YourCardNo = $('#card-number').val(); var month = $('#month').val(); var year = $('#year').val(); var cvc = $('#cvc').val(); $("#preview-error-status").html("").hide(); if (name.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } if (email.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } if (YourCardNo.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } if (month.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } if (year.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } if (cvc.trim() == "") { $("#preview-error-status").html("All Fields are required").show(); valid = false; } return valid; } //set your publishable key Stripe.setPublishableKey("<?php echo STRIPE_PUBLISHABLE_KEY; ?>"); //callback to handle the response from stripe function liveResStripeHandler(status, response) { if (response.error) { //enable the submit button $("#submit-btn").html("").show(); $( "#data_loader" ).addClass("display-none"); //display the errors on the form $("#preview-error-status").html(response.error.message).show(); } else { //get token id var token = response['id']; //insert the token into the form $("#livePaymentFrmStripe").append("<input type='hidden' name='token' value='" + token + "' />"); //submit form to the server $("#livePaymentFrmStripe").submit(); } } function stripePay(e) { e.preventDefault(); var valid = checkCardValid(); if(valid == true) { $("#submit-btn").html("").hide(); $( "#data_loader" ).removeClass("display-none"); Stripe.createToken({ number: $('#card-number').val(), cvc: $('#cvc').val(), exp_month: $('#month').val(), exp_year: $('#year').val() }, liveResStripeHandler); return false; } } </script> </body> </html>
Step 4 : Stripe Payment DBController
DBController.php
<?php class DBController { private $host = "localhost"; private $user = "root"; private $password = "atmiya"; private $database = "pakadetails_blogs"; private $conn; function __construct() { $this->conn = $this->connectDB(); } function connectDB() { $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database); return $conn; } function runBaseQuery($query) { $result = $this->conn->query($query); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $resultset[] = $row; } } return $resultset; } function runQuery($query, $argu_type, $lbl_argu_array) { $sql = $this->conn->prepare($query); $this->bindQueryParams($sql, $argu_type, $lbl_argu_array); $sql->execute(); $result = $sql->get_result(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $resultset[] = $row; } } if(!empty($resultset)) { return $resultset; } } function bindQueryParams($sql, $argu_type, $lbl_argu_array) { $param_value_reference[] = & $argu_type; for($i=0; $i<count($lbl_argu_array); $i++) { $param_value_reference[] = & $lbl_argu_array[$i]; } call_user_func_array(array( $sql, 'bind_param' ), $param_value_reference); } function insert($query, $argu_type, $lbl_argu_array) { $sql = $this->conn->prepare($query); $this->bindQueryParams($sql, $argu_type, $lbl_argu_array); $sql->execute(); } function update($query, $argu_type, $lbl_argu_array) { $sql = $this->conn->prepare($query); $this->bindQueryParams($sql, $argu_type, $lbl_argu_array); $sql->execute(); } } ?>
Step 5 : Get Response StripePayment
response.php
<html> <head> <link href="style.css" rel="stylesheet" type="text/css" / > </head> <body> <?php if(!empty($_GET["status"]) && $_GET["status"] == "success") { ?> <div id="results-card" class="success"> <div class="logo-img-results"><img src="https://www.pakainfo.com/stripe-payment-gateway-integration-php/success.png" /></div> <div class="status-ask-data">Thank You!</div> <div>Stripe Payment is Completed Successfully.</div> </div> <?php } else {?> <div id="results-card" class="error"> <div class="logo-img-results"><img src="https://www.pakainfo.com/stripe-payment-gateway-integration-php/error.png" /></div> <div class="status-ask-data">Payment Failed!</div> <div>Problem in Processing Stripe Payment. </div> </div> <?php } ?> </body> </html>
Step 6 : Save and Display Stripe Payment Response
sendPayment.php
<?php use \PakaInfo\Service\StripePayment; if (!empty($_POST["token"])) { require_once 'StripePayment.php'; $stripePayment = new StripePayment(); $liveResStripe = $stripePayment->chargeAmountFromCard($_POST); require_once "DBController.php"; $dbController = new DBController(); $amount = $liveResStripe["amount"] /100; $argu_type = 'ssdssss'; $lbl_argu_array = array( $_POST['email'], $_POST['item_number'], $amount, $liveResStripe["currency"], $liveResStripe["balance_transaction"], $liveResStripe["status"], json_encode($liveResStripe) ); $query = "INSERT INTO tbl_stripe_payments (email, item_number, amount, currency_code, txn_id, payment_status, payment_response) values (?, ?, ?, ?, ?, ?, ?)"; $id = $dbController->insert($query, $argu_type, $lbl_argu_array); if ($liveResStripe['amount_refunded'] == 0 && empty($liveResStripe['failure_code']) && $liveResStripe['paid'] == 1 && $liveResStripe['captured'] == 1 && $liveResStripe['status'] == 'succeeded') { $status = "success"; } else { $status = "error"; } header("Location: response.php?status=" .$status); } ?>
Step 7 : PHP Process Charges Stripe Payment Response
StripePayment.php
<?php namespace PakaInfo\Service; require_once 'vendor/stripe/autoload.php'; use \Stripe\Stripe; use \Stripe\Customer; use \Stripe\ApiOperations\Create; use \Stripe\Charge; class StripePayment { private $apiKey; private $stripeService; public function __construct() { require_once "config.php"; $this->apiKey = STRIPE_YOUR_SECRET_KEY; $this->stripeService = new \Stripe\Stripe(); $this->stripeService->setVerifySslCerts(false); $this->stripeService->setApiKey($this->apiKey); } public function addCustomer($detailsCustArray) { $customer = new Customer(); $customerDetails = $customer->create($detailsCustArray); return $customerDetails; } public function chargeAmountFromCard($detailsCardCust) { $detailsCustArray = array( 'email' => $detailsCardCust['email'], 'source' => $detailsCardCust['token'] ); $customerResult = $this->addCustomer($detailsCustArray); $charge = new Charge(); $detailsCardArray = array( 'customer' => $customerResult->id, 'amount' => $detailsCardCust['amount']*100 , 'currency' => $detailsCardCust['currency_code'], 'description' => $detailsCardCust['item_name'], 'metadata' => array( 'order_id' => $detailsCardCust['item_number'] ) ); $result = $charge->create($detailsCardArray); return $result->jsonSerialize(); } }
Step 8 : Custom Stripe Payment CSS
style.css
#livePaymentFrmStripe { border-top: #a2d0c8 2px solid; background: #e0f7f3; padding: 25px; width: 270px; } #livePaymentFrmStripe .infinity-row { margin-bottom: 20px; } #livePaymentFrmStripe div label { margin: 5px 0px 0px 5px; color: #49615d; width: auto; } .lblInpBox { padding: 10px; border: #a5d2ca 1px solid; border-radius: 4px; background-color: #FFF; width: 100%; margin-top: 5px; box-sizing:border-box; } .liveCustomSelect { padding: 10px; border: #a5d2ca 1px solid; border-radius: 4px; background-color: #FFF; margin-top: 5px; } select.liveCustomSelect { height: 35px; margin-right: 10px; } .error { background-color: #FF6600; padding: 8px 10px; border-radius: 4px; font-size: 0.9em; } .success { background-color: #c3c791; padding: 8px 10px; border-radius: 4px; font-size: 0.9em; } .details { font-size: .8em; color: #FF6600; letter-spacing: 2px; padding-left: 5px; } .lblBtnCard { background-color: #82a9a2; padding: 10px 40px; color: #FFF; border: #739690 1px solid; border-radius: 4px; cursor:pointer; } .lblBtnCard:focus { outline: none; } .column-right { margin-right: 6px; } .myref-row { display: inline-block; } .lbl-cvv-card { width: 60px; } #preview-error-status { margin: 20px 0px 0px; background: #ffd6d6; padding: 10px; border-radius: 4px; line-height: 25px; font-size: 0.9em; color: #907575; display:none; } .display-none { display:none; } #results-card { padding: 40px 20px; width: 270px; text-align:center; } .status-ask-data { font-size: 1.5em; margin-bottom: 20px; } #results-card.success { border-top: #b0dad3 2px solid; background: #e9fdfa; } #results-card.error { border-top: #c3b4b4 2px solid; background: #f5e3e3; } .logo-img-results { margin-bottom: 30px; }
Stripe Payment Form Output
stripe checkout demo
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Stripe Payment gateway integration in PHP with DEMO Examples.
I would like to have feedback on my Pakainfo.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.