friendly_shipping 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +7 -0
  4. data/.env.test +4 -0
  5. data/.env.test.local.template +6 -0
  6. data/.gitignore +1 -0
  7. data/.rubocop-relaxed.yml +7 -23
  8. data/.rubocop.yml +11 -2
  9. data/.rubocop_todo.yml +21 -0
  10. data/.yardopts +6 -0
  11. data/CHANGELOG.md +82 -0
  12. data/Gemfile +17 -0
  13. data/friendly_shipping.gemspec +4 -13
  14. data/lib/friendly_shipping/api_error.rb +6 -4
  15. data/lib/friendly_shipping/api_error_handler.rb +10 -6
  16. data/lib/friendly_shipping/api_failure.rb +4 -0
  17. data/lib/friendly_shipping/api_result.rb +16 -4
  18. data/lib/friendly_shipping/carrier.rb +28 -8
  19. data/lib/friendly_shipping/http_client.rb +25 -2
  20. data/lib/friendly_shipping/inflections.rb +10 -0
  21. data/lib/friendly_shipping/item_options.rb +3 -0
  22. data/lib/friendly_shipping/label.rb +41 -20
  23. data/lib/friendly_shipping/package_options.rb +21 -2
  24. data/lib/friendly_shipping/rate.rb +50 -15
  25. data/lib/friendly_shipping/request.rb +23 -7
  26. data/lib/friendly_shipping/response.rb +21 -6
  27. data/lib/friendly_shipping/services/rl/api_error.rb +33 -0
  28. data/lib/friendly_shipping/services/rl/bol_options.rb +107 -0
  29. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  30. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  31. data/lib/friendly_shipping/services/rl/item_options.rb +38 -0
  32. data/lib/friendly_shipping/services/rl/package_options.rb +40 -0
  33. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +46 -0
  34. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +50 -0
  35. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +47 -0
  37. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +66 -0
  38. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +76 -0
  39. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +86 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  42. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  43. data/lib/friendly_shipping/services/rl/serialize_location.rb +46 -0
  44. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  45. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  46. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  47. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  48. data/lib/friendly_shipping/services/rl/shipment_options.rb +50 -0
  49. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  50. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  51. data/lib/friendly_shipping/services/rl.rb +204 -0
  52. data/lib/friendly_shipping/services/ship_engine/api_error.rb +33 -0
  53. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  54. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  55. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -5
  56. data/lib/friendly_shipping/services/ship_engine/label_options.rb +31 -14
  57. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -11
  58. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +77 -0
  59. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -0
  61. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +26 -6
  62. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +101 -0
  63. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +4 -0
  64. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -4
  65. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +28 -0
  66. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +61 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +20 -0
  68. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  71. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  73. data/lib/friendly_shipping/services/ship_engine.rb +94 -21
  74. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  75. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +50 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +50 -0
  77. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +53 -0
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +82 -0
  79. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +52 -0
  80. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +46 -0
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +143 -0
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  83. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl.rb +153 -0
  86. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +43 -0
  87. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +42 -0
  88. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  89. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  90. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +165 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +113 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +65 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  98. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  99. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +121 -0
  100. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +94 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +58 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +31 -0
  104. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  105. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +12 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +162 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +12 -0
  108. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +49 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  112. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  113. data/lib/friendly_shipping/services/tforce_freight.rb +202 -0
  114. data/lib/friendly_shipping/services/ups/label_options.rb +14 -2
  115. data/lib/friendly_shipping/services/ups/label_package_options.rb +8 -4
  116. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  117. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +12 -0
  118. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -1
  120. data/lib/friendly_shipping/services/ups/serialize_package_node.rb +11 -1
  121. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -1
  122. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  123. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +8 -4
  124. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  125. data/lib/friendly_shipping/services/ups.rb +1 -1
  126. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -5
  127. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  128. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +3 -20
  129. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +2 -20
  130. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  131. data/lib/friendly_shipping/services/ups_freight/generate_location_hash.rb +10 -6
  132. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  133. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  134. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -2
  135. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -1
  136. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  137. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +31 -21
  138. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -8
  139. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +46 -0
  140. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  141. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +49 -0
  142. data/lib/friendly_shipping/services/ups_freight.rb +3 -1
  143. data/lib/friendly_shipping/services/ups_json/access_token.rb +29 -0
  144. data/lib/friendly_shipping/services/ups_json/api_error.rb +29 -0
  145. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  146. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  147. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +211 -0
  148. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  149. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +86 -0
  150. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  151. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  153. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +77 -0
  154. data/lib/friendly_shipping/services/ups_json/label_options.rb +177 -0
  155. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +51 -0
  156. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  157. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +44 -0
  158. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  159. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  160. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  161. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +105 -0
  162. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  163. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  164. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +22 -0
  165. data/lib/friendly_shipping/services/ups_json/rates_options.rb +113 -0
  166. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  167. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  168. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  169. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  170. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  171. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  172. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +8 -7
  173. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +12 -8
  174. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -1
  175. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -1
  176. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -2
  177. data/lib/friendly_shipping/services/usps/shipping_methods.rb +8 -5
  178. data/lib/friendly_shipping/services/usps.rb +1 -1
  179. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +5 -4
  180. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -1
  181. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +3 -3
  182. data/lib/friendly_shipping/services/usps_international.rb +1 -1
  183. data/lib/friendly_shipping/services/usps_ship/access_token.rb +37 -0
  184. data/lib/friendly_shipping/services/usps_ship/api_error.rb +29 -0
  185. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +53 -0
  186. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  187. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +80 -0
  188. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  189. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +124 -0
  190. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +55 -0
  191. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  192. data/lib/friendly_shipping/services/usps_ship/timing_options.rb +9 -0
  193. data/lib/friendly_shipping/services/usps_ship.rb +199 -0
  194. data/lib/friendly_shipping/shipment_options.rb +13 -1
  195. data/lib/friendly_shipping/shipping_method.rb +38 -11
  196. data/lib/friendly_shipping/structure_options.rb +38 -0
  197. data/lib/friendly_shipping/timing.rb +42 -7
  198. data/lib/friendly_shipping/version.rb +1 -1
  199. data/lib/friendly_shipping.rb +7 -0
  200. metadata +149 -172
  201. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  202. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
@@ -16,6 +16,7 @@ module FriendlyShipping
16
16
  # Optional:
17
17
  #
18
18
  # @param shipper [Physical::Location] The company sending the shipment. Defaults to the shipment's origin.
19
+ # @param sub_version [String] The UPS API sub-version to use for requests. Default: 1707
19
20
  # @param customer_context [String ] Optional element to identify transactions between client and server
20
21
  # @param validate_address [Boolean] Validate the city field with ZIP code and state. If false, only ZIP code
21
22
  # and state are validated. Default: true
@@ -40,6 +41,7 @@ module FriendlyShipping
40
41
  # @option reason_for_export [String] A reason to export the current shipment. Possible values: 'SALE', 'GIFT', 'SAMPLE',
41
42
  # 'RETURN', 'REPAIR', 'INTERCOMPANYDATA', Any other reason. Default: 'SALE'.
42
43
  # @option invoice_date [Date] The invoice date for the shipment
44
+ # @param declaration_statement [String ] Optional element to add customs declaration text
43
45
  #
44
46
  class Ups
45
47
  class LabelOptions < FriendlyShipping::ShipmentOptions
@@ -83,9 +85,12 @@ module FriendlyShipping
83
85
  ups_pack_collect_3_attemt_box_5: 20 # UPS Pack & Collect Service 1-Attempt Box 5
84
86
  }.freeze
85
87
 
88
+ SUB_VERSIONS = %w[1601 1607 1701 1707 1801 1807 2108 2205].freeze
89
+
86
90
  attr_reader :shipping_method,
87
91
  :shipper_number,
88
92
  :shipper,
93
+ :sub_version,
89
94
  :customer_context,
90
95
  :validate_address,
91
96
  :negotiated_rates,
@@ -97,12 +102,14 @@ module FriendlyShipping
97
102
  :carbon_neutral,
98
103
  :paperless_invoice,
99
104
  :reason_for_export,
100
- :invoice_date
105
+ :invoice_date,
106
+ :declaration_statement
101
107
 
102
108
  def initialize(
103
109
  shipping_method:,
104
110
  shipper_number:,
105
111
  shipper: nil,
112
+ sub_version: '1707',
106
113
  customer_context: nil,
107
114
  validate_address: true,
108
115
  negotiated_rates: false,
@@ -119,11 +126,15 @@ module FriendlyShipping
119
126
  reason_for_export: 'SALE',
120
127
  invoice_date: nil,
121
128
  package_options_class: LabelPackageOptions,
129
+ declaration_statement: nil,
122
130
  **kwargs
123
131
  )
132
+ raise ArgumentError, "Invalid sub-version: #{sub_version}" unless sub_version.in?(SUB_VERSIONS)
133
+
124
134
  @shipping_method = shipping_method
125
135
  @shipper_number = shipper_number
126
136
  @shipper = shipper
137
+ @sub_version = sub_version
127
138
  @customer_context = customer_context
128
139
  @validate_address = validate_address
129
140
  @negotiated_rates = negotiated_rates
@@ -139,7 +150,8 @@ module FriendlyShipping
139
150
  @terms_of_shipment = terms_of_shipment
140
151
  @reason_for_export = reason_for_export
141
152
  @invoice_date = invoice_date
142
- super(**kwargs.merge(package_options_class: package_options_class))
153
+ @declaration_statement = declaration_statement
154
+ super(**kwargs.reverse_merge(package_options_class: package_options_class))
143
155
  end
144
156
 
145
157
  def delivery_confirmation_code
@@ -11,8 +11,10 @@ module FriendlyShipping
11
11
  # values are _reference number values_. Example: `{ reference_numbers: { xn: 'my_reference_1 }`
12
12
  # @option delivery_confirmation [Symbol] Can be set to any key from PACKAGE_DELIVERY_CONFIRMATION_CODES.
13
13
  # Only possible for domestic shipments or shipments between the US and Puerto Rico.
14
- # @option shipper_release [Boolean] Indicates that the package may be released by driver without a signature from the
15
- # consignee. Default: false
14
+ # @option shipper_release [Boolean] Indicates that the package may be released by driver without a signature from
15
+ # the consignee. Default: false
16
+ # @option declared_value [Boolean] When true, declared value (calculated as the sum of all items in the shipment)
17
+ # will be included in the request. Default: false
16
18
  class LabelPackageOptions < FriendlyShipping::PackageOptions
17
19
  PACKAGE_DELIVERY_CONFIRMATION_CODES = {
18
20
  delivery_confirmation: 1,
@@ -20,18 +22,20 @@ module FriendlyShipping
20
22
  delivery_confirmation_adult_signature_required: 3
21
23
  }.freeze
22
24
 
23
- attr_reader :reference_numbers, :shipper_release
25
+ attr_reader :reference_numbers, :shipper_release, :declared_value
24
26
 
25
27
  def initialize(
26
28
  reference_numbers: {},
27
29
  delivery_confirmation: nil,
28
30
  shipper_release: false,
31
+ declared_value: false,
29
32
  **kwargs
30
33
  )
31
34
  @reference_numbers = reference_numbers
32
35
  @delivery_confirmation = delivery_confirmation
33
36
  @shipper_release = shipper_release
34
- super(**kwargs.merge(item_options_class: LabelItemOptions))
37
+ @declared_value = declared_value
38
+ super(**kwargs.reverse_merge(item_options_class: LabelItemOptions))
35
39
  end
36
40
 
37
41
  def delivery_confirmation_code
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class Ups
6
+ class ParseModifierElement
7
+ # @param [Nokogiri::XML::Element] element The modifier element from the source XML
8
+ # @param [String] currency_code The currency code for this modifier's amount (i.e. 'USD')
9
+ # @return [Array<String, Money>]
10
+ def self.call(element, currency_code:)
11
+ return unless element
12
+
13
+ amount = element.at('Amount').text.to_d
14
+ return if amount.zero?
15
+
16
+ currency = Money::Currency.new(currency_code)
17
+ amount = Money.new(amount * currency.subunit_to_unit, currency)
18
+
19
+ modifier_type = element.at('ModifierType').text
20
+ modifier_description = element.at('ModifierDesc').text
21
+
22
+ label = "#{modifier_type} (#{modifier_description})"
23
+
24
+ [label, amount]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'friendly_shipping/services/ups/parse_xml_response'
4
4
  require 'friendly_shipping/services/ups/parse_money_element'
5
+ require 'friendly_shipping/services/ups/parse_modifier_element'
5
6
 
6
7
  module FriendlyShipping
7
8
  module Services
@@ -68,6 +69,7 @@ module FriendlyShipping
68
69
 
69
70
  def build_packages(rated_shipment)
70
71
  rated_shipment.css('RatedPackage').map do |rated_package|
72
+ currency_code = rated_package.at('TotalCharges/CurrencyCode').text
71
73
  {
72
74
  transportation_charges: ParseMoneyElement.call(rated_package.at('TransportationCharges'))&.last,
73
75
  base_service_charge: ParseMoneyElement.call(rated_package.at('BaseServiceCharge'))&.last,
@@ -75,6 +77,7 @@ module FriendlyShipping
75
77
  itemized_charges: extract_charges(rated_package.xpath('ItemizedCharges')),
76
78
  total_charges: ParseMoneyElement.call(rated_package.at('TotalCharges'))&.last,
77
79
  negotiated_charges: extract_charges(rated_package.xpath('NegotiatedCharges/ItemizedCharges')),
80
+ rate_modifiers: extract_modifiers(rated_package.xpath('RateModifier'), currency_code: currency_code),
78
81
  weight: BigDecimal(rated_package.at('Weight').text),
79
82
  billing_weight: BigDecimal(rated_package.at('BillingWeight/Weight').text)
80
83
  }.compact
@@ -86,6 +89,15 @@ module FriendlyShipping
86
89
  ParseMoneyElement.call(element)
87
90
  end.compact.to_h
88
91
  end
92
+
93
+ # @param [Nokogiri::XML::NodeSet] node The RateModifier node set from the source XML
94
+ # @param [String] currency_code The currency code for the modifier amounts (i.e. 'USD')
95
+ # @return [Array<Hash>]
96
+ def extract_modifiers(node, currency_code:)
97
+ node.map do |element|
98
+ ParseModifierElement.call(element, currency_code: currency_code)
99
+ end.compact.to_h
100
+ end
89
101
  end
90
102
  end
91
103
  end
@@ -37,7 +37,7 @@ module FriendlyShipping
37
37
  pickup: pickup,
38
38
  delivery: delivery,
39
39
  guaranteed: guaranteed,
40
- properties: {
40
+ data: {
41
41
  business_transit_days: business_transit_days
42
42
  }
43
43
  )
@@ -17,10 +17,13 @@ module FriendlyShipping
17
17
  # @option customer_classification [Symbol] Which kind of rates to request. See UPS docs for more details. Default: `shipper_number`
18
18
  # @option negotiated_rates [Boolean] if truthy negotiated rates will be requested from ups. Only valid if
19
19
  # shipper account has negotiated rates. Default: false
20
+ # @option pickup_type [String] UPS pickup type. See UPS docs for more details. Default: `daily_pickup`
21
+ # @option pickup_date [Time] UPS pickup date/time. Default: nil
20
22
  # @option saturday_delivery [Boolean] should we request Saturday delivery?. Default: false
21
23
  # @option saturday_pickup [Boolean] should we request Saturday pickup?. Default: false
22
24
  # @option shipping_method [FriendlyShipping::ShippingMethod] Request rates for a particular shipping method only?
23
25
  # Default is `nil`, which translates to 'All shipping methods' (The "Shop" option in UPS parlance)
26
+ # @option sub_version [String] The UPS API sub-version to use for requests. Default: 1707
24
27
  # @option with_time_in_transit [Boolean] Whether to request timing information alongside the rates
25
28
  # @option package_options_class [Class] See FriendlyShipping::ShipmentOptions
26
29
  #
@@ -45,15 +48,19 @@ module FriendlyShipping
45
48
  standard_rates: "53"
46
49
  }.freeze
47
50
 
51
+ SUB_VERSIONS = %w[1601 1607 1701 1707 1801 2108 2201 2205].freeze
52
+
48
53
  attr_reader :carbon_neutral,
49
54
  :customer_context,
50
55
  :destination_account,
51
56
  :negotiated_rates,
57
+ :pickup_date,
52
58
  :saturday_delivery,
53
59
  :saturday_pickup,
54
60
  :shipper,
55
61
  :shipper_number,
56
62
  :shipping_method,
63
+ :sub_version,
57
64
  :with_time_in_transit
58
65
 
59
66
  def initialize(
@@ -64,14 +71,18 @@ module FriendlyShipping
64
71
  destination_account: nil,
65
72
  negotiated_rates: false,
66
73
  pickup_type: :daily_pickup,
74
+ pickup_date: nil,
67
75
  saturday_delivery: false,
68
76
  saturday_pickup: false,
69
77
  shipper: nil,
70
78
  shipping_method: nil,
79
+ sub_version: "1707",
71
80
  with_time_in_transit: false,
72
81
  package_options_class: FriendlyShipping::Services::Ups::RateEstimatePackageOptions,
73
82
  **kwargs
74
83
  )
84
+ raise ArgumentError, "Invalid sub-version: #{sub_version}" unless sub_version.in?(SUB_VERSIONS)
85
+
75
86
  @carbon_neutral = carbon_neutral
76
87
  @customer_context = customer_context
77
88
  @customer_classification = customer_classification
@@ -79,12 +90,14 @@ module FriendlyShipping
79
90
  @negotiated_rates = negotiated_rates
80
91
  @shipper_number = shipper_number
81
92
  @pickup_type = pickup_type
93
+ @pickup_date = pickup_date
82
94
  @saturday_delivery = saturday_delivery
83
95
  @saturday_pickup = saturday_pickup
84
96
  @shipper = shipper
85
97
  @shipping_method = shipping_method
98
+ @sub_version = sub_version
86
99
  @with_time_in_transit = with_time_in_transit
87
- super(**kwargs.merge(package_options_class: package_options_class))
100
+ super(**kwargs.reverse_merge(package_options_class: package_options_class))
88
101
  end
89
102
 
90
103
  def pickup_type_code
@@ -10,7 +10,8 @@ module FriendlyShipping
10
10
  reference_numbers: {},
11
11
  delivery_confirmation_code: nil,
12
12
  shipper_release: false,
13
- transmit_dimensions: true
13
+ transmit_dimensions: true,
14
+ declared_value: false
14
15
  )
15
16
  xml.Package do
16
17
  xml.PackagingType do
@@ -49,6 +50,15 @@ module FriendlyShipping
49
50
  xml.DCISType(delivery_confirmation_code)
50
51
  end
51
52
  end
53
+ if declared_value
54
+ xml.DeclaredValue do
55
+ xml.CurrencyCode('USD')
56
+ monetary_value = package.items.inject(Money.new(0, 'USD')) do |package_sum, item|
57
+ package_sum + (item.cost || Money.new(0, 'USD'))
58
+ end
59
+ xml.MonetaryValue(monetary_value)
60
+ end
61
+ end
52
62
  end
53
63
 
54
64
  reference_numbers.each do |reference_code, reference_number|
@@ -18,7 +18,7 @@ module FriendlyShipping
18
18
  # If no shipping method is given, request all of them
19
19
  # I one is given, omit the request option. It then becomes "Rate", the default.
20
20
  xml.RequestOption('Shop') unless options.shipping_method
21
- xml.SubVersion('1707')
21
+ xml.SubVersion(options.sub_version)
22
22
  # Optional element to identify transactions between client and server.
23
23
  if options.customer_context
24
24
  xml.TransactionReference do
@@ -56,6 +56,15 @@ module FriendlyShipping
56
56
  end
57
57
  end
58
58
 
59
+ if options.pickup_date && options.sub_version.to_i >= 2205
60
+ xml.DeliveryTimeInformation do
61
+ xml.Pickup do
62
+ xml.Date options.pickup_date.strftime('%Y%m%d')
63
+ xml.Time options.pickup_date.strftime('%H%M')
64
+ end
65
+ end
66
+ end
67
+
59
68
  shipment.packages.each do |package|
60
69
  package_options = options.options_for_package(package)
61
70
  SerializePackageNode.call(
@@ -9,7 +9,7 @@ module FriendlyShipping
9
9
  xml.ShipmentAcceptRequest do
10
10
  xml.Request do
11
11
  xml.RequestAction('ShipAccept')
12
- xml.SubVersion('1707')
12
+ xml.SubVersion(options.sub_version)
13
13
  if options.customer_context
14
14
  xml.TransactionReference do
15
15
  xml.CustomerContext(options.customer_context)
@@ -27,7 +27,7 @@ module FriendlyShipping
27
27
  xml.RequestAction('ShipConfirm')
28
28
  # Required element controls level of address validation.
29
29
  xml.RequestOption(options.validate_address ? 'validate' : 'nonvalidate')
30
- xml.SubVersion('1707')
30
+ xml.SubVersion(options.sub_version)
31
31
  # Optional element to identify transactions between client and server.
32
32
  if options.customer_context
33
33
  xml.TransactionReference do
@@ -106,6 +106,8 @@ module FriendlyShipping
106
106
  xml.SoldTo do
107
107
  sold_to_location = options.sold_to || shipment.destination
108
108
  SerializeShipmentAddressSnippet.call(xml: xml, location: sold_to_location)
109
+ xml.AccountNumber(options.sold_to.account_number) if options.sold_to.try(:account_number).present?
110
+ xml.TaxIdentificationNumber(options.sold_to.tax_id_number) if options.sold_to.try(:tax_id_number).present?
109
111
  end
110
112
  end
111
113
 
@@ -124,7 +126,7 @@ module FriendlyShipping
124
126
 
125
127
  contents_description = shipment.packages.flat_map do |package|
126
128
  package.items.map(&:description)
127
- end.compact.join(', ').slice(0, 50)
129
+ end.compact.uniq.join(', ').slice(0, 50)
128
130
 
129
131
  unless contents_description.empty?
130
132
  xml.Description(contents_description)
@@ -160,7 +162,8 @@ module FriendlyShipping
160
162
  package: package,
161
163
  reference_numbers: reference_numbers,
162
164
  delivery_confirmation_code: delivery_confirmation_code,
163
- shipper_release: package_options.shipper_release
165
+ shipper_release: package_options.shipper_release,
166
+ declared_value: package_options.declared_value
164
167
  )
165
168
  end
166
169
  end
@@ -239,6 +242,7 @@ module FriendlyShipping
239
242
  xml.InvoiceDate(invoice_date.strftime('%Y%m%d'))
240
243
  xml.ReasonForExport(reason_for_export)
241
244
  xml.CurrencyCode(options.billing_options.currency || 'USD')
245
+ xml.DeclarationStatement(options.declaration_statement) if options.declaration_statement
242
246
 
243
247
  if options.terms_of_shipment_code
244
248
  xml.TermsOfShipment(options.terms_of_shipment_code)
@@ -263,7 +267,7 @@ module FriendlyShipping
263
267
  xml.CommodityCode(item_options.commodity_code)
264
268
  xml.OriginCountryCode(item_options.country_of_origin || shipment.origin.country.code)
265
269
  xml.Unit do
266
- xml.Value(cost * items.length)
270
+ xml.Value(cost)
267
271
  xml.Number(items.length)
268
272
  xml.UnitOfMeasurement do
269
273
  xml.Code(item_options.product_unit_of_measure_code)
@@ -4,7 +4,7 @@ module FriendlyShipping
4
4
  module Services
5
5
  class Ups
6
6
  EU_COUNTRIES = %w(
7
- AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PO PT RO SE SI SK
7
+ AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK
8
8
  ).map { |country_code| Carmen::Country.coded(country_code) }
9
9
 
10
10
  class << self
@@ -27,7 +27,7 @@ require 'friendly_shipping/services/ups/timing_options'
27
27
  module FriendlyShipping
28
28
  module Services
29
29
  class Ups
30
- include Dry::Monads[:result]
30
+ include Dry::Monads::Result::Mixin
31
31
 
32
32
  attr_reader :test, :key, :login, :password, :client
33
33
 
@@ -8,16 +8,17 @@ module FriendlyShipping
8
8
  class ApiError < FriendlyShipping::ApiError
9
9
  # @param [RestClient::Exception] cause
10
10
  def initialize(cause)
11
- super cause, parse_message(cause)
11
+ super(cause, parse_message(cause))
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- # @param [RestClient::Exception] cause
17
- def parse_message(cause)
18
- return cause.message unless cause.response
16
+ # @param [RestClient::Exception] error
17
+ # @return [String]
18
+ def parse_message(error)
19
+ return error.message unless error.response
19
20
 
20
- parsed_json = JSON.parse(cause.response.body)
21
+ parsed_json = JSON.parse(error.response.body)
21
22
 
22
23
  if parsed_json['httpCode'].present?
23
24
  status = [parsed_json['httpCode'], parsed_json['httpMessage']].compact.join(" ")
@@ -31,6 +32,8 @@ module FriendlyShipping
31
32
  [status, desc].compact.join(": ").presence || "UPS Freight could not process the request."
32
33
  end.join("\n")
33
34
  end
35
+ rescue JSON::ParserError, KeyError => _e
36
+ nil
34
37
  end
35
38
  end
36
39
  end
@@ -4,26 +4,72 @@ module FriendlyShipping
4
4
  module Services
5
5
  class UpsFreight
6
6
  class GenerateCommodityInformation
7
- def self.call(shipment:, options:)
8
- shipment.packages.flat_map do |package|
9
- package_options = options.options_for_package(package)
10
- package.items.map do |item|
11
- item_options = package_options.options_for_item(item)
12
- {
13
- # This is a required field
14
- Description: item.description || 'Commodities',
15
- Weight: {
16
- UnitOfMeasurement: {
17
- Code: 'LBS' # Only Pounds are supported
7
+ class << self
8
+ # @param shipment [Physical::Shipment]
9
+ # @param options [#options_for_package, #options_for_structure]
10
+ # @return [Array<Hash>]
11
+ def call(shipment:, options:)
12
+ if shipment.packages.any?
13
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
14
+ serialize_packages(shipment.packages, options)
15
+ else
16
+ serialize_structures(shipment.structures, options)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ # @param packages [Array<Physical::Package>]
23
+ # @param options [#options_for_package]
24
+ # @return [Array<Hash>]
25
+ def serialize_packages(packages, options)
26
+ packages.flat_map do |package|
27
+ package_options = options.options_for_package(package)
28
+ package.items.map do |item|
29
+ item_options = package_options.options_for_item(item)
30
+ {
31
+ # This is a required field
32
+ Description: item.description || 'Commodities',
33
+ Weight: {
34
+ UnitOfMeasurement: {
35
+ Code: 'LBS' # Only Pounds are supported
36
+ },
37
+ Value: item.weight.convert_to(:pounds).value.to_f.round(2).to_s
38
+ },
39
+ NumberOfPieces: '1', # We won't support this yet.
40
+ PackagingType: {
41
+ Code: item_options.packaging_code
42
+ },
43
+ FreightClass: item_options.freight_class
44
+ }
45
+ end
46
+ end
47
+ end
48
+
49
+ # @param structures [Array<Physical::Structure>]
50
+ # @param options [#options_for_structure]
51
+ # @return [Array<Hash>]
52
+ def serialize_structures(structures, options)
53
+ structures.flat_map do |structure|
54
+ structure_options = options.options_for_structure(structure)
55
+ structure.packages.map do |package|
56
+ package_options = structure_options.options_for_package(package)
57
+ {
58
+ # This is a required field
59
+ Description: package.description || 'Commodities',
60
+ Weight: {
61
+ UnitOfMeasurement: {
62
+ Code: 'LBS' # Only Pounds are supported
63
+ },
64
+ Value: package.weight.convert_to(:pounds).value.to_f.round(2).to_s
65
+ },
66
+ NumberOfPieces: '1', # We won't support this yet.
67
+ PackagingType: {
68
+ Code: package_options.packaging_code
18
69
  },
19
- Value: item.weight.convert_to(:pounds).value.to_f.round(2).to_s
20
- },
21
- NumberOfPieces: '1', # We won't support this yet.
22
- PackagingType: {
23
- Code: item_options.packaging_code
24
- },
25
- FreightClass: item_options.freight_class
26
- }
70
+ FreightClass: package_options.freight_class
71
+ }
72
+ end
27
73
  end
28
74
  end
29
75
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'friendly_shipping/services/ups_freight/generate_location_hash'
4
4
  require 'friendly_shipping/services/ups_freight/generate_pickup_request_hash'
5
+ require 'friendly_shipping/services/ups_freight/generate_handling_units_hash'
5
6
 
6
7
  module FriendlyShipping
7
8
  module Services
@@ -22,31 +23,13 @@ module FriendlyShipping
22
23
  Commodity: options.commodity_information_generator.call(shipment: shipment, options: options),
23
24
  TimeInTransitIndicator: 'true',
24
25
  PickupRequest: GeneratePickupRequestHash.call(pickup_request_options: options.pickup_request_options),
25
- }.compact.merge(handling_units(shipment, options).reduce(&:merge).to_h)
26
+ }.compact.
27
+ merge(GenerateHandlingUnitsHash.call(shipment: shipment, options: options))
26
28
  }
27
29
  end
28
30
 
29
31
  private
30
32
 
31
- def handling_units(shipment, options)
32
- all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
33
- all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
34
- [options_group.first, options_group.length]
35
- end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
36
- end
37
-
38
- def handling_unit_hash(package_options, quantity)
39
- {
40
- package_options.handling_unit_tag => {
41
- Quantity: quantity.to_s,
42
- Type: {
43
- Code: package_options.handling_unit_code,
44
- Description: package_options.handling_unit_description
45
- }
46
- }
47
- }
48
- end
49
-
50
33
  def request_options(customer_context)
51
34
  return {} unless customer_context
52
35
 
@@ -6,6 +6,7 @@ require 'friendly_shipping/services/ups_freight/generate_document_options_hash'
6
6
  require 'friendly_shipping/services/ups_freight/generate_email_options_hash'
7
7
  require 'friendly_shipping/services/ups_freight/generate_pickup_options_hash'
8
8
  require 'friendly_shipping/services/ups_freight/generate_delivery_options_hash'
9
+ require 'friendly_shipping/services/ups_freight/generate_handling_units_hash'
9
10
 
10
11
  module FriendlyShipping
11
12
  module Services
@@ -33,7 +34,7 @@ module FriendlyShipping
33
34
  DeliveryInstructions: options.delivery_instructions,
34
35
  PickupRequest: GeneratePickupRequestHash.call(pickup_request_options: options.pickup_request_options),
35
36
  }.compact.
36
- merge(handling_units(shipment, options).reduce(&:merge).to_h).
37
+ merge(GenerateHandlingUnitsHash.call(shipment: shipment, options: options)).
37
38
  merge(GenerateReferenceHash.call(reference_numbers: options.reference_numbers))
38
39
  }
39
40
  }
@@ -48,25 +49,6 @@ module FriendlyShipping
48
49
  [email_options, pickup_options, delivery_options].compact.presence
49
50
  end
50
51
 
51
- def handling_units(shipment, options)
52
- all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
53
- all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
54
- [options_group.first, options_group.length]
55
- end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
56
- end
57
-
58
- def handling_unit_hash(package_options, quantity)
59
- {
60
- package_options.handling_unit_tag => {
61
- Quantity: quantity.to_s,
62
- Type: {
63
- Code: package_options.handling_unit_code,
64
- Description: package_options.handling_unit_description
65
- }
66
- }
67
- }
68
- end
69
-
70
52
  def payment_information(options)
71
53
  payer_address = GenerateLocationHash.call(location: options.billing_address).
72
54
  merge(ShipperNumber: options.shipper_number)
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsFreight
6
+ # Generates a handling units hash for JSON serialization.
7
+ class GenerateHandlingUnitsHash
8
+ class << self
9
+ # @param shipment [Physical::Shipment]
10
+ # @param options [ShipmentOptions]
11
+ # @return [Hash]
12
+ def call(shipment:, options:)
13
+ handling_units(shipment, options).reduce(&:merge)
14
+ end
15
+
16
+ private
17
+
18
+ # @param shipment [Physical::Shipment]
19
+ # @param options [ShipmentOptions]
20
+ # @return [Array<Hash>]
21
+ def handling_units(shipment, options)
22
+ if shipment.packages.any?
23
+ warn "[DEPRECATION] `packages` is deprecated. Please use `structures` instead."
24
+ all_package_options = shipment.packages.map { |package| options.options_for_package(package) }
25
+ all_package_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
26
+ [options_group.first, options_group.length]
27
+ end.map { |package_options, quantity| handling_unit_hash(package_options, quantity) }
28
+ else
29
+ all_structure_options = shipment.structures.map { |structure| options.options_for_structure(structure) }
30
+ all_structure_options.group_by(&:handling_unit_code).map do |_handling_unit_code, options_group|
31
+ [options_group.first, options_group.length]
32
+ end.map { |structure_options, quantity| handling_unit_hash(structure_options, quantity) }
33
+ end
34
+ end
35
+
36
+ # @param options [PackageOptions, StructureOptions]
37
+ # @param quantity [Integer]
38
+ # @return [Hash]
39
+ def handling_unit_hash(options, quantity)
40
+ {
41
+ options.handling_unit_tag.to_sym => {
42
+ Quantity: quantity.to_s,
43
+ Type: {
44
+ Code: options.handling_unit_code,
45
+ Description: options.handling_unit_description
46
+ }
47
+ }
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end