Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Has anyone worked with payment integration using payfast to process cartData?
I am trying to process cartData to payfast payment integration gateway using pay using payfast not custom or onsite. Now I am getting this error when processing:
amount: Amount must be a valid payment amount.

How do I resolve this to allow an amount for payment?

What I have tried:

PHP
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Your cart data obtained from previous steps
$cartData = json_decode($_GET['cartData'], true); // Replace with the method you use to retrieve cart data

// Calculate cart total
$cartTotal = 0;
foreach ($cartData['cartItems'] as $item) {
    $cartTotal += $item['price'] * $item['quantity'];
}

$formattedAmount = number_format($cartTotal * 100, 2, '.', ''); // Use $cartTotal here

$data = array(
    // Merchant details
    'merchant_id' => '***067', // Replace with your merchant ID
    'merchant_key' => ' ****irme', // Replace with your merchant key
    'return_url' => 'https://www.splashonline.co.za/return.php', // Replace with your return URL
    'cancel_url' => 'https://www.splashonline.co.za/cancel.php', // Replace with your cancel URL
    'notify_url' => 'https://www.splashonline.co.za/notify.php', // Replace with your notify URL
    // Buyer details
    'name_first' => 'First Name',
    'name_last'  => 'Last Name',
    'email_address'=> 'gcira2023@outlook.com',
    // Transaction details
    'm_payment_id' => '1234', // Unique payment ID to pass through to notify_url
    'amount' => $formattedAmount,
    'item_name' => 'Order#123'
);

// If in testing mode make use of either sandbox.payfast.co.za or www.payfast.co.za
$testingMode = true;
$pfHost =  'www.payfast.co.za';
$htmlForm = '<form action="https://' . $pfHost . '/eng/process" method="post">';
foreach ($data as $name => $value) {
    $htmlForm .= '<input name="' . $name . '" type="hidden" value=\'' . $value . '\' />';
}
$htmlForm .= '<input type="submit" value="Pay Now" /></form>';
echo $htmlForm;
?>
Posted
Updated 28-Aug-23 9:40am
v2
Comments
[no name] 25-Aug-23 9:56am    
Did you try displaying the "amount", that is not "valid", to see what it is?
Gcobani Mkontwana 25-Aug-23 10:00am    
@Gerry i did try to use 100.00 then it automatical redirected straight, but i want it to be dynamic it must capture data from cartData including the total dont want user to enter the total manual
BillWoodruff 26-Aug-23 8:29am    
you need to add run-time validation when the current value is visible: how you can do that in PHP i don't have a clue.

1 solution

The 'number_format()' function you are using is incorrect - number_format() function[^]

I am not sure why you multiply the returned amount '$cartTotal' by 100.
First, test this value to see if it returns anything and that it is a number (check if you have added 'R' or '$' before the number, it will fail), if not catch the zero return and let the user know.
If it has a value, format the number properly -
PHP
$formattedAmount = number_format($cartTotal,2,".",",");


'$cartTotal' is the returned number
2 is the decimal counter, in this case '2/00'
The dot '.' is the cents/placemark seperator to show cents following the dot
The comma ',' is the thousand seperator

If I run the following, it returns '1,010.39' -
PHP
<?php
   $cartTotal = 110.3856;
?>

<span>
    <?php echo $formattedAmount;  ?>

Output is 1,010.39
</span>


You also need to check what is the requirements for PayFast as they might require a different format as accepted amount, yours need to be in the exact same format.
 
Share this answer
 
Comments
Gcobani Mkontwana 28-Aug-23 10:02am    
@Andre Oosthuizen, well noted, what also reliased is the total payment as you indicated is not the same. e.g 364zar its showing 36kzar. How can i fix this so it can show correct total payment.
Andre Oosthuizen 28-Aug-23 10:09am    
How do you get the value '36kzar' in your code, the problem will be there. Also, where it is suppose to be '364zar, you need to drop the zar from the value and then format the 364. I need to see how you get these values before I can help.

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



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