0 Replies Latest reply on Jan 8, 2008 2:50 PM by lucas131313

    UPS Rates interface

    lucas131313
      //After a long and frustrating try and mistake week, I got to get the naughty UPS Rate system to talk to my flex application; I tough someone will appreciate to have it ready to go...

      // the request:
      private function builtXMLRates():void
      {
      var toSend:String='';
      toSend='<?xml version="1.0"?>';
      toSend+="<AccessRequest xml:lang='en-US'>";
      // remove the square brackets
      toSend+='<AccessLicenseNumber>[ NOT THE DEVELOPPER KEY!!! ]</AccessLicenseNumber>';
      toSend+='<UserId>[ YOUR UPS ID ]</UserId>';
      toSend+='<Password>[YOUR UPS PASS ]</Password>';
      toSend+='</AccessRequest>';

      toSend+='<?xml version="1.0"?>';
      toSend+="<RatingServiceSelectionRequest xml:lang='en-US'>";
      toSend+='<Request>';
      toSend+='<TransactionReference>';
      toSend+='<CustomerContext>Rating and Service</CustomerContext>';
      toSend+='<XpciVersion>1.0</XpciVersion>';
      toSend+='</TransactionReference>';
      toSend+='<RequestAction>Rate</RequestAction>';
      toSend+='<RequestOption>Shop</RequestOption>';
      toSend+='</Request>';
      toSend+='<PickupType>';
      toSend+='<Code>07</Code>';
      toSend+='<Description>Rate</Description>';
      toSend+='</PickupType>';
      toSend+='<Shipment>';
      toSend+='<Description>Rate Description</Description>';
      toSend+='<Shipper>';
      toSend+='<Name>Name</Name>';
      toSend+='<PhoneNumber>1234567890</PhoneNumber>';
      toSend+='<ShipperNumber>Ship Number</ShipperNumber>';
      toSend+='<Address>';
      toSend+='<AddressLine1>Address Line</AddressLine1>';
      toSend+='<City>City</City>';
      toSend+='<StateProvinceCode>NJ</StateProvinceCode>';
      toSend+='<PostalCode>07430</PostalCode>';
      toSend+='<CountryCode>US</CountryCode>';
      toSend+='</Address>';
      toSend+='</Shipper>';
      toSend+='<ShipTo>';
      toSend+='<CompanyName>Company Name</CompanyName>';
      toSend+='<PhoneNumber>1234567890</PhoneNumber>';
      toSend+='<Address>';
      toSend+='<AddressLine1>Address Line</AddressLine1>';
      toSend+='<City>Corado</City>';
      toSend+='<PostalCode>00646</PostalCode>';
      toSend+='<CountryCode>PR</CountryCode>';
      toSend+='</Address>';
      toSend+='</ShipTo>';

      toSend+='<ShipFrom>';
      toSend+='<CompanyName>Company Name</CompanyName>';
      toSend+='<AttentionName>Attention Name</AttentionName>';
      toSend+='<PhoneNumber>1234567890</PhoneNumber>';
      toSend+='<FaxNumber>1234567890</FaxNumber>';
      toSend+='<Address>';
      toSend+='<AddressLine1>Address Line</AddressLine1>';
      toSend+='<City>Boca Raton</City>';
      toSend+='<StateProvinceCode>FL</StateProvinceCode>';
      toSend+='<PostalCode>33434</PostalCode>';
      toSend+='<CountryCode>US</CountryCode>';
      toSend+='</Address>';
      toSend+='</ShipFrom>';

      toSend+='<Service>';
      toSend+='<Code>03</Code>';
      toSend+='</Service>';
      toSend+='<PaymentInformation>';
      toSend+='<Prepaid>';
      toSend+='<BillShipper>';
      toSend+='<AccountNumber>Ship Number</AccountNumber>';
      toSend+='</BillShipper>';
      toSend+='</Prepaid>';
      toSend+='</PaymentInformation>';
      toSend+='<Package>';
      toSend+='<PackagingType>';

      toSend+='<Code>02</Code>';
      toSend+='<Description>Customer Supplied</Description>';
      toSend+='</PackagingType>';
      toSend+='<Description>Rate</Description>';
      toSend+='<PackageWeight>';
      toSend+='<UnitOfMeasurement>';
      toSend+='<Code>LBS</Code>';
      toSend+='</UnitOfMeasurement>';
      toSend+='<Weight>10</Weight>';
      toSend+='</PackageWeight>';
      toSend+='</Package>';
      toSend+='<ShipmentServiceOptions>';
      toSend+='<OnCallAir>';
      toSend+='<Schedule>';
      toSend+='<PickupDay>02</PickupDay>';
      toSend+='<Method>02</Method>';
      toSend+='</Schedule>';
      toSend+='</OnCallAir>';
      toSend+='</ShipmentServiceOptions>';
      toSend+='</Shipment>';
      toSend+='</RatingServiceSelectionRequest>';
      sendUPSRequest(toSend);}

      //SEND THE REQUEST

      public function sendUPSRequest(toSend:String) {
      var loader:URLLoader = new URLLoader();
      configureListeners(loader);

      //now the ups test server url, change this to : https://onlinetools.ups.com/ups.app/xml/Rate
      // when you get everything working fine
      //##########
      var request:URLRequest = new URLRequest("https://wwwcie.ups.com/ups.app/xml/Rate");
      //##########
      request.data = toSend;
      request.method = URLRequestMethod.POST;
      try {
      loader.load(request);
      } catch (error:Error)
      {
      trace("Unable to load requested document.");
      }
      }

      private function configureListeners(dispatcher:IEventDispatcher):void
      {
      dispatcher.addEventListener(Event.COMPLETE, getUpsListHandler);
      dispatcher.addEventListener(Event.OPEN, openHandler);
      dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
      dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
      dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
      dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      }


      //UPS response:
      private function getUpsListHandler(event:Event):void {

      var UPSRatesArray:ArrayCollection = new ArrayCollection()
      var results:XML = new XML(event.currentTarget.data);

      trace(results.RatedShipment);

      if(results.Response.ResponseStatusCode == '1')
      {
      for each(var UPS:XML in results.RatedShipment)
      {
      // NOTE: I ADDED THE UpsObj at the end of this post

      var upsObj:UpsObj = new UpsObj( 'ups',
      UPS.Service.Code,
      UPS.RatedShipmentWarning,
      UPS.BillingWeight.Weight,
      UPS.TransportationCharges.MonetaryValue, UPS.ServiceOptionsCharges.MonetaryValue,
      UPS.TotalCharges.MonetaryValue,
      UPS.RatedPackage.TransportationCharges.MonetaryValue,
      UPS.RatedPackage.ServiceOptionsCharges.MonetaryValue,
      UPS.RatedPackage.TotalCharges.MonetaryValue);
      UPSRatesArray.addItem(upsObj);
      }
      dispatchEventX('ratesIn',UPSRatesArray)
      }
      else
      {
      dispatchEventX('fault',UPS.ResponseStatusDescription);
      }

      }

      private function faultHandler(event:FaultEvent):void
      {
      trace(event.fault.faultDetail);
      }
      private function completeHandler(event:Event):void {
      var loader:URLLoader = URLLoader(event.target);
      trace("completeHandler: " + loader.data);
      }

      private function openHandler(event:Event):void {
      trace("openHandler: " + event);
      }

      private function progressHandler(event:ProgressEvent):void {
      trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
      }

      private function securityErrorHandler(event:SecurityErrorEvent):void {
      trace("securityErrorHandler: " + event);
      }

      private function httpStatusHandler(event:HTTPStatusEvent):void {
      trace("httpStatusHandler: " + event);
      }

      private function ioErrorHandler(event:IOErrorEvent):void {
      trace("ioErrorHandler: " + event);
      }

      // NOW THE UpsObj OBJECT:
      package classes
      {
      import mx.rpc.events.ResultEvent;

      public class UpsObj
      {
      import classes.CustomerRegistrationObj
      public var deliveryCompanyName:String = 'Ups';
      public var Service:String;
      public var RatedShipmentWarning:String;
      public var BillingWeightWeight:String;
      public var TransportationChargesMonetaryValue:String;
      public var ServiceOptionsChargesMonetaryValue:String;
      public var TotalChargesMonetaryValue:String;
      public var RatedPackageTransportationChargesMonetaryValue:String;
      public var RatedPackageServiceOptionsChargesMonetaryValue:String;
      public var RatedPackageTotalChargesMonetaryValue:String;
      public var deliveryCustomer:CustomerRegistrationObj;
      public var description:String;

      public function UpsObj( _deliveryCompanyName:String='UPS',
      _Service:String='',
      _RatedShipmentWarning:String='',
      _BillingWeightWeight:String='',
      _TransportationChargesMonetaryValue:String='',
      _ServiceOptionsChargesMonetaryValue:String='',
      _TotalChargesMonetaryValue:String='',
      _RatedPackageTransportationChargesMonetaryValue:String='',
      _RatedPackageServiceOptionsChargesMonetaryValue:String='',
      _RatedPackageTotalChargesMonetaryValue:String=''):void
      {

      deliveryCompanyName= _deliveryCompanyName;
      Service= _Service;
      setDescription(Service);
      RatedShipmentWarning= _RatedShipmentWarning;
      BillingWeightWeight= _BillingWeightWeight;
      TransportationChargesMonetaryValue= _TransportationChargesMonetaryValue;
      ServiceOptionsChargesMonetaryValue= _ServiceOptionsChargesMonetaryValue;
      TotalChargesMonetaryValue= _TotalChargesMonetaryValue;
      RatedPackageTransportationChargesMonetaryValue= _RatedPackageTransportationChargesMonetaryValue;
      RatedPackageServiceOptionsChargesMonetaryValue= _RatedPackageServiceOptionsChargesMonetaryValue;
      RatedPackageTotalChargesMonetaryValue= _RatedPackageTotalChargesMonetaryValue;
      deliveryCustomer= _deliveryCustomer;

      }
      }

      }
      }