PHP code example of visual-host / payfast

1. Go to this page and download the library: Download visual-host/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/ */

    

visual-host / payfast example snippets


'providers' => [
    //

    VisualHost\Payfast\PayfastServiceProvider::class,
];

'return_url' => 'https://xxxxxxxx.ngrok.io/success',

protected $except = [
        '/itn'
    ];


/*
|--------------------------------------------------------------------------
| Merchant Settings
|--------------------------------------------------------------------------
| All Merchant settings below are for example purposes only. for more info
| see www.payfast.co.za. The Merchant ID and Merchant Key can be obtained
| from your payfast.co.za account.
|
*/

[

    'testing' => true, // Set to false when in production.

    'currency' => 'ZAR', // ZAR is the only supported currency at this point.

    'merchant' => [
        'merchant_id' => '10000100', // TEST Credentials. Replace with your merchant ID from Payfast.
        'merchant_key' => '46f0cd694581a', // TEST Credentials. Replace with your merchant key from Payfast.
        'return_url' => 'http://your-domain.co.za/success', // The URL the customer should be redirected to after a successful payment.
        'cancel_url' => 'http://your-domain.co.za/cancelled', // The URL the customer should be redirected to after a payment is cancelled.
        'notify_url' => 'http://your-domain.co.za/itn', // The URL to which Payfast will post return variables.
    ]

];



use VisualHost\Payfast\Contracts\PaymentProcessor;

Class PaymentController extends Controller
{

    public function confirmPayment(PaymentProcessor $payfast)
    {
        // Eloquent example.
        $cartTotal = 9999;
        $order = Order::create([
                'm_payment_id' => '001', // A unique reference for the order.
                'amount'       => $cartTotal     
            ]);

        // Build up payment Paramaters.
        $payfast->setBuyer('first name', 'last name', 'email');

        //PS ADD DIVISION BY 100 FOR CENTS AS THE NEW DEPENDENCY "mathiasverraes/money": "^1.3" DOESN'T DO CONVERSION
        payfast->setAmount($purchase->amount / 100);

        $payfast->setItem('item-title', 'item-description');
        $payfast->setMerchantReference($order->m_payment_id);

        // Return the payment form.
        return $payfast->paymentForm('Place Order');
    }

}


use VisualHost\Payfast\Contracts\PaymentProcessor;

Class PaymentController extends Controller
{

    public function itn(Request $request, PaymentProcessor $payfast)
    {
        // Retrieve the Order from persistance. Eloquent Example.
        $order = Order::where('m_payment_id', $request->get('m_payment_id'))->firstOrFail(); // Eloquent Example

        try {
            $verification = $payfast->verify($request, $order->amount);
            $status = $payfast->status();
        } catch (\Exception $e) {
            Log::error('PAYFAST ERROR: ' . $e->getMessage());
            $status = false;
        }

        // Verify the payment status.
        $status = (int) $payfast->verify($request, $order->amount, /*$order->m_payment_id*/)->status();

        // Handle the result of the transaction.
        switch( $status )
        {
            case 'COMPLETE': // Things went as planned, update your order status and notify the customer/admins.
                break;
            case 'FAILED': // We've got problems, notify admin and contact Payfast Support.
                break;
            case 'PENDING': // We've got problems, notify admin and contact Payfast Support.
                break;
            default: // We've got problems, notify admin to check logs.
                break;
        }
    }       

}


 return $payfast->responseVars();



[
    'm_payment_id' => '',
    'pf_payment_id' => '',
    'payment_status' => '',
    'item_name' => '',
    'item_description' => '',
    'amount_gross' => '',
    'amount_fee' => '',
    'amount_net' => '',
    'custom_str1' => '',
    'custom_str2' => '',
    'custom_str3' => '',
    'custom_str4' => '',
    'custom_str5' => '',
    'custom_int1' => '',
    'custom_int2' => '',
    'custom_int3' => '',
    'custom_int4' => '',
    'custom_int5' => '',
    'name_first' => '',
    'name_last' => '',
    'email_address' => '',
    'merchant_id' => '',
    'signature' => '',
];



$cartTotal = 9999;
// "mathiasverraes/money": "^1.3" doesn't convert from cents correctly yet
// That's why a manual division by 100 is necessary
$payfast->setAmount($cartTotal / 100);



$payfast->getPaymentForm() // Default Text: 'Pay Now'

$payfast->getPaymentForm(false) // No submit button, handy for submitting the form via javascript

$payfast->getPaymentForm('Confirm and Pay') // Override Default Submit Button Text.