Reputation: 13
I am creatin a payment integration uses payfast integration, but the problem now i am getting an error 404 bad request unable to process payment due to Signature: Generated signature does not match submitted signature. How do i fix this issue so it can meet payfast payment integration?
//payment_integration.php
<?php
session_start();
// PayFast Integration Logic
// Get the order details from the checkout process
//$orderID = $_POST['order_id'];
//$products = $_POST['products'];
//$grandTotal = $_POST['grand_total'];
$amount = $_POST['amount'] = 100;
//$signature = $_POST['signature'];
$allItems = $_SESSION['allItems'];
$grand_total = $_SESSION['grand_total'];
//$name = $_POST['name'];
//$email = $_POST['email'];
//$phone = $_POST['phone'];
//$address = $_POST['address'];
// Set your PayFast merchant details
$merchantID = ' 10196067'; // Replace with your actual merchant ID
$merchantKey = 'n3wqwtxa1irme'; // Replace with your actual merchant key
// Set the PayFast URL based on your environment (testing or production)
$isTestingMode = true; // Set to true for testing environment or false for production environment
$payfastURL = $isTestingMode ? 'https://sandbox.payfast.co.za/eng/process' : 'https://www.payfast.co.za/eng/process';
// Function to generate the signature
function generateSignature($data, $passphrase = null)
{
$pfOutput = '';
foreach ($data as $key => $value) {
if ($value !== '') {
$pfOutput .= $key . '=' . urlencode(trim($value)) . '&';
}
}
$pfOutput = substr($pfOutput, 0, -1); // Remove the trailing '&'
if ($passphrase !== null) {
$pfOutput .= '&passphrase=' . urlencode(trim($passphrase));
}
$signature = md5($pfOutput);
return $signature;
}
$merchantID = '***'; // Replace with your actual merchant ID
$merchantKey = '***z'; // Replace with your actual merchant key
$passphrase = 'jt7NOE43FZPn'; // Replace with your actual passphrase (if applicable)
$data = array(
'merchant_id' => $merchantID,
'merchant_key' => $merchantKey,
'return_url' => 'http://localhost/payment_success.php',
'cancel_url' => 'http://localhost/payment_cancel.php',
'notify_url' => 'http://localhost/payment_notify.php',
'amount' => $amount,
'item_name' => 'Cell Phone',
'item_description' => 'Electronics',
'email_address' => '[email protected]',
'name_first' => 'John',
'name_last' => 'Doe',
);
// Generate the signature
$signature = generateSignature($data, $passphrase);
// Add the signature to the payment data
$data['signature'] = $signature;
// If in testing mode, use the sandbox URL, otherwise use the production URL
$testingMode = true;
$pfHost = $testingMode ? 'sandbox.payfast.co.za' : 'www.payfast.co.za';
// Create the HTML form
$htmlForm = '<div class="container">
<div class="card">
<div class="card-body text-center">
<form action="https://' . $pfHost . '/eng/process" method="post">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="mb-3">';
foreach ($data as $name => $value) {
$htmlForm .= '<input name="' . $name . '" type="hidden" value=\'' . $value . '\' />';
}
$htmlForm .= '<button type="submit" class="btn btn-primary">Pay Now</button></div></div></div></form>
</div>
</div>
</div>';
echo $htmlForm;
?>
Upvotes: 0
Views: 598