friendly_shipping 0.8.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -1
  3. data/.env.template +3 -0
  4. data/.env.test.local.template +6 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop-relaxed.yml +7 -23
  7. data/.rubocop.yml +17 -2
  8. data/.rubocop_todo.yml +21 -0
  9. data/.yardopts +6 -0
  10. data/CHANGELOG.md +71 -0
  11. data/Gemfile +17 -0
  12. data/README.md +1 -1
  13. data/friendly_shipping.gemspec +8 -17
  14. data/lib/friendly_shipping/access_token.rb +24 -0
  15. data/lib/friendly_shipping/api_error.rb +8 -4
  16. data/lib/friendly_shipping/api_error_handler.rb +11 -9
  17. data/lib/friendly_shipping/api_failure.rb +2 -7
  18. data/lib/friendly_shipping/api_result.rb +26 -4
  19. data/lib/friendly_shipping/carrier.rb +28 -8
  20. data/lib/friendly_shipping/http_client.rb +25 -8
  21. data/lib/friendly_shipping/inflections.rb +10 -0
  22. data/lib/friendly_shipping/item_options.rb +3 -0
  23. data/lib/friendly_shipping/label.rb +41 -22
  24. data/lib/friendly_shipping/package_options.rb +21 -2
  25. data/lib/friendly_shipping/rate.rb +50 -15
  26. data/lib/friendly_shipping/request.rb +23 -7
  27. data/lib/friendly_shipping/response.rb +21 -6
  28. data/lib/friendly_shipping/services/rl/api_error.rb +31 -0
  29. data/lib/friendly_shipping/services/rl/bol_options.rb +103 -0
  30. data/lib/friendly_shipping/services/rl/bol_packages_serializer.rb +32 -0
  31. data/lib/friendly_shipping/services/rl/bol_structures_serializer.rb +31 -0
  32. data/lib/friendly_shipping/services/rl/item_options.rb +36 -0
  33. data/lib/friendly_shipping/services/rl/package_options.rb +38 -0
  34. data/lib/friendly_shipping/services/rl/parse_create_bol_response.rb +43 -0
  35. data/lib/friendly_shipping/services/rl/parse_invoice_response.rb +47 -0
  36. data/lib/friendly_shipping/services/rl/parse_print_bol_response.rb +44 -0
  37. data/lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb +44 -0
  38. data/lib/friendly_shipping/services/rl/parse_rate_quote_response.rb +93 -0
  39. data/lib/friendly_shipping/services/rl/parse_transit_times_response.rb +73 -0
  40. data/lib/friendly_shipping/services/rl/rate_quote_options.rb +82 -0
  41. data/lib/friendly_shipping/services/rl/rate_quote_packages_serializer.rb +54 -0
  42. data/lib/friendly_shipping/services/rl/rate_quote_structures_serializer.rb +53 -0
  43. data/lib/friendly_shipping/services/rl/serialize_create_bol_request.rb +86 -0
  44. data/lib/friendly_shipping/services/rl/serialize_location.rb +53 -0
  45. data/lib/friendly_shipping/services/rl/serialize_rate_quote_request.rb +69 -0
  46. data/lib/friendly_shipping/services/rl/serialize_transit_times_request.rb +38 -0
  47. data/lib/friendly_shipping/services/rl/shipment_document.rb +40 -0
  48. data/lib/friendly_shipping/services/rl/shipment_information.rb +41 -0
  49. data/lib/friendly_shipping/services/rl/shipment_options.rb +48 -0
  50. data/lib/friendly_shipping/services/rl/shipping_methods.rb +28 -0
  51. data/lib/friendly_shipping/services/rl/structure_options.rb +13 -0
  52. data/lib/friendly_shipping/services/rl.rb +187 -0
  53. data/lib/friendly_shipping/services/ship_engine/api_error.rb +31 -0
  54. data/lib/friendly_shipping/services/ship_engine/customs_items_serializer.rb +36 -0
  55. data/lib/friendly_shipping/services/ship_engine/label_customs_options.rb +10 -7
  56. data/lib/friendly_shipping/services/ship_engine/label_item_options.rb +10 -7
  57. data/lib/friendly_shipping/services/ship_engine/label_options.rb +30 -17
  58. data/lib/friendly_shipping/services/ship_engine/label_package_options.rb +18 -14
  59. data/lib/friendly_shipping/services/ship_engine/parse_address_validation_response.rb +75 -0
  60. data/lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb +9 -2
  61. data/lib/friendly_shipping/services/ship_engine/parse_label_response.rb +4 -2
  62. data/lib/friendly_shipping/services/ship_engine/{parse_rate_estimate_response.rb → parse_rate_estimates_response.rb} +27 -9
  63. data/lib/friendly_shipping/services/ship_engine/parse_rates_response.rb +99 -0
  64. data/lib/friendly_shipping/services/ship_engine/parse_void_response.rb +5 -1
  65. data/lib/friendly_shipping/services/ship_engine/rate_estimates_options.rb +17 -6
  66. data/lib/friendly_shipping/services/ship_engine/rates_item_options.rb +26 -0
  67. data/lib/friendly_shipping/services/ship_engine/rates_options.rb +59 -0
  68. data/lib/friendly_shipping/services/ship_engine/rates_package_options.rb +18 -0
  69. data/lib/friendly_shipping/services/ship_engine/serialize_address_residential_indicator.rb +27 -0
  70. data/lib/friendly_shipping/services/ship_engine/serialize_address_validation_request.rb +31 -0
  71. data/lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb +22 -27
  72. data/lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb +41 -16
  73. data/lib/friendly_shipping/services/ship_engine/serialize_rates_request.rb +126 -0
  74. data/lib/friendly_shipping/services/ship_engine.rb +89 -33
  75. data/lib/friendly_shipping/services/ship_engine_ltl/api_error.rb +12 -0
  76. data/lib/friendly_shipping/services/ship_engine_ltl/item_options.rb +24 -5
  77. data/lib/friendly_shipping/services/ship_engine_ltl/package_options.rb +39 -4
  78. data/lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb +4 -2
  79. data/lib/friendly_shipping/services/ship_engine_ltl/parse_quote_response.rb +10 -2
  80. data/lib/friendly_shipping/services/ship_engine_ltl/quote_options.rb +29 -11
  81. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_packages.rb +7 -2
  82. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_quote_request.rb +50 -16
  83. data/lib/friendly_shipping/services/ship_engine_ltl/serialize_structures.rb +42 -0
  84. data/lib/friendly_shipping/services/ship_engine_ltl/shipment_options.rb +47 -0
  85. data/lib/friendly_shipping/services/ship_engine_ltl/structure_options.rb +17 -0
  86. data/lib/friendly_shipping/services/ship_engine_ltl.rb +47 -39
  87. data/lib/friendly_shipping/services/tforce_freight/access_token.rb +28 -0
  88. data/lib/friendly_shipping/services/tforce_freight/api_error.rb +40 -0
  89. data/lib/friendly_shipping/services/tforce_freight/bol_options.rb +180 -0
  90. data/lib/friendly_shipping/services/tforce_freight/document_options.rb +100 -0
  91. data/lib/friendly_shipping/services/tforce_freight/generate_commodity_information.rb +92 -0
  92. data/lib/friendly_shipping/services/tforce_freight/generate_create_bol_request_hash.rb +153 -0
  93. data/lib/friendly_shipping/services/tforce_freight/generate_document_options_hash.rb +36 -0
  94. data/lib/friendly_shipping/services/tforce_freight/generate_handling_units_hash.rb +51 -0
  95. data/lib/friendly_shipping/services/tforce_freight/generate_location_hash.rb +25 -0
  96. data/lib/friendly_shipping/services/tforce_freight/generate_pickup_request_hash.rb +111 -0
  97. data/lib/friendly_shipping/services/tforce_freight/generate_rates_request_hash.rb +63 -0
  98. data/lib/friendly_shipping/services/tforce_freight/generate_reference_hash.rb +28 -0
  99. data/lib/friendly_shipping/services/tforce_freight/item_options.rb +93 -0
  100. data/lib/friendly_shipping/services/tforce_freight/package_options.rb +119 -0
  101. data/lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb +90 -0
  102. data/lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb +45 -0
  103. data/lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb +50 -0
  104. data/lib/friendly_shipping/services/tforce_freight/parse_shipment_document.rb +29 -0
  105. data/lib/friendly_shipping/services/tforce_freight/pickup_options.rb +82 -0
  106. data/lib/friendly_shipping/services/tforce_freight/rates_item_options.rb +10 -0
  107. data/lib/friendly_shipping/services/tforce_freight/rates_options.rb +159 -0
  108. data/lib/friendly_shipping/services/tforce_freight/rates_package_options.rb +10 -0
  109. data/lib/friendly_shipping/services/tforce_freight/shipment_document.rb +38 -0
  110. data/lib/friendly_shipping/services/tforce_freight/shipment_information.rb +104 -0
  111. data/lib/friendly_shipping/services/tforce_freight/shipment_options.rb +47 -0
  112. data/lib/friendly_shipping/services/tforce_freight/shipping_methods.rb +25 -0
  113. data/lib/friendly_shipping/services/tforce_freight/structure_options.rb +44 -0
  114. data/lib/friendly_shipping/services/tforce_freight.rb +176 -0
  115. data/lib/friendly_shipping/services/ups/label_item_options.rb +0 -2
  116. data/lib/friendly_shipping/services/ups/label_options.rb +14 -5
  117. data/lib/friendly_shipping/services/ups/label_package_options.rb +1 -3
  118. data/lib/friendly_shipping/services/ups/parse_address_validation_response.rb +1 -1
  119. data/lib/friendly_shipping/services/ups/parse_modifier_element.rb +29 -0
  120. data/lib/friendly_shipping/services/ups/parse_rate_response.rb +11 -3
  121. data/lib/friendly_shipping/services/ups/parse_shipment_accept_response.rb +0 -3
  122. data/lib/friendly_shipping/services/ups/parse_shipment_confirm_response.rb +0 -2
  123. data/lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb +1 -1
  124. data/lib/friendly_shipping/services/ups/parse_void_shipment_response.rb +0 -2
  125. data/lib/friendly_shipping/services/ups/parse_xml_response.rb +1 -1
  126. data/lib/friendly_shipping/services/ups/rate_estimate_options.rb +14 -3
  127. data/lib/friendly_shipping/services/ups/rate_estimate_package_options.rb +0 -2
  128. data/lib/friendly_shipping/services/ups/serialize_access_request.rb +0 -2
  129. data/lib/friendly_shipping/services/ups/serialize_rating_service_selection_request.rb +10 -5
  130. data/lib/friendly_shipping/services/ups/serialize_shipment_accept_request.rb +1 -1
  131. data/lib/friendly_shipping/services/ups/serialize_shipment_confirm_request.rb +2 -4
  132. data/lib/friendly_shipping/services/ups/shipping_methods.rb +1 -1
  133. data/lib/friendly_shipping/services/ups.rb +2 -24
  134. data/lib/friendly_shipping/services/ups_freight/api_error.rb +8 -7
  135. data/lib/friendly_shipping/services/ups_freight/generate_commodity_information.rb +65 -19
  136. data/lib/friendly_shipping/services/ups_freight/generate_freight_rate_request_hash.rb +2 -23
  137. data/lib/friendly_shipping/services/ups_freight/generate_freight_ship_request_hash.rb +1 -27
  138. data/lib/friendly_shipping/services/ups_freight/generate_handling_units_hash.rb +54 -0
  139. data/lib/friendly_shipping/services/ups_freight/label_options.rb +36 -10
  140. data/lib/friendly_shipping/services/ups_freight/label_structure_options.rb +13 -0
  141. data/lib/friendly_shipping/services/ups_freight/parse_freight_label_response.rb +2 -5
  142. data/lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb +0 -4
  143. data/lib/friendly_shipping/services/ups_freight/parse_shipment_document.rb +1 -3
  144. data/lib/friendly_shipping/services/ups_freight/rates_item_options.rb +18 -9
  145. data/lib/friendly_shipping/services/ups_freight/rates_options.rb +30 -24
  146. data/lib/friendly_shipping/services/ups_freight/rates_package_options.rb +79 -10
  147. data/lib/friendly_shipping/services/ups_freight/rates_structure_options.rb +44 -0
  148. data/lib/friendly_shipping/services/ups_freight/shipment_information.rb +7 -3
  149. data/lib/friendly_shipping/services/ups_freight/shipment_options.rb +47 -0
  150. data/lib/friendly_shipping/services/ups_freight.rb +2 -20
  151. data/lib/friendly_shipping/services/ups_json/access_token.rb +20 -0
  152. data/lib/friendly_shipping/services/ups_json/api_error.rb +27 -0
  153. data/lib/friendly_shipping/services/ups_json/generate_address_classification_payload.rb +29 -0
  154. data/lib/friendly_shipping/services/ups_json/generate_address_hash.rb +30 -0
  155. data/lib/friendly_shipping/services/ups_json/generate_city_state_lookup_payload.rb +23 -0
  156. data/lib/friendly_shipping/services/ups_json/generate_labels_payload.rb +208 -0
  157. data/lib/friendly_shipping/services/ups_json/generate_package_hash.rb +76 -0
  158. data/lib/friendly_shipping/services/ups_json/generate_rates_payload.rb +84 -0
  159. data/lib/friendly_shipping/services/ups_json/generate_timings_payload.rb +44 -0
  160. data/lib/friendly_shipping/services/ups_json/label.rb +20 -0
  161. data/lib/friendly_shipping/services/ups_json/label_billing_options.rb +41 -0
  162. data/lib/friendly_shipping/services/ups_json/label_item_options.rb +75 -0
  163. data/lib/friendly_shipping/services/ups_json/label_options.rb +174 -0
  164. data/lib/friendly_shipping/services/ups_json/label_package_options.rb +49 -0
  165. data/lib/friendly_shipping/services/ups_json/parse_address_classification_response.rb +31 -0
  166. data/lib/friendly_shipping/services/ups_json/parse_city_state_lookup_response.rb +44 -0
  167. data/lib/friendly_shipping/services/ups_json/parse_json_response.rb +51 -0
  168. data/lib/friendly_shipping/services/ups_json/parse_labels_response.rb +71 -0
  169. data/lib/friendly_shipping/services/ups_json/parse_money_hash.rb +128 -0
  170. data/lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb +28 -0
  171. data/lib/friendly_shipping/services/ups_json/parse_rates_response.rb +107 -0
  172. data/lib/friendly_shipping/services/ups_json/parse_timings_response.rb +56 -0
  173. data/lib/friendly_shipping/services/ups_json/parse_void_response.rb +32 -0
  174. data/lib/friendly_shipping/services/ups_json/rates_item_options.rb +20 -0
  175. data/lib/friendly_shipping/services/ups_json/rates_options.rb +111 -0
  176. data/lib/friendly_shipping/services/ups_json/rates_package_options.rb +17 -0
  177. data/lib/friendly_shipping/services/ups_json/shipping_methods.rb +111 -0
  178. data/lib/friendly_shipping/services/ups_json/timings_options.rb +33 -0
  179. data/lib/friendly_shipping/services/ups_json.rb +216 -0
  180. data/lib/friendly_shipping/services/usps/choose_package_rate.rb +3 -3
  181. data/lib/friendly_shipping/services/usps/machinable_package.rb +1 -1
  182. data/lib/friendly_shipping/services/usps/parse_package_rate.rb +6 -6
  183. data/lib/friendly_shipping/services/usps/parse_rate_response.rb +0 -4
  184. data/lib/friendly_shipping/services/usps/parse_time_in_transit_response.rb +6 -9
  185. data/lib/friendly_shipping/services/usps/parse_xml_response.rb +1 -1
  186. data/lib/friendly_shipping/services/usps/rate_estimate_options.rb +1 -3
  187. data/lib/friendly_shipping/services/usps/rate_estimate_package_options.rb +6 -3
  188. data/lib/friendly_shipping/services/usps/serialize_rate_request.rb +2 -4
  189. data/lib/friendly_shipping/services/usps/shipping_methods.rb +4 -3
  190. data/lib/friendly_shipping/services/usps.rb +2 -13
  191. data/lib/friendly_shipping/services/usps_international/parse_package_rate.rb +3 -3
  192. data/lib/friendly_shipping/services/usps_international/parse_rate_response.rb +0 -3
  193. data/lib/friendly_shipping/services/usps_international/rate_estimate_options.rb +1 -3
  194. data/lib/friendly_shipping/services/usps_international/rate_estimate_package_options.rb +0 -2
  195. data/lib/friendly_shipping/services/usps_international/serialize_rate_request.rb +1 -3
  196. data/lib/friendly_shipping/services/usps_international.rb +2 -6
  197. data/lib/friendly_shipping/services/usps_ship/access_token.rb +22 -0
  198. data/lib/friendly_shipping/services/usps_ship/api_error.rb +27 -0
  199. data/lib/friendly_shipping/services/usps_ship/machinable_package.rb +55 -0
  200. data/lib/friendly_shipping/services/usps_ship/parse_rate_estimates_response.rb +80 -0
  201. data/lib/friendly_shipping/services/usps_ship/parse_timings_response.rb +82 -0
  202. data/lib/friendly_shipping/services/usps_ship/rate_estimate_options.rb +45 -0
  203. data/lib/friendly_shipping/services/usps_ship/rate_estimate_package_options.rb +122 -0
  204. data/lib/friendly_shipping/services/usps_ship/serialize_rate_estimates_request.rb +53 -0
  205. data/lib/friendly_shipping/services/usps_ship/shipping_methods.rb +38 -0
  206. data/lib/friendly_shipping/services/{ship_engine_ltl/bad_request.rb → usps_ship/timing_options.rb} +2 -2
  207. data/lib/friendly_shipping/services/usps_ship.rb +185 -0
  208. data/lib/friendly_shipping/shipment_options.rb +13 -1
  209. data/lib/friendly_shipping/shipping_method.rb +38 -11
  210. data/lib/friendly_shipping/structure_options.rb +38 -0
  211. data/lib/friendly_shipping/timing.rb +42 -7
  212. data/lib/friendly_shipping/version.rb +1 -1
  213. data/lib/friendly_shipping.rb +20 -15
  214. metadata +143 -174
  215. data/lib/friendly_shipping/services/ship_engine/bad_request.rb +0 -29
  216. data/lib/friendly_shipping/services/ship_engine/bad_request_handler.rb +0 -33
  217. data/lib/friendly_shipping/services/ship_engine_ltl/bad_request_handler.rb +0 -33
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ # Represents billing-related options for obtaining shipment labels.
7
+ # @option bill_third_party [Boolean] When truthy, bill an account other than the shipper's.
8
+ # Specified by billing_(account, zip and country)
9
+ # @option bill_to_consignee [Boolean] If billing a third party, bill the consignee instead of the 3rd party shipper
10
+ # @option prepay [Boolean] If truthy the shipper will be billed immediately. Otherwise the shipper is billed
11
+ # when the label is used. Default: false
12
+ class LabelBillingOptions
13
+ attr_reader :bill_third_party,
14
+ :bill_to_consignee,
15
+ :prepay,
16
+ :billing_account,
17
+ :billing_zip,
18
+ :billing_country,
19
+ :currency
20
+
21
+ def initialize(
22
+ bill_third_party: false,
23
+ bill_to_consignee: false,
24
+ prepay: false,
25
+ billing_account: nil,
26
+ billing_zip: nil,
27
+ billing_country: nil,
28
+ currency: nil
29
+ )
30
+ @bill_third_party = bill_third_party
31
+ @bill_to_consignee = bill_to_consignee
32
+ @prepay = prepay
33
+ @billing_account = billing_account
34
+ @billing_zip = billing_zip
35
+ @billing_country = billing_country
36
+ @currency = currency
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class LabelItemOptions < FriendlyShipping::ItemOptions
7
+ PRODUCT_UNIT_OF_MEASURE_CODES = {
8
+ barrel: 'BA',
9
+ bundle: 'BE',
10
+ bag: 'BG',
11
+ bunch: 'BH',
12
+ box: 'BOX',
13
+ bolt: 'BT',
14
+ butt: 'BU',
15
+ canister: 'CI',
16
+ centimeter: 'CM',
17
+ container: 'CON',
18
+ crate: 'CR',
19
+ case: 'CS',
20
+ carton: 'CT',
21
+ cylinder: 'CY',
22
+ dozen: 'DOZ',
23
+ each: 'EA',
24
+ envelope: 'EN',
25
+ feet: 'FT',
26
+ kilogram: 'KG',
27
+ kilograms: 'KGS',
28
+ pound: 'LB',
29
+ pounds: 'LBS',
30
+ liter: 'L',
31
+ meter: 'M',
32
+ number: 'NMB',
33
+ packet: 'PA',
34
+ pallet: 'PAL',
35
+ piece: 'PC',
36
+ pieces: 'PCS',
37
+ proof_liters: 'PF',
38
+ package: 'PKG',
39
+ pair: 'PR',
40
+ pairs: 'PRS',
41
+ roll: 'RL',
42
+ set: 'SET',
43
+ square_meters: 'SME',
44
+ square_yards: 'SYD',
45
+ tube: 'TU',
46
+ yard: 'YD',
47
+ other: 'OTH'
48
+ }.freeze
49
+
50
+ attr_reader :commodity_code,
51
+ :country_of_origin
52
+
53
+ def initialize(
54
+ commodity_code: nil,
55
+ country_of_origin: nil,
56
+ product_unit_of_measure: :number,
57
+ **kwargs
58
+ )
59
+ @commodity_code = commodity_code
60
+ @country_of_origin = country_of_origin
61
+ @product_unit_of_measure = product_unit_of_measure
62
+ super(**kwargs)
63
+ end
64
+
65
+ def product_unit_of_measure_code
66
+ PRODUCT_UNIT_OF_MEASURE_CODES[product_unit_of_measure]
67
+ end
68
+
69
+ private
70
+
71
+ attr_reader :product_unit_of_measure
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ # Option container for a generating UPS labels for a shipment
6
+ #
7
+ # Required:
8
+ #
9
+ # @param shipping_method [FriendlyShipping::ShippingMethod] The shipping method to use. We only need the
10
+ # service_code to be set.
11
+ # @param shipper_number [String] account number for the shipper
12
+ #
13
+ # Optional:
14
+ #
15
+ # @param shipper [Physical::Location] The company sending the shipment. Defaults to the shipment's origin.
16
+ # @param sub_version [String] The UPS API sub-version to use for requests. Default: 1707
17
+ # @param customer_context [String ] Optional element to identify transactions between client and server
18
+ # @param validate_address [Boolean] Validate the city field with ZIP code and state. If false, only ZIP code
19
+ # and state are validated. Default: true
20
+ # @param negotiated_rates [Boolean] if truthy negotiated rates will be requested from ups. Only valid if
21
+ # shipper account has negotiated rates. Default: false
22
+ # @option sold_to [Physical::Location] The person or company who imports and pays any duties due on the
23
+ # current shipment. Default: The shipment's destination
24
+ # @option saturday_delivery [Boolean] should we request Saturday delivery?. Default: false
25
+ # @option label_format [String] GIF, EPL, ZPL, STARPL and SPL
26
+ # @option label_size [Array<Integer>] Dimensions of the label. Default: [4, 6]
27
+ # @option delivery_confirmation [Symbol] Can be set to any key from SHIPMENT_DELIVERY_CONFIRMATION_CODES.
28
+ # Only possible for international shipments that are not between the US and Puerto Rico.
29
+ # @option carbon_neutral [Boolean] Ship with UPS' carbon neutral program
30
+ # @option return_service_code [Symbol] If present, marks this a return label. The kind
31
+ # of return label is specified by the symbol, one of the keys in RETURN_SERVICE_CODES. Default: nil
32
+ #
33
+ # Shipment options for international shipping:
34
+ #
35
+ # @option paperless_invoice [Boolean] set to truthy if using paperless invoice to ship internationally. Default false
36
+ # @option terms_of_shipment [Symbol] used with paperless invoice to specify who pays duties and taxes.
37
+ # See TERMS_OF_SHIPMENT constant for possible options.
38
+ # @option reason_for_export [String] A reason to export the current shipment. Possible values: 'SALE', 'GIFT', 'SAMPLE',
39
+ # 'RETURN', 'REPAIR', 'INTERCOMPANYDATA', Any other reason. Default: 'SALE'.
40
+ # @option invoice_date [Date] The invoice date for the shipment
41
+ # @param declaration_statement [String ] Optional element to add customs declaration text
42
+ #
43
+ class UpsJson
44
+ class LabelOptions < FriendlyShipping::ShipmentOptions
45
+ SHIPMENT_DELIVERY_CONFIRMATION_CODES = {
46
+ delivery_confirmation_signature_required: 1,
47
+ delivery_confirmation_adult_signature_required: 2
48
+ }.freeze
49
+
50
+ TERMS_OF_SHIPMENT_CODES = {
51
+ cost_and_freight: 'CFR',
52
+ cost_insurance_and_freight: 'CIF',
53
+ carriage_and_insurance_paid: 'CIP',
54
+ carriage_paid_to: 'CPT',
55
+ delivered_at_frontier: 'DAF',
56
+ delivery_duty_paid: 'DDP',
57
+ delivery_duty_unpaid: 'DDU',
58
+ delivered_ex_quay: 'DEQ',
59
+ delivered_ex_ship: 'DES',
60
+ ex_works: 'EXW',
61
+ free_alongside_ship: 'FAS',
62
+ free_carrier: 'FCA',
63
+ free_on_board: 'FOB'
64
+ }.freeze
65
+
66
+ RETURN_SERVICE_CODES = {
67
+ ups_print_and_mail: 2, # UPS Print and Mail (PNM)
68
+ ups_return_1_attempt: 3, # UPS Return Service 1-Attempt
69
+ ups_return_3_attempt: 5, # UPS Return Service 3-Attempt (RS3)
70
+ ups_electronic_return_label: 8, # UPS Electronic Return Label (ERL)
71
+ ups_print_return_label: 9, # UPS Print Return Label (PRL)
72
+ ups_exchange_print_return: 10, # UPS Exchange Print Return Label
73
+ ups_pack_collect_1_attemt_box_1: 11, # UPS Pack & Collect Service 1-Attempt Box 1
74
+ ups_pack_collect_1_attemt_box_2: 12, # UPS Pack & Collect Service 1-Attempt Box 2
75
+ ups_pack_collect_1_attemt_box_3: 13, # UPS Pack & Collect Service 1-Attempt Box 3
76
+ ups_pack_collect_1_attemt_box_4: 14, # UPS Pack & Collect Service 1-Attempt Box 4
77
+ ups_pack_collect_1_attemt_box_5: 15, # UPS Pack & Collect Service 1-Attempt Box 5
78
+ ups_pack_collect_3_attemt_box_1: 16, # UPS Pack & Collect Service 1-Attempt Box 1
79
+ ups_pack_collect_3_attemt_box_2: 17, # UPS Pack & Collect Service 1-Attempt Box 2
80
+ ups_pack_collect_3_attemt_box_3: 18, # UPS Pack & Collect Service 1-Attempt Box 3
81
+ ups_pack_collect_3_attemt_box_4: 19, # UPS Pack & Collect Service 1-Attempt Box 4
82
+ ups_pack_collect_3_attemt_box_5: 20 # UPS Pack & Collect Service 1-Attempt Box 5
83
+ }.freeze
84
+
85
+ SUB_VERSIONS = %w[1601 1607 1701 1707 1801 1807 2108 2205].freeze
86
+
87
+ attr_reader :shipping_method,
88
+ :shipper_number,
89
+ :shipper,
90
+ :sub_version,
91
+ :customer_context,
92
+ :validate_address,
93
+ :negotiated_rates,
94
+ :billing_options,
95
+ :sold_to,
96
+ :saturday_delivery,
97
+ :label_format,
98
+ :label_size,
99
+ :carbon_neutral,
100
+ :paperless_invoice,
101
+ :reason_for_export,
102
+ :invoice_date,
103
+ :declaration_statement
104
+
105
+ def initialize(
106
+ shipping_method:,
107
+ shipper_number:,
108
+ shipper: nil,
109
+ sub_version: '2205',
110
+ customer_context: nil,
111
+ validate_address: true,
112
+ negotiated_rates: false,
113
+ billing_options: LabelBillingOptions.new,
114
+ sold_to: nil,
115
+ saturday_delivery: false,
116
+ label_format: 'GIF',
117
+ label_size: [4, 6],
118
+ delivery_confirmation: nil,
119
+ carbon_neutral: true,
120
+ return_service: nil,
121
+ paperless_invoice: false,
122
+ terms_of_shipment: nil,
123
+ reason_for_export: 'SALE',
124
+ invoice_date: nil,
125
+ package_options_class: LabelPackageOptions,
126
+ declaration_statement: nil,
127
+ **kwargs
128
+ )
129
+ raise ArgumentError, "Invalid sub-version: #{sub_version}" unless sub_version.in?(SUB_VERSIONS)
130
+
131
+ @shipping_method = shipping_method
132
+ @shipper_number = shipper_number
133
+ @shipper = shipper
134
+ @sub_version = sub_version
135
+ @customer_context = customer_context
136
+ @validate_address = validate_address
137
+ @negotiated_rates = negotiated_rates
138
+ @billing_options = billing_options
139
+ @sold_to = sold_to
140
+ @saturday_delivery = saturday_delivery
141
+ @label_format = label_format
142
+ @label_size = label_size
143
+ @delivery_confirmation = delivery_confirmation
144
+ @carbon_neutral = carbon_neutral
145
+ @return_service = return_service
146
+ @paperless_invoice = paperless_invoice
147
+ @terms_of_shipment = terms_of_shipment
148
+ @reason_for_export = reason_for_export
149
+ @invoice_date = invoice_date
150
+ @declaration_statement = declaration_statement
151
+ super(**kwargs.reverse_merge(package_options_class: package_options_class))
152
+ end
153
+
154
+ def delivery_confirmation_code
155
+ SHIPMENT_DELIVERY_CONFIRMATION_CODES[delivery_confirmation]
156
+ end
157
+
158
+ def terms_of_shipment_code
159
+ TERMS_OF_SHIPMENT_CODES[terms_of_shipment]
160
+ end
161
+
162
+ def return_service_code
163
+ RETURN_SERVICE_CODES[return_service]
164
+ end
165
+
166
+ private
167
+
168
+ attr_reader :terms_of_shipment,
169
+ :return_service,
170
+ :delivery_confirmation
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ # Package properties relevant for generating a UPS shipping label
7
+ #
8
+ # @option reference_numbers [Hash] a Hash where keys are _reference number codes_ and
9
+ # values are _reference number values_. Example: `{ reference_numbers: { xn: 'my_reference_1 }`
10
+ # @option delivery_confirmation [Symbol] Can be set to any key from PACKAGE_DELIVERY_CONFIRMATION_CODES.
11
+ # Only possible for domestic shipments or shipments between the US and Puerto Rico.
12
+ # @option shipper_release [Boolean] Indicates that the package may be released by driver without a signature from
13
+ # the consignee. Default: false
14
+ # @option declared_value [Boolean] When true, declared value (calculated as the sum of all items in the shipment)
15
+ # will be included in the request. Default: false
16
+ class LabelPackageOptions < FriendlyShipping::PackageOptions
17
+ PACKAGE_DELIVERY_CONFIRMATION_CODES = {
18
+ delivery_confirmation: 1,
19
+ delivery_confirmation_signature_required: 2,
20
+ delivery_confirmation_adult_signature_required: 3
21
+ }.freeze
22
+
23
+ attr_reader :reference_numbers, :shipper_release, :declared_value
24
+
25
+ def initialize(
26
+ reference_numbers: {},
27
+ delivery_confirmation: nil,
28
+ shipper_release: false,
29
+ declared_value: false,
30
+ **kwargs
31
+ )
32
+ @reference_numbers = reference_numbers
33
+ @delivery_confirmation = delivery_confirmation
34
+ @shipper_release = shipper_release
35
+ @declared_value = declared_value
36
+ super(**kwargs.reverse_merge(item_options_class: LabelItemOptions))
37
+ end
38
+
39
+ def delivery_confirmation_code
40
+ PACKAGE_DELIVERY_CONFIRMATION_CODES[delivery_confirmation]
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :delivery_confirmation
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ParseAddressClassificationResponse
7
+ extend Dry::Monads::Result::Mixin
8
+
9
+ class << self
10
+ def call(request:, response:)
11
+ parsed_response = ParseJsonResponse.call(
12
+ request: request,
13
+ response: response,
14
+ expected_root_key: 'XAVResponse'
15
+ )
16
+ parsed_response.bind do |classification_response|
17
+ address_type = classification_response.dig('XAVResponse', 'AddressClassification', 'Description')&.downcase
18
+ Success(
19
+ FriendlyShipping::ApiResult.new(
20
+ address_type,
21
+ original_request: request,
22
+ original_response: response
23
+ )
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ParseCityStateLookupResponse
7
+ extend Dry::Monads::Result::Mixin
8
+
9
+ def self.call(request:, response:)
10
+ parsed_response = ParseJsonResponse.call(
11
+ request: request,
12
+ response: response,
13
+ expected_root_key: 'XAVResponse'
14
+ )
15
+ parsed_response.bind do |city_state_lookup_response|
16
+ if city_state_lookup_response['XAVResponse'].keys.include?('NoCandidatesIndicator')
17
+ Failure(
18
+ FriendlyShipping::ApiResult.new(
19
+ "No candidates found.",
20
+ original_request: request,
21
+ original_response: response
22
+ )
23
+ )
24
+ else
25
+ candidate = city_state_lookup_response.dig('XAVResponse', 'Candidate').first
26
+ Success(
27
+ FriendlyShipping::ApiResult.new(
28
+ Physical::Location.new(
29
+ city: candidate.dig('AddressKeyFormat', 'PoliticalDivision2'),
30
+ region: candidate.dig('AddressKeyFormat', 'PoliticalDivision1'),
31
+ country: candidate.dig('AddressKeyFormat', 'CountryCode'),
32
+ zip: candidate.dig('AddressKeyFormat', 'PostcodePrimaryLow')
33
+ ),
34
+ original_request: request,
35
+ original_response: response
36
+ )
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ParseJsonResponse
7
+ extend Dry::Monads::Result::Mixin
8
+ SUCCESSFUL_RESPONSE_STATUS_CODE = '1'
9
+ UNEXPECTED_ROOT_KEY_STRING = 'Empty or unexpected root key'
10
+
11
+ class << self
12
+ def call(request:, response:, expected_root_key:)
13
+ api_error_message = response.headers.try(:[], :errordescription)
14
+ response_body = JSON.parse(response.body)
15
+
16
+ # UPS may return a 2xx status code on an unsuccessful request and include the error description in
17
+ # the response headers, which we will consider a failure
18
+ if api_error_message.present?
19
+ wrap_failure(api_error(api_error_message), request, response)
20
+ elsif response_body.nil? || response_body.keys.first != expected_root_key
21
+ wrap_failure(api_error(UNEXPECTED_ROOT_KEY_STRING), request, response)
22
+ else
23
+ Success(response_body)
24
+ end
25
+ rescue JSON::ParserError => e
26
+ # when the response is not valid JSON(?!), the error description in the header is more descriptive
27
+ return wrap_failure(api_error(api_error_message), request, response) if api_error_message
28
+
29
+ wrap_failure(e, request, response)
30
+ end
31
+
32
+ private
33
+
34
+ def api_error(message)
35
+ FriendlyShipping::ApiError.new(nil, message)
36
+ end
37
+
38
+ def wrap_failure(error, request, response)
39
+ Failure(
40
+ FriendlyShipping::ApiResult.new(
41
+ error,
42
+ original_request: request,
43
+ original_response: response
44
+ )
45
+ )
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ParseLabelsResponse
7
+ class << self
8
+ def call(request:, response:)
9
+ parsed_response = ParseJsonResponse.call(
10
+ request: request,
11
+ response: response,
12
+ expected_root_key: 'ShipmentResponse'
13
+ )
14
+ parsed_response.fmap do |parsing_result|
15
+ FriendlyShipping::ApiResult.new(
16
+ build_labels(parsing_result),
17
+ original_request: request,
18
+ original_response: response
19
+ )
20
+ end
21
+ end
22
+
23
+ def build_labels(labels_result)
24
+ shipment_result = labels_result["ShipmentResponse"]["ShipmentResults"]
25
+
26
+ Array.wrap(shipment_result["PackageResults"]).map do |package|
27
+ cost_breakdown = build_cost_breakdown(package)
28
+ package_cost = cost_breakdown.values.any? ? cost_breakdown.values.sum : nil
29
+
30
+ FriendlyShipping::Services::UpsJson::Label.new(
31
+ shipment_id: shipment_result["ShipmentIdentificationNumber"],
32
+ tracking_number: package["TrackingNumber"],
33
+ usps_tracking_number: package["USPSPICNumber"],
34
+ label_data: Base64.decode64(package["ShippingLabel"]["GraphicImage"]),
35
+ label_format: package["ShippingLabel"]["ImageFormat"]["Code"],
36
+ label_href: package["LabelURL"] || shipment_result["LabelURL"],
37
+ cost: package_cost,
38
+ shipment_cost: get_shipment_cost(shipment_result),
39
+ data: {
40
+ cost_breakdown: cost_breakdown,
41
+ negotiated_rate: get_negotiated_rate(shipment_result),
42
+ customer_context: labels_result["ShipmentResponse"]["Response"]["TransactionReference"]["CustomerContext"]
43
+ }.compact
44
+ )
45
+ end
46
+ end
47
+
48
+ def build_cost_breakdown(package)
49
+ costs = [
50
+ package["BaseServiceCharge"]&.merge("Code" => "BaseServiceCharge"),
51
+ package["ServiceOptionsCharges"],
52
+ package["ItemizedCharges"]
53
+ ].flatten
54
+
55
+ costs.map { |cost| ParseMoneyHash.call(cost, "UnknownSurcharge") }.compact.to_h
56
+ end
57
+
58
+ def get_shipment_cost(shipment_result)
59
+ total_charges_hash = shipment_result.dig("ShipmentCharges", "TotalCharges")
60
+ ParseMoneyHash.call(total_charges_hash, "TotalCharges")&.last
61
+ end
62
+
63
+ def get_negotiated_rate(shipment_result)
64
+ negotiated_total_hash = shipment_result.dig("NegotiatedRateCharges", "TotalCharge")
65
+ ParseMoneyHash.call(negotiated_total_hash, "TotalCharge")&.last
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class ParseMoneyHash
7
+ def self.call(money_hash, key_name)
8
+ return unless money_hash
9
+
10
+ monetary_value = money_hash['MonetaryValue']&.to_d
11
+ return if monetary_value.zero?
12
+
13
+ currency_code = money_hash['CurrencyCode']
14
+ currency = Money::Currency.new(currency_code)
15
+ amount = Money.new(monetary_value * currency.subunit_to_unit, currency)
16
+
17
+ surcharge_code = money_hash['Code']
18
+ label = UPS_SURCHARGE_CODES[surcharge_code] || surcharge_code || key_name
19
+
20
+ [label, amount]
21
+ end
22
+
23
+ UPS_SURCHARGE_CODES = {
24
+ "100" => "ADDITIONAL HANDLING",
25
+ "110" => "COD",
26
+ "120" => "DELIVERY CONFIRMATION",
27
+ "121" => "SHIP DELIVERY CONFIRMATION",
28
+ "153" => "PKG EMAIL SHIP NOTIFICATION",
29
+ "154" => "PKG EMAIL RETURN NOTIFICATION",
30
+ "155" => "PKG EMAIL INBOUND RETURN NOTIFICATION",
31
+ "156" => "PKG EMAIL QUANTUM VIEW SHIP NOTIFICATION",
32
+ "157" => "PKG EMAIL QUANTUM VIEW EXCEPTION NOTIFICATION",
33
+ "158" => "PKG EMAIL QUANTUM VIEW DELIVERY NOTIFICATION",
34
+ "165" => "PKG FAX INBOUND RETURN NOTIFICATION",
35
+ "166" => "PKG FAX QUANTUM VIEW SHIP NOTIFICATION",
36
+ "171" => "SHIP EMAIL ERL NOTIFICATION",
37
+ "173" => "SHIP EMAIL SHIP NOTIFICATION",
38
+ "174" => "SHIP EMAIL RETURN NOTIFICATION",
39
+ "175" => "SHIP EMAIL INBOUND RETURN NOTIFICATION",
40
+ "176" => "SHIP EMAIL QUANTUM VIEW SHIP NOTIFICATION",
41
+ "177" => "SHIP EMAIL QUANTUM VIEW EXCEPTION NOTIFICATION",
42
+ "178" => "SHIP EMAIL QUANTUM VIEW DELIVERY NOTIFICATION",
43
+ "179" => "SHIP EMAIL QUANTUM VIEW NOTIFY",
44
+ "187" => "SHIP UPS ACCESS POINT NOTIFICATION",
45
+ "188" => "SHIP EEI FILING NOTIFICATION",
46
+ "189" => "SHIP UAP SHIPPER NOTIFICATION",
47
+ "190" => "EXTENDED AREA",
48
+ "200" => "DRY ICE",
49
+ "220" => "HOLD FOR PICKUP",
50
+ "240" => "ORIGIN CERTIFICATE",
51
+ "250" => "PRINT RETURN LABEL",
52
+ "258" => "EXPORT LICENSE VERIFICATION",
53
+ "260" => "PRINT N MAIL",
54
+ "270" => "RESIDENTIAL ADDRESS",
55
+ "280" => "RETURN SERVICE 1ATTEMPT",
56
+ "290" => "RETURN SERVICE 3ATTEMPT",
57
+ "300" => "SATURDAY DELIVERY",
58
+ "310" => "SATURDAY PICKUP",
59
+ "330" => "PKG VERBAL CONFIRMATION",
60
+ "350" => "ELECTRONIC RETURN LABEL",
61
+ "372" => "QUANTUM VIEW NOTIFY DELIVERY",
62
+ "374" => "UPS PREPARED SED FORM",
63
+ "375" => "FUEL SURCHARGE",
64
+ "376" => "DELIVERY AREA",
65
+ "377" => "LARGE PACKAGE",
66
+ "378" => "SHIPPER PAYS DUTY TAX",
67
+ "379" => "SHIPPER PAYS DUTY TAX UNPAID",
68
+ "400" => "INSURANCE",
69
+ "401" => "SHIP ADDITIONAL HANDLING",
70
+ "402" => "SHIPPER RELEASE",
71
+ "403" => "CHECK TO SHIPPER",
72
+ "404" => "UPS PROACTIVE RESPONSE",
73
+ "405" => "GERMAN PICKUP",
74
+ "406" => "GERMAN ROAD TAX",
75
+ "407" => "EXTENDED AREA PICKUP",
76
+ "410" => "RETURN OF DOCUMENT",
77
+ "430" => "PEAK SEASON",
78
+ "431" => "LARGE PACKAGE SEASONAL SURCHARGE",
79
+ "432" => "ADDITIONAL HANDLING SEASONAL SURCHARGE",
80
+ "440" => "SHIP LARGE PACKAGE",
81
+ "441" => "CARBON NEUTRAL",
82
+ "442" => "PKG QV IN TRANSIT NOTIFICATION",
83
+ "443" => "SHIP QV IN TRANSIT NOTIFICATION",
84
+ "444" => "IMPORT CONTROL",
85
+ "445" => "COMMERCIAL INVOICE REMOVAL",
86
+ "446" => "IMPORT CONTROL ELECTRONIC LABEL",
87
+ "447" => "IMPORT CONTROL PRINT LABEL",
88
+ "448" => "IMPORT CONTROL PRINT AND MAIL LABEL",
89
+ "449" => "IMPORT CONTROL ONE PICK UP ATTEMPT LABEL",
90
+ "450" => "IMPORT CONTROL THREE PICK UP ATTEMPT LABEL",
91
+ "452" => "REFRIGERATION",
92
+ "454" => "PAC 1A BOX1",
93
+ "455" => "PAC 3A BOX1",
94
+ "456" => "PAC 1A BOX2",
95
+ "457" => "PAC 3A BOX2",
96
+ "458" => "PAC 1A BOX3",
97
+ "459" => "PAC 3A BOX3",
98
+ "460" => "PAC 1A BOX4",
99
+ "461" => "PAC 3A BOX4",
100
+ "462" => "PAC 1A BOX5",
101
+ "463" => "PAC 3A BOX5",
102
+ "464" => "EXCHANGE PRINT RETURN LABEL",
103
+ "465" => "EXCHANGE FORWARD",
104
+ "466" => "SHIP PREALERT NOTIFICATION",
105
+ "470" => "COMMITTED DELIVERY WINDOW",
106
+ "480" => "SECURITY SURCHARGE",
107
+ "492" => "CUSTOMER TRANSACTION FEE",
108
+ "500" => "SHIPMENT COD",
109
+ "510" => "LIFT GATE FOR PICKUP",
110
+ "511" => "LIFT GATE FOR DELIVERY",
111
+ "512" => "DROP OFF AT UPS FACILITY",
112
+ "515" => "UPS PREMIUM CARE",
113
+ "520" => "OVERSIZE PALLET",
114
+ "530" => "FREIGHT DELIVERY SURCHARGE",
115
+ "531" => "FREIGHT PICKUP SURCHARGE",
116
+ "540" => "DIRECT TO RETAIL",
117
+ "541" => "DIRECT DELIVERY ONLY",
118
+ "542" => "DELIVER TO ADDRESSEE ONLY",
119
+ "543" => "DIRECT TO RETAIL COD",
120
+ "544" => "RETAIL ACCESS POINT545 SHIPPING TICKET NOTIFICATION",
121
+ "546" => "ELECTRONIC PACKAGE RELEASE AUTHENTICATION",
122
+ "547" => "PAY AT STORE"
123
+ }.freeze
124
+ private_constant :UPS_SURCHARGE_CODES
125
+ end
126
+ end
127
+ end
128
+ end