How to get the UPS Tracking number programatically on magento?

Posted: November 17, 2011 in Magento

First we will call the UPSRequest XML .It will give to response XML with shipmentdigestnumber.

After that we will call that UPSAccept Request XML it will give to response XML with UPS Tracking number, transport charge, service charge, etc.

Here i have attached with class file.

///Call the UPS XML file Class//
$upsclass=new upswebservicecall();
//Call the upsconfirmrequestxml function
$shipconfirresponse=$upsclass->upsconfirmrequest($cur_od_id,$shi_Addr_id,$descval,$code,$region_code);
$errornotificatio=$shipconfirresponse[’errornotification’];
$shipconfirdigenumber=$shipconfirresponse[’shipmentdigestnumber’];

//Call the upsacceptrequestxml function
$shipacceptrespone=$upsclass->upsacceptrequest($shipconfirdigenumber);
$ups_tracking_number=$shipacceptrespone[’Trackinnumber’];
$ups_shipmentidentification=$shipacceptrespone[’Shiidentifynum’];
$ups_transpotchare=$shipacceptrespone[’transportcharge’];
$ups_servicecharge=$shipacceptrespone[’servicecharge’];
$ups_totalcharge=$shipacceptrespone[’TotalCharges’];
$ups_negocharge=$shipacceptrespone[’negotatiedcharge’];
$ups_pickupnum=$shipacceptrespone[’pickrequestnum’];

Here i have wrote the class file….

 

<?php
class upswebservicecall
{
function upsconfirmrequest($order_id,$shipadd_id,$descval,$code,$region_code)
{

$connection = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);

$order = Mage::getModel(‘sales/order’);
$order->load($order_id);
$val=$order->getData();

$shiping_desc=$val[‘shipping_description’];

$shiping_addr_id=$val[‘shipping_address_id’];

 
$shipping_address = Mage::getModel(‘sales/order_address’);
$shipping_address->load($shiping_addr_id);
$valship=$shipping_address->getData();
$ship_companyname=$valship[‘company’];
$ship_name=$valship[‘firstname’];
$ship_phone=$valship[‘telephone’];
$ship_city=$valship[‘city’];
$ship_state=$valship[‘company’];
$ship_countrycode=$valship[‘country_id’];
$ship_postalcode=$valship[‘postcode’];
$ship_street=$valship[‘street’];

$store_name=Mage::app()->getStore()->getName();

$AccessLicenseNumber = ‘6C736EDBE5A1F6E0’; // Your license number
$UserId = ‘cgt777’; // Username
$Password = ‘meister’; // Password

// =============== DON’T CHANGE BELOW THIS LINE ===============

$data =”<?xml version=’1.0′?>
<AccessRequest xml:lang=’en-US’>
<AccessLicenseNumber>6C736EDBE5A1F6E0</AccessLicenseNumber>
<UserId>cgt777</UserId>
<Password>meister</Password>
</AccessRequest>
<ShipmentConfirmRequest xml:lang=’en-US’>
<Request>
<TransactionReference>
<CustomerContext>Customer Comment</CustomerContext>
<XpciVersion/>
</TransactionReference>
<RequestAction>ShipConfirm</RequestAction>
<RequestOption>validate</RequestOption>
</Request>
<LabelSpecification>
<LabelPrintMethod>
<Code>GIF</Code>
<Description>gif file</Description>
</LabelPrintMethod>
<HTTPUserAgent>Mozilla/4.5</HTTPUserAgent>
<LabelImageFormat>
<Code>GIF</Code>
<Description>gif</Description>
</LabelImageFormat>
</LabelSpecification>
<Shipment>
<RateInformation>
<NegotiatedRatesIndicator/>
</RateInformation>
<Description/>
<Shipper>
<Name>Edwin</Name>
<PhoneNumber>9003349450</PhoneNumber>
<ShipperNumber>8V948R</ShipperNumber>
<TaxIdentificationNumber>1234567877</TaxIdentificationNumber>
<Address>
<AddressLine1>12782 Monarch Street</AddressLine1>
<City>GARDEN GROVE</City>
<StateProvinceCode>CA</StateProvinceCode>
<PostalCode>92842</PostalCode>
<PostcodeExtendedLow></PostcodeExtendedLow>
<CountryCode>US</CountryCode>
</Address>
</Shipper>
<ShipTo>
<CompanyName>Fast stream Technologies</CompanyName>
<AttentionName>”.$ship_name.”</AttentionName>
<PhoneNumber>”.$ship_phone.”</PhoneNumber>
<Address>
<AddressLine1>”.$ship_street.”</AddressLine1>
<City>”.$ship_city.”</City>
<StateProvinceCode>”.$region_code.”</StateProvinceCode>
<PostalCode>”.$ship_postalcode.”</PostalCode>
<CountryCode>”.$ship_countrycode.”</CountryCode>
</Address>
</ShipTo>
<ShipFrom>
<CompanyName>Custom Greekth</CompanyName>
<AttentionName>Edwin</AttentionName>
<PhoneNumber>9600569055</PhoneNumber>
<TaxIdentificationNumber>1234567877</TaxIdentificationNumber>
<Address>
<AddressLine1>12782 Monarch Street</AddressLine1>
<City>GARDEN GROVE</City>
<StateProvinceCode>CA</StateProvinceCode>
<PostalCode>92842</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</ShipFrom>
<PaymentInformation>
<Prepaid>
<BillShipper>
<AccountNumber>8V948R</AccountNumber>
</BillShipper>
</Prepaid>
</PaymentInformation>
<Service>
<Code>”.$code.”</Code>
<Description>”.$descval.”</Description>
</Service>
<Package>
<PackagingType>
<Code>02</Code>
<Description>Customer Supplied</Description>
</PackagingType>
<Description>Package Description</Description>
<ReferenceNumber>
<Code>00</Code>
<Value>Package</Value>
</ReferenceNumber>
<PackageWeight>
<UnitOfMeasurement/>
<Weight>60.0</Weight>
</PackageWeight>
<LargePackageIndicator/>
<AdditionalHandling>0</AdditionalHandling>
</Package>
</Shipment>
</ShipmentConfirmRequest>”; // https://wwwcie.ups.com/ups.app/xml/ShipConfirm

$ch = curl_init(“https://wwwcie.ups.com/ups.app/xml/ShipConfirm&#8221;);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
// ‘<!– ‘. $result. ‘ –>’;
// THIS LINE IS FOR DEBUG PURPOSES ONLY-IT WILL SHOW IN HTML COMMENTS
$data = strstr($result, ‘<?’);
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem[‘type’] == ‘open’) {
if (array_key_exists(‘attributes’,$xml_elem)) {
list($level[$xml_elem[‘level’]],$extra) = array_values($xml_elem[‘attributes’]);
} else {
$level[$xml_elem[‘level’]] = $xml_elem[‘tag’];
}
}
if ($xml_elem[‘type’] == ‘complete’) {
$start_level = 1;
$php_stmt = ‘$params’;
while($start_level < $xml_elem[‘level’]) {
$php_stmt .= ‘[$level[‘.$start_level.’]]’;
$start_level++;
}
$php_stmt .= ‘[$xml_elem[\’tag\’]] = $xml_elem[\’value\’];’;
eval($php_stmt);
}
}
curl_close($ch);
//echo
$paramsxmlresponseval = array();
//return $params[‘RATINGSERVICESELECTIONRESPONSE’][‘RATEDSHIPMENT’][‘TOTALCHARGES’][‘MONETARYVALUE’];
$paramsxmlresponseval[‘customercontext’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘RESPONSE’][‘TRANSACTIONREFERENCE’][‘CUSTOMERCONTEXT’];
$paramsxmlresponseval[‘XPCIVERSION’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘RESPONSE’][‘TRANSACTIONREFERENCE’][‘XPCIVERSION’];
$paramsxmlresponseval[‘RESPONSESTATUSCODE’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘RESPONSE’][‘RESPONSESTATUSCODE’];
$paramsxmlresponseval[‘ResponseStatusDescription’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘RESPONSE’][‘RESPONSESTATUSDESCRIPTION’];
$paramsxmlresponseval[‘shipmentdigestnumber’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘SHIPMENTDIGEST’];
$paramsxmlresponseval[‘errornotification’]=$params[‘SHIPMENTCONFIRMRESPONSE’][‘RESPONSE’][‘ERROR’][‘ERRORDESCRIPTION’];
//ResponseStatusDescriptionShipmentDigest
/*echo “<pre>”;
print_r($params);
exit;*/
return $paramsxmlresponseval;

}

function upsacceptrequest($shidignum)
{
$AccessLicenseNumber = ‘6C736EDBE5A1F6E0’; // Your license number
$UserId = ‘cgt777’; // Username
$Password = ‘meister’; // Password

// =============== DON’T CHANGE BELOW THIS LINE ===============

$data =”<?xml version=’1.0′ encoding=’ISO-8859-1′?>
<AccessRequest>
<AccessLicenseNumber>6C736EDBE5A1F6E0</AccessLicenseNumber>
<UserId>cgt777</UserId>
<Password>meister</Password>
</AccessRequest>
<?xml version=’1.0′ encoding=’ISO-8859-1′?>
<ShipmentAcceptRequest>
<Request>
<TransactionReference>
<CustomerContext>Customer Comment</CustomerContext>
</TransactionReference>
<RequestAction>ShipAccept</RequestAction>
<RequestOption>1</RequestOption>
</Request>
<ShipmentDigest>”.$shidignum.”</ShipmentDigest>
</ShipmentAcceptRequest>
“;
$ch = curl_init(“https://wwwcie.ups.com/ups.app/xml/ShipAccept&#8221;);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
//echo ‘<!– ‘. $result. ‘ –>’;
// THIS LINE IS FOR DEBUG PURPOSES ONLY-IT WILL SHOW IN HTML COMMENTS
$data = strstr($result, ‘<?’);
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem[‘type’] == ‘open’) {
if (array_key_exists(‘attributes’,$xml_elem)) {
list($level[$xml_elem[‘level’]],$extra) = array_values($xml_elem[‘attributes’]);
} else {
$level[$xml_elem[‘level’]] = $xml_elem[‘tag’];
}
}
if ($xml_elem[‘type’] == ‘complete’) {
$start_level = 1;
$php_stmt = ‘$params’;

while($start_level < $xml_elem[‘level’]) {
$php_stmt .= ‘[$level[‘.$start_level.’]]’;
$start_level++;
}
$php_stmt .= ‘[$xml_elem[\’tag\’]] = $xml_elem[\’value\’];’;
eval($php_stmt);
}
}

$paramsxmlresponsevalacc = array();
$paramsxmlresponsevalacc[‘Trackinnumber’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘PACKAGERESULTS’][‘TRACKINGNUMBER’];
$paramsxmlresponsevalacc[‘Shiidentifynum’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘SHIPMENTIDENTIFICATIONNUMBER’];
$paramsxmlresponsevalacc[‘pickrequestnum’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘PICKUPREQUESTNUMBER’];
$paramsxmlresponsevalacc[‘Labelimagehtmlimage’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘PACKAGERESULTS’][‘LABELIMAGE’][‘HTMLIMAGE’];
$paramsxmlresponsevalacc[‘Graphicimage’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘PACKAGERESULTS’][‘LABELIMAGE’][‘GRAPHICIMAGE’];
$paramsxmlresponsevalacc[‘transportcharge’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘SHIPMENTCHARGES’][‘TRANSPORTATIONCHARGES’][‘MONETARYVALUE’];
$paramsxmlresponsevalacc[‘servicecharge’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘SHIPMENTCHARGES’][‘SERVICEOPTIONSCHARGES’][‘MONETARYVALUE’];
$paramsxmlresponsevalacc[‘TotalCharges’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘SHIPMENTCHARGES’][‘TOTALCHARGES’][‘MONETARYVALUE’];
$paramsxmlresponsevalacc[‘negotatiedcharge’]=$params[‘SHIPMENTACCEPTRESPONSE’][‘SHIPMENTRESULTS’][‘NEGOTIATEDRATES’][‘NETSUMMARYCHARGES’][‘GRANDTOTAL’][‘MONETARYVALUE’];
/*echo “<pre>”;
print_r($params);
exit;*/
//print_r(base64_decode($params));*/
//ShipmentIdentificationNumber

$data_xml=”<?xml version=’1.0′ encoding=’ISO-8859-1′?>
<ShipmentAcceptResponse>
<Response>
<TransactionReference>
<CustomerContext>Customer Comment</CustomerContext>
</TransactionReference>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusDescription>Success</ResponseStatusDescription>
</Response>
<ShipmentResults>
<ShipmentCharges>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>118.48</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>118.48</MonetaryValue>
</TotalCharges>
</ShipmentCharges>
<NegotiatedRates>
<NetSummaryCharges>
<GrandTotal>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>115.14</MonetaryValue>
</GrandTotal>
</NetSummaryCharges>
</NegotiatedRates>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>90.0</Weight>
</BillingWeight>
<ShipmentIdentificationNumber>1Z2220060292353829</ShipmentIdentificationNumber>
<PickupRequestNumber>297BL02P1A3</PickupRequestNumber>
<PackageResults>
<TrackingNumber>1Z2220060292353829</TrackingNumber>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<LabelImage>
<LabelImageFormat>
<Code>GIF</Code>
</LabelImageFormat>
<GraphicImage>”.$paramsxmlresponsevalacc[‘Graphicimage’].”</GraphicImage>
<HTMLImage>”.$paramsxmlresponsevalacc[‘Labelimagehtmlimage’].”</HTMLImage>
</LabelImage>
</PackageResults>
</ShipmentResults>
</ShipmentAcceptResponse>
“;
$f=fopen(“Upsimage/ShipAccept_Tool_SampleResponse.xml”,”w”);
$dat=fwrite($f,$data_xml);
fclose($f);

return $paramsxmlresponsevalacc;
}
}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s