Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I need some help, i have two tables when merchant process payment one is cancel_payment(this updates when merchants cancel their process) but payments table is the one that is not storing the merchant details and need some help from this part.

What I have tried:

// payment_integration logic to process payment through payfast live .
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();
}



// 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;


?>


//payment_cancel.php
PHP
<pre><?php


//Include the save_payment
require_once 'save_payment_data.php';


// Function to save canceled payment data in MySQL table
function saveCanceledPaymentData($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 canceled_payments (payment_id, cancel_reason, cancellation_time) 
                           VALUES (:payment_id, :cancel_reason, NOW())");

    // Bind the values to the parameters
    $stmt->bindParam(':payment_id', $data['payment_id']);
    $stmt->bindParam(':cancel_reason', $data['cancel_reason']);

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


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve the payment ID and cancel reason from the form submission
    $payment_id = $_POST['payment_id'];
    $cancel_reason = $_POST['cancel_reason'];

    // Create an associative array with the payment ID and cancel reason
    $data = array(
        'payment_id' => $payment_id,
        'cancel_reason' => $cancel_reason
    );

    // Call the saveCanceledPaymentData function with the data array
    saveCanceledPaymentData($data);

    // Redirect to the cancellation confirmation page
    header("Location: cancel_confirmation.php");
    exit;
}
?>


//save_payment_data.php
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();
}
?>
Posted
Updated 24-Jun-23 1:11am
Comments
Chris Copeland 23-Jun-23 9:24am    
A PDOStatement->execute() returns whether the query was executed or not, have you tried checking the return value of that and seeing what the result is? If it returns false then you need to check $stmt->errorCode() and $stmt->errorInfo() to get what caused the error.
Gcobani Mkontwana 23-Jun-23 9:42am    
@Chris Copeland havent i am going to check

1 solution

In your 'payment_cancel.php' you are setting actual values to your '$data' array and the call the cancel function -
// Create an associative array with the payment ID and cancel reason
    $data = array(
        'payment_id' => $payment_id,
        'cancel_reason' => $cancel_reason
    );
// Call the saveCanceledPaymentData function with the data array
    saveCanceledPaymentData($data);

    // Redirect to the cancellation confirmation page
    header("Location: cancel_confirmation.php");
    exit;


This allows the cancel part to save the data to your cancel table.

In your 'save_payment_data.php' you are just showing the function with no call made to it which means that no statement will execute at all until the array has been filled with values and the call to save has been made.
 
Share this answer
 
Comments
Gcobani Mkontwana 25-Jun-23 20:54pm    
@Andre Oosthuizen interesting point, the function is called but not used? Whereas with cancelled its called and used? That is the reason its cancelling and same time save those fields to the table?
Andre Oosthuizen 26-Jun-23 3:25am    
You are making a reference to the 'save_payment_data.php' page but nowhere you are calling the actual function, it will not execute unless you tell it to.
Gcobani Mkontwana 26-Jun-23 4:07am    
@Andre Oosthuizen thanks made a change and it does saves the fields to the table
Andre Oosthuizen 26-Jun-23 8:50am    
Pleasure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900