Class: ActiveShipping::CanadaPostPWS
- 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
-
#customer_number ⇒ Object
Returns the value of attribute customer_number.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#language ⇒ Object
Returns the value of attribute language.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#platform_id ⇒ Object
Returns the value of attribute platform_id.
Attributes inherited from Carrier
Class Method Summary collapse
Instance Method Summary collapse
-
#build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
rating.
-
#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.
- #build_tracking_events(events) ⇒ Object
-
#create_shipment(origin, destination, package, line_items = [], options = {}) ⇒ Object
line_items should be a list of PackageItem's.
- #error_response(response, response_klass) ⇒ Object
- #find_option_details(option_code, options = {}) ⇒ Object
- #find_rates(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
- #find_service_options(service_code, country, options = {}) ⇒ Object
- #find_services(country = nil, options = {}) ⇒ Object
- #find_shipment_receipt(shipping_id, options = {}) ⇒ Object
- #find_tracking_info(pin, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ CanadaPostPWS
constructor
A new instance of CanadaPostPWS.
- #log(msg) ⇒ Object
- #maximum_address_field_length ⇒ Object
- #maximum_weight ⇒ Object
- #parse_merchant_details_response(response) ⇒ Object
- #parse_option_response(response) ⇒ Object
- #parse_rates_response(response, origin, destination, exclude_tax) ⇒ Object
- #parse_register_token_response(response) ⇒ Object
- #parse_service_options_response(response) ⇒ Object
-
#parse_services_response(response) ⇒ Object
service discovery.
- #parse_shipment_receipt_response(response) ⇒ Object
- #parse_shipment_response(response) ⇒ Object
-
#parse_tracking_response(response) ⇒ Object
tracking.
- #price_from_node(node, exclude_tax) ⇒ Object
- #references_node(xml, options) ⇒ Object
- #register_merchant(options = {}) ⇒ Object
- #requirements ⇒ Object
- #retrieve_merchant_details(options = {}) ⇒ Object
- #retrieve_shipment(shipping_id, options = {}) ⇒ Object
- #retrieve_shipping_label(shipping_response, options = {}) ⇒ Object
- #shipment_customs_node(xml, destination, line_items, options) ⇒ Object
- #shipment_destination_node(xml, destination, options) ⇒ Object
- #shipment_notification_node(xml, options) ⇒ Object
- #shipment_options_node(xml, options) ⇒ Object
- #shipment_parcel_node(xml, package, options = {}) ⇒ Object
- #shipment_preferences_node(xml, options) ⇒ Object
- #shipment_sender_node(xml, sender, options) ⇒ Object
- #shipment_service_code_node(xml, options) ⇒ Object
Methods inherited from Carrier
#available_services, #cancel_shipment, #save_request, #timestamp_from_business_day, #valid_credentials?
Constructor Details
#initialize(options = {}) ⇒ CanadaPostPWS
55 56 57 58 59 60 61 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 55 def initialize( = {}) @language = LANGUAGE[[:language]] || LANGUAGE['en'] @endpoint = [:endpoint] || ENDPOINT @platform_id = [:platform_id] @customer_number = [:customer_number] super() end |
Instance Attribute Details
#customer_number ⇒ Object
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 |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
53 54 55 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53 def endpoint @endpoint end |
#language ⇒ Object
Returns the value of attribute language.
53 54 55 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53 def language @language end |
#logger ⇒ Object
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_id ⇒ Object
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 |
Class Method Details
.default_location ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 67 def self.default_location { :country => 'CA', :province => 'ON', :city => 'Ottawa', :address1 => '61A York St', :postal_code => 'K1N5T2' } end |
Instance Method Details
#build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
rating
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 259 def build_rates_request(origin, destination, line_items = [], = {}, 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, ) contract_id_node(xml, ) quote_type_node(xml, ) expected_mailing_date_node(xml, shipping_date()) if [:shipping_date] (xml, RATES_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
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 363 def build_shipment_request(origin, destination, package, line_items = [], = {}) 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, ) shipment_sender_node(xml, origin, ) shipment_destination_node(xml, destination, ) (xml, ) shipment_parcel_node(xml, package) shipment_notification_node(xml, ) shipment_preferences_node(xml, ) references_node(xml, ) # optional > user defined custom notes shipment_customs_node(xml, destination, line_items, ) # COD Remittance defaults to sender end end end builder.to_xml end |
#build_tracking_events(events) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 339 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 = DateTime.parse("#{date} #{time} #{zone}") time = Time.utc(.utc.year, .utc.month, .utc.day, .utc.hour, .utc.min, .utc.sec) = 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, ) end end |
#create_shipment(origin, destination, package, line_items = [], options = {}) ⇒ Object
line_items should be a list of PackageItem's
100 101 102 103 104 105 106 107 108 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 100 def create_shipment(origin, destination, package, line_items = [], = {}) request_body = build_shipment_request(origin, destination, package, line_items, ) response = ssl_post(create_shipment_url(), request_body, headers(, 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
574 575 576 577 578 579 580 581 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 574 def error_response(response, response_klass) doc = Nokogiri.XML(response) doc.remove_namespaces! = doc.xpath('messages/message') = .map { |m| m.at('description').text }.join(", ") code = .map { |m| m.at('code').text }.join(", ") response_klass.new(false, , {}, :carrier => @@name, :code => code) end |
#find_option_details(option_code, options = {}) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 160 def find_option_details(option_code, = {}) url = endpoint + "rs/ship/option/#{option_code}" response = ssl_get(url, headers(, 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
77 78 79 80 81 82 83 84 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 77 def find_rates(origin, destination, line_items = [], = {}, package = nil, services = []) url = endpoint + "rs/ship/price" request = build_rates_request(origin, destination, line_items, , package, services) response = ssl_post(url, request, headers(, RATE_MIMETYPE, RATE_MIMETYPE)) parse_rates_response(response, origin, destination, !![:exclude_tax]) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_service_options(service_code, country, options = {}) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 153 def (service_code, country, = {}) response = ssl_get(services_url(country, service_code), headers(, RATE_MIMETYPE)) (response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_services(country = nil, options = {}) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 146 def find_services(country = nil, = {}) response = ssl_get(services_url(country), headers(, 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
115 116 117 118 119 120 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 115 def find_shipment_receipt(shipping_id, = {}) response = ssl_get(shipment_receipt_url(shipping_id, ), headers(, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE)) parse_shipment_receipt_response(response) rescue ActiveUtils::ResponseError => e raise ActiveShipping::ResponseError.new(e.) end |
#find_tracking_info(pin, options = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 86 def find_tracking_info(pin, = {}) response = ssl_get(tracking_url(pin), headers(, 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., {}, :carrier => @@name) end rescue InvalidPinFormatError CPPWSTrackingResponse.new(false, "Invalid Pin Format", {}, :carrier => @@name) end |
#log(msg) ⇒ Object
583 584 585 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 583 def log(msg) logger.debug(msg) if logger end |
#maximum_address_field_length ⇒ Object
172 173 174 175 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 172 def maximum_address_field_length # https://www.canadapost.ca/cpo/mc/business/productsservices/developers/services/shippingmanifest/createshipment.jsf 44 end |
#maximum_weight ⇒ Object
168 169 170 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 168 def maximum_weight Measured::Weight.new(MAX_WEIGHT, :kg) end |
#parse_merchant_details_response(response) ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 524 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? = { :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, "", {}, ) end |
#parse_option_response(response) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 237 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, exclude_tax) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 278 def parse_rates_response(response, origin, destination, exclude_tax) 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 = price_from_node(node, exclude_tax) expected_date = expected_date_from_node(node) = { :service_code => service_code, :total_price => total_price, :currency => 'CAD', :delivery_range => [expected_date, expected_date] } RateEstimate.new(origin, destination, @@name, service_name, ) end CPPWSRateResponse.new(true, "", {}, :rates => rates) end |
#parse_register_token_response(response) ⇒ Object
514 515 516 517 518 519 520 521 522 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 514 def parse_register_token_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Registration Token" unless doc.at('token') = { :token_id => doc.root.at('token-id').text } CPPWSRegisterResponse.new(true, "", {}, ) end |
#parse_service_options_response(response) ⇒ Object
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 228 229 230 231 232 233 234 235 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 197 def (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') = 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 => , :restrictions => restrictions } end |
#parse_services_response(response) ⇒ Object
service discovery
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 179 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
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 539 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
499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 499 def parse_shipment_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Shipping" unless doc.at('non-contract-shipment-info') = { :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'], } [:tracking_number] = doc.root.at('tracking-pin').text if doc.root.at('tracking-pin') CPPWSShippingResponse.new(true, "", {}, ) end |
#parse_tracking_response(response) ⇒ Object
tracking
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 309 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)) = { :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, "", {}, ) end |
#price_from_node(node, exclude_tax) ⇒ Object
299 300 301 302 303 304 305 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 299 def price_from_node(node, exclude_tax) price = node.at('price-details/due').text return price unless exclude_tax children = node.at('price-details/taxes').children tax_total_cents = children.sum { |node| node.elem? ? Package.cents_from(node.text) : 0 } Package.cents_from(price) - tax_total_cents end |
#references_node(xml, options) ⇒ Object
443 444 445 446 447 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 443 def references_node(xml, ) # custom values # xml.public_send('references') do # end end |
#register_merchant(options = {}) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 127 def register_merchant( = {}) 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 |
#requirements ⇒ Object
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
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 135 def retrieve_merchant_details( = {}) raise MissingTokenIdError unless token_id = [: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.) end |
#retrieve_shipment(shipping_id, options = {}) ⇒ Object
110 111 112 113 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 110 def retrieve_shipment(shipping_id, = {}) response = ssl_post(shipment_url(shipping_id, ), nil, headers(, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE)) parse_shipment_response(response) end |
#retrieve_shipping_label(shipping_response, options = {}) ⇒ Object
122 123 124 125 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 122 def retrieve_shipping_label(shipping_response, = {}) raise MissingShippingNumberError unless shipping_response && shipping_response.shipping_id ssl_get(shipping_response.label_url, headers(, "application/pdf")) end |
#shipment_customs_node(xml, destination, line_items, options) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 449 def shipment_customs_node(xml, destination, line_items, ) return unless location_from_hash(destination).country_code != 'CA' xml.public_send('customs') do currency = [:currency] || "CAD" xml.public_send('currency', currency) xml.public_send('conversion-from-cad', [:conversion_from_cad].to_s) if currency != 'CAD' && [:conversion_from_cad] xml.public_send('reason-for-export', 'SOG') # SOG - Sale of Goods xml.public_send('other-reason', [:customs_other_reason]) if [:customs_reason_for_export] && [:customs_other_reason] xml.public_send('additional-customs-info', [:customs_addition_info]) if [: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.[:country_of_origin]) if line_item. && line_item.[:country_of_origin] && !line_item.[:country_of_origin].empty? xml.public_send('province-of-origin', line_item.[:province_of_origin]) if line_item. && line_item.[:province_of_origin] && !line_item.[:province_of_origin].empty? end end end end end |
#shipment_destination_node(xml, destination, options) ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 404 def shipment_destination_node(xml, destination, ) location = location_from_hash(destination) 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', get_sanitized_postal_code(location)) end end end |
#shipment_notification_node(xml, options) ⇒ Object
425 426 427 428 429 430 431 432 433 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 425 def shipment_notification_node(xml, ) return unless [:notification_email] xml.public_send('notification') do xml.public_send('email', [: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
421 422 423 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 421 def (xml, ) (xml, SHIPPING_OPTIONS, ) end |
#shipment_parcel_node(xml, package, options = {}) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 478 def shipment_parcel_node(xml, package, = {}) 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
435 436 437 438 439 440 441 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 435 def shipment_preferences_node(xml, ) xml.public_send('preferences') do xml.public_send('show-packing-instructions', [:packing_instructions] || true) xml.public_send('show-postage-rate', [:show_postage_rate] || false) xml.public_send('show-insured-value', true) end end |
#shipment_sender_node(xml, sender, options) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 387 def shipment_sender_node(xml, sender, ) location = location_from_hash(sender) 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', get_sanitized_postal_code(location)) end end end |
#shipment_service_code_node(xml, options) ⇒ Object
383 384 385 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 383 def shipment_service_code_node(xml, ) xml.public_send('service-code', [:service]) end |