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
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 185 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 169 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
69 70 71 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 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 69 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 51 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
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 110 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
8 9 10 11 12 13 14 15 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 8 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
17 18 19 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 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 17 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
106 107 108 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 106 def international?(shipment) shipment.origin.country != shipment.destination.country end |
.international_forms(shipment, options) ⇒ Object
124 125 126 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 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 124 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
200 201 202 203 |
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 200 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 |