Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Say why a carrier refused to ship from a location
Damian Legawieca470e6b312d4
diff --git a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rbindex 7adb123d41..5277763b47 100644--- a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb+++ b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb@@ -208,9 +208,7 @@ module SpreeEasyPost# Labels bought on EasyPost's own carrier accounts (USPS) require an# EndShipper — the legally responsible shipping party. Created fresh per# purchase so it always matches the current warehouse address; label buys- # are rare enough that caching one would only buy staleness. When the- # mandatory contact fields cannot be assembled the purchase proceeds- # without one, and carriers that insist reject with their own message.+ # are rare enough that caching one would only buy staleness.def buy(integration, stock_location, store, shipment_id, rate_id)buy_params = { rate: { id: rate_id } }end_shipper_id = end_shipper_id(integration, stock_location, store)@@ -219,9 +217,15 @@ module SpreeEasyPostintegration.client.shipment.buy(shipment_id, **buy_params)end- # A rejected address is the merchant's to fix and says so by name: the- # carrier's own answer to a buy without an end shipper is a bare- # "malformed syntax", which sends them looking at the wrong thing.+ # Both ways this can fail are the merchant's to fix, so both say what to+ # fix and where. Buying without an end shipper was the alternative, and a+ # carrier that requires one answers that with a bare "malformed syntax"+ # naming no field at all.+ # Whatever the warehouse can supply is sent and EasyPost judges it: what+ # it requires here is its rule to change, and a local copy of that list+ # would quietly disagree with the real one the first time it moved. Its+ # answer names the field ("Phone number is empty"), which is what the+ # merchant needs, so it is passed through with the location that owns it.def end_shipper_id(integration, stock_location, store)params = SpreeEasyPost.end_shipper_params(stock_location, store)return if params.nil?@@ -230,8 +234,26 @@ module SpreeEasyPostrescue EasyPost::Errors::EasyPostError => ereport(e, stock_location)raise Spree::Core::LabelPurchaseRefused,- Spree.t('easypost.errors.origin_address_rejected',- location: stock_location.try(:name).presence || stock_location.try(:address1))+ Spree.t('easypost.errors.origin_address_refused',+ location: location_name(stock_location), reason: carrier_reason(e))+ end++ # EasyPost's summary line is often generic ("Missing required parameter")+ # while the field it means sits in the errors array, so the detail is+ # preferred and the summary is the fallback.+ def carrier_reason(error)+ details = Array(error.try(:errors)).filter_map do |detail|+ next detail if detail.is_a?(String)++ field = detail['field'].to_s.split('.').last+ [field.presence&.humanize, detail['message']].compact_blank.join(' ')+ end++ details.compact_blank.presence&.to_sentence || error.message+ end++ def location_name(stock_location)+ stock_location.try(:name).presence || stock_location.try(:address1)enddef label_purchase(shipment)diff --git a/spree/providers/easypost/config/locales/en.yml b/spree/providers/easypost/config/locales/en.ymlindex eba59f13e0..17ff52243c 100644--- a/spree/providers/easypost/config/locales/en.yml+++ b/spree/providers/easypost/config/locales/en.yml@@ -4,7 +4,7 @@ en:easypost:errors:no_destination: This parcel has no delivery address, so no carrier can quote it.- origin_address_rejected: "The carrier could not verify the address for %{location}. Check that this location's street address, city, state and postcode are a real, deliverable address."+ origin_address_refused: "%{location} cannot be used as the shipping origin: %{reason}"rate_not_quoted: This delivery rate was not quoted by EasyPost. Re-quote the fulfillment and pick a carrier service before buying a label.service_unavailable: "%{service} is no longer offered for this parcel. Re-quote the fulfillment and pick another service."integrations:diff --git a/spree/providers/easypost/lib/spree_easypost.rb b/spree/providers/easypost/lib/spree_easypost.rbindex 380399bc35..b2b4df647a 100644--- a/spree/providers/easypost/lib/spree_easypost.rb+++ b/spree/providers/easypost/lib/spree_easypost.rb@@ -222,24 +222,21 @@ module SpreeEasyPost# EndShipper payload — the party legally responsible for the shipment,# required when buying labels on EasyPost's own carrier accounts (USPS- # refuses the purchase without one). Unlike a plain address, EasyPost- # makes every field mandatory here including phone and email, so the- # store's contact details fill in what the stock location does not carry.+ # refuses the purchase without one). Whatever the warehouse and store can+ # supply is sent as-is: which fields EasyPost demands is its rule, and it+ # answers a payload it cannot accept by naming the field, which is more+ # use to a merchant than a list maintained here that would drift out of+ # step the first time that rule changed.## @param stock_location [Spree::StockLocation, nil]# @param store [Spree::Store, nil]- # @return [Hash, nil] nil when the mandatory fields cannot be assembled+ # @return [Hash, nil] nil only when there is no location to describedef self.end_shipper_params(stock_location, store)return if stock_location.nil?- params = address_params(stock_location)- params[:phone] = stock_location.phone.presence || store&.contact_phone.presence- params[:email] = store&.mail_from_address.presence || store&.customer_support_email.presence-- mandatory = params.values_at(:street1, :city, :state, :zip, :country, :phone, :email)- mandatory << (params[:name].presence || params[:company].presence)- return if mandatory.any?(&:blank?)-- params.compact_blank+ address_params(stock_location).merge(+ phone: stock_location.phone.presence || store&.contact_phone.presence,+ email: store&.mail_from_address.presence || store&.customer_support_email.presence+ ).compact_blankendenddiff --git a/spree/providers/easypost/spec/models/spree_easypost/fulfillment_provider_spec.rb b/spree/providers/easypost/spec/models/spree_easypost/fulfillment_provider_spec.rbindex 07dbaf291d..5e50093379 100644--- a/spree/providers/easypost/spec/models/spree_easypost/fulfillment_provider_spec.rb+++ b/spree/providers/easypost/spec/models/spree_easypost/fulfillment_provider_spec.rb@@ -146,14 +146,20 @@ RSpec.describe SpreeEasyPost::FulfillmentProvider do)end- it 'buys without an end shipper when the contact details cannot be assembled' do+ # Which fields EasyPost demands is EasyPost's rule, so an incomplete+ # warehouse is still sent and its answer — which names the field — is+ # what the merchant is shown.+ it 'passes the carrier reason through when the warehouse is incomplete' dofulfillment.stock_location.update_columns(phone: nil)store.update_columns(contact_phone: nil)- allow(shipment_service).to receive(:buy).- with('shp_recorded1', rate: { id: 'rate_recorded1' }).and_return(purchased_shipment)+ allow(Rails.error).to receive(:report)+ allow(end_shipper_service).to receive(:create).and_raise(+ EasyPost::Errors::EasyPostError.new('PHONENUMBER.EMPTY: Phone number is empty.')+ )- expect(provider.purchase_label(fulfillment)).to be_present- expect(end_shipper_service).not_to have_received(:create)+ expect { provider.purchase_label(fulfillment) }.+ to raise_error(Spree::Core::LabelPurchaseRefused, /Phone number is empty/)+ expect(end_shipper_service).to have_received(:create)end# The carrier's own answer to a buy without an end shipper is a bare