SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Nottingham, UK
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to post XML to URL in Perl

    Hello,

    I need to post some XML I have in a variable (see below just a snippet of the XML in $xml_string) to a url for credit card verification and then receive a response. I've looked on the forum but I can't find anything about this. Could someone please point me in the right direction.
    THanks for any help.

    my $xml_string ="<?xml version='1.0'?>\n<!DOCTYPE paymentService PUBLIC '-//Streamline/DTD Streamline PaymentService v1//EN''http://dtd.streamline-esolutions.com/paymentService_v1.dtd'>\n<paymentService version ='1.0' merchantCode='DEMO'>\n<submit>\n <order orderCode = 'code24'>\n</order>\n</submit>\n</paymentService>";

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    shouldn't you be using a form to send data to the other server? They must have some instructions about how to communicate with their applications.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Nottingham, UK
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THanks for the reply.
    The problem is the whole order process is written in Lasso.
    I want pass variables including the XML into a perl cgi script (which I've done) and then send it for verification. They then send back an XML file with details of the result that I have to process, and then I have to send on again to for the user to choose a payment selection method.
    Have you any ides? They give examples in PHP, ASP and Java but not in Perl.
    Here is the PHP version in the code below. I don't know if I'm thinking the right way about this.
    One option is to do it all in Lasso but I want to move away from Lasso.
    How can I do it in Perl?

    Code:
    <?php
    
    $merchantCode="YourMerchantCode";  // Don't put this in a public readable place
    $password="YourMerchantPassword";  // Don't put this in a public readable place
    $orderCode="example".time();
    
    // Shopper specific details
    $shopperEmailAddress="myemail@demo.com";
    $shopperID="shopper-123456";
    $firstName="John";
    $lastName="Doe";
    $shopperStreet="11 Hereortherestreet";
    $postalCode="1234 KL";
    $shopperCity="myCiti";
    $shopperTelephone="00123456789";
    $countryCode="TP";
    
    $url ="https://$merchantCode:$password@secure-test.streamline-esolutions.com/jsp/merchant/xml/paymentService.jsp";//it is better to keep this url outside your HTML dir which has public (internet) access
    
    //$xml is the order string to send to Streamline
    $xml="
    
    
    <?xml version='1.0'?>
    <!DOCTYPE paymentService PUBLIC '-//Streamline/DTD Streamline PaymentService v1//EN' 'http://dtd.streamline-esolutions.com/paymentService_v1.dtd'>
    <paymentService version='1.0' merchantCode='DEMO'>
    <submit>
    <order orderCode = '".$orderCode."'>
    <description>PDD webshop</description>
    <amount value='1982' currencyCode = 'EUR' exponent = '2'/>
    <orderContent>
    <![CDATA[
    <center><table>
    <tr><td bgcolor='#CCCCCC'>Your Internet Order:</td><td colspan='2' bgcolor='#ffff00' align='right'>AY 845</td></tr>
    <tr><td bgcolor='#ffff00'>Description:</td><td>14 Tulip bulbs</td><td align='right'>1,00</td></tr>
    <tr><td colspan='2'>Subtotal:</td><td align='right'>14,00</td></tr>
    <tr><td colspan='2'>VAT: 13%</td><td align='right'>1,82</td></tr>
    <tr><td colspan='2'>Shipping and Handling:</td><td align='right'>4,00</td></tr>
    <tr><td colspan='2' bgcolor='#c0c0c0'>Total cost:</td><td bgcolor='#c0c0c0' align='right'>Euro 19,82</td></tr>
    <tr><td colspan='3'>&nbsp;</td></tr>
    <tr><td bgcolor='#ffff00' colspan='3'>Your billing address:</td></tr>
    <tr><td colspan='3'>Mr. $lastName,<br>$shopperStreet,<br>$postalCode $shopperCity,<br>Thisplace.</td></tr>
    <tr><td colspan='3'>&nbsp;</td></tr>
    <tr><td bgcolor='#ffff00' colspan='3'>Your shipping address:</td></tr>
    <tr><td colspan='3'>Mr. $lastName,<br>$shopperStreet,<br>$postalCode $shopperCity,<br>Thisplace.</td></tr>
    <tr><td colspan='3'>&nbsp;</td></tr>
    <tr><td bgcolor='#ffff00' colspan='3'>Our contact information:</td></tr>
    <tr><td colspan='3'>ACME Webshops Int. Inc.,<br>11 Strangewood Blv.,<br>1255 KZ Thisisit,<br>Nowhereatall.<br><br>acmeweb@acme.inc<br>(555) 1235 456</td></tr>
    <tr><td colspan='3'>&nbsp;</td></tr>
    <tr><td bgcolor='#c0c0c0' colspan='3'>Billing notice:</td></tr>
    <tr><td colspan='3'>Your payment will be handled by Streamline Global Payments Services<br>This name may appear on your bank statement<br>http://www.Streamline.com</td></tr>
    </table></center>
    ]]>
    </orderContent>
    <paymentMethodMask>
    <include code='ALL'/>
    </paymentMethodMask>
    <shopper>
    <shopperEmailAddress>$shopperEmailAddress</shopperEmailAddress> <authenticatedShopperID>$shopperID</authenticatedShopperID>
    </shopper>
    <shippingAddress>
    <address>
    <firstName>$firstName</firstName>
    <lastName>$lastName</lastName>
    <street>$shopperStreet</street>
    <postalCode>$postalCode</postalCode>
    <city>$shopperCity</city>
    <countryCode>$countryCode</countryCode>
    <telephoneNumber>$shopperTelephone</telephoneNumber>
    </address>
    </shippingAddress>
    </order>
    </submit>
    </paymentService>";//$xml is the order string to send to Streamline
    
    
    //let's make the socket connection
    
    //make socket connection with Streamline
    //the curl library is used. make sure you have it installed propperly
    // more info: http://www.php.net/manual/en/ref.curl.php
    $ch = curl_init ($this->url);
                     curl_setopt($ch, CURLOPT_POST,1);
                     curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);   //$xml is the xml string
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                     curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
    
                    // echo "ch: $ch<HR>" ;
    
                     $result = curl_exec ($ch); // result will contain XML reply from Streamline curl_close ($ch);
                    if ( $result == false )
                    {
                            $this->error = "Curl could not retrieve page '$this->url', curl_exec returns false";               }
                    return $result;
            }
    
    
    //now we have the result from Streamline containing the xml answer. we need to parse this through the XML parser
    // initialize parser //using SAX parser
    //
    //more info on SAX/PHP on these urls
    // http://www.devshed.com/Server_Side/XML/XMLwithPHP/XMLwithPHP1/page1.html
    // http://www.php.net/manual/en/ref.xml.php
    
    
    function startElement($parser, $name, $attrs) 
    {
    	global $currentTag,$ordercode,$referenceID,$errorcode,$url_togoto;
    	$currentTag = $name;
    
    	switch ($name) {
    		case "ERROR": 
    
    /*	
    THERE IS AN XML ERROR REPLY
    1 : internal error, could be everything
    2 : parse error, invalid xml
    3 : invalid number of transactions in batch
    4 : security error
    5 : invalid request
    6 : invalid content, occurs when xml is valid but content of xml not
    7 : payment details in the order element are incorrect
    */
    		$errorcode = $attrs['CODE']; //example of how to catch the error code number (i.e. 1 to 7)
    		$url_error="error_order.php";
    			break;
    
    		case "REFERENCE":
    			$referenceID = $attrs['ID'];//for storage in your own database
    			break;
    			
    		case "ORDERSTATUS":
    			$ordercode = $attrs['ORDERCODE'];
    			break;
    			
    		default:
    			break;
    	}
    }
    
    
    //////////////////
    function endElement($parser, $name) {
    	global $currentTag;
    	$currentTag = "";
    }
    
    /////////////////
    function characterData($parser, $result) {
    
    	global $currentTag;
    	global $url_togoto;
    
    	switch ($currentTag) {
    
    	case "REFERENCE":
    		//there is a REFERENCE so there must be an url which was provided by Streamline for the actual payment. echo $result;
    		$url_togoto=$result;
    		break;
    
    	default:
    		break;
    	}
    }
    
    global $currentTag,$ordercode,$referenceID,$errorcode,$url_togoto;//define globals
    $xml_parser = xml_parser_create();
    
    // set callback functions
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    
    if (!xml_parse($xml_parser, $result))
    {
        die(sprintf("XML error: %s at line %d",
    	xml_error_string(xml_get_error_code($xml_parser)),
    	xml_get_current_line_number($xml_parser)));
    }
    
    // clean up
    xml_parser_free($xml_parser);
    //now we have a few important variables. Depending on what variables are set you can now perform an action like storing id's in your database or refer to a different url.
    echo "ordercode=$ordercode<BR>";
    echo "referenceID=$referenceID<BR>";
    echo "errorcode=$errorcode<BR>";
    echo "url_togoto=$url_togoto<BR>";
    
    ?>

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure it could be done, there are a number of xml parsing modules on cpan and you can use a module like LWP or LWP::Simple or WWW::Mechanize to send/recieve data, but how that all hooks into what you are trying to do or the code you posted, I don't know.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Nottingham, UK
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THanks Kevin,

    THis I found comes close to what I'm trying to do if anyone is interested -
    http://www.archivesat.com/New_Perl-X...ead2278232.htm
    but my latest idea is to use a Java example that the place I need to send the XML to provide, which should fit in with how our system works now and in the future.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •