Method: McFedex::Base#label

Defined in:
lib/mc-fedex.rb

#label(options = {}) ⇒ Object

Generate a new shipment and return associated data, including price, tracking number, and the label itself.

fedex = Fedex::Base.new(options)
price, label, tracking_number = fedex.label(fields)

Returns the actual price for the label, the Base64-decoded label in the format specified in Fedex::Base, and the tracking_number for the shipment.

Required options for label

:shipper      - A hash containing contact information and an address for the shipper.  (See below.)
:recipient    - A hash containing contact information and an address for the recipient.  (See below.)
:weight       - The total weight of the shipped package.
:service_type - One of Fedex::ServiceTypes

Address format

The ‘shipper’ and ‘recipient’ address values should be hashes. Like this:

shipper = {:contact => {:name => 'John Doe',
                        :phone_number => '4805551212'},
           :address => address} # See "Address" for under price.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/mc-fedex.rb', line 103

def label(options = {})
  puts options.inspect if @debug
  # Check overall options
  check_required_options(:label, options)

  # Check Address Options
  check_required_options(:contact, options[:shipper][:contact])
  check_required_options(:address, options[:shipper][:address])

  # Check Contact Options
  check_required_options(:contact, options[:recipient][:contact])
  check_required_options(:address, options[:recipient][:address])

  # Prepare variables
  shipper             = options[:shipper]
  recipient           = options[:recipient]

  shipper_contact     = shipper[:contact]
  shipper_address     = shipper[:address]

  recipient_contact   = recipient[:contact]
  recipient_address   = recipient[:address]

  service_type        = options[:service_type]
  count               = options[:count] || 1
  weight              = options[:weight]
  rma                 = options[:rma] || ""

  time                = options[:time] || Time.now
  puts time.class
  time                = time.iso8601 if time.is_a?(Time)

  residential         = !!recipient_address[:residential]

  service_type        = resolve_service_type(service_type, residential)

  # Create the driver
  driver = create_driver(:ship)

  smart_post_data = {
    :SmartPostDetail          => {
        :Indicia => "PARCEL_RETURN",
        :HubId   => @smart_post_hub
    },
     :SpecialServicesRequested => {
         :SpecialServiceTypes  => "RETURN_SHIPMENT",
         :ReturnShipmentDetail => {
             :ReturnType => "PRINT_RETURN_LABEL",
             :Rma => {
                        :Number => rma
                    }
         }
     }
  } if @use_smart_post

  result = driver.processShipment(common_options.merge(
    :RequestedShipment => {
      :ShipTimestamp => time,
      :DropoffType => @dropoff_type,
      :ServiceType => service_type,
      :PackagingType => @packaging_type,
      :TotalWeight => { :Units => @units, :Value => weight },
      :PreferredCurrency => @currency,
      :Shipper => {
        :Contact => {
          :PersonName => shipper_contact[:name],
          :PhoneNumber => shipper_contact[:phone_number]
        },
        :Address => {
          :CountryCode => shipper_address[:country],
          :StreetLines => shipper_address[:street],
          :City => shipper_address[:city],
          :StateOrProvinceCode => shipper_address[:state],
          :PostalCode => shipper_address[:zip]
        }
      },
      :Recipient => {
        :Contact => {
          :PersonName => recipient_contact[:name],
          :PhoneNumber => recipient_contact[:phone_number]
        },
        :Address => {
          :CountryCode => recipient_address[:country],
          :StreetLines => recipient_address[:street],
          :City => recipient_address[:city],
          :StateOrProvinceCode => recipient_address[:state],
          :PostalCode => recipient_address[:zip],
          :Residential => residential
        }
      },
      :ShippingChargesPayment => {
        :PaymentType => @payment_type,
        :Payor => {
          :AccountNumber => @account_number,
          :CountryCode => shipper_address[:country]
        }
      },
      :LabelSpecification => {
        :LabelFormatType => @label_type,
        :ImageType => @label_image_type
      },
      :RateRequestTypes => @rate_request_type,
      :PackageCount => count,
      :RequestedPackageLineItems => [ {:SequenceNumber=>1, :Weight => {:Units => @units, :Value => weight} } ]
    }.merge(smart_post_data || {})
  ))

  successful = successful?(result)

  msg = error_msg(result, false)
  if successful && msg !~ /There are no valid services available/
    if @use_smart_post
      charge = 0
      tracking_number = result.completedShipmentDetail.completedPackageDetails.trackingIds[1].trackingNumber
    else
      pre = result.completedShipmentDetail.shipmentRating.shipmentRateDetails
      charge = ((pre.class == Array ? pre[0].totalNetCharge.amount.to_f : pre.totalNetCharge.amount.to_f) * 100).to_i
      tracking_number = result.completedShipmentDetail.completedPackageDetails.trackingIds.trackingNumber
    end
    label = Base64.decode64(result.completedShipmentDetail.completedPackageDetails.label.parts.image)
    [charge, label, tracking_number]
  else
    raise FedexError.new("Unable to get label from Fedex: #{msg}")
  end
end