Close Window
Direct Interface PHP Example

<?php

// NOTE TO DEVELOPERS - THIS CODE COMES WITH NO WARRANTY. IT IS ONLY AN EXAMPLE.
//                    - IF YOU MODIFY THIS CODE AND IT STOPS WORKING, DO NOT CALL DIRECTONE SUPPORT.
//                    - DIRECTONE SUPPORT HANDLES ACCOUNT SETUP AND BUSINESS ENQUIRIES.
//                    - FOR PROGRAMMING ENQUIRIES CONSULT GOOGLE OR A PROFESSIONAL PROGRAMMER.

// Do not allow browser to cache this page.
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// NOTE. Apache on Windows can generate the following warning: Warning: fgets(): SSL: fatal protocol error in ...  This is not really fatal, so we set the following:
error_reporting(E_ERROR | E_PARSE);

   switch ($_GET[pageid])
   {
      case "":
?>

<!-- Customer-entered fields: -->

	<html>
	<head>
		<H3>DirectOne Direct Method</H3>
	</head>

	<body>
		<form method="post" action="DirectOne_PHP_example.php?pageid=process">
		
		<table cellspacing="0" cellpadding="5" border="1">

		<input type=hidden name=vendor_name value=sarahflower>
        <input type=hidden name=vendor_password value=abc123>

		</tr>
			<td>Name on Card:</td>
			<td><input type="text" name="card_holder" size="16" maxlength="16" value="Test" /></td>
		</tr>

		<tr>
			<td>Card Number:</td>
			<td><input name="card_number" type="text" size="16" maxlength="16" value="4444333322221111" /></td>   
		</tr>

        <tr>
			<td>Expiry:</td>
			<td>
				<select name="card_expiry_month"> 
					<option value="01">01</option>
					<option value="02" selected="selected">02</option>
					<option value="03">03</option>
					<option value="04">04</option>
					<option value="05">05</option>
					<option value="06">06</option>
					<option value="07">07</option>
					<option value="08">08</option>
					<option value="09">09</option>
					<option value="10">10</option>
					<option value="11">11</option>
					<option value="12">12</option>
				</select>
				 /   
				<select name="card_expiry_year"> 
					<option value="05">05</option>
					<option value="06">06</option>
					<option value="07">07</option>
					<option value="08" selected="selected">08</option>
					<option value="09">09</option>
					<option value="10">10</option>
					<option value="11">11</option>
				</select>
			</td>
		</tr>
	    
		<tr>
			<td><input type=hidden name=card_type value=AUTO></td>
		</tr>

		<tr>
			<td>Card CVV:</td>
			<td><input type="text" name="card_cvv" size="3" maxlength="4" value="123" /></td>
		</tr>

		<tr>
			<td>Payment Reference </td>
			<td><input type="text" name="payment_reference" size="16"maxlength="16" value="My_Reference" /></td>
		</tr>
 
        <tr>
			<td>Payment Amount </td>
			<td><input type="text" name="payment_amount" size="16" maxlength="16" value="1.00" /></td>
		</tr>

        <tr>
	      <td><b><input type="submit" name="submit" value="Make Payment"></b></td>
	   </tr>
		
		</form>

	</body>
	
	</html>
<!-- end Customer-entered fields: -->

<?php
	break;
	case "process":
    
	/*Test payment URL */
	$host = "vault.safepay.com.au/cgi-bin/direct_test.pl";

   /* Live payment URL */
   //$host = "vault.safepay.com.au/cgi-bin/direct_process.pl";

	$vars = "vendor_name=sarahflower&vendor_password=abc123&card_number=".$_POST["card_number"]."&card_expiry=".$_POST["card_expiry_month"]."/" .$_POST["card_expiry_year"]."&card_holder=".$_POST["card_holder"]."&card_cvv=".$_POST["card_cvv"]."&payment_amount=".$_POST["payment_amount"]."&payment_reference=".
	$_POST["payment_reference"];

   $post = OpenConnection($host,$vars);

   $result = preg_split("/\n/", $post, -1);

   foreach($result as $key)
   {
      $response = split("=",$key, -1); 
   
      switch ($response[0])
      {
	     case "payment_number":
	        $payment_number = $response[1];
	        echo"payment_number = ".$payment_number."<br/>";
	     break;

	     case "payment_reference":
	        $payment_reference = $response[1];
	        echo"payment_reference = ".$payment_reference."<br/>";
	     break;

	     case "bank_reference":
		    $bank_reference = $response[1];
	        echo"bank_reference = ".$bank_reference."<br/>";
	     break;

	     case "summary_code":
	        $summary_code = $response[1];
		    echo"summary_code = ".$summary_code."<br/>";
	     break;

	     case "response_code":
	        $response_code = $response[1];
		     echo"response_code = ".$response_code."<br/>";
	      break;

	      case "response_text":
		     $response_text = $response[1];
		     echo"response_text = ".$response_text."<br/>";
	      break;
     }
	
   }

echo("<hr><a href=DirectOne_PHP_example.php>Back to Payment Form</hr>");
?>

<?php
   break;
}
   function OpenConnection($host,$query){
   /* Break the URL into usable parts, explode - Split a string by string */
   /*$host = "vault.safepay.com.au/cgi-bin/direct_test.pl"*/
      $path = explode('/',$host);
      $host = $path[0];
	 /* unset the path */
       unset($path[0]);
		/*implode - Join array elements with a string */
       $path = '/'.(implode('/',$path));

        /* Prepare the post query */
        $post  = "POST $path HTTP/1.1\r\n";
        $post .= "Host: $host\r\n";
        $post .= "Content-type: application/x-www-form-urlencoded\r\n";
        $post .= "Content-length: ". strlen($query)."\r\n";
        $post .= "Connection: close\r\n\r\n$query";

        /***********************************************/
        /* Open the secure socket and post the message */
        /***********************************************/
         $h = fsockopen("ssl://".$host, 443, $errno, $errstr);
         if ($errstr)
         print "$errstr ($errno)<br/>\n";
         fwrite($h,$post);

        /*******************************************/
        /* Retrieve the HTML headers (and discard) */
        /*******************************************/
         $headers = "";
         while ($str = trim(fgets($h, 4096))) {
            $headers .= "$str\n";
        }

        /*******************************************/
        /* Retrieve the body of the response */
        /******************************************/
         $body = "";
         while (!feof($h)) {
               $body .= fgets($h, 4096);
		 }

        // Close the socket
         fclose($h);
        // Return the body of the response
         return $body;
   }

?>