PHP code example of berzel / paynow-php

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

    

berzel / paynow-php example snippets


$paynow = Paynow::getInstance($appId, $appKey);

$fields = [$resultUrl, $returnUrl, $amount, $reference, $info, $status, $email];

$order = $paynow->createOrder($fields);

$result = $paynow->initiateTransaction($order);

$fields = [
    'resulturl' => $resUrl,
    'returnurl' => $retUrl,
    'amount' => $amt,
    'reference' => $ref,
    'info' => $info,
    'status' => $status,
    'email' => $email
];

$order = $paynow->createOrder($fields);

$orderStatus = $paynow->returnFromPaynow($order->pollUrl);


use Berzel\Paynow\Paynow;

public function checkout($cart)
{
    // lets grab the id and key and initUrl from config
    $id = config('paynow.id');
    $key = config('paynow.key');

    //begin a database transaction
    $this->db->beginTransaction();

    //create a new order 
    $order = $this->user()->createOrder($cart);

    //order details. Please note I am using laravel's route helper function here but any valid url will do
    $fields = [
        'resulturl' => route('order.result', $order->getId()),
        'returnurl' => route('order.return', $order->getId()),
        'amount' => $order->getTotal(),
        'reference' => $order->getReference(),
        'info' => $order->getAdditionalInfo(),
        'status' => $order->getCurrentStatus(),
        'email' => $order->user()->getEmail()
    ];


    try {
        //create/get a new paynow instance
        $paynow = Paynow::getInstance($id, $key);

        // initiate a transaction on paynow
        $result = $paynow->initiateTransaction($paynow->createOrder($fields));

        // before redirecting the user to paynow lets save the results to local db
        $order->setStatus($result['status']);

        $order->setPaynowPollUrl($result['pollurl']);

        $order->save();

        // commit database changes
        $this->db->commit();
        
        // redirect the user to paynow
        return redirect()->url($result['browserurl']);

    } catch (Exception $e) {
        // rollback database changes
        $this->db->rollBack();

        $err = $e->getMessage();
    }
}



public function getFromPaynow(Order $order)
{
    try {
        $orderStatus = Paynow::getInstance($id, $key)->returnFromPaynow($order->getPollUrl());
        
        // show the user that the order status

    } catch (Exception $e) {
        // show the user the error that occured
    }
}



// this could be where you are showing the user his/her order details
public function show(Order $order)
{
    try {
        $orderStatus = Paynow::getInstance($id, $key)->updateFromPaynow($order->pollUrl);

        // show the user the user the status
    } catch (Exception $e) {
        // show the user the error message
    }
}