Paystack is the fastest, simplest way to start accepting online payments in Nigeria. From signup to receiving real payments can take under 15 minutes.

How To Integrate Paystack in PHP with Database MYSQL 2

 

Database Connection file: config.php


<?php
error_reporting(0);
$mysqli=new mysqli("localhost","root","","jollofblog"); // database connection code
?>


index.php


<html>
<head>

<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css'>

<style>
 body {
 background-color: orange;
 }

select.selectpicker {
 display: block;
 margin: 0 auto;
 padding-left: 20px;
}
.btn-new {
 background-color: white;
}

 </style>

<script>
function showUser(str) {
 if (str=="") {
 document.getElementById("txtHint").innerHTML="";
 return;
 } 
 if (window.XMLHttpRequest) {
 // code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 } else { // code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function() {
 if (this.readyState==4 && this.status==200) {
 document.getElementById("txtHint").innerHTML=this.responseText;
 }
 }
 xmlhttp.open("GET","get_loan_status_sql.php?q="+str,true);
 xmlhttp.send();
}
</script>

</head>

<body>
<br>
<div align="center">
<h3 align="center">Tutorial - <a href="">View How To Retrieve Data From Database Using AJAX Without Submit</a></h3>
<form>
 
<select name="users" onchange="showUser(this.value)" class="selectpicker" data-style="btn-new">
<option value="">Select from the list:</option>
<option value="0">All Loan Customers</option>
<option value="1">All Active Loan Customers</option>
<option value="2">All Fully Paid Loan Customers</option>
<option value="3">All Overdue Loan Customers</option>
</select>
</form>

<div id="txtHint"><b>Information of the selected list will be displayed below</b></div>
</div>
</div>

</html>

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js'></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js'></script>


loan_status_sql.php for getting records


<?php
include "config.php"; // called the database connection file here

$get_criteria= mysqli_real_escape_string($mysqli,$_GET['q']); // filter the input coming from the user's selection
?>
 <meta charset="utf-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
 
 <title>Ajax Live Data Search using Jquery PHP MySql With Pagination</title>

</head>
<body>

<div class="wrapper">
 <div class="fresh-table full-color-orange full-screen-table">
 <!-- Available colors for the full background: full-color-blue, full-color-azure, full-color-green, full-color-red, full-color-orange 
 Available colors only for the toolbar: toolbar-color-blue, toolbar-color-azure, toolbar-color-green, toolbar-color-red, toolbar-color-orange
 -->
 
 <table id="fresh-table" class="table">
 <thead>
 <th data-field="id">ID</th>
 <th data-field="name" data-sortable="true">Name</th>
 <th data-field="country" data-sortable="true">Country</th>
 <th data-field="city">City</th>
 <th data-field="salary" data-sortable="true">Loan Amount</th>
 <th data-field="actions" data-formatter="operateFormatter" data-events="operateEvents">Pay</th>
 </thead>
 <?php
 $c=0; // initialize a counter for serial number her
$sql1=mysqli_query($mysqli,"SELECT * FROM ajax_php_jquery_live_search A WHERE A.status='$get_criteria'"); // sql statement to fetch record based on the status picked
 while($row = mysqli_fetch_array($sql1))
 {
 $c++;
 extract($row);
?>

<tbody>
 <tr>
 
 <td><?php echo $c;?></td>
 <td><?php echo $name;?></td>
 <td><?php echo $country;?></td>
 <td><?php echo $city;?></td>
 <td><?php echo number_format($amount,2);?></td>
 <td><a class="btn btn-primary btn-lg" href="make_payment.php?id=<?php echo $id;?>">Make Payment Now</a> </td>
 </tr>
 </tbody>
 <?php } ?>
 </table>
 </div>
 
</div>

</body>


How To Integrate Paystack in PHP with Database MYSQL 2

make_payment.php for calling paystack for payment handling


<?php
include "config.php";
$subscription=mysqli_real_escape_string($mysqli,$_GET["id"]);
?>
 <!--End Left Menu--> 
 <?php
 $stmt ="SELECT * FROM ajax_php_jquery_live_search WHERE id='$subscription'";
 $ok=mysqli_query($mysqli,$stmt);
 while($row=mysqli_fetch_array($ok)){
 extract($row);
 
 ?> 
<style>
 body {
 background-color: orange;
 }
</style>
<section style="background-color:#f1f1f1;padding:0% 0px 3% 0px;" id="packages">
 <div class="row">
 <div class="container">
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
 <h2 style="text-align:center;margin: 0px;margin-bottom: 2%;">Make Loan Repayment of <?php echo number_format($amount,2);?></h2>
 </div>

<center>
 <div class="row">
 <div class="col-lg-1 col-md-1 col-sm-12 col-xs-12 "></div>

 <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12 pkg">
 <div class="base">
 <h1><?php echo ucwords($name) ?></h1>
 </div>
 
 
 
 </div>
 <div class="foot">
<form >
 <script src="https://js.paystack.co/v1/inline.js"></script>
 
 <button type="button" onclick="payWithPaystack()" class="btn btn-primary">Make Payment Now</button> 
 
</form>
 
 <script>
 function payWithPaystack(){

var handler = PaystackPop.setup({
 key: 'pk_test_2a472933746b82aea810b8d877819e8b6e1ef71c',
 email: 'idowuolaideridwan@gmail.com',
 amount: 100*'<?php echo $amount;?>',
 ref: '<?php echo mt_rand(00000000,12345678);?>', // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
 metadata: {
 custom_fields: [
 {
 display_country: "<?php echo $country;?>",
 display_city: "<?php echo $city;?>",
 display_id: "<?php echo $id;?>"
 }
 ]
 },
 callback: function(response){
 window.location.replace("pay_verify.php?ref="+response.reference);
 },
 onClose: function(){
 alert('window closed');
 }
 });
 handler.openIframe();
 }
</script>

 </div>
 </div>

<?php
 }
 
 ?>
 
 
 </div>
 </div> 
 </div>
 </center>
 </section>
</div>

How To Integrate Paystack in PHP with Database MYSQL 2
pay_verify.php for checking if payment was successfully for assigning values to the database


<?php
include 'config.php';

$result = array();
$generated_ref=trim($_GET["ref"]);
//echo $generated_ref;

//The parameter after verify/ is the transaction reference to be verified
$url = 'https://api.paystack.co/transaction/verify/'.$generated_ref;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
 $ch, CURLOPT_HTTPHEADER, [
 'Authorization: Bearer sk_test_0210a6dc4a71558cae2c91874c7305508dbba1be']
);
$request = curl_exec($ch);
curl_close($ch);

if ($request) {
 $result = json_decode($request,true);
 
 var_dump($result);
 // comment above code to not display the json response

}

if (array_key_exists('data', $result) && array_key_exists('status', $result['data']) && ($result['data']['status'] === 'success'))
 {

$trans_id=$result['data']['id'];
 $trans_amount=$result['data']['amount']/100;
 $trans_currency=$result['data']['currency'];
 $trans_date=$result['data']['transaction_date'];
 $trans_reference=$result['data']['reference'];

$customer_country=$result['data']['metadata']['custom_fields'][0]['display_country'];
 $customer_city=$result['data']['metadata']['custom_fields'][0]['display_city'];
 $customer_id=$result['data']['metadata']['custom_fields'][0]['display_id'];
 
 $payment_channel=$result['data']['channel'];
 $payee_ip=$result['data']['ip_address'];
 $payee_time= $result['data']['log']['time_spent'];
 $payee_attempts=$result['data']['log']['attempts'];
 $customer_id= $result['data']['customer']['id'];
 $customer_email=$result['data']['customer']['email'];
 $trans_status=$result['data']['status'];

// uncomment this line to update customer payment status

mysqli_query($mysqli,"UPDATE ajax_php_jquery_live_search SET amount=0 WHERE id='$customer_id'");

echo "<script>window.location.href='index.php'</script>";

}

?>
</div>