Introduction to PayFast Payment Gateway
Before starting the PayFast Payment Gateway Integration using PHP, we have to know some introduction about the gateway. The PayFast Payment Gateway is the best payment gateway platform in South Africa. This gateway accepts local and international payments from buyers by using Instant EFT, credit cards, debit cards, and more.
It is also easy to integrate on websites using PHP and other supported languages. This also provides plugins and extensions for CMSs (Content Management Systems) like WordPress, Shopify, Magento, Moodle, etc.
Here are two ways to integrate PayFast Payment Gateway in PHP provided by PayFast,
- Onsite Payment Integration (Popup-based).
- Custom Page Integration.
This tutorial explains both ways to integrate PayFast Payment using PHP.
Onsite PayFast Payment Integration
Here are some major points about onsite integration,
- This only works with secure sites meaning website URLs must have HTTPS protocol.
- To test the onsite integration you have to use a live merchant account.
Note: If you want to test it on your local servers like XAMPP or WAMP, you can use Ngrok to create a local secure URL (
https
). You can check here how we can use Ngrok How to install and Use ngrok.
Complete PHP Script to Integrate Onsite PayFast Payment Gateway
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<script src="https://www.payfast.co.za/onsite/engine.js"></script> <?php /** * @param array $data * @param null $passPhrase * @return string */ function generateSignature($data, $passPhrase = null) { // Create parameter string $pfOutput = ''; foreach( $data as $key => $val ) { if($val !== '') { $pfOutput .= $key .'='. urlencode( trim( $val ) ) .'&'; } } // Remove last ampersand $getString = substr( $pfOutput, 0, -1 ); if( $passPhrase !== null ) { $getString .= '&passphrase='. urlencode( trim( $passPhrase ) ); } return md5( $getString ); } $passPhrase = ''; $data = [ 'merchant_id' => LIVE_MERCHANT_ID, 'merchant_key' => 'LIVE_MERCHANT_KEY', 'return_url' => 'https://f8c1265a84a2.ngrok.io/return.php', 'cancel_url' => 'https://f8c1265a84a2.ngrok.io/cancel.php', 'notify_url' => 'https://f8c1265a84a2.ngrok.io/notify.php', 'name_first' => 'First Name', 'name_last' => 'Last Name', 'email_address'=> 'test@test.com', 'm_payment_id' => '1234', 'amount' => 5.00, 'item_name' => 'Order#123', ]; function dataToString($dataArray) { // Create parameter string $pfOutput = ''; foreach( $dataArray as $key => $val ) { if($val !== '') { $pfOutput .= $key .'='. urlencode( trim( $val ) ) .'&'; } } // Remove last ampersand return substr( $pfOutput, 0, -1 ); } function generatePaymentIdentifier($pfParamString, $pfProxy = null) { // Use cURL (if available) if( in_array( 'curl', get_loaded_extensions(), true ) ) { // Variable initialization $url = 'https://www.payfast.co.za/onsite/process'; // Create default cURL object $ch = curl_init(); // Set cURL options - Use curl_setopt for greater PHP compatibility // Base settings curl_setopt( $ch, CURLOPT_USERAGENT, NULL ); // Set user agent curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return output as string rather than outputting it curl_setopt( $ch, CURLOPT_HEADER, false ); // Don't include header in output curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); // Standard settings curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $pfParamString ); if( !empty( $pfProxy ) ) curl_setopt( $ch, CURLOPT_PROXY, $pfProxy ); // Execute cURL $response = curl_exec( $ch ); curl_close( $ch ); echo $response; $rsp = json_decode($response, true); if ($rsp['uuid']) { return $rsp['uuid']; } } return null; } // Generate signature (see Custom Integration -> Step 2) $signature = generateSignature($data); $data["signature"] = $signature;//generateSignature($data, $passPhrase); // Convert the data array to a string $pfParamString = dataToString($data); // Generate payment identifier $identifier = generatePaymentIdentifier($pfParamString); if($identifier != ''){ echo '<script type="text/javascript">window.payfast_do_onsite_payment({"uuid":"'.$identifier.'"});</script>'; } ?> |
How to Run It On Local Server:
- Copy the complete code and paste it on your local server path.
- Set Ngrok for secure URL (HTTPS URL).
- Set the Live merchant ID and merchant Key.
- Now run the script using Ngrok URL. URL should look like Ex: https://9fb7ggb2998f8.ngrok.io/onsiteIntegration.php
Now the output of the script looks like this,
PayFast Custom Page Integration
Here are some major points about PayFast custom page integration,
- HTTPS or a secure URL is not necessary.
- You can use Test Merchant credentials.
- No need to use Ngrok for secure URLs.
PHP Script to Integrate PayFast Custom Page Integration
Here are the test credentials,
Merchant ID: ‘10000100’
Merchant Key: ’46f0cd694581a’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php // Construct variables $cartTotal = 10.00;// This amount needs to be sourced from your application $data = array( // Merchant details 'merchant_id' => '10000100', 'merchant_key' => '46f0cd694581a', 'return_url' => 'http://www.yourdomain.co.za/return.php', 'cancel_url' => 'http://www.yourdomain.co.za/cancel.php', 'notify_url' => 'http://www.yourdomain.co.za/notify.php', // Buyer details 'name_first' => 'First Name', 'name_last' => 'Last Name', 'email_address'=> 'test@test.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' ); function generateSignature($data, $passPhrase = null) { // Create parameter string $pfOutput = ''; foreach( $data as $key => $val ) { if($val !== '') { $pfOutput .= $key .'='. urlencode( trim( $val ) ) .'&'; } } // Remove last ampersand $getString = substr( $pfOutput, 0, -1 ); if( $passPhrase !== null ) { $getString .= '&passphrase='. urlencode( trim( $passPhrase ) ); } return md5( $getString ); } $signature = generateSignature($data); $data['signature'] = $signature; // If in testing mode make use of either sandbox.payfast.co.za or www.payfast.co.za $testingMode = true; $pfHost = $testingMode ? 'sandbox.payfast.co.za' : '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; ?> |
How to Run It On Local Server:
- Just copy and paste the complete code on your local server path and open it on the browser.
- Then you get the button and click on it for the payment page.
The output of the custom integration looks like this,
Above is the complete tutorial on How anyone can integrate the PayFast payment Gateway Using PHP, onsite, and custom types of integration.
To know more about the PayFast payment gateway, check here PayFast South Africa | Fast & Secure Online Payment Gateway.
If you face any issues please let me know in the comment.
Also Read:
- Trigger Submit Button By Pressing Enter Key in JavaScript And JQuery
- Download HTML Page as PDF Using JavaScript
- Best YouTube Channels For Programming in 2021
- JavaScript While Loop with an Example
Happy Coding..!
Hello thanks for post i have followed your tutorial for payfast onsite integration but it’s not working my side in response it’s returning json urls with popup and in popup not showing credit card and debit card option please check below url
https://domainName/Easy_shopping/payfast.php
It’s working please check
https://nimb.ws/5Tduol
Hi sir,
I have implemented the payfast code in my domain.
Its working fine but am not getting the response after complete(success) the payment.
URL is : http://demo.triazinesoft.com/ticketkore/payfast1.php
Used code is :
10024496,
‘merchant_key’ => ‘s1gocfm1hflea’,
‘return_url’ => ‘http://demo.triazinesoft.com/ticketkore/success.php’,
‘cancel_url’ => ‘http://demo.triazinesoft.com/ticketkore/success.php’,
‘notify_url’ => ‘http://demo.triazinesoft.com/ticketkore/success.php’,
// Buyer details
‘name_first’ => ‘First Name’,
‘name_last’ => ‘Last Name’,
’email_address’=> ‘test@test.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′
);
function generateSignature($data, $passPhrase = null) {
// Create parameter string
$pfOutput = ”;
foreach( $data as $key => $val ) {
if($val !== ”) {
$pfOutput .= $key .’=’. urlencode( trim( $val ) ) .’&’;
}
}
// Remove last ampersand
$getString = substr( $pfOutput, 0, -1 );
if( $passPhrase !== null ) {
$getString .= ‘&passphrase=’. urlencode( trim( $passPhrase ) );
}
return md5( $getString );
}
$signature = generateSignature($data);
$data[‘signature’] = $signature;
// If in testing mode make use of either sandbox.payfast.co.za or http://www.payfast.co.za
$testingMode = true;
$pfHost = $testingMode ? ‘sandbox.payfast.co.za’ : ‘www.payfast.co.za’;
$htmlForm = ”;
foreach($data as $name=> $value)
{
$htmlForm .= ”;
}
$htmlForm .= ”;
echo $htmlForm;
?>
Their gateway doesn’t provide any response, so you have to manage accordingly.
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]
[…] How To Integrate PayFast Payment Gateway in PHP – … […]
[…] How To Integrate PayFast Payment Gateway in PHP – … […]
[…] How To Integrate PayFast Payment Gateway in PHP – … […]
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]
[…] How To Integrate PayFast Payment Gateway in PHP – … […]
[…] How To Integrate PayFast Payment Gateway in PHP – PHPCODER … […]