Skip to content
Home » How To Integrate PayFast Payment Gateway in PHP

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, 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?
YesNo

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

  2. Pingback: amazon payment gateway integration in php - bankschools.com

  3. Pingback: ccbill payment gateway integration in php - banklights.com

  4. Pingback: benefit payment gateway integration in php - bankschools.com

  5. Pingback: airpay payment gateway integration in php - banklights.com

  6. Pingback: payment gateway integration in php - banklights.com

  7. Pingback: payment gateway in php pdf - banklights.com

  8. Pingback: payment gateway in php - banklights.com

  9. Pingback: types of payment gateway in php - banklights.com

  10. Pingback: php payment gateway integration script - banklights.com

  11. Pingback: sbi payment gateway integration in php - banklights.com

Leave a Reply

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