<?
/*class.xpay.php
Author: James A Dean, hide@address.com
License: Use and distribute at non-profit at will, please let me know if you make any changes, and feedback would be great
*/
class Xpay
{
var $host;
var $port;
var $sitereference;
var $certificate;
function Xpay($sitereference,$certlocation,$port = 5000,$host = "127.0.0.1")
{
$this->host = $host;
$this->port = $port;
$this->sitereference = $sitereference; //string
$this->certificate = $certlocation; //string
}
function alive()
{
$open_socket = fsockopen($this->host,$this->port);
if(!open_socket){
return false;
}else{
fclose($open_socket);
return true;
}
}
function auth($operation,$customerinfo,$paymentmethod,$order,$optional = array())
{
$xml = "\n<RequestBlock Version=\"3.15\">\n";
$xml.= "<Request Type=\"AUTH\">\n";
//operation
$xml.= "<Operation>\n";
$xml.= " <Amount>".$operation["amount"]."</Amount>\n";
$xml.= " <Currency>GBP</Currency>\n";
$xml.= " <SiteReference>".$this->sitereference."</SiteReference>\n";
$xml.= " <SettlementDay>1</SettlementDay>\n";
$xml.= "</Operation>\n\n";
//customerinfo
$xml.= "<CustomerInfo>\n";
$xml.= " <Postal>\n";
$xml.= " <Name>\n";
$xml.= " <NamePrefix>".$customerinfo["nameprefix"]."</NamePrefix>\n";
$xml.= " <FirstName>".$customerinfo["firstname"]."</FirstName>\n";
$xml.= " <MiddleName>".$customerinfo["middlename"]."</MiddleName>\n";
$xml.= " <LastName>".$customerinfo["lastname"]."</LastName>\n";
$xml.= " <NameSuffix>".$customerinfo["namesuffix"]."</NameSuffix>\n";
$xml.= " </Name>\n";
$xml.= " <Company>".$customerinfo["company"]."</Company>\n";
$xml.= " <Street>".$customerinfo["street"]."</Street>\n";
$xml.= " <City>".$customerinfo["city"]."</City>\n";
$xml.= " <StateProv>".$customerinfo["stateprov"]."</StateProv>\n";
$xml.= " <PostalCode>".$customerinfo["postcode"]."</PostalCode>\n";
$xml.= " <CountryCode>".$customerinfo["countrycode"]."</CountryCode>\n";
$xml.= " </Postal>\n";
$xml.= " <Telecom>\n";
$xml.= " <Phone>".$customerinfo["phone"]."</Phone>\n";
$xml.= " </Telecom>\n";
$xml.= " <Online>\n";
$xml.= " <Email>".$customerinfo["email"]."</Email>\n";
$xml.= " </Online>\n";
$xml.= "</CustomerInfo>\n\n";
//paymentmethod
$xml.= "<PaymentMethod>\n";
$xml.= " <CreditCard>\n";
$xml.= " <Type>".$paymentmethod["type"]."</Type>\n";
$xml.= " <Number>".$paymentmethod["number"]."</Number>\n";
$xml.= " <Issue>".$paymentmethod["issue"]."</Issue>\n";
$xml.= " <StartDate>".$paymentmethod["startdate"]."</StartDate>\n";
$xml.= " <SecurityCode>".$paymentmethod["securitycode"]."</SecurityCode>\n";
$xml.= " <ExpiryDate>".$paymentmethod["expirydate"]."</ExpiryDate>\n";
$xml.= " </CreditCard>\n";
$xml.= "</PaymentMethod>\n\n";
//order
$xml.= "<Order>\n";
$xml.= " <OrderReference>".$order["orderreference"]."</OrderReference>\n";
$xml.= " <OrderInformation>".$order["orderinformation"]."</OrderInformation>\n";
$xml.= "</Order>\n\n";
//optional
$xml.= "<Optional>\n";
$xml.= " <Processor>class.xpay.php</Processor>\n";
foreach($optional as $key => $val){
$xml.= " <".$key.">";
if(is_array($val)){
foreach($val as $subkey1 => $subval1){
$xml.= "\n <".$subkey1.">".$subval1."</".$subkey1.">";
}
}else{
$xml.= $val;
}
$xml.= " </".$key.">\n";
}
$xml.= "</Optional>\n";
$xml.= "</Request>\n\n";
//certificate
$xml.= "<Certificate>".fread(fopen($this->certificate,"r"),filesize($this->certificate))."</Certificate>\n\n";
$xml.= "</RequestBlock>\n\n";
$return_xml = "";
$open_socket = fsockopen($this->host,$this->port,$errno,$errstr,30);
if(!$open_socket){
echo "$errstr ($errno)<br />\n";
}else{
fputs($open_socket,$xml);
$i = 0;
while(!feof($open_socket)){
$line = fgets($open_socket,128);
if($i > 2)
$return_xml.= $line;
$i++;
}
fclose($open_socket);
}
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $return_xml, $vals, $index);
xml_parser_free($p);
$return_array["TransactionReference"] = $vals[$index["TransactionReference"][0]]["value"];
$return_array["AuthCode"] = $vals[$index["AuthCode"][0]]["value"];
$return_array["Result"] = $vals[$index["Result"][0]]["value"];
$return_array["Message"] = $vals[$index["Message"][0]]["value"];
$return_array["SecurityMessage"] = $vals[$index["SecurityMessage"][0]]["value"];
$return_array["TransactionCompletedTimestamp"] = $vals[$index["TransactionCompletedTimestamp"][0]]["value"];
$return_array["TransactionVerifier"] = $vals[$index["TransactionVerifier"][0]]["value"];
$return_array["OrderReference"] = $vals[$index["OrderReference"][0]]["value"];
$return_array["OrderInformation"] = $vals[$index["OrderInformation"][0]]["value"];
return $return_array;
}
}
?>