Wednesday 24 September 2014

Getting labels from fedex

Here we will start on getting rates from fedex with rails..

 require 'fedex' 
fedex = Fedex::Shipment.new(:key => 'xxx', 
       :password => 'xxxx', 
       :account_number => 'xxxx', 
       :meter => 'xxx', 
       :mode => 'production') 

Generating a shipping label with PDF Format

To create a label for a shipment:

label = fedex.label(:filename => "label_dir/label.pdf",
:shipper=>shipper,
:recipient =>recipient,
:packages =>packages,
:service_type =>"FEDEX_GROUND",
:shipping_options=> shipping_options)

shipper is the test shipper we defined in the previous blog . Recipient is the
same test recipient also packages .

Generate a shipping label in any available format

Change the filename extension and pass a label_specification hash. 

For example:

example_spec = {
:image_type => "EPL2",
:label_stock_type => "STOCK_4X6"
}

label = fedex.label(:filename =>"my_dir/example_epl2.pcx",
:shipper=>shipper,
:recipient =>recipient,
:packages =>packages,
:service_type =>"FEDEX_GROUND",
:shipping_options=> shipping_options, 
:label_specification => example_spec)

Generate shipping labels for multi-package shipments (MPS)

Customer can have multiple packages for a single pick-up, destination and payer 

can be combined into a single MPS shipment. The first label will provide a
master tracking number which must be used in the subsequent calls for
the remaining packages in the shipment.

Parameters for the first label:

label = fedex.label(
:filename => file_name,
:shipper => shipper,
:recipient => recipient,
:packages => packages,
:service_type => service_type,
:shipping_details => shipping_details,
:shipping_charges_payment => shipping_charges_payment,
:customs_clearance_detail => customs_clearance_detail,
:mps => {:package_count =>
package_count, :total_weight => total_weight, :sequence_number =>
'1'}

)

Parameters for labels 2 through 'n':

fedex.label(
:filename => file_name,
:shipper => shipper,
:recipient => recipient,
:packages => packages,
:service_type => service_type,
:shipping_details =>shipping_details,
:shipping_charges_payment =>shipping_charges_payment,
:customs_clearance_detail =>customs_clearance_detail,
:mps => { :master_tracking_id =>{
:tracking_id_type => 'FEDEX', 
:tracking_number=>tracking_number},
:package_count =>package_count,
:total_weight => total_weight,
:sequence_number => 'n'      }   )


Tracking a shipment

To track a shipment:

results = fedex.track(:tracking_number=> "1234567890123")

# => [#<Fedex::TrackingInformation>]

# Pull the first result from the returned array

tracking_info = results.first

tracking_info.tracking_number

# => "1234567890123"

tracking_info.status

# => "Delivered"

tracking_info.events.first.description

# => "On FedEx vehicle for
delivery"

Deleting a shipment
If you dont want to use a fedex label, you should delete the shipment. Fedex will not charge you .

For deleting fedex shipment :
fedex.delete(:tracking_number => "1121123123345")

Address verification with fedex


address = {
:address     => "5 Elm Street",
:city        => "Littleton",
:state       => "CO",
:postal_code => "80125",
:country     => "USA"
}

address_result = fedex.validate_address(:address => address)


# =>  #<Fedex::Address:0xb2e60ac
@changes=["NORMALIZED", "HOUSE_OR_BOX_NUMBER_NOT_FOUND"],
@score=0, @confirmed=false, @available=true, @status="UNDETERMINED",
@residential=false, @business=false, @company=nil, @street_lines="5
ELM ST", @city="LITTLETON", @state="CO",
@province_code="CO", @postal_code="80125",
@country_code="US", @options={:score=>"0",
:changes=>["NORMALIZED",
"HOUSE_OR_BOX_NUMBER_NOT_FOUND"],
:residential_status=>"UNDETERMINED",
:delivery_point_validation=>"UNCONFIRMED",
:address=>{:street_lines=>"5 ELM ST",
:city=>"LITTLETON", :state_or_province_code=>"CO",
:postal_code=>"80125", :country_code=>"US"},
:removed_non_address_data=>nil}>


address_result.residential

# => false

address_result.score

# => 0

address_result.postal_code

# => "80125"

These are the operations working for fedex with this gem. Hope this will help to understand the integration of fedex with rails . Thanks









1 comment:

  1. Hi, debadatt thankyou for your post.it easy to understand, and it help me lot to understand the fedex integration

    ReplyDelete