Class: Spree::ShippingLabel

Inherits:
Object
  • Object
show all
Includes:
HasStatus, HasTrackingCarrier, Metadata, SingleStoreResource
Defined in:
app/models/spree/shipping_label.rb

Overview

A carrier document the merchant bought through a connected carrier account or uploaded after buying postage elsewhere (docs/plans/6.0-shipping-labels-and-deliveries.md).

Owned polymorphically: a Spree::Fulfillment for an outbound parcel, a Spree::Return for the inbound one. What the merchant paid the carrier is recorded here as accounting data — it never feeds the order's shipping charge and never reaches a store serializer.

Every status change is a workflow (+Spree::ShippingLabels::Purchase+, ::Refund, ::Record); the model holds associations, validations and reads only.

Constant Summary collapse

SOURCES =
%w[purchased uploaded].freeze
FORMATS =
%w[pdf png zpl].freeze
OWNER_TYPES =
%w[Spree::Fulfillment Spree::Return].freeze
MAX_FILE_SIZE =
10.megabytes
FILE_CONTENT_TYPES =

ZPL is plain text as far as any byte-sniffer can tell.

%w[application/pdf image/png text/plain].freeze

Instance Method Summary collapse

Methods included from Metadata

#private_metadata, #private_metadata=

Methods included from HasTrackingCarrier

#carrier_name

Instance Method Details

#display_cost ⇒ Spree::Money

Returns:



130
131
132
# File 'app/models/spree/shipping_label.rb', line 130

def display_cost
  Spree::Money.new(cost, currency: currency.presence || store.default_currency)
end

#download_filename ⇒ String

Returns the filename a download is served under.

Returns:

  • (String) —

    the filename a download is served under



135
136
137
138
139
# File 'app/models/spree/shipping_label.rb', line 135

def download_filename
  return file.filename.to_s if file.attached?

  "#{tracking_number.presence || prefixed_id}.#{format.presence || 'pdf'}"
end

#event_serializer_class ⇒ Object

Custom events carry the label rather than the minimal payload; the store never sees a label, so there is no storefront serializer to find by convention.



144
145
146
# File 'app/models/spree/shipping_label.rb', line 144

def event_serializer_class
  'Spree::Api::V3::ShippingLabelEventSerializer'.safe_constantize
end

#file_pending? ⇒ Boolean

Returns whether the file is still to be fetched from the provider's URL.

Returns:

  • (Boolean) —

    whether the file is still to be fetched from the provider's URL



106
107
108
# File 'app/models/spree/shipping_label.rb', line 106

def file_pending?
  !file.attached? && file_url.present?
end

#file_url ⇒ String?

The provider's hosted copy of the label, kept only until the file has been fetched into Spree's own storage.

Answered only for an https URL: the value is whatever the provider wrote, and it is followed by a browser on a print click and fetched server-side by Spree::ShippingLabels::StoreFile, so a javascript: or file: value would be an open redirect wearing a label's clothes.

Returns:

  • (String, nil)


119
120
121
122
123
124
125
126
127
# File 'app/models/spree/shipping_label.rb', line 119

def file_url
  value = ['file_url'].to_s
  return if value.blank?

  uri = URI.parse(value)
  value if uri.is_a?(URI::HTTPS) && uri.host.present?
rescue URI::InvalidURIError
  nil
end

#provider ⇒ Spree::FulfillmentProvider::Base

The provider that sold this label, which is the only one that can refund it. Resolved from the connected account it was bought through rather than from the parcel's current delivery method: a merchant who reroutes a parcel after buying postage still has to give that postage back to the carrier who sold it.



77
78
79
80
81
82
83
# File 'app/models/spree/shipping_label.rb', line 77

def provider
  klass = integration && Spree.fulfillment_providers.find do |candidate|
    candidate.integration_class.to_s == integration.type
  end

  klass ? klass.new : owner.provider
end

#refundable? ⇒ Boolean

Returns whether a refund can still be asked of the carrier. A label the carrier is still deciding on counts: some settle refunds asynchronously and a re-ask is how a stuck one is re-driven.

Returns:

  • (Boolean) —

    whether a refund can still be asked of the carrier. A label the carrier is still deciding on counts: some settle refunds asynchronously and a re-ask is how a stuck one is re-driven.



88
89
90
# File 'app/models/spree/shipping_label.rb', line 88

def refundable?
  !uploaded? && !refunded?
end

#release_unmoved_delivery ⇒ void

This method returns an undefined value.

Drops the consignment this label minted, when the parcel never moved. Postage voided before anything travelled leaves nothing behind; once it has travelled the journey is history and stays, label or no label.



97
98
99
100
101
102
# File 'app/models/spree/shipping_label.rb', line 97

def release_unmoved_delivery
  return if delivery.nil?
  return if owner.is_a?(Spree::Fulfillment) ? owner.fulfilled_or_delivered? : owner.received?

  delivery.destroy!
end

#uploaded? ⇒ Boolean

Returns whether the merchant uploaded this label rather than buying it through a provider.

Returns:

  • (Boolean) —

    whether the merchant uploaded this label rather than buying it through a provider



66
67
68
# File 'app/models/spree/shipping_label.rb', line 66

def uploaded?
  source == 'uploaded'
end