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 (isset($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 = "https://".$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 = "FAILURECould 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 ("/<(.*?)>(.*?)\\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 .= "" . $pdata["track"] . ""; $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["state"] . ""; if (isset($pdata["szip"])) $xml .= "" . $pdata["szip"] . ""; elseif (isset($pdata["zip"])) $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["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 .= "
" . $pdata["dl"] . "
"; if (isset($pdata["dlstate"])) $xml .= "" . $pdata["dlstate"] . ""; if (isset($pdata["checknumber"])) $xml .= "" . $pdata["checknumber"] . ""; if (isset($pdata["accounttype"])) $xml .= "" . $pdata["accounttype"] . ""; $xml .= "
"; } ### PERIODIC NODE ### if (isset($pdata["startdate"])) { $xml .= ""; $xml .= "" . $pdata["startdate"] . ""; if (isset($pdata["installments"])) $xml .= "" . $pdata["installments"] . ""; if (isset($pdata["threshold"])) $xml .= "" . $pdata["threshold"] . ""; if (isset($pdata["periodicity"])) $xml .= "" . $pdata["periodicity"] . ""; if (isset($pdata["pbcomments"])) $xml .= "" . $pdata["pbcomments"] . ""; if (isset($pdata["action"])) $xml .= "" . $pdata["action"] . ""; $xml .= ""; } ### NOTES NODE ### if (isset($pdata["comments"]) || isset($pdata["referred"])) { $xml .= ""; if (isset($pdata["comments"])) $xml .= "" . $pdata["comments"] . ""; if (isset($pdata["referred"])) $xml .= "" . $pdata["referred"] . ""; $xml .= ""; } ### ITEMS AND OPTIONS NODES ### if ($this->debugging) // make it easy to see { // LSGS doesn't mind whitespace reset($pdata); while (list ($key, $val) = each ($pdata)) { if (is_array($val)) { $otag = 0; $ostag = 0; $items_array = $val; $xml .= "\n\n"; while(list($key1, $val1) = each ($items_array)) { $xml .= "\t\n"; while (list($key2, $val2) = each ($val1)) { if (!is_array($val2)) $xml .= "\t\t<$key2>$val2\n"; else { if (!$ostag) { $xml .= "\t\t\n"; $ostag = 1; } $xml .= "\t\t\t\n"; $otag = 0; } } if ($ostag) { $xml .= "\t\t\n"; $ostag = 0; } $xml .= "\t\n"; } $xml .= "\n"; } } } else // !debugging { while (list ($key, $val) = each ($pdata)) { if (is_array($val)) { $otag = 0; $ostag = 0; $items_array = $val; $xml .= ""; while(list($key1, $val1) = each ($items_array)) { $xml .= ""; while (list($key2, $val2) = each ($val1)) { if (!is_array($val2)) $xml .= "<$key2>$val2"; else { if (!$ostag) { $xml .= ""; $ostag = 1; } $xml .= ""; $otag = 0; } } if ($ostag) { $xml .= ""; $ostag = 0; } $xml .= ""; } $xml .= ""; } } } $xml .= "
"; return $xml; } } ?>