Code Sample

RSS
Modified on 2016/05/23 14:38 by cjones Paths: Integration Categorized as Uncategorized

Code Sample Library

Posting Order XML with ASP

HTTP Post using Microsofts' XMLDOM object. If you're using ASP, you can easily post orders directly from your server and bypass the need for HTML forms.

The following is a sample of how to do this in ASP using VBScript:

' –– Create the object 
Set xmlPost = server.CreateObject ("MSXML2.XMLHTTP")
 
' –– Open a "POST" connection to the EZPrints Server. 
xmlPost.Open "POST",

"http://order.ezprints.com/PostXmlOrder.axd?PartnerNumber=XXX &PartnerReference=yyy" or

"https://order.ezprints.com/PostXmlOrder.axd?PartnerNumber=XXX &PartnerReference=yyy"


PartnerNumber corresponds to the partnerID and PartnerReference is the orderid you use (the orderid you are passing to us). You get a GUID (ezpreferencenum) in response. All XML is received, then unmarshalled into our order system later. You will receive immediate feedback if the order posted was not successful. The EZP orderid is assigned to your order when the XML is unmarshalled.

' –– send the xml 
xmlPost.send strXML

' –– Retrieve the response Response.Write xmlPost.ResponseText

The above code assumes that strXML is the string you built from your order data in the format described in Order XML Format section.

Sample function for posting XML in VB.Net

Public Shared Function PostString(ByVal Data As String, ByVal Url As String, _
  ByVal encode As Encoding) As String
    Dim result As String
    Dim webRequest As WebRequest = WebRequest.Create(Url)
    webRequest.Method = "POST"
    Dim bdata As Byte() = encode.GetBytes(Data)
    webRequest.ContentLength = bdata.Length
    Using requestStream As Stream = webRequest.GetRequestStream
        requestStream.Write(bdata, 0, bdata.Length)
    End Using
    Using webResponse As WebResponse = webRequest.GetResponse
        Using memoryStream As MemoryStream = New MemoryStream
            Dim length As Long = FileTransfer.TransferBytes(webResponse.GetResponseStream, memoryStream, _
               &H2000, Nothing, New TimeSpan(0, 5, 0), Nothing, Nothing, New WaitHandle(0  - 1) {})
            result = encode.GetString(memoryStream.GetBuffer, 0, CInt(length))
        End Using
    End Using
    Return result
End Function
 
Public Shared Function PostString(ByVal Data As String, ByVal Url As String) As String
    Return WebHelper.PostString(Data, Url, Encoding.UTF8)
End Function

If you put these two functions in a class and then call the 2nd function with your string and URL, it will return the result.

Posting Order XML with PHP

Sample PHP code that will post an order using the text/xml content type to EZPrints Order Collection and pull out the reference number:

 
<?php
$PartnerNumber = '1'; // Your Partner Number Here
$PartnerReference = '––Your Order Reference Here––';
$OrderXml = '<Order Test="true">––Your Order XML Here––</Order>';
$urlConn = curl_init ("https://order.ezprints.com/PostXmlOrder.axd?
  PartnerNumber=".$PartnerNumber."&PartnerReference=".urlencode($PartnerReference));
curl_setopt ($urlConn, CURLOPT_POST, 1);
curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type", "text/xml"));
curl_setopt ($urlConn, CURLOPT_POSTFIELDS, $OrderXml);
curl_setopt ($urlConn, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($urlConn);
if (empty($result)) {
  print "ERROR: " . curl_error($urlConn) . "\n";
  exit;
}
// Parse the response
$xml = simplexml_load_string($result);
echo "Obj:\r\n";
print_r($xml);
echo "Reference=";
echo $xml->attributes()->Reference;
?>


Here is essentially the same sample PHP that will post an order using multipart/form-data content type to G2 Order Collection and pull out the reference number:

<?php
$PartnerNumber = '1'; // Your Partner Number Here
$PartnerReference = '––Your Order Reference Here––';
$OrderXml = '<Order Test="true">––Your Order XML Here––</Order>';
$OrderXml = 'xml='.urlencode($OrderXml);
$urlConn = curl_init ("https://order.ezprints.com/PostXmlOrder.axd?
  PartnerNumber=".$PartnerNumber."&PartnerReference=".urlencode($PartnerReference));
curl_setopt ($urlConn, CURLOPT_POST, 1);
// Can use application/x-www-urlencoded as well here
curl_setopt ($urlConn, CURLOPT_HTTPHEADER, array("Content-type", "multipart/form-data"));
curl_setopt ($urlConn, CURLOPT_POSTFIELDS, $OrderXml);
curl_setopt ($urlConn, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($urlConn, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($urlConn);
if (empty($result)) {
  print "ERROR: " . curl_error($urlConn) . "\n";
  exit;
}
// Parse the response
$xml = simplexml_load_string($result);
echo "Obj:\r\n";
print_r($xml);
echo "Reference=";
echo $xml->attributes()->Reference;
?>

Receive and Parse Event Notifications with PHP

<?php
$xml=$HTTP_RAW_POST_DATA;
$obj=simplexml_load_string($xml);
echo "Obj:\r\n";
print_r($obj);
echo "Test=";
echo $obj->attributes()->Test;
?>

When this XML is posted:

<Order Test="true" />

It responds by printing out the entire xml object and then getting the Test attribute of the root element and its value. Link to the documentation for the SimpleXML functions the above sample code uses: http://us3.php.net/manual/en/book.simplexml.php

Posting to the Shipping Calculator with Perl

// Generate an XML stringto send to EZPrints. $doc is a DOMDocument populated with
// required EZPrints data. Alternatively the $ezPrintsXML string can be generated manually.

$ezPrintsXML = $doc->saveXML();

// create a header array that specifies a blank content type. $header[] = "Content-type:";

// initialize CURL to use the EZPrints URL $ch = curl_init("http://g1order.ezprints.com/ezpartners/shippingcalculator/xmlshipcalc.asp"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Returns response data instead of TRUE curl_setopt($ch, CURLOPT_POST, true); // specify that POST should be used curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // set the customized headers curl_setopt($ch, CURLOPT_POSTFIELDS, $ezPrintsXML); // use HTTP POST to send data $resp = curl_exec($ch); //execute post and get results as string into $resp variable curl_close ($ch); // close the curl object

// $resp is the response from EZPrints. This will output that result

echo $resp;

Calculating Panoramic Print Prices with JavaScript

Pano Length = (Pano Height x Image Length)/Image Height. So if you want to make a 10" high pano with a 1000x2000 image the length will be: (10 x 2000)/1000 = 20" long. ezprints will bill in 6" increments with a minimum of 12" so if the price for a 10" panorama is $2.00, the charge would be: Ceiling(20/6) * $2.00 or 4 * $2.00 = $8.00. Here is some actual JavaScript code that calculates this:

var panoPrice = skuHash.get( sku ).price;
var panoLength = " ";
var panoInches = 0;
var qty6InSegments = "";
var imgWidth = Number( item.originalWidth );
var imgHeight =  Number( item.originalHeight );
var paperWidthInches = parseInt( skuHash.get( sku ).description );
var segmentLength = 6;
if( imgWidth > imgHeight )
            panoInches = ( imgWidth/imgHeight ) * paperWidthInches;
else
            panoInches = ( imgHeight/imgWidth ) * paperWidthInches;
qty6InSegments = Math.ceil(panoInches / segmentLength );
if (qty6InSegments < 2) qty6InSegments = 2;       // Minimum length is 12 inches.

panoPrice = ( skuHash.get( sku ).price * qty6InSegments) panoLength = Math.round(panoInches) + ' in long';