Payment table is not saving merchant details when payment is cancelled or processed

Hi Team
I have two tables one is cancel_payment(this works as it saves data to the record) and payments(this is not saving and when debug no errors). What am doing wrong or missing on my below logic to save the details of a merchant when payment is processed by payfast?

// payment_integration page.
<pre><?php
session_start();

// Function to save payment data in MySQL table
function savePaymentData($data)
{
    // Assuming you have already established a database connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ecommerce_store";

    // Create a new PDO instance
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    
    // Prepare the SQL statement
    $stmt = $pdo->prepare("INSERT INTO payments (merchant_id, merchant_key, return_url, cancel_url, notify_url, name_first, name_last, email_address, m_payment_id, amount, item_name) 
                            VALUES (:merchant_id, :merchant_key, :return_url, :cancel_url, :notify_url, :name_first, :name_last, :email_address, :m_payment_id, :amount, :item_name)");
    
    // Bind the values to the parameters
    $stmt->bindParam(':merchant_id', $data['merchant_id']);
    $stmt->bindParam(':merchant_key', $data['merchant_key']);
    $stmt->bindParam(':return_url', $data['return_url']);
    $stmt->bindParam(':cancel_url', $data['cancel_url']);
    $stmt->bindParam(':notify_url', $data['notify_url']);
    $stmt->bindParam(':name_first', $data['name_first']);
    $stmt->bindParam(':name_last', $data['name_last']);
    $stmt->bindParam(':email_address', $data['email_address']);
    $stmt->bindParam(':m_payment_id', $data['m_payment_id']);
    $stmt->bindParam(':amount', $data['amount']);
    $stmt->bindParam(':item_name', $data['item_name']);
  

    // Execute the SQL statement
    $stmt->execute();
}



// Get the order details from the checkout process
$cartTotal = 10.00;
$passphrase = '';
$data = array(
    // Merchant details
    'merchant_id' => '***',
    'merchant_key' => '***',
    'return_url' => 'http://localhost:8080/payment_return.php',
    'cancel_url' => 'http://localhost:8080/eCommerce/eshopper/eshopper-1.0.0/payment_cancel.php',
    'notify_url' => 'http://localhost:8080/payment_notify.php',
    // Buyer details
    'name_first' => 'Jackson ',
    'name_last'  => 'Pty Ltd',
    'email_address'=> 'gcira2023@outlook.com',
    // Transaction details
    'm_payment_id' => '1234', //Unique payment ID to pass through to notify_url
    'amount' => number_format( sprintf( '%.2f', $cartTotal ), 2, '.', '' ),
    'item_name' => 'Order#123'
);

// If in testing mode make use of either sandbox.payfast.co.za or www.payfast.co.za
$testingMode = false;
$pfHost = 'www.payfast.co.za';

// Define the missing variables
$merchantID = $data['merchant_id'];
$merchantKey = $data['merchant_key'];
$amount = $data['amount'];

// Create the HTML form
$htmlForm = '<div class="card text-center">
    <div class="card-body text-center p-0 d-flex flex-column align-items-center">
        <div class="col-lg-6 mx-auto">
            <div class="blog-3-each mb-30 transition-4">
                <div class="blog-date text-left text-sm-center">
                    <img src="img/payfast.png" alt="Icon">
                </div>
                <div class="blog-content">
                    <h5 class="fs-19 f-700 mb-10">
                        <a href="#"> PayFast Method</a>
                    </h5>
                    <div class="hr-1 opacity-1 mt-10 mb-10"></div>
                    <p> You can easily make an online payment below using PayFast.</p>
                    
                    <form name="PayFastPayNowForm" action="https://www.payfast.co.za/eng/process" method="post">
                        <input required type="hidden" name="cmd" value="_paynow">
                        <input required type="hidden" name="receiver" pattern="[0-9]" value="' . $data['merchant_id'] . '">
    
                        <input required type="hidden" name="item_name" maxlength="255" value="Pay Now">
                        <input required type="amount" name="amount" value="'. $data['amount'].'">
                        <input required type="hidden" name="merchant_id" value="' . $data['merchant_id'] . '">
                        <input required type="hidden" name="merchant_key" value="' . $data['merchant_key'] . '">
                        <input required type="hidden" name="return_url" value="' . $data['return_url'] . '">
                        <input required type="hidden" name="cancel_url" value="' . $data['cancel_url'] . '">
                        <input required type="hidden" name="notify_url" value="' . $data['notify_url'] . '">
                        <input required type="hidden" name="email_address" value="' . $data['email_address'] . '">
                        <input required type="hidden" name="name_first" value="' . $data['name_first'] . '">
                        <input required type="hidden" name="name_last" value="' . $data['name_last'] . '">
                        
                        <div class="text-center">
						<input type="image" src="https://my.payfast.io/images/buttons/PayNow/Dark-Large-PayNow.png" alt="Pay Now" title="Pay Now with Payfast">
                            
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>';

echo $htmlForm;
//save_payment_data.php
<pre><?php

session_start();
// Function to save payment data in MySQL table
function savePaymentData($data)
{
    // Assuming you have already established a database connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ecommerce_store";

    // Create a new PDO instance
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    
    // Prepare the SQL statement
    $stmt = $pdo->prepare("INSERT INTO payments (merchant_id, merchant_key, return_url, cancel_url, notify_url, name_first, name_last, email_address, m_payment_id, amount, item_name) 
                            VALUES (:merchant_id, :merchant_key, :return_url, :cancel_url, :notify_url, :name_first, :name_last, :email_address, :m_payment_id, :amount, :item_name)");
    
    // Bind the values to the parameters
    $stmt->bindParam(':merchant_id', $data['merchant_id']);
    $stmt->bindParam(':merchant_key', $data['merchant_key']);
    $stmt->bindParam(':return_url', $data['return_url']);
    $stmt->bindParam(':cancel_url', $data['cancel_url']);
    $stmt->bindParam(':notify_url', $data['notify_url']);
    $stmt->bindParam(':name_first', $data['name_first']);
    $stmt->bindParam(':name_last', $data['name_last']);
    $stmt->bindParam(':email_address', $data['email_address']);
    $stmt->bindParam(':m_payment_id', $data['m_payment_id']);
    $stmt->bindParam(':amount', $data['amount']);
    $stmt->bindParam(':item_name', $data['item_name']);

    // Execute the SQL statement
    $stmt->execute();
}
?>

<?php

session_start();
// Function to save payment data in MySQL table
function savePaymentData($data)
{
    // Assuming you have already established a database connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ecommerce_store";

    // Create a new PDO instance
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    
    // Prepare the SQL statement
    $stmt = $pdo->prepare("INSERT INTO payments (merchant_id, merchant_key, return_url, cancel_url, notify_url, name_first, name_last, email_address, m_payment_id, amount, item_name) 
                            VALUES (:merchant_id, :merchant_key, :return_url, :cancel_url, :notify_url, :name_first, :name_last, :email_address, :m_payment_id, :amount, :item_name)");
    
    // Bind the values to the parameters
    $stmt->bindParam(':merchant_id', $data['merchant_id']);
    $stmt->bindParam(':merchant_key', $data['merchant_key']);
    $stmt->bindParam(':return_url', $data['return_url']);
    $stmt->bindParam(':cancel_url', $data['cancel_url']);
    $stmt->bindParam(':notify_url', $data['notify_url']);
    $stmt->bindParam(':name_first', $data['name_first']);
    $stmt->bindParam(':name_last', $data['name_last']);
    $stmt->bindParam(':email_address', $data['email_address']);
    $stmt->bindParam(':m_payment_id', $data['m_payment_id']);
    $stmt->bindParam(':amount', $data['amount']);
    $stmt->bindParam(':item_name', $data['item_name']);

    // Execute the SQL statement
    $stmt->execute();
}
?>

How far through the code does it get before it starts to go wrong, when you debug it? Is the $data[] array made up the way that it should be?

Is it correct that there are three functions of the same name, or have you just posted it three times?

What type of column is your amount? You’re sending a formatted string to it, does that make any difference?

Where do you call savePaymentData() ?

@droopsnoot first call it on payment_integration page after the session_start() function

I see only one table.
Are the tables structured identically? Are your queries structured identically?
You say you’ve got one working and one not; what’s the difference between the two other than the name of the table?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.