lib/active_shipping/carriers/ups.rb in active_shipping-1.12.1 vs lib/active_shipping/carriers/ups.rb in active_shipping-1.13.0

- old
+ new

@@ -127,10 +127,14 @@ US_TERRITORIES_TREATED_AS_COUNTRIES = %w(AS FM GU MH MP PW PR VI) IMPERIAL_COUNTRIES = %w(US LR MM) + COUNTRY_MAPPING = { + 'XK' => 'KV' + }.freeze + DEFAULT_SERVICE_NAME_TO_CODE = Hash[UPS::DEFAULT_SERVICES.to_a.map(&:reverse)] DEFAULT_SERVICE_NAME_TO_CODE['UPS 2nd Day Air'] = "02" DEFAULT_SERVICE_NAME_TO_CODE['UPS 3 Day Select'] = "12" DEFAULT_SERVICE_NAME_TO_CODE['UPS Next Day Air Early'] = "14" @@ -670,11 +674,11 @@ xml.AddressLine3(location.address3) unless location.address3.blank? xml.City(location.city) unless location.city.blank? xml.StateProvinceCode(location.province) unless location.province.blank? # StateProvinceCode required for negotiated rates but not otherwise, for some reason xml.PostalCode(location.postal_code) unless location.postal_code.blank? - xml.CountryCode(location.country_code(:alpha2)) unless location.country_code(:alpha2).blank? + xml.CountryCode(mapped_country_code(location.country_code(:alpha2))) unless location.country_code(:alpha2).blank? xml.ResidentialAddressIndicator(true) unless location.commercial? # the default should be that UPS returns residential rates for destinations that it doesn't know about # not implemented: Shipment/(Shipper|ShipTo|ShipFrom)/Address/ResidentialAddressIndicator element end end end @@ -682,11 +686,11 @@ def build_address_artifact_format_location(xml, name, location) xml.public_send(name) do xml.AddressArtifactFormat do xml.PoliticalDivision2(location.city) xml.PoliticalDivision1(location.province) - xml.CountryCode(location.country_code(:alpha2)) + xml.CountryCode(mapped_country_code(location.country_code(:alpha2))) xml.PostcodePrimaryLow(location.postal_code) xml.ResidentialAddressIndicator(true) unless location.commercial? end end end @@ -785,11 +789,11 @@ xml.public_send(node_type) do xml.AccountNumber(options[:billing_account]) xml.ThirdParty do xml.Address do xml.PostalCode(options[:billing_zip]) - xml.CountryCode(options[:billing_country]) + xml.CountryCode(mapped_country_code(options[:billing_country])) end end end end else @@ -1000,11 +1004,11 @@ xml.AddressLine(location.address2) end xml.PoliticalDivision2(location.city) xml.PoliticalDivision1(location.state) xml.PostcodePrimaryLow(location.postal_code) - xml.CountryCode(location.country_code) + xml.CountryCode(mapped_country_code(location.country_code)) end end end xml_builder.to_xml @@ -1022,11 +1026,11 @@ if success if xml.at('AddressClassification/Code').present? classification_code = xml.at('AddressClassification/Code').text end - + classification = case classification_code when "1" :commercial when "2" :residential @@ -1054,11 +1058,11 @@ # Converts from a AddressKeyFormat XML node to a Location def location_from_address_key_format_node(address) return nil unless address country = address.at('CountryCode').try(:text) country = 'US' if country == 'ZZ' # Sometimes returned by SUREPOST in the US - + address_lines = address.css('AddressLine') Location.new( :country => country, :postal_code => address.at('PostcodePrimaryLow').try(:text), @@ -1200,8 +1204,12 @@ # otherwise the delivery_confirmation option must be specified on the entire shipment. # See Appendix P of UPS Shipping Package XML Developers Guide for the rules on which the logic below is based. def package_level_delivery_confirmation?(origin, destination) origin.country_code == destination.country_code || [['US','PR'], ['PR','US']].include?([origin,destination].map(&:country_code)) + end + + def mapped_country_code(country_code) + COUNTRY_MAPPING[country_code].presence || country_code end end end