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
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/ups_json/generate_address_hash'
4
+ require 'friendly_shipping/services/ups_json/generate_package_hash'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class UpsJson
9
+ class GenerateLabelsPayload
10
+ class << self
11
+ def call(shipment:, options:)
12
+ payload = initialize_payload(shipment, options)
13
+ apply_options(payload, options)
14
+ apply_international_forms_options(payload, shipment, options) if international?(shipment)
15
+ add_packages(payload, shipment, options)
16
+ add_label_specification(payload, options)
17
+ payload.compact
18
+ end
19
+
20
+ def initialize_payload(shipment, options)
21
+ contents_description = shipment.packages.flat_map do |package|
22
+ package.items.map(&:description)
23
+ end.compact.uniq.join(', ').slice(0, 50)
24
+
25
+ {
26
+ ShipmentRequest: {
27
+ Request: {
28
+ RequestOption: options.validate_address ? "validate" : "nonvalidate",
29
+ SubVersion: options.sub_version
30
+ },
31
+ Shipment: {
32
+ Description: contents_description,
33
+ Service: {
34
+ Code: options.shipping_method.service_code
35
+ },
36
+ Shipper: GenerateAddressHash.call(location: options.shipper || shipment.origin, international: international?(shipment), shipper_number: options.shipper_number),
37
+ ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)),
38
+ ShipmentDate: Time.current.strftime("%Y%m%d"),
39
+ PaymentInformation: {
40
+ ShipmentCharge: [
41
+ {
42
+ Type: "01", # Transportation
43
+ BillShipper: {
44
+ AccountNumber: options.shipper_number
45
+ }
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ }
51
+ }
52
+ end
53
+
54
+ def apply_options(payload, options)
55
+ if options.customer_context.present?
56
+ payload[:ShipmentRequest][:Request][:TransactionReference] = { CustomerContext: options.customer_context }
57
+ end
58
+
59
+ if options.negotiated_rates
60
+ payload[:ShipmentRequest][:Shipment][:ShipmentRatingOptions] = { NegotiatedRatesIndicator: "X" }
61
+ end
62
+
63
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions] = {
64
+ UPScarbonneutralIndicator: options.carbon_neutral ? "X" : nil,
65
+ SaturdayDeliveryIndicator: options.saturday_delivery ? "X" : nil,
66
+ LabelDelivery: {
67
+ LabelLinksIndicator: "X"
68
+ }
69
+ }.compact
70
+ end
71
+
72
+ def apply_international_forms_options(payload, shipment, options)
73
+ if options.terms_of_shipment_code == "DDP"
74
+ payload[:ShipmentRequest][:Shipment][:PaymentInformation][:ShipmentCharge].append(build_ddp_billing_info(options))
75
+ end
76
+
77
+ if options.paperless_invoice
78
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms] = international_forms(shipment, options)
79
+ end
80
+
81
+ sold_to_location = options.sold_to || shipment.destination
82
+
83
+ international_forms_additions = {}
84
+ international_forms_additions[:Contacts] = { SoldTo: GenerateAddressHash.call(location: sold_to_location, international: true) }
85
+ international_forms_additions[:Contacts][:SoldTo].merge(
86
+ {
87
+ Phone: { Number: sold_to_location.phone },
88
+ TaxIdentificationNumber: sold_to_location.try(:tax_id_number),
89
+ EmailAddress: sold_to_location.email
90
+ }
91
+ ).compact
92
+ payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms].merge!(international_forms_additions)
93
+
94
+ return unless shipment.origin.country.code == "US" && ["CA", "PR"].include?(shipment.destination.country.code)
95
+
96
+ # Required for shipments from the US to Canada or Puerto Rico
97
+ # We'll assume USD as the origin country is the United States here.
98
+ total_value = shipment.packages.inject(Money.new(0, "USD")) do |shipment_sum, package|
99
+ shipment_sum + package.items.inject(Money.new(0, "USD")) do |package_sum, item|
100
+ package_sum + (item.cost || Money.new(0, "USD"))
101
+ end
102
+ end
103
+ payload[:ShipmentRequest][:Shipment][:InvoiceLineTotal] = {
104
+ CurrencyCode: total_value.currency.iso_code,
105
+ MonetaryValue: total_value.to_f.to_s
106
+ }
107
+ end
108
+
109
+ def international?(shipment)
110
+ shipment.origin.country != shipment.destination.country
111
+ end
112
+
113
+ def build_ddp_billing_info(options)
114
+ billing_options = options.billing_options
115
+ {
116
+ Type: "02", # Duties and taxes
117
+ BillThirdParty: {
118
+ AccountNumber: billing_options.billing_account,
119
+ Address: {
120
+ PostalCode: billing_options.billing_zip,
121
+ CountryCode: billing_options.billing_country
122
+ }
123
+ }
124
+ }
125
+ end
126
+
127
+ def international_forms(shipment, options)
128
+ invoice_date = options.invoice_date || Date.current
129
+ result = {
130
+ FormType: "01", # 01 is "Invoice"
131
+ InvoiceDate: invoice_date.strftime("%Y%m%d"),
132
+ ReasonForExport: options.reason_for_export,
133
+ CurrencyCode: options.billing_options.currency || "USD",
134
+ DeclarationStatement: options.declaration_statement,
135
+ TermsOfShipment: options.terms_of_shipment_code,
136
+ Product: []
137
+ }.compact
138
+
139
+ all_items = shipment.packages.map(&:items).map(&:to_a).flatten
140
+ all_item_options = shipment.packages.flat_map do |package|
141
+ package_options = options.options_for_package(package)
142
+ package.items.flat_map do |item|
143
+ package_options.options_for_item(item)
144
+ end
145
+ end
146
+
147
+ all_items.group_by(&:description).each do |description, items|
148
+ # This is a group of identically described items
149
+ reference_item = items.first
150
+ cost = reference_item.cost || Money.new(0, "USD")
151
+ # Get the options for this item
152
+ item_options = all_item_options.detect { |o| o.item_id == reference_item.id } || LabelItemOptions.new(item_id: nil)
153
+
154
+ product_hash = {
155
+ Description: description&.slice(0, 35), # Description of the product. Applies to all International Forms. Optional for Partial Invoice. Must be present at least once and can occur for a maximum of 3 times.
156
+ CommodityCode: item_options.commodity_code,
157
+ OriginCountryCode: item_options.country_of_origin || shipment.origin.country.code,
158
+ Unit: {
159
+ Number: items.length.to_s,
160
+ UnitOfMeasurement: {
161
+ Code: item_options.product_unit_of_measure_code
162
+ },
163
+ Value: cost.to_d
164
+ }
165
+ }
166
+
167
+ result[:Product] << product_hash
168
+ end
169
+ result
170
+ end
171
+
172
+ def add_packages(payload, shipment, options)
173
+ payload[:ShipmentRequest][:Shipment][:Package] = shipment.packages.map do |package|
174
+ package_options = options.options_for_package(package)
175
+ delivery_confirmation_code = package_level_delivery_confirmation?(shipment) ? package_options.delivery_confirmation_code : nil
176
+
177
+ GeneratePackageHash.call(
178
+ package: package,
179
+ delivery_confirmation_code: delivery_confirmation_code,
180
+ shipper_release: package_options.shipper_release,
181
+ declared_value: package_options.declared_value,
182
+ package_flavor: 'labels',
183
+ reference_numbers: package_options.reference_numbers
184
+ )
185
+ end
186
+ end
187
+
188
+ def add_label_specification(payload, options)
189
+ payload[:ShipmentRequest][:LabelSpecification] = {
190
+ LabelImageFormat: {
191
+ Code: options.label_format
192
+ },
193
+ LabelStockSize: {
194
+ Width: options.label_size[0].to_s, # "Valid value is 4."
195
+ Height: options.label_size[1].to_s # "Only valid values are 6 or 8."
196
+ }
197
+ }
198
+ end
199
+
200
+ # For certain origin/destination pairs, UPS allows each package in a shipment to have a specified
201
+ # delivery_confirmation option otherwise the delivery_confirmation option must be specified on the entire shipment.
202
+ # See https://developer.ups.com/api/reference/rating/appendix?loc=en_US for details
203
+ def package_level_delivery_confirmation?(shipment)
204
+ origin, destination = shipment.origin.country.code, shipment.destination.country.code
205
+ origin == destination || [['US', 'PR'], ['PR', 'US']].include?([origin, destination])
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GeneratePackageHash
7
+ class << self
8
+ def call(package:,
9
+ delivery_confirmation_code: nil,
10
+ shipper_release: false,
11
+ transmit_dimensions: true,
12
+ declared_value: false,
13
+ package_flavor: nil,
14
+ reference_numbers: nil)
15
+ # UPS consistency across apis is a bit of a mess
16
+ packaging_type_key = package_flavor == "rates" ? :PackagingType : :Packaging
17
+
18
+ package_hash = {
19
+ packaging_type_key => {
20
+ Code: "02"
21
+ },
22
+ PackageWeight: {
23
+ UnitOfMeasurement: {
24
+ Code: "LBS"
25
+ },
26
+ Weight: [package.weight.convert_to(:pounds).value.to_f.round(2), 1].max.to_s
27
+ },
28
+ PackageServiceOptions: {},
29
+ Description: package.items.map(&:description).compact.uniq.join(', ').slice(0, 35) # required for return labels
30
+ }
31
+
32
+ if transmit_dimensions && package.dimensions.all? { |dim| !dim.value.zero? && !dim.value.infinite? }
33
+ package_hash[:Dimensions] = {
34
+ UnitOfMeasurement: {
35
+ Code: "IN"
36
+ },
37
+ Length: package.length.convert_to(:inches).value.to_f.round(2).to_s,
38
+ Width: package.width.convert_to(:inches).value.to_f.round(2).to_s,
39
+ Height: package.height.convert_to(:inches).value.to_f.round(2).to_s
40
+ }
41
+ end
42
+
43
+ package_hash[:PackageServiceOptions][:ShipperReleaseIndicator] = true if shipper_release
44
+ if delivery_confirmation_code
45
+ package_hash[:PackageServiceOptions][:DeliveryConfirmation] = { DCISType: delivery_confirmation_code }
46
+ end
47
+
48
+ total_value = package.items.inject(Money.new(0, "USD")) do |package_sum, item|
49
+ package_sum + (item.cost || Money.new(0, "USD"))
50
+ end
51
+
52
+ # this key is invalid for labels, UPS responds with "Accessory may not be combined with the product."
53
+ if package_flavor == "rates" && declared_value
54
+ package_hash[:PackageServiceOptions][:DeclaredValue] = {
55
+ CurrencyCode: total_value.currency.iso_code,
56
+ MonetaryValue: total_value.to_s
57
+ }
58
+ end
59
+
60
+ if reference_numbers.present?
61
+ package_hash[:ReferenceNumber] = reference_numbers.map do |code, value|
62
+ {
63
+ Code: code.to_s,
64
+ Value: value.to_s
65
+ }
66
+ end
67
+ end
68
+
69
+ package_hash[:PackageServiceOptions].compact_blank!
70
+ package_hash.compact_blank!
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/services/ups_json/generate_address_hash'
4
+ require 'friendly_shipping/services/ups_json/generate_package_hash'
5
+
6
+ module FriendlyShipping
7
+ module Services
8
+ class UpsJson
9
+ class GenerateRatesPayload
10
+ def self.call(shipment:, options:)
11
+ payload =
12
+ {
13
+ RateRequest: {
14
+ PickupType: {
15
+ Code: options.pickup_type_code
16
+ },
17
+ CustomerClassification: {
18
+ Code: options.customer_classification_code
19
+ },
20
+ Shipment: {
21
+ Shipper: GenerateAddressHash.call(location: options.shipper || shipment.origin, international: international?(shipment), shipper_number: options.shipper_number),
22
+ ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)),
23
+ ShipFrom: GenerateAddressHash.call(location: shipment.origin),
24
+ PaymentDetails: {
25
+ ShipmentCharge: [
26
+ {
27
+ Type: "01", # Transportation
28
+ BillShipper: {
29
+ AccountNumber: options.shipper_number
30
+ }
31
+ }
32
+ ]
33
+ },
34
+ NumOfPieces: shipment.packages.count
35
+ }
36
+ }
37
+ }
38
+
39
+ if options.customer_context.present?
40
+ payload[:RateRequest][:Request] = { TransactionReference: { CustomerContext: options.customer_context } }
41
+ end
42
+
43
+ payload[:RateRequest][:Shipment][:Package] = shipment.packages.map do |package|
44
+ package_options = options.options_for_package(package)
45
+ transmit_dimensions = package_options.transmit_dimensions || false
46
+ GeneratePackageHash.call(
47
+ package: package,
48
+ package_flavor: 'rates',
49
+ transmit_dimensions: transmit_dimensions
50
+ )
51
+ end
52
+
53
+ if options.pickup_date && options.sub_version.to_i >= 2205
54
+ payload[:RateRequest][:Shipment][:DeliveryTimeInformation] = {
55
+ PackageBillType: "03", # Non-document
56
+ Pickup: {
57
+ Date: options.pickup_date.strftime('%Y%m%d'),
58
+ Time: options.pickup_date.strftime('%H%M%S')
59
+ }
60
+ }
61
+ end
62
+
63
+ if options.shipping_method
64
+ payload[:RateRequest][:Shipment][:Service] = { Code: options.shipping_method.service_code }
65
+ end
66
+
67
+ payload[:RateRequest][:Shipment][:ShipmentServiceOptions] = {
68
+ UPScarbonneutralIndicator: options.carbon_neutral,
69
+ SaturdayDelivery: options.saturday_delivery,
70
+ SaturdayPickup: options.saturday_pickup
71
+ }.compact
72
+
73
+ if options.negotiated_rates
74
+ payload[:RateRequest][:Shipment][:NegotiatedRatesIndicator] = options.negotiated_rates
75
+ end
76
+
77
+ payload
78
+ end
79
+
80
+ def self.international?(shipment)
81
+ shipment.origin.country != shipment.destination.country
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class GenerateTimingsPayload
7
+ def self.call(shipment:, options:)
8
+ {
9
+ originCountryCode: shipment.origin.country.code,
10
+ originStateProvince: region_name_or_code(shipment.origin, shipment.origin.region),
11
+ originCityName: shipment.origin.city,
12
+ originPostalCode: shipment.origin.zip,
13
+ destinationCountryCode: shipment.destination.country.code,
14
+ destinationStateProvince: region_name_or_code(shipment.destination, shipment.destination.region),
15
+ destinationCityName: shipment.destination.city,
16
+ destinationPostalCode: shipment.destination.zip,
17
+ residentialIndicator: shipment.destination.commercial? ? '02' : '01',
18
+ shipDate: options.pickup.strftime('%Y-%m-%d'),
19
+ shipmentContentsCurrencyCode: 'USD',
20
+ shipmentContentsValue: shipment_contents_value(shipment),
21
+ weight: shipment.packages.sum(&:weight).value.to_s,
22
+ weightUnitOfMeasure: 'LBS',
23
+ billType: '02',
24
+ numberOfPackages: shipment.packages.size.to_s
25
+ }
26
+ end
27
+
28
+ def self.shipment_contents_value(shipment)
29
+ shipment.packages.map do |package|
30
+ package.items.sum { |item| item.cost || 0 }
31
+ end.sum.to_d
32
+ end
33
+
34
+ def self.region_name_or_code(location, region)
35
+ if location.country == "US"
36
+ region.name
37
+ else
38
+ region.code
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyShipping
4
+ module Services
5
+ class UpsJson
6
+ class Label < FriendlyShipping::Label
7
+ attr_reader :usps_tracking_number
8
+
9
+ # @param [String] usps_tracking_number The label's usps tracking number. Limited to SUREPOST
10
+ def initialize(
11
+ usps_tracking_number: nil,
12
+ **kwargs
13
+ )
14
+ @usps_tracking_number = usps_tracking_number
15
+ super(**kwargs)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -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,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'friendly_shipping/item_options'
4
+
5
+ module FriendlyShipping
6
+ module Services
7
+ class UpsJson
8
+ class LabelItemOptions < FriendlyShipping::ItemOptions
9
+ PRODUCT_UNIT_OF_MEASURE_CODES = {
10
+ barrel: 'BA',
11
+ bundle: 'BE',
12
+ bag: 'BG',
13
+ bunch: 'BH',
14
+ box: 'BOX',
15
+ bolt: 'BT',
16
+ butt: 'BU',
17
+ canister: 'CI',
18
+ centimeter: 'CM',
19
+ container: 'CON',
20
+ crate: 'CR',
21
+ case: 'CS',
22
+ carton: 'CT',
23
+ cylinder: 'CY',
24
+ dozen: 'DOZ',
25
+ each: 'EA',
26
+ envelope: 'EN',
27
+ feet: 'FT',
28
+ kilogram: 'KG',
29
+ kilograms: 'KGS',
30
+ pound: 'LB',
31
+ pounds: 'LBS',
32
+ liter: 'L',
33
+ meter: 'M',
34
+ number: 'NMB',
35
+ packet: 'PA',
36
+ pallet: 'PAL',
37
+ piece: 'PC',
38
+ pieces: 'PCS',
39
+ proof_liters: 'PF',
40
+ package: 'PKG',
41
+ pair: 'PR',
42
+ pairs: 'PRS',
43
+ roll: 'RL',
44
+ set: 'SET',
45
+ square_meters: 'SME',
46
+ square_yards: 'SYD',
47
+ tube: 'TU',
48
+ yard: 'YD',
49
+ other: 'OTH'
50
+ }.freeze
51
+
52
+ attr_reader :commodity_code,
53
+ :country_of_origin
54
+
55
+ def initialize(
56
+ commodity_code: nil,
57
+ country_of_origin: nil,
58
+ product_unit_of_measure: :number,
59
+ **kwargs
60
+ )
61
+ @commodity_code = commodity_code
62
+ @country_of_origin = country_of_origin
63
+ @product_unit_of_measure = product_unit_of_measure
64
+ super(**kwargs)
65
+ end
66
+
67
+ def product_unit_of_measure_code
68
+ PRODUCT_UNIT_OF_MEASURE_CODES[product_unit_of_measure]
69
+ end
70
+
71
+ private
72
+
73
+ attr_reader :product_unit_of_measure
74
+ end
75
+ end
76
+ end
77
+ end