Class: FriendlyShipping::Services::UpsJson::GenerateLabelsPayload
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::UpsJson::GenerateLabelsPayload
- Defined in:
- lib/friendly_shipping/services/ups_json/generate_labels_payload.rb
Class Method Summary collapse
- .add_label_specification(payload, options) ⇒ Object
- .add_packages(payload, shipment, options) ⇒ Object
- .apply_international_forms_options(payload, shipment, options) ⇒ Object
- .apply_options(payload, options) ⇒ Object
- .build_ddp_billing_info(options) ⇒ Object
- .call(shipment:, options:) ⇒ Object
- .initialize_payload(shipment, options) ⇒ Object
- .international?(shipment) ⇒ Boolean
- .international_forms(shipment, options) ⇒ Object
-
.package_level_delivery_confirmation?(shipment) ⇒ Boolean
For certain origin/destination pairs, UPS allows each package in a shipment to have a specified delivery_confirmation option otherwise the delivery_confirmation option must be specified on the entire shipment.
Class Method Details
.add_label_specification(payload, options) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 188 def add_label_specification(payload, ) payload[:ShipmentRequest][:LabelSpecification] = { LabelImageFormat: { Code: .label_format }, LabelStockSize: { Width: .label_size[0].to_s, # "Valid value is 4." Height: .label_size[1].to_s # "Only valid values are 6 or 8." } } end |
.add_packages(payload, shipment, options) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 172 def add_packages(payload, shipment, ) payload[:ShipmentRequest][:Shipment][:Package] = shipment.packages.map do |package| = .(package) delivery_confirmation_code = package_level_delivery_confirmation?(shipment) ? .delivery_confirmation_code : nil GeneratePackageHash.call( package: package, delivery_confirmation_code: delivery_confirmation_code, shipper_release: .shipper_release, declared_value: .declared_value, package_flavor: 'labels', reference_numbers: .reference_numbers ) end end |
.apply_international_forms_options(payload, shipment, options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 72 def (payload, shipment, ) if .terms_of_shipment_code == "DDP" payload[:ShipmentRequest][:Shipment][:PaymentInformation][:ShipmentCharge].append(build_ddp_billing_info()) end if .paperless_invoice payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms] = international_forms(shipment, ) end sold_to_location = .sold_to || shipment.destination international_forms_additions = {} international_forms_additions[:Contacts] = { SoldTo: GenerateAddressHash.call(location: sold_to_location, international: true) } international_forms_additions[:Contacts][:SoldTo].merge( { Phone: { Number: sold_to_location.phone }, TaxIdentificationNumber: sold_to_location.try(:tax_id_number), EmailAddress: sold_to_location.email } ).compact payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms].merge!(international_forms_additions) return unless shipment.origin.country.code == "US" && ["CA", "PR"].include?(shipment.destination.country.code) # Required for shipments from the US to Canada or Puerto Rico # We'll assume USD as the origin country is the United States here. total_value = shipment.packages.inject(Money.new(0, "USD")) do |shipment_sum, package| shipment_sum + package.items.inject(Money.new(0, "USD")) do |package_sum, item| package_sum + (item.cost || Money.new(0, "USD")) end end payload[:ShipmentRequest][:Shipment][:InvoiceLineTotal] = { CurrencyCode: total_value.currency.iso_code, MonetaryValue: total_value.to_f.to_s } end |
.apply_options(payload, options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 54 def (payload, ) if .customer_context.present? payload[:ShipmentRequest][:Request][:TransactionReference] = { CustomerContext: .customer_context } end if .negotiated_rates payload[:ShipmentRequest][:Shipment][:ShipmentRatingOptions] = { NegotiatedRatesIndicator: "X" } end payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions] = { UPScarbonneutralIndicator: .carbon_neutral ? "X" : nil, SaturdayDeliveryIndicator: .saturday_delivery ? "X" : nil, LabelDelivery: { LabelLinksIndicator: "X" } }.compact end |
.build_ddp_billing_info(options) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 113 def build_ddp_billing_info() = . { Type: "02", # Duties and taxes BillThirdParty: { AccountNumber: .billing_account, Address: { PostalCode: .billing_zip, CountryCode: .billing_country } } } end |
.call(shipment:, options:) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 11 def call(shipment:, options:) payload = initialize_payload(shipment, ) (payload, ) (payload, shipment, ) if international?(shipment) add_packages(payload, shipment, ) add_label_specification(payload, ) payload.compact end |
.initialize_payload(shipment, options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 20 def initialize_payload(shipment, ) contents_description = shipment.packages.flat_map do |package| package.items.map(&:description) end.compact.uniq.join(', ').slice(0, 50) { ShipmentRequest: { Request: { RequestOption: .validate_address ? "validate" : "nonvalidate", SubVersion: .sub_version }, Shipment: { Description: contents_description, Service: { Code: .shipping_method.service_code }, Shipper: GenerateAddressHash.call(location: .shipper || shipment.origin, international: international?(shipment), shipper_number: .shipper_number), ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)), ShipmentDate: Time.current.strftime("%Y%m%d"), PaymentInformation: { ShipmentCharge: [ { Type: "01", # Transportation BillShipper: { AccountNumber: .shipper_number } } ] } } } } end |
.international?(shipment) ⇒ Boolean
109 110 111 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 109 def international?(shipment) shipment.origin.country != shipment.destination.country end |
.international_forms(shipment, options) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 127 def international_forms(shipment, ) invoice_date = .invoice_date || Date.current result = { FormType: "01", # 01 is "Invoice" InvoiceDate: invoice_date.strftime("%Y%m%d"), ReasonForExport: .reason_for_export, CurrencyCode: ..currency || "USD", DeclarationStatement: .declaration_statement, TermsOfShipment: .terms_of_shipment_code, Product: [] }.compact all_items = shipment.packages.map(&:items).map(&:to_a).flatten = shipment.packages.flat_map do |package| = .(package) package.items.flat_map do |item| .(item) end end all_items.group_by(&:description).each do |description, items| # This is a group of identically described items reference_item = items.first cost = reference_item.cost || Money.new(0, "USD") # Get the options for this item = .detect { |o| o.item_id == reference_item.id } || LabelItemOptions.new(item_id: nil) product_hash = { 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. CommodityCode: .commodity_code, OriginCountryCode: .country_of_origin || shipment.origin.country.code, Unit: { Number: items.length.to_s, UnitOfMeasurement: { Code: .product_unit_of_measure_code }, Value: cost.to_d } } result[:Product] << product_hash end result end |
.package_level_delivery_confirmation?(shipment) ⇒ Boolean
For certain origin/destination pairs, UPS allows each package in a shipment to have a specified delivery_confirmation option otherwise the delivery_confirmation option must be specified on the entire shipment. See https://developer.ups.com/api/reference/rating/appendix?loc=en_US for details
203 204 205 206 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 203 def package_level_delivery_confirmation?(shipment) origin, destination = shipment.origin.country.code, shipment.destination.country.code origin == destination || [['US', 'PR'], ['PR', 'US']].include?([origin, destination]) end |