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
Tell the merchant why a label purchase was refused
Damian Legawiec3cb0321f3627
diff --git a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rbindex 8e9633271c..7adb123d41 100644--- a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb+++ b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb@@ -76,6 +76,11 @@ module SpreeEasyPost# fix it. Passed through rather than reported as a connection problem.report(e, owner)raise Spree::Core::LabelPurchaseRefused, e.message+ rescue Spree::Core::LabelPurchaseRefused+ # Already the merchant's answer, raised by name from further in. It is a+ # StandardError, so without this it would be swallowed as nil below and+ # the buy would fail with no reason given.+ raiserescue StandardError => ereport(e, owner)nil@@ -214,6 +219,9 @@ 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.def end_shipper_id(integration, stock_location, store)params = SpreeEasyPost.end_shipper_params(stock_location, store)return if params.nil?@@ -221,7 +229,9 @@ module SpreeEasyPostintegration.client.end_shipper.create(**params).idrescue EasyPost::Errors::EasyPostError => ereport(e, stock_location)- nil+ raise Spree::Core::LabelPurchaseRefused,+ Spree.t('easypost.errors.origin_address_rejected',+ 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 9421e75676..eba59f13e0 100644--- a/spree/providers/easypost/config/locales/en.yml+++ b/spree/providers/easypost/config/locales/en.yml@@ -4,6 +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."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/spec/models/spree_easypost/fulfillment_provider_spec.rb b/spree/providers/easypost/spec/models/spree_easypost/fulfillment_provider_spec.rbindex 66f1f91ea4..07dbaf291d 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@@ -118,10 +118,14 @@ RSpec.describe SpreeEasyPost::FulfillmentProvider doexpect(provider.purchase_label(fulfillment).tracking_number).to eq('9405500207552012345678')end- it 'buys nothing when the exact service is not offered' do+ # Silently buying a different service than the customer paid for is+ # worse than no label, so the refusal names the service rather than+ # answering nil with no reason given.+ it 'refuses by name when the exact service is not offered' doallow(fresh_rate).to receive(:service).and_return('Express')- expect(provider.purchase_label(fulfillment)).to be_nil+ expect { provider.purchase_label(fulfillment) }.+ to raise_error(Spree::Core::LabelPurchaseRefused, /Ground/)endend@@ -152,6 +156,21 @@ RSpec.describe SpreeEasyPost::FulfillmentProvider doexpect(end_shipper_service).not_to have_received(:create)end+ # The carrier's own answer to a buy without an end shipper is a bare+ # "malformed syntax", which sends a merchant looking at the integration+ # rather than at the warehouse address that was actually refused.+ it 'names the location when the carrier will not verify its address' do+ allow(Rails.error).to receive(:report)+ fresh_rate = double(id: 'rate_fresh', carrier: 'USPS', service: 'Priority')+ allow(shipment_service).to receive(:create).and_return(double(id: 'shp_fresh', rates: [fresh_rate]))+ fulfillment.selected_delivery_rate.update_columns(carrier: 'USPS', service_level: 'Priority')+ allow(end_shipper_service).to receive(:create).+ and_raise(EasyPost::Errors::EasyPostError.new('Unable to verify address.'))++ expect { provider.purchase_label(fulfillment) }.+ to raise_error(Spree::Core::LabelPurchaseRefused, /#{Regexp.escape(fulfillment.stock_location.name)}/)+ end+it 'reports and answers nil on an API failure' doallow(shipment_service).to receive(:buy).and_raise(StandardError.new('boom'))allow(shipment_service).to receive(:create).and_raise(StandardError.new('boom'))