How To Integrate PayFast Payment Gateway in PHP

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, and Moodle, etc.

Here is two ways to integrate PayFast Payment Gateway in PHP provide 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 is only works with secure sites, means website URLs must have https protocol.
  2. To test the onsite integration you have to use 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 output of the script look like this,

Onsite PayFast Payment Integration
Onsite PayFast Payment Integration

PayFast Custom Page Integration

Here are some major point 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 look like this,

Payfast custom integration using PHP
Payfast Custom Integration

Above is the complete turorial of How anyone can integrate PayFast payment Gateway Using PHP, onsite and custom both type of integration.

To know more about PayFast payment gateway, check here PayFast South Africa | Fast & Secure Online Payment Gateway.

If you face any issue please let me know on the comment.

Also Read:

Happy Coding..!

Was this article helpful?
YesNo
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.
Posts created 188

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 : 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;
    ?>

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top