To achieve this we need 3 basic functions, whatever our shopping cart, i’ll take you through the process with code snippets below but there’s a full working example downloadable here.

[bsbutton text="Download PHP Example" link="?smd_process_download=1&download_id=243" target="This page" style="primary" theme="flat" size="large" icon="icon-arrow-down"]

Initiate the Transaction on Paynow

This method should be called when the user hits the Pay on Paynow button, handling the initial submit to try and create a transaction on paynow and subsequently taking appropriate action (redirect to Paynow on success or go back to your shopping cart).

  • function WereCreatingATransaction()
    {
    	global $_POST;
    	global $integration_id;
    	global $integration_key;
    	global $authemail;
    	global $initiate_transaction_url;
    	global $orders_data_file;
    	global $checkout_url;
    	$local_order_id = $_POST['order_id'];
    	
    	
    	//set POST variables
    	$values = array('resulturl' => $_POST['resulturl'],
    			'returnurl' =>  $_POST['returnurl'],
    			'reference' =>  $_POST['reference'],
    			'amount' =>  $_POST['amount'],
    			'id' =>  $integration_id,
    			'additionalinfo' =>  $_POST['additionalinfo'],
    			'authemail' =>  $authemail,
    			'status' =>  'Message'); //just a simple message
    			
    	$fields_string = CreateMsg($values, $integration_key);
    	
    	//open connection
    	$ch = curl_init();
    	$url = $initiate_transaction_url;
    	
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_POST, true);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    	//execute post
    	$result = curl_exec($ch);
    
    	if($result)
    	{
    		$msg = ParseMsg($result);
    		
    		//first check status, take appropriate action
    		if ($msg["status"] == ps_error){
    			header("Location: $checkout_url");
    			exit;
    		}
    		else if ($msg["status"] == ps_ok){
    		
    			//second, check hash
    			$validateHash = CreateHash($msg, $integration_key);
    			if($validateHash != $msg["hash"]){
    				$error =  "Paynow reply hashes do not match : " . $validateHash . " - " . $msg["hash"];
    			}
    			else
    			{
    				
    				$theProcessUrl = $msg["browserurl"];
    
    				/***** IMPORTANT ****
    				On User has approved paying you, maybe they are awaiting delivery etc
    				
    					Here is where you
    					1. Save the PollURL that we will ALWAYS use to VERIFY any further incoming Paynow Notifications FOR THIS PARTICULAR ORDER
    					1. Update your local shopping cart of Payment Status etc and do appropriate actions here, Save any other relavant data to DB
    					2. Email, SMS Notifications to customer, merchant etc
    					3. Any other thing
    				
    				*** END OF IMPORTANT ****/
    				
    				//1. Saving mine to a PHP.INI type of file, you should save it to a db etc
    				$orders_array = array();
    				if (file_exists($orders_data_file))
    				{
    					$orders_array = parse_ini_file($orders_data_file, true);
    				}
    				
    				$orders_array['OrderNo_'.$local_order_id] = $msg;
    				
    				write_ini_file($orders_array, $orders_data_file, true);				
    				
    			}
    		}
    		else {						
    			//unknown status or one you dont want to handle locally
    			$error =  "Invalid status in from Paynow, cannot continue.";
    		}
    
    	}
    	else
    	{
    	   $error = curl_error($ch);
    	}
    	
    	//close connection
    	curl_close($ch);
    
    			
    	//Choose where to go
    	if(isset($error))
    	{
    		//back to checkout, show the user what they need to do
    		header("Location: $checkout_url");
    	}
    	else
    	{
    		//redirect to paynow for user to complete payment
    		header("Location: $theProcessUrl");
    	}
    	exit;	
    	
    }

Receiving Payment Notification

This method should be called in on your website LISTENING route that waits to be posted to by Paynow on status updates

  • function PaynowJustUpdatingUs()
    {
    	global $integration_id;
    	global $integration_key;
    	global $checkout_url;
    	global $orders_data_file;
    
    	$local_order_id = $_GET['order_id'];
    	
    	//write a file to show that paynow silently visisted us sometime
    	file_put_contents('sellingmilk_log.txt', date('d m y h:i:s').'   Paynow visited us for order id '.$local_order_id.'\n', FILE_APPEND | LOCK_EX);
    		
    	//Lets get our locally saved settings for this order
    	$orders_array = array();
    	if (file_exists($orders_data_file))
    	{
    		$orders_array = parse_ini_file($orders_data_file, true);
    	}
    	
    	$order_data = $orders_array['OrderNo_'.$local_order_id];
    	
    	$ch = curl_init();
    
    	//set the url, number of POST vars, POST data
    	curl_setopt($ch, CURLOPT_URL, $order_data['pollurl']);
    	curl_setopt($ch, CURLOPT_POST, 0);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, '');
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    	//execute post
    	$result = curl_exec($ch);
    
    	if($result) {
    
    		//close connection
    		$msg = ParseMsg($result);
    		
    		$MerchantKey =  $integration_key;
    		$validateHash = CreateHash($msg, $MerchantKey);
    
    		if($validateHash != $msg["hash"]){
    			header("Location: $checkout_url");
    		}
    		else
    		{
    			/***** IMPORTANT ****
    			On Paynow, payment status has changed, say from Awaiting Delivery to Delivered
    			
    				Here is where you
    				1. Update your local shopping cart of Payment Status etc and do appropriate actions here, Save data to DB
    				2. Email, SMS Notifications to customer, merchant etc
    				3. Any other thing
    			
    			*** END OF IMPORTANT ****/
    			
    			//1. Lets write the updated settings
    			$orders_array['OrderNo_'.$local_order_id] = $msg;
    			$orders_array['OrderNo_'.$local_order_id]['visited_by_paynow'] = 'yes';
    			
    			write_ini_file($orders_array, $orders_data_file, true);	
    		}
    	}
    	exit;	
    }
    

Returning from Paynow

This is the route Paynow will redirect to after completing its operations during the user’s shopping online

  • function WereBackFromPaynow()
    {
    	global $integration_id;
    	global $integration_key;
    	global $checkout_url;
    	global $orders_data_file;
    
    	$local_order_id = $_GET['order_id'];
    			
    	//Lets get our locally saved settings for this order
    	$orders_array = array();
    	if (file_exists($orders_data_file))
    	{
    		$orders_array = parse_ini_file($orders_data_file, true);
    	}
    	
    	$order_data = $orders_array['OrderNo_'.$local_order_id];
    	
    	$ch = curl_init();
    
    	//set the url, number of POST vars, POST data
    	curl_setopt($ch, CURLOPT_URL, $order_data['pollurl']);
    	curl_setopt($ch, CURLOPT_POST, 0);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, '');
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    	//execute post
    	$result = curl_exec($ch);
    
    	if($result) {
    
    		//close connection
    		$msg = ParseMsg($result);
    		
    		$MerchantKey =  $integration_key;
    		$validateHash = CreateHash($msg, $MerchantKey);
    
    		if($validateHash != $msg["hash"]){
    			header("Location: $checkout_url");
    		}
    		else
    		{
    			/***** IMPORTANT ****
    			On Paynow, payment status has changed, say from Awaiting Delivery to Delivered
    			
    				Here is where you
    				1. Update your local shopping cart of Payment Status etc and do appropriate actions here, Save data to DB
    				2. Email, SMS Notifications to customer, merchant etc
    				3. Any other thing
    			
    			*** END OF IMPORTANT ****/
    			//1. Lets write the updated settings
    			$orders_array['OrderNo_'.$local_order_id] = $msg;
    			$orders_array['OrderNo_'.$local_order_id]['returned_from_paynow'] = 'yes';
    			
    			write_ini_file($orders_array, $orders_data_file, true);	
    		}
    	}
    	
    	//Thank	your customer
    	getBackFromPaynowHTML();
    }

Basically that’s it. You can see a working example here with an HTML button that begins the whole procedure.

I hope you quickly grasp and get your hands dirty working on your own cart integration.

Kind Regards.