When integrating a secure and efficient payment gateway into your web application, Paystack is a popular choice for developers working in Africa. We’ve written a guide will walk you through integrating Paystack using AJAX and PHP, enabling seamless payment processing and real-time feedback for your users.
To get started with Paystack payment integration with AJAX and PHP, you need an account. Follow these steps:
You’ll need a frontend form where users can initiate payments. Here’s a basic HTML form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paystack Integration</title>
<script src="https://js.paystack.co/v1/inline.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>Paystack Payment Integration</h2>
<form id="paymentForm">
<label for="email">Email:</label>
<input type="email" id="email" required><br><br>
<label for="amount">Amount (in Naira):</label>
<input type="number" id="amount" required><br><br>
<button type="button" >
The callback in the above script sends the payment reference to a PHP backend script for verification. Below is the verify-payment.php
script:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$reference = $_POST['reference']; // Get the transaction reference
if (!$reference) {
echo json_encode(['status' => false, 'message' => 'No reference supplied']);
exit;
}
$url = 'https://api.paystack.co/transaction/verify/' . $reference;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer your-secret-key-here' // Replace with your Paystack secret key
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['status'] && $result['data']['status'] === 'success') {
// Payment was successful
echo json_encode([
'status' => true,
'message' => 'Payment verified successfully',
'reference' => $reference
]);
} else {
// Payment verification failed
echo json_encode([
'status' => false,
'message' => 'Payment verification failed',
'reference' => $reference
]);
}
}
?>
See also FlutterWave Payment Integration With PHP
See also Remita Payment Integration
Paystack payment integration with AJAX and PHP can provide a seamless payment experience for your users. By following this guide, you’ve set up a secure and efficient payment flow with real-time feedback. Remember to test your implementation thoroughly and follow best practices to ensure a smooth experience for your users.
When working with JavaScript as a software engineer, date manipulation is a common task. One…
Introducing the Model P Smart WiFi-enabled Pizza Oven, the latest innovation in pizza-making that lets…
If you’ve been developing with React for a while, chances are you’ve used npx create-react-app…
Amazon Web Services (AWS) is one of the leading cloud platforms in the world, offering…
Apple is set to make its streaming service, Apple TV+, free for all users during…
This month, hackers successfully modified several Chrome extensions with malicious code after infiltrating admin accounts…