debugging = 1;
# print out incoming hash
if ( $webspace ) { // use html-friendly output
echo "at process, incoming data: ";
while ( list( $key, $value ) = each( $data ) )
echo htmlspecialchars( $key ) . " = " . htmlspecialchars( $value ) . " \n";
} else { // don't use html output
echo "at process, incoming data: \n";
while ( list( $key, $value ) = each( $data ) )
echo "$key = $value\n";
}
reset( $data );
}
}
if ( isset( $data["xml"] ) ) { // if XML string is passed in, we'll use it
$using_xml = 1;
$xml = $data["xml"];
} else {
// otherwise convert incoming hash to XML string
$xml = $this->buildXML($data);
}
// then set up transaction variables
$key = $data["keyfile"];
$host = $data["host"];
$port = $data[port];
# FOR PERFORMANCE, Use the 'extensions' statement in your php.ini to load
# this library at PHP startup, then comment out the next seven lines
// load library
if ( !extension_loaded( 'liblphp' ) ) {
if ( !dl( 'liblphp.so' ) ) {
exit( "cannot load liblphp.so, bye\n" );
}
}
if ( $this->debugging ) {
if ( $webspace )
echo " sending xml string: " . htmlspecialchars($xml) . "
";
else
echo "\nsending xml string:\n$xml\n\n";
}
// send transaction to LSGS
$retstg = send_stg( $xml, $key, $host, $port );
if ( strlen( $retstg ) < 4 )
exit ( "cannot connect to lsgs, exiting" );
if ( $this->debugging ) {
if ( $this->webspace ) // we're web space
echo " server responds: " . htmlspecialchars($retstg) . "
";
else // not html output
echo "\nserver responds:\n $retstg\n\n";
}
if ( $using_xml != 1 ) {
// convert xml response back to hash
$retarr = $this->decodeXML($retstg);
// and send it back to caller
return ( $retarr );
} else {
// send server response back
return $retstg;
}
}
#####################################################
#
# F U N C T I O N c u r l _ p r o c e s s ( )
#
# process hash table or xml string table using
# curl, either with PHP built-in curl methods
# or binary executable curl
#
#####################################################
function curl_process( $data )
{
$using_xml = 0;
$webspace = 1;
if ( isset( $data["webspace"] ) ) {
if ( $data["webspace"] == "false" ) // if explicitly set to false, don't use html output
$webspace = 0;
}
if ( isset( $data["debugging"] ) || isset( $data["debug"] ) ) {
if ( $data["debugging"] == "true" || $data["debug"] == "true" ) {
$this->debugging = 1;
# print out incoming hash
if ( $webspace ) { // use html-friendly output
echo "at curl_process, incoming data: ";
while ( list( $key, $value ) = each( $data ) )
echo htmlspecialchars( $key ) . " = " . htmlspecialchars( $value ) . " \n";
} else { // don't use html output
echo "at curl_process, incoming data: \n";
while ( list( $key, $value ) = each( $data ) )
echo "$key = $value\n";
}
reset( $data );
}
}
if ( isset( $data["xml"] ) ) { // if XML string is passed in, we'll use it
$using_xml = 1;
$xml = $data["xml"];
} else {
// otherwise convert incoming hash to XML string
$xml = $this->buildXML( $data );
}
if ( $this->debugging ) {
if ( $webspace )
echo " sending xml string: " . htmlspecialchars( $xml ) . "
";
else
echo "\nsending xml string:\n$xml\n\n";
}
// set up transaction variables
$key = $data["keyfile"];
$port = $data["port"];
$host = $data["host"].":".$port."/LSGSXML";
if ( isset($data["cbin"]) ) { //using BINARY curl methods
if ( $data["cbin"] == "true" ) {
if ( isset( $data["cpath"] ) )
$cpath = $data["cpath"];
else { // curl path has not been set, try to find curl binary
if ( getenv("OS") == "Windows_NT" )
$cpath = "c:\\curl\\curl.exe";
else
$cpath = "/usr/bin/curl";
}
// look for $cargs variable, otherwise use default curl arguments
if ( isset($data["cargs"]) )
$args = $data["cargs"];
else
$args = "-m 300 -s -S"; // default curl args; 5 min. timeout
# TRANSACT #
if ( getenv("OS") == "Windows_NT" ) {
if ( $this->debugging )
$result = exec ( "$cpath -v -d \"$xml\" -E $key -k $host", $retarr, $retnum );
else
$result = exec ( "$cpath -d \"$xml\" -E $key -k $host", $retarr, $retnum );
} else { //*nix string
if ( $this->debugging )
$result = exec ( "'$cpath' $args -v -E '$key' -d '$xml' '$host'", $retarr, $retnum );
else
$result = exec ( "'$cpath' $args -E '$key' -d '$xml' '$host'", $retarr, $retnum );
}
# EVALUATE RESPONSE #
if ( strlen( $result ) < 2 ) { // no response
$result = "FAILURECould not connect.";
return $result;
}
if ( $this->debugging ) {
if ( $this->webspace )
echo " server responds: " . htmlspecialchars( $result ) . "
";
else // non html output
echo "\nserver responds:\n $result\n\n";
}
if ( $using_xml == 1 ) {
// return xml string straight from server
return ( $result );
} else {
// convert xml response back to hash
$retarr = $this->decodeXML( $result );
// and send it back to caller. Done.
return ( $retarr );
}
}
} else { // using BUILT-IN PHP curl methods
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $host );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );
curl_setopt( $ch, CURLOPT_SSLCERT, $key );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
if ( $this->debugging )
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
# use curl to send the xml SSL string
$result = curl_exec( $ch );
curl_close( $ch );
if ( strlen( $result ) < 2 ) { # no response
$result = "FAILURE 2Could not connect.";
return $result;
}
if ( $this->debugging ) {
if ( $webspace ) // html-friendly output
echo " server responds: " . htmlspecialchars( $result ) . "
";
else
echo "\nserver responds:\n $result\n\n";
}
if ( $using_xml ) {
# send xml response back
return $result;
} else {
#convert xml response to hash
$retarr = $this->decodeXML( $result );
# and send it back
return ( $retarr );
}
}
}
#############################################
#
# F U N C T I O N d e c o d e X M L ( )
#
# converts the LSGS response xml string
# to a hash of name-value pairs
#
#############################################
function decodeXML( $xmlstg )
{
preg_match_all ( "/<(.*?)>(.*?)\", $xmlstg, $out, PREG_SET_ORDER );
$n = 0;
while ( isset( $out[$n] ) )
{
$retarr[$out[$n][1]] = strip_tags( $out[$n][0] );
$n++;
}
return $retarr;
}
############################################
#
# F U N C T I O N b u i l d X M L ( )
#
# converts a hash of name-value pairs
# to the correct XML format for LSGS
#
############################################
function buildXML( $pdata )
{
// while (list($key, $value) = each($pdata))
// echo htmlspecialchars($key) . " = " . htmlspecialchars($value) . " \n";
### ORDEROPTIONS NODE ###
$xml = "";
if ( isset( $pdata["ordertype"] ) )
$xml .= "" . $pdata["ordertype"] . "";
if ( isset( $pdata["result"] ) )
$xml .= "" . $pdata["result"] . "";
$xml .= "";
### CREDITCARD NODE ###
$xml .= "";
if ( isset( $pdata["cardnumber"] ) )
$xml .= "" . $pdata["cardnumber"] . "";
if ( isset( $pdata["cardexpmonth"] ) )
$xml .= "" . $pdata["cardexpmonth"] . "";
if ( isset( $pdata["cardexpyear"] ) )
$xml .= "" . $pdata["cardexpyear"] . "";
if ( isset( $pdata["cvmvalue"] ) )
$xml .= "" . $pdata["cvmvalue"] . "";
if ( isset( $pdata["cvmindicator"] ) )
$xml .= "" . $pdata["cvmindicator"] . "";
if ( isset( $pdata["track"] ) )
$xml .= "";
$xml .= "";
### BILLING NODE ###
$xml .= "";
if ( isset( $pdata["name"] ) )
$xml .= "" . $pdata["name"] . "";
if ( isset( $pdata["company"] ) )
$xml .= "" . $pdata["company"] . "";
if ( isset( $pdata["address1"] ) )
$xml .= "" . $pdata["address1"] . "";
elseif ( isset( $pdata["address"] ) )
$xml .= "" . $pdata["address"] . "";
if ( isset( $pdata["address2"] ) )
$xml .= "" . $pdata["address2"] . "";
if ( isset( $pdata["city"] ) )
$xml .= "" . $pdata["city"] . "";
if ( isset( $pdata["state"] ) )
$xml .= "" . $pdata["state"] . "";
if ( isset( $pdata["zip"] ) )
$xml .= "" . $pdata["zip"] . "";
if ( isset( $pdata["country"] ) )
$xml .= "" . $pdata["country"] . "";
if ( isset( $pdata["userid"] ) )
$xml .= "" . $pdata["userid"] . "";
if ( isset( $pdata["email"] ) )
$xml .= "" . $pdata["email"] . "";
if ( isset( $pdata["phone"] ) )
$xml .= "" . $pdata["phone"] . "";
if ( isset( $pdata["fax"] ) )
$xml .= "" . $pdata["fax"] . "";
if ( isset( $pdata["addrnum"] ) )
$xml .= "" . $pdata["addrnum"] . "";
$xml .= "";
## SHIPPING NODE ##
$xml .= "";
if ( isset( $pdata["sname"] ) )
$xml .= "" . $pdata["sname"] . "";
if ( isset( $pdata["saddress1"] ) )
$xml .= "" . $pdata["saddress1"] . "";
if ( isset( $pdata["saddress2"] ) )
$xml .= "" . $pdata["saddress2"] . "";
if ( isset( $pdata["scity"] ) )
$xml .= "" . $pdata["scity"] . "";
if ( isset( $pdata["sstate"] ) )
$xml .= "" . $pdata["sstate"] . "";
elseif ( isset( $pdata["state"] ) )
$xml .= "" . $pdata["sstate"] . "";
if ( isset( $pdata["szip"] ) )
$xml .= "" . $pdata["szip"] . "";
elseif ( isset( $pdata["sip"] ) )
$xml .= "" . $pdata["zip"] . "";
if ( isset( $pdata["scountry"] ) )
$xml .= "" . $pdata["scountry"] . "";
if ( isset( $pdata["scarrier"] ) )
$xml .= "" . $pdata["scarrier"] . "";
if ( isset( $pdata["sitems"] ) )
$xml .= "" . $pdata["sitems"] . "";
if ( isset( $pdata["sweight"] ) )
$xml .= "" . $pdata["sweight"] . "";
if ( isset( $pdata["stotal"] ) )
$xml .= "" . $pdata["stotal"] . "";
$xml .= "";
### TRANSACTIONDETAILS NODE ###
$xml .= "";
if ( isset( $pdata["oid"] ) )
$xml .= "" . $pdata["oid"] . "";
if ( isset( $pdata["ponumber"] ) )
$xml .= "" . $pdata["ponumber"] . "";
if ( isset( $pdata["recurring"] ) )
$xml .= "" . $pdata["recurring"] . "";
if ( isset( $pdata["taxexempt"] ) )
$xml .= "" . $pdata["taxexempt"] . "";
if ( isset( $pdata["terminaltype"] ) )
$xml .= "" . $pdata["terminaltype"] . "";
if ( isset( $pdata["ip"] ) )
$xml .= "" . $pdata["ip"] . "";
if ( isset( $pdata["reference_number"] ) )
$xml .= "" . $pdata["reference_number"] . "";
if ( isset( $pdata["transactionorigin"] ) )
$xml .= "" . $pdata["transactionorigin"] . "";
if ( isset( $pdata["invoice_number"] ) )
$xml .= "" . $pdata["invoice_number"] . "";
if ( isset( $pdata["tdate"] ) )
$xml .= "" . $pdata["tdate"] . "";
$xml .= "";
### MERCHANTINFO NODE ###
$xml .= "";
if ( isset( $pdata["configfile"] ) )
$xml .= "" . $pdata["configfile"] . "";
if ( isset( $pdata["keyfile"] ) )
$xml .= "" . $pdata["keyfile"] . "";
if ( isset( $pdata["host"] ) )
$xml .= "" . $pdata["host"] . "";
if ( isset( $pdata["port"] ) )
$xml .= "" . $pdata["port"] . "";
if ( isset( $pdata["appname"] ) )
$xml .= "" . $pdata["appname"] . "";
$xml .= "";
### PAYMENT NODE ###
$xml .= "";
if ( isset( $pdata["chargetotal"] ) )
$xml .= "" . $pdata["chargetotal"] . "";
if ( isset( $pdata["tax"] ) )
$xml .= "" . $pdata["tax"] . "";
if ( isset( $pdata["vattax"] ) )
$xml .= "" . $pdata["vattax"] . "";
if ( isset( $pdata["shipping"] ) )
$xml .= "" . $pdata["shipping"] . "";
if ( isset( $pdata["subtotal"] ) )
$xml .= "" . $pdata["subtotal"] . "";
$xml .= "";
### CHECK NODE ###
if ( isset( $pdata["voidcheck"] ) ) {
$xml .= "1";
} elseif ( isset( $pdata["routing"] ) ) {
$xml .= "";
$xml .= "" . $pdata["routing"] . "";
if ( isset( $pdata["account"] ) )
$xml .= "" . $pdata["account"] . "";
if ( isset( $pdata["bankname"] ) )
$xml .= "" . $pdata["bankname"] . "";
if ( isset( $pdata["bankstate"] ) )
$xml .= "" . $pdata["bankstate"] . "";
if ( isset( $pdata["ssn"] ) )
$xml .= "" . $pdata["ssn"] . "";
if ( isset( $pdata["dl"] ) )
$xml .= "