logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: [ILUG-Webdev] PHP XML conversion class: msg#00002

Subject: Re: [ILUG-Webdev] PHP XML conversion class
Hi Adam,

What I use for this simple stuff is SimpleXML function in PHP. (http://ie.php.net/simplexml). Alos please anyone have a look at the class attached as well, while we're on Simple XML and the like. I am trying to figure out how to get the post payment method to retun false when there is no response form the server or if the server is down. Anyhelp welcome.

HTH  (If not call me :)
AJ

<?php
// Load our XMl into a string var
$xmlstuff = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<packet version="1.3.1.0">
<client>
 <add>
  <gen_info>
   <pname>Test User</pname>
   <login>tuser</login>
   <passwd>testing</passwd>
   <phone>123456</phone>
   <email>user@xxxxxxx</email>
   <address>Address</address>
   <city>City</city>
   <state>Cork</state>
   <country>IE</country>
  </gen_info>
 </add>
</client>
</packet>
XML;
// Use Simple XML here
$xmlstuff = simplexml_load_string($xmlstuff);
// Get the value of login
$login = $xmlstuff->client[0]->add[0]->gen_info[0]->login;
echo $login;
?>
p.S. Assume Copyright me but licence GPL.

<?php
/**
* Provides user Payment autorisation and other payment methods
* @author               AJ McKee <aj.mckee@xxxxxxxxxxxxx>
* @version              0.1
* @since                0.1
* @access               public
* @package              Payment Manager
* @copyright            druid-dns.com
*/
class PaymentManager {
        
        /**
        * Unix timestamp used throught out the class.
        * @var int $Timestamp The time in yyyymmddhhmmss format, e.g. 
20050130123359 Year 2005, 01 Month, 30th Day, 12th Hour, 33rd Min 59th Second.
        * @version 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        private $Timestamp = null;
        
        /**
        * Order ID that will apply to this transaction
        * @var int $Timestamp plus random numbers
        * @version 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        public $OrderID = null;
        
        /**
* * @var string $ReturnedXML the response from the payment provider.
        * @version 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        public $ReturnedXML = null;

        
        /**
        * This constructor sets up the OrderID and Timestamp for use within this
        * class.
        * @version 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access public (Must be public as its a constructor)
        */      
        public function __construct() {
                $this->Timestamp = strftime("%Y%m%d%H%M%S");
                mt_srand((double)microtime()*10000000);
                $this->OrderID = $this->Timestamp."-".mt_rand(1, 9999999);
        }
        
        /**
        * 
ProcessPayment($MerchantID,$Account,$AutoSettle,$RequestType,$Currency,$Amount,$CardType,$CardNumber,$CardExpiryDate,$CardHoldersName,$CardSecurityCode,$PaymentServer);
        *
        * This method takes inthe required credit card and merchant information 
and calls the private methods of the object to validate the credit card
        * and perform the trasaction.
        * @param string $MerchantID The merchant ID to use for this transaction.
        * @param string $Account The account the payment is to be assigned to.
        * @param bool $AutoSettle wether or not this transaction is to be 
settled imediatly or not.
        * @param string $RequestType The type of request being generated. Valid 
types are Auth, Rebate, Settle and void.
        * @param string $Currency The currency that this transaction is to be 
carried out in.
        * @param int $Amount The mount of money this trasaction is for.
        * @param string $CardType The type of credit card that is being used 
for this transaction.
        * @param int $CardNumber The credit card number to be used.
        * @param int $CardExpiryDate The card expiry date in MMYY format.
        * @param string $CardHoldersName The name on the credit card.
        * @param int $CardSecurityCode The security code found on the card.
        * @param string $PaymentServer The address of the payment server to use.
        * @version 0.1
        * @since 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access public
        */
        public function 
ProcessPayment($MerchantID,$Secret,$Account,$AutoSettle,$RequestType,$Currency,$Amount,$CardType,$CardNumber,$CardExpiryDate,$CardHoldersName,$CardSecurityCode,$PaymentServer)
 {
                // Generate an MD5Hash first
                $MD5Hash= 
$this->GenerateMD5Hash($this->Timestamp,$MerchantID,$this->OrderID,$Amount,$Currency,$CardNumber,$Secret);
                $XML = 
$this->GenerateRequest($RequestType,$this->Timestamp,$AutoSettle,$MerchantID,$Account,$this->OrderID,$Currency,$Amount,$CardType,$CardNumber,$CardExpiryDate,$CardHoldersName,$CardSecurityCode,$MD5Hash);
                $this->ReturnedXML = 
simplexml_load_string($this->PostTransaction($XML,$PaymentServer));
                return $this->ReturnedXML;
        }

        /**
        * 
GenerateRequest($RequestType,$Timestamp,$AutoSettle,$MerchantID,$Account,$OrderID,$Currency,$Amount,$CardType,$CardNumber,$CardExpiryDate,$CardHoldersName,$Md5Hash)
        *
        * This method, takes in the given parameters and formats the relavant 
XML query that is to be passed
        * onto the payment gateway.
        * @param string $RequestType The type of request being generated. Valid 
types are Auth, Rebate, Settle and void.
        * @param int $Timestamp The time in yyyymmddhhmmss format, e.g. 
20050130123359 Year 2005, 01 Month, 30th Day, 12th Hour, 33rd Min 59th Second.
        * @param bool $AutoSettle weter or not this transaction is to be 
settled imediatly or not.
        * @param string $MerchantID The merchant ID to use for this transaction.
        * @param string $Account The account the payment is to be assigned to.
        * @param int $OrderID The order id that this trasaction is to use.
        * @param string $Currency The currency that this transaction is to be 
carried out in.
        * @param int $Amount The mount of money this trasaction is for.
        * @param string $CardType The type of credit card that is being used 
for this transaction.
        * @param int $CardNumber The credit card number to be used.
        * @param int $CardExpiryDate The card expiry date in MMYY format.
        * @param string $CardHoldersName The name on the credit card.
        * @param int $CardSecurityCode The security code found on the card.
        * @param string $Md5Hash The MD5hash for this transaction.
        * @return string $xml The request in properly formatted XML
        * @version 0.1
        * @since 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        private function 
GenerateRequest($RequestType,$Timestamp,$AutoSettle,$MerchantID,$Account,$OrderID,$Currency,$Amount,$CardType,$CardNumber,$CardExpiryDate,$CardHoldersName,$CardSecurityCode,$Md5Hash)
 {
                // If Request type is Auth
                if($RequestType == "auth") {          
                        $xml = "<request type='auth' timestamp='$Timestamp'>
                                                
<merchantid>$MerchantID</merchantid>
                                                <account>$Account</account>
                                                <orderid>$OrderID</orderid>
                                                <amount 
currency='$Currency'>$Amount</amount>
<card> <number>$CardNumber</number>
                                                                
<expdate>$CardExpiryDate</expdate>
                                                                
<type>$CardType</type>
                                                                <cvn>
                                                                        
<number>$CardSecurityCode</number>
                                                                        
<presind>1</presind>
</cvn> <chname>$CardHoldersName</chname> </card> <autosettle flag='$AutoSettle'/>
                                                <md5hash>$Md5Hash</md5hash>
                                                <tssinfo>
                                                        
<custipaddress>".$_SERVER['REMOTE_ADDR']."</custipaddress>
                                                        <address 
type=\"billing\">
                                                                
<country>ie</country>
                                                        </address>
                                                </tssinfo>
                                        </request>";
                        return $xml;
                }
                return false;
        }
        
        /**
        * GenerateMD5Hash ($Timestamp, $MerchantID, $OrderID, $Amount, 
$Currency, $CardNumber)
        *
        * Generates a unique MD5Hash that is used in the trasaction to verify 
data.
        * @param int $Timestamp The time in yyyymmddhhmmss format, e.g. 
20050130123359 Year 2005, 01 Month, 30th Day, 12th Hour, 33rd Min 59th Second.
        * @param string $MerchantID The merchant ID to use for this transaction.
        * @param int $OrderID The order id that this trasaction is to use.
        * @param int $Amount The mount of money this trasaction is for.
        * @param string $Currency The currency that this transaction is to be 
carried out in.
        * @param int $CardNumber The credit card number to be used.
        * @param string $Secret The secret assigned to the merchant ID.
        * @return string $MD5Hash The MD5 Hash to be used.
        * @version 0.1
        * @since 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        private function GenerateMD5Hash ($Timestamp, $MerchantID, $OrderID, 
$Amount, $Currency, $CardNumber, $Secret) {
                $tmp = 
"$Timestamp.$MerchantID.$OrderID.$Amount.$Currency.$CardNumber";
                $MD5Hash = md5($tmp);
                $tmp = "$MD5Hash.$Secret";
                $MD5Hash= md5($tmp);
                return $MD5Hash;
        }
        
        /**
        * PostTransaction ($xml,$PaymentServer)
        *
        * PostTransaction simply takes in the XML that has been generated
        * and submits it to the payment server for processing.
        * @param string $xml The XML that is to be sent to the server
        * @param string $PaymentServer The URL of the Payment server.
        * @return mixed $results if transaction was submitted and a result 
returned. False otherwise.
        * @version 0.1
        * @since 0.1
        * @author AJ McKee <aj.mckee@xxxxxxxxxxxxx>
        * @access private
        */
        private function PostTransaction ($xml,$PaymentServer) {
                // Set our types.
                settype($xml, "string");
                settype($PaymentServer, "string");
                // Begin CURL.
$curl = curl_init(); // CURL Options.
                curl_setopt($curl, CURLOPT_URL, $PaymentServer);
curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_USERAGENT, "VEC - VECOCA Self Contained Payment Class for Realex aj.mckee@xxxxxxxxxxxxxx"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 600);
                // Execute the CURL command and get the result.
$response = curl_exec ($curl); curl_close ($curl); /**
                * @todo Sort this out, should return false if the friggin
                * payment gateway is down or worse, we are down.
                */
//              if(!$response) {
//                      return false;
//              }
                return $response;
        }
        


        /*
        * VerifyResponse()
        *
        * This method verifies the data returned from the payment server to 
ensure that the data
        * was not corrupted or comprimised on the way back.
        */
        private function VerifyResponse() {
                /**
                * @todo Complete method for the payment gateway response
                * verification.
                */
        }

}
?>

adam beecher wrote:

Any /very/ simple PHP scripts or classes out there for converting XML into
an array or object? (Even better if it can convert in the other direction
too.)

The XML won't get much more complex than the examples below, so I don't want
any advanced functionality. (Or `crap`, as I like to call it.)

"XML for Dummies" tips/links welcome too. I haven't bothered my arse to look
into it in any detail yet, but really have to.

Thanks,
adam


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<packet version="1.3.1.0">
<server>
 <get>
  <filter>1</filter>
  <gen_info/>
 </get>
</server>
</packet>


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<packet version="1.3.1.0">
<client>
 <add>
  <gen_info>
   <pname>Test User</pname>
   <login>tuser</login>
   <passwd>testing</passwd>
   <phone>123456</phone>
   <email>user@xxxxxxx</email>
   <address>Address</address>
   <city>City</city>
   <state>Cork</state>
   <country>IE</country>
  </gen_info>
 </add>
</client>
</packet>


_____________
BEECHER.NET
ICT Services



--
ILUG Web Development
http://mail.linux.ie/mailman/listinfo/webdev/



<Prev in Thread] Current Thread [Next in Thread>