PHP code example of thepublicgood / payfast

1. Go to this page and download the library: Download thepublicgood/payfast library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thepublicgood / payfast example snippets


$merchant = new \TPG\PayFast\Merchant('MERCHANT_ID', 'MERCHANT_KEY', 'PASSPHRASE');

$merchant
    ->setReturnUrl($returnUrl)
    ->setCancelUrl($cancelUrl)
    ->setNotifyUrl($notifyUrl);

$customer = new \TPG\PayFast\Customer();

$customer
    ->setName('First', 'Last')
    ->setEmail('[email protected]')
    ->setCellNumber('1234567890');

$transaction = new \TPG\PayFast\Transaction($merchant, 10000, 'Item Name');

$transaction
    ->setCustomer($customer)                  // Set a customer
    ->setMerchantPaymentId('PAYID123').       // A payment reference
    ->setDescription('Item Description')      // A payment description
    ->setCustomIntegers([                     // Up to 5 custom integers
        1,
        2,
        3,
        4,
        5,
    ])
    ->setCustomStrings([                     // Up to 5 custom strings
        'S1',
        'S2',
        'S3',
        'S4',
        'S5'
    ])
    ->setEmailConfirmation(true)            // Where to send email confirmations
    ->setEmailConfirmationAddress('[email protected]')  // The confirmation email
    ->setPaymentMethod(\TPG\PayFast\PaymentMethod::ALL); // Payment method

PaymentMethod::ALL;  // All payment methods allowed
PaymentMethod::CC;   // Credit Cards
PaymentMethod::DC;   // Debit cards
PaymentMethod::EFT;  // EFT
PaymentMethod::MP;   // MasterPass
PaymentMethod::MC;   // Mobicred
PaymentMethod::SC;   // SCode

$payfast = new \TPG\PayFast\PayFast($transaction);

$submissionDelay = 10; // seconds to wait before automatically submitting the form.
$form = $payfast->form($submissionDelay);

echo $form;

namespace App\Http\Controllers;

class PayFastController
{
    public function webhook(Request $request)
    {
        // Create a new validator
        $validator = new \TPG\PayFast\ItnValidator($request->input());
        
        // From the PayFast docs... Send a 200 response right away...
        $validator->flush();
    
        // You have access to all the response data through the `PayfastResponse` class.
        $response = $validator->response();
        
        $mpid = $response->merchantPaymentId();  // Original payment ID set on the transaction
        $pfid = $response->payFastPaymentId();   // PayFast's payment ID
        $name = $response->name();           // Item name or order number
        $description = $response->description();    // Item or order description
        $gross = $response->amountGross();        // Total charge
        $fee = $response->amountFee();          // Payfast fee amount
        $net = $response->amountNet();          // Net amount
        $integer = $response->customIntegers();    // Array of custom integers
        $string = $response->customStrings();     // Array of custom strings
        
        $firstName = $response->customer()->firstName();      // Customers first name
        $lastName = $response->customer()->lastName();       // Customers last name
        $emailAddress = $response->customer()->emailAddress();   // Customers email address
        $cellNumber = $response->customer()->cellNumber();     // Customers cell number
        
        $signature = $response->signature();                  // Signature for validation
        
        //--------------------
        
        // To validate the transaction, first ensure the transaction is COMPLETE:
        if ($response->paymentStatus() !== \TPG\PayFast\PaymentStatus::COMPLETE) {
            // incomplete...
        }
        
        // Then `validate()` will return true or throw an exception
        $valid = $validator->validate(10000, $passphrase, $request->ip());
        
        if (!$valid) {
            echo $validator->error();
        }
        
        // validated!
    }
}

$transaction = new Transaction($merchant, 10000);
$transaction->subscription();

$transaction->subscription(
    Transaction::SUBSCRIPTION_FREQUENCY_QUARTERLY,  // frequency
    10,                                             // number of cycles
    new DateTime('tomorrow'),                       // Billing start date
);

$monthly = Transaction::SUBSCRIPTION_FREQUENCY_MONTHLY;  // default
$quarterly = Transaction::SUBSCRIPTION_FREQUENCY_QUARTERLY;
$biannually = Transaction::SUBSCRIPTION_FREQUENCY_BIANNUALLY;
$annually = Transaction::SUBSCRIPTION_FREQUENCY_ANNUALLY;

$validator = new ItnValidator($request->input());

if ($validator->validate(10000, 'passphrase', $request->ip()) {
    $token = $validator->token();
}

$subscription = new Subscription($merchant, $token);
$subscription->fetch();

$data = $subscription->toArray();

$subscription->pause();

$subscription->fetch()->runDate();  // Will skip the next billing date

//---------------------------------------

$subscription->pause(2);

$subscription->fetch()->runDate(); // Will skip the next two billing dates

$subscription->unpause();

$subscription->pause();
$subscription->paused();  // true

$subscription->cancel();
$subscription->cancelled();  // true

$payfast = new PayFast($transaction);

$form = $payfast->testing()->form();

$validator = new ItnValidator($request->input());
$valid = $validator->testing()->validate(10000, $passphrase, $request->ip());

$subscription = new Subscription($merchant, 'TOKEN');
$subscription->testing()->pause();