Class: ActiveShipping::CanadaPostPWS

Inherits:
Carrier
  • Object
show all
Defined in:
lib/active_shipping/carriers/canada_post_pws.rb

Constant Summary collapse

SHIPPING_SERVICES =
{
  "DOM.RP"        => "Regular Parcel",
  "DOM.EP"        => "Expedited Parcel",
  "DOM.XP"        => "Xpresspost",
  "DOM.XP.CERT"   => "Xpresspost Certified",
  "DOM.PC"        => "Priority",
  "DOM.LIB"       => "Library Books",

  "USA.EP"        => "Expedited Parcel USA",
  "USA.PW.ENV"    => "Priority Worldwide Envelope USA",
  "USA.PW.PAK"    => "Priority Worldwide pak USA",
  "USA.PW.PARCEL" => "Priority Worldwide Parcel USA",
  "USA.SP.AIR"    => "Small Packet USA Air",
  "USA.SP.SURF"   => "Small Packet USA Surface",
  "USA.XP"        => "Xpresspost USA",

  "INT.XP"        => "Xpresspost International",
  "INT.IP.AIR"    => "International Parcel Air",
  "INT.IP.SURF"   => "International Parcel Surface",
  "INT.PW.ENV"    => "Priority Worldwide Envelope Int'l",
  "INT.PW.PAK"    => "Priority Worldwide pak Int'l",
  "INT.PW.PARCEL" => "Priority Worldwide parcel Int'l",
  "INT.SP.AIR"    => "Small Packet International Air",
  "INT.SP.SURF"   => "Small Packet International Surface"
}
ENDPOINT =

production

"https://soa-gw.canadapost.ca/"
SHIPMENT_MIMETYPE =
"application/vnd.cpc.ncshipment+xml"
RATE_MIMETYPE =
"application/vnd.cpc.ship.rate+xml"
TRACK_MIMETYPE =
"application/vnd.cpc.track+xml"
REGISTER_MIMETYPE =
"application/vnd.cpc.registration+xml"
LANGUAGE =
{
  'en' => 'en-CA',
  'fr' => 'fr-CA'
}
SHIPPING_OPTIONS =
[:d2po, :d2po_office_id, :cov, :cov_amount, :cod, :cod_amount, :cod_includes_shipping,
:cod_method_of_payment, :so, :dc, :dns, :pa18, :pa19, :hfp, :lad,
:rase, :rts, :aban]
RATES_OPTIONS =
[:cov, :cov_amount, :cod, :so, :dc, :dns, :pa18, :pa19, :hfp, :lad]
MAX_WEIGHT =

kg

30
@@name =
"Canada Post PWS"

Instance Attribute Summary collapse

Attributes inherited from Carrier

#last_request, #test_mode

Instance Method Summary collapse

Methods inherited from Carrier

#cancel_shipment, default_location, #save_request, #timestamp_from_business_day, #valid_credentials?

Constructor Details

#initialize(options = {}) ⇒ CanadaPostPWS

Returns a new instance of CanadaPostPWS.



55
56
57
58
59
60
61
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 55

def initialize(options = {})
  @language = LANGUAGE[options[:language]] || LANGUAGE['en']
  @endpoint = options[:endpoint] || ENDPOINT
  @platform_id = options[:platform_id]
  @customer_number = options[:customer_number]
  super(options)
end

Instance Attribute Details

#customer_numberObject

Returns the value of attribute customer_number.



53
54
55
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53

def customer_number
  @customer_number
end

#endpointObject

Returns the value of attribute endpoint.



53
54
55
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53

def endpoint
  @endpoint
end

#languageObject

Returns the value of attribute language.



53
54
55
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53

def language
  @language
end

#loggerObject

Returns the value of attribute logger.



53
54
55
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53

def logger
  @logger
end

#platform_idObject

Returns the value of attribute platform_id.



53
54
55
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53

def platform_id
  @platform_id
end

Instance Method Details

#build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object

rating



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 247

def build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = [])
  line_items = Array(line_items)

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.public_send('mailing-scenario', :xmlns => "http://www.canadapost.ca/ws/ship/rate") do
      customer_number_node(xml, options)
      contract_id_node(xml, options)
      quote_type_node(xml, options)
      expected_mailing_date_node(xml, shipping_date(options)) if options[:shipping_date]
      shipping_options_node(xml, RATES_OPTIONS, options)
      parcel_node(xml, line_items, package)
      origin_node(xml, origin)
      destination_node(xml, destination)
      services_node(xml, services) unless services.blank?
    end
  end
  builder.to_xml
end

#build_shipment_request(origin, destination, package, line_items = [], options = {}) ⇒ Object

options :service => 'DOM.EP' :notification_email :packing_instructions :show_postage_rate :cod, :cod_amount, :insurance, :insurance_amount, :signature_required, :pa18, :pa19, :hfp, :dns, :lad



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 343

def build_shipment_request(origin, destination, package, line_items = [], options = {})
  origin = sanitize_location(origin)
  destination = sanitize_location(destination)

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.public_send('non-contract-shipment', :xmlns => "http://www.canadapost.ca/ws/ncshipment") do
      xml.public_send('delivery-spec') do
        shipment_service_code_node(xml, options)
        shipment_sender_node(xml, origin, options)
        shipment_destination_node(xml, destination, options)
        shipment_options_node(xml, options)
        shipment_parcel_node(xml, package)
        shipment_notification_node(xml, options)
        shipment_preferences_node(xml, options)
        references_node(xml, options)             # optional > user defined custom notes
        shipment_customs_node(xml, destination, line_items, options)
        # COD Remittance defaults to sender
      end
    end
  end
  builder.to_xml
end

#build_tracking_events(events) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 319

def build_tracking_events(events)
  events.map do |event|
    date      = event.at('event-date').text
    time      = event.at('event-time').text
    zone      = event.at('event-time-zone').text
    timestamp = DateTime.parse("#{date} #{time} #{zone}")
    time      = Time.utc(timestamp.utc.year, timestamp.utc.month, timestamp.utc.day, timestamp.utc.hour, timestamp.utc.min, timestamp.utc.sec)
    message   = event.at('event-description').text
    location  = [event.at('event-retail-name'), event.at('event-site'), event.at('event-province')].
                  reject { |e| e.nil? || e.text.empty? }.join(", ")
    name      = event.at('event-identifier').text
    ShipmentEvent.new(name, time, location, message)
  end
end

#create_shipment(origin, destination, package, line_items = [], options = {}) ⇒ Object

line_items should be a list of PackageItem's



90
91
92
93
94
95
96
97
98
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 90

def create_shipment(origin, destination, package, line_items = [], options = {})
  request_body = build_shipment_request(origin, destination, package, line_items, options)
  response = ssl_post(create_shipment_url(options), request_body, headers(options, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE))
  parse_shipment_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSShippingResponse)
rescue MissingCustomerNumberError
  CPPWSShippingResponse.new(false, "Missing Customer Number", {}, :carrier => @@name)
end

#error_response(response, response_klass) ⇒ Object



555
556
557
558
559
560
561
562
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 555

def error_response(response, response_klass)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  messages = doc.xpath('messages/message')
  message = messages.map { |m| m.at('description').text }.join(", ")
  code = messages.map { |m| m.at('code').text }.join(", ")
  response_klass.new(false, message, {}, :carrier => @@name, :code => code)
end

#find_option_details(option_code, options = {}) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 148

def find_option_details(option_code, options = {})
  url = endpoint + "rs/ship/option/#{option_code}"
  response = ssl_get(url, headers(options, RATE_MIMETYPE))
  parse_option_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSRateResponse)
end

#find_rates(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 67

def find_rates(origin, destination, line_items = [], options = {}, package = nil, services = [])
  url = endpoint + "rs/ship/price"
  request  = build_rates_request(origin, destination, line_items, options, package, services)
  response = ssl_post(url, request, headers(options, RATE_MIMETYPE, RATE_MIMETYPE))
  parse_rates_response(response, origin, destination)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSRateResponse)
end

#find_service_options(service_code, country, options = {}) ⇒ Object



141
142
143
144
145
146
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 141

def find_service_options(service_code, country, options = {})
  response = ssl_get(services_url(country, service_code), headers(options, RATE_MIMETYPE))
  parse_service_options_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSRateResponse)
end

#find_services(country = nil, options = {}) ⇒ Object



134
135
136
137
138
139
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 134

def find_services(country = nil, options = {})
  response = ssl_get(services_url(country), headers(options, RATE_MIMETYPE))
  parse_services_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSRateResponse)
end

#find_shipment_receipt(shipping_id, options = {}) ⇒ Object



105
106
107
108
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 105

def find_shipment_receipt(shipping_id, options = {})
  response = ssl_get(shipment_receipt_url(shipping_id, options), headers(options, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE))
  parse_shipment_receipt_response(response)
end

#find_tracking_info(pin, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 76

def find_tracking_info(pin, options = {})
  response = ssl_get(tracking_url(pin), headers(options, TRACK_MIMETYPE))
  parse_tracking_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  if e.response
    error_response(e.response.body, CPPWSTrackingResponse)
  else
    CPPWSTrackingResponse.new(false, e.message, {}, :carrier => @@name)
  end
rescue InvalidPinFormatError
  CPPWSTrackingResponse.new(false, "Invalid Pin Format", {}, :carrier => @@name)
end

#log(msg) ⇒ Object



564
565
566
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 564

def log(msg)
  logger.debug(msg) if logger
end

#maximum_address_field_lengthObject



160
161
162
163
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 160

def maximum_address_field_length
  # https://www.canadapost.ca/cpo/mc/business/productsservices/developers/services/shippingmanifest/createshipment.jsf
  44
end

#maximum_weightObject



156
157
158
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 156

def maximum_weight
  Mass.new(MAX_WEIGHT, :kilograms)
end

#parse_merchant_details_response(response) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 505

def parse_merchant_details_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  raise "No Merchant Info" unless doc.at('merchant-info')
  raise "No Merchant Info" if doc.root.at('customer-number').blank?
  options = {
    :customer_number => doc.root.at('customer-number').text,
    :contract_number => doc.root.at('contract-number').text,
    :username => doc.root.at('merchant-username').text,
    :password => doc.root.at('merchant-password').text,
    :has_default_credit_card => doc.root.at('has-default-credit-card').text == 'true'
  }
  CPPWSMerchantDetailsResponse.new(true, "", {}, options)
end

#parse_option_response(response) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 225

def parse_option_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!

  conflicts = doc.root.xpath('conflicting-options/option-code').map(&:text)
  prereqs = doc.root.xpath('prerequisite-options/option-code').map(&:text)
  option = {
    :code => doc.root.at('option-code').text,
    :name => doc.root.at('option-name').text,
    :class => doc.root.at('option-class').text,
    :prints_on_label => doc.root.at('prints-on-label').text != "false",
    :qualifier_required => doc.root.at('qualifier-required').text != "false",
  }
  option[:conflicting_options] = conflicts if conflicts
  option[:prerequisite_options] = prereqs if prereqs

  option[:qualifier_max] = doc.root.at("qualifier-max").text.to_i if doc.root.at("qualifier-max")
  option
end

#parse_rates_response(response, origin, destination) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 266

def parse_rates_response(response, origin, destination)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  raise ActiveShipping::ResponseError, "No Quotes" unless doc.at('price-quotes')

  rates = doc.root.xpath('price-quote').map do |node|
    service_name  = node.at("service-name").text
    service_code  = node.at("service-code").text
    total_price   = node.at('price-details/due').text
    expected_date = expected_date_from_node(node)
    options = {
      :service_code   => service_code,
      :total_price    => total_price,
      :currency       => 'CAD',
      :delivery_range => [expected_date, expected_date]
    }
    RateEstimate.new(origin, destination, @@name, service_name, options)
  end
  CPPWSRateResponse.new(true, "", {}, :rates => rates)
end

#parse_register_token_response(response) ⇒ Object



495
496
497
498
499
500
501
502
503
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 495

def parse_register_token_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  raise ActiveShipping::ResponseError, "No Registration Token" unless doc.at('token')
  options = {
    :token_id => doc.root.at('token-id').text
  }
  CPPWSRegisterResponse.new(true, "", {}, options)
end

#parse_service_options_response(response) ⇒ Object



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
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 185

def parse_service_options_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!

  service_code = doc.root.at("service-code").text
  service_name = doc.root.at("service-name").text

  option_nodes = doc.root.xpath('options/option')
  options = option_nodes.map do |node|
    option = {
      :code => node.at("option-code").text,
      :name => node.at("option-name").text,
      :required => node.at("mandatory").text != "false",
      :qualifier_required => node.at("qualifier-required").text != "false",
    }
    option[:qualifier_max] = node.at("qualifier-max").text.to_i if node.at("qualifier-max")
    option
  end

  restrictions_node = doc.root.at('restrictions')
  dimensions_node = restrictions_node.at('dimensional-restrictions')
  restrictions = {
    :min_weight => restrictions_node.at("weight-restriction").attributes['min'].value.to_i,
    :max_weight => restrictions_node.at("weight-restriction").attributes['max'].value.to_i,
    :min_length => dimensions_node.at("length").attributes['min'].value.to_f,
    :max_length => dimensions_node.at("length").attributes['max'].value.to_f,
    :min_height => dimensions_node.at("height").attributes['min'].value.to_f,
    :max_height => dimensions_node.at("height").attributes['max'].value.to_f,
    :min_width => dimensions_node.at("width").attributes['min'].value.to_f,
    :max_width => dimensions_node.at("width").attributes['max'].value.to_f
  }

  {
    :service_code => service_code,
    :service_name => service_name,
    :options => options,
    :restrictions => restrictions
  }
end

#parse_services_response(response) ⇒ Object

service discovery



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 167

def parse_services_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  service_nodes = doc.xpath('services/service')
  service_nodes.inject({}) do |result, node|
    service_code = node.at("service-code").text
    service_name = node.at("service-name").text
    service_link = node.at("link").attributes['href'].value
    service_link_media_type = node.at("link").attributes['media-type'].value
    result[service_code] = {
      :name => service_name,
      :link => service_link,
      :link_media_type => service_link_media_type
    }
    result
  end
end

#parse_shipment_receipt_response(response) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 520

def parse_shipment_receipt_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  root = doc.at('non-contract-shipment-receipt')
  cc_details_node = root.at('cc-receipt-details')
  service_standard_node = root.at('service-standard')
  receipt = {
    :final_shipping_point => root.at("final-shipping-point").text,
    :shipping_point_name => root.at("shipping-point-name").text,
    :service_code => root.at("service-code").text,
    :rated_weight => root.at("rated-weight").text.to_f,
    :base_amount => root.at("base-amount").text.to_f,
    :pre_tax_amount => root.at("pre-tax-amount").text.to_f,
    :gst_amount => root.at("gst-amount").text.to_f,
    :pst_amount => root.at("pst-amount").text.to_f,
    :hst_amount => root.at("hst-amount").text.to_f,
    :charge_amount => cc_details_node.at("charge-amount").text.to_f,
    :currency => cc_details_node.at("currency").text,
    :expected_transit_days => service_standard_node.at("expected-transit-time").text.to_i,
    :expected_delivery_date => service_standard_node.at("expected-delivery-date").text
  }
  option_nodes = root.xpath('priced-options/priced-option')

  receipt[:priced_options] = if option_nodes.length > 0
    option_nodes.inject({}) do |result, node|
      result[node.at("option-code").text] = node.at("option-price").text.to_f
      result
      end
  else
    {}
  end

  receipt
end

#parse_shipment_response(response) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 480

def parse_shipment_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  raise ActiveShipping::ResponseError, "No Shipping" unless doc.at('non-contract-shipment-info')
  options = {
    :shipping_id      => doc.root.at('shipment-id').text,
    :details_url      => doc.root.at_xpath("links/link[@rel='details']")['href'],
    :label_url        => doc.root.at_xpath("links/link[@rel='label']")['href'],
    :receipt_url      => doc.root.at_xpath("links/link[@rel='receipt']")['href'],
  }
  options[:tracking_number] = doc.root.at('tracking-pin').text if doc.root.at('tracking-pin')

  CPPWSShippingResponse.new(true, "", {}, options)
end

#parse_tracking_response(response) ⇒ Object

tracking



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 289

def parse_tracking_response(response)
  doc = Nokogiri.XML(response)
  doc.remove_namespaces!
  raise ActiveShipping::ResponseError, "No Tracking" unless doc.at('tracking-detail')

  events = doc.root.xpath('significant-events/occurrence')

  shipment_events  = build_tracking_events(events)
  change_date      = doc.root.at('changed-expected-date').text
  expected_date    = doc.root.at('expected-delivery-date').text
  dest_postal_code = doc.root.at('destination-postal-id').text
  destination      = Location.new(:postal_code => dest_postal_code)
  origin           = Location.new(origin_hash_for(doc.root))
  options = {
    :carrier                 => @@name,
    :service_name            => doc.root.at('service-name').text,
    :expected_date           => expected_date.blank? ? nil : Date.parse(expected_date),
    :changed_date            => change_date.blank? ? nil : Date.parse(change_date),
    :change_reason           => doc.root.at('changed-expected-delivery-reason').text.strip,
    :destination_postal_code => doc.root.at('destination-postal-id').text,
    :shipment_events         => shipment_events,
    :tracking_number         => doc.root.at('pin').text,
    :origin                  => origin,
    :destination             => destination,
    :customer_number         => doc.root.at('mailed-by-customer-number').text
  }

  CPPWSTrackingResponse.new(true, "", {}, options)
end

#references_node(xml, options) ⇒ Object



424
425
426
427
428
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 424

def references_node(xml, options)
  # custom values
  # xml.public_send('references') do
  # end
end

#register_merchant(options = {}) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 115

def register_merchant(options = {})
  url = endpoint + "ot/token"
  response = ssl_post(url, nil, headers({}, REGISTER_MIMETYPE, REGISTER_MIMETYPE).merge("Content-Length" => "0"))
  parse_register_token_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSRegisterResponse)
end

#requirementsObject



63
64
65
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 63

def requirements
  [:api_key, :secret]
end

#retrieve_merchant_details(options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 123

def retrieve_merchant_details(options = {})
  raise MissingTokenIdError unless token_id = options[:token_id]
  url = endpoint + "ot/token/#{token_id}"
  response = ssl_get(url, headers({}, REGISTER_MIMETYPE, REGISTER_MIMETYPE))
  parse_merchant_details_response(response)
rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e
  error_response(e.response.body, CPPWSMerchantDetailsResponse)
rescue Exception => e
  raise ResponseError.new(e.message)
end

#retrieve_shipment(shipping_id, options = {}) ⇒ Object



100
101
102
103
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 100

def retrieve_shipment(shipping_id, options = {})
  response = ssl_post(shipment_url(shipping_id, options), nil, headers(options, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE))
  parse_shipment_response(response)
end

#retrieve_shipping_label(shipping_response, options = {}) ⇒ Object



110
111
112
113
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 110

def retrieve_shipping_label(shipping_response, options = {})
  raise MissingShippingNumberError unless shipping_response && shipping_response.shipping_id
  ssl_get(shipping_response.label_url, headers(options, "application/pdf"))
end

#shipment_customs_node(xml, destination, line_items, options) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 430

def shipment_customs_node(xml, destination, line_items, options)
  return unless destination.country_code != 'CA'

  xml.public_send('customs') do
    currency = options[:currency] || "CAD"
    xml.public_send('currency', currency)
    xml.public_send('conversion-from-cad', options[:conversion_from_cad].to_s) if currency != 'CAD' && options[:conversion_from_cad]
    xml.public_send('reason-for-export', 'SOG') # SOG - Sale of Goods
    xml.public_send('other-reason', options[:customs_other_reason]) if options[:customs_reason_for_export] && options[:customs_other_reason]
    xml.public_send('additional-customs-info', options[:customs_addition_info]) if options[:customs_addition_info]
    xml.public_send('sku-list') do
      line_items.each do |line_item|
        kg = '%#2.3f' % [sanitize_weight_kg(line_item.kg)]
        xml.public_send('item') do
          xml.public_send('hs-tariff-code', line_item.hs_code) if line_item.hs_code && !line_item.hs_code.empty?
          xml.public_send('sku', line_item.sku) if line_item.sku && !line_item.sku.empty?
          xml.public_send('customs-description', line_item.name.slice(0, 44))
          xml.public_send('unit-weight', kg)
          xml.public_send('customs-value-per-unit', '%.2f' % sanitize_price_from_cents(line_item.value))
          xml.public_send('customs-number-of-units', line_item.quantity)
          xml.public_send('country-of-origin', line_item.options[:country_of_origin]) if line_item.options && line_item.options[:country_of_origin] && !line_item.options[:country_of_origin].empty?
          xml.public_send('province-of-origin', line_item.options[:province_of_origin]) if line_item.options && line_item.options[:province_of_origin] && !line_item.options[:province_of_origin].empty?
        end
      end
    end

  end
end

#shipment_destination_node(xml, location, options) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 386

def shipment_destination_node(xml, location, options)
  xml.public_send('destination') do
    xml.public_send('name', location.name)
    xml.public_send('company', location.company) if location.company.present?
    xml.public_send('client-voice-number', location.phone)
    xml.public_send('address-details') do
      xml.public_send('address-line-1', location.address1)
      xml.public_send('address-line-2', location.address2_and_3) unless location.address2_and_3.blank?
      xml.public_send('city', location.city)
      xml.public_send('prov-state', location.province) unless location.province.blank?
      xml.public_send('country-code', location.country_code)
      xml.public_send('postal-zip-code', location.postal_code)
    end
  end
end

#shipment_notification_node(xml, options) ⇒ Object



406
407
408
409
410
411
412
413
414
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 406

def shipment_notification_node(xml, options)
  return unless options[:notification_email]
  xml.public_send('notification') do
    xml.public_send('email', options[:notification_email])
    xml.public_send('on-shipment', true)
    xml.public_send('on-exception', true)
    xml.public_send('on-delivery', true)
  end
end

#shipment_options_node(xml, options) ⇒ Object



402
403
404
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 402

def shipment_options_node(xml, options)
  shipping_options_node(xml, SHIPPING_OPTIONS, options)
end

#shipment_parcel_node(xml, package, options = {}) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 459

def shipment_parcel_node(xml, package, options = {})
  weight = sanitize_weight_kg(package.kilograms.to_f)
  xml.public_send('parcel-characteristics') do
    xml.public_send('weight', "%#2.3f" % weight)
    pkg_dim = package.cm
    if pkg_dim && !pkg_dim.select { |x| x != 0 }.empty?
      xml.public_send('dimensions') do
        xml.public_send('length', '%.1f' % ((pkg_dim[2] * 10).round / 10.0)) if pkg_dim.size >= 3
        xml.public_send('width', '%.1f' % ((pkg_dim[1] * 10).round / 10.0)) if pkg_dim.size >= 2
        xml.public_send('height', '%.1f' % ((pkg_dim[0] * 10).round / 10.0)) if pkg_dim.size >= 1
      end
      xml.public_send('document', false)
    else
      xml.public_send('document', true)
    end

    xml.public_send('mailing-tube', package.tube?)
    xml.public_send('unpackaged', package.unpackaged?)
  end
end

#shipment_preferences_node(xml, options) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 416

def shipment_preferences_node(xml, options)
  xml.public_send('preferences') do
    xml.public_send('show-packing-instructions', options[:packing_instructions] || true)
    xml.public_send('show-postage-rate', options[:show_postage_rate] || false)
    xml.public_send('show-insured-value', true)
  end
end

#shipment_sender_node(xml, location, options) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 370

def shipment_sender_node(xml, location, options)
  xml.public_send('sender') do
    xml.public_send('name', location.name)
    xml.public_send('company', location.company) if location.company.present?
    xml.public_send('contact-phone', location.phone)
    xml.public_send('address-details') do
      xml.public_send('address-line-1', location.address1)
      xml.public_send('address-line-2', location.address2_and_3) unless location.address2_and_3.blank?
      xml.public_send('city', location.city)
      xml.public_send('prov-state', location.province)
      # xml.public_send('country-code', location.country_code)
      xml.public_send('postal-zip-code', location.postal_code)
    end
  end
end

#shipment_service_code_node(xml, options) ⇒ Object



366
367
368
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 366

def shipment_service_code_node(xml, options)
  xml.public_send('service-code', options[:service])
end