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,

  1. This only works with secure sites meaning website URLs must have HTTPS protocol.
  2. 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

How to Run It On Local Server:

Now the output of the script looks like this,

Onsite PayFast Payment Integration
Onsite PayFast Payment Integration

PayFast Custom Page Integration

Here are some major points about PayFast custom page integration,

  1. HTTPS or a secure URL is not necessary.
  2. You can use Test Merchant credentials.
  3. 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’

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,

Payfast custom integration using PHP
Payfast Custom Integration

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:

Happy Coding..!

Was this article helpful?

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

14 thoughts on “How To Integrate PayFast Payment Gateway in PHP”
  1. 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 :

    Used code is :

    ‘merchant_key’ => ‘s1gocfm1hflea’,
    ‘return_url’ => ‘’,
    ‘cancel_url’ => ‘’,
    ‘notify_url’ => ‘’,
    // Buyer details
    ‘name_first’ => ‘First Name’,
    ‘name_last’ => ‘Last Name’,
    ’email_address’=> ‘’,
    // 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 or
    $testingMode = true;
    $pfHost = $testingMode ? ‘’ : ‘’;
    $htmlForm = ”;
    foreach($data as $name=> $value)
    $htmlForm .= ”;
    $htmlForm .= ”;
    echo $htmlForm;

Leave a Reply

Your email address will not be published. Required fields are marked *