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 the carrier refused to void a label
Damian Legawiecab49c0114ef2
diff --git a/spree/core/app/workflows/spree/shipping_labels/refund.rb b/spree/core/app/workflows/spree/shipping_labels/refund.rbindex 126f70c35a..3a21af65c7 100644--- a/spree/core/app/workflows/spree/shipping_labels/refund.rb+++ b/spree/core/app/workflows/spree/shipping_labels/refund.rb@@ -45,12 +45,16 @@ module Spreefailure(shipping_label, Spree.t("shipping_labels.errors.#{reason}"))end+ # A provider that knows why the carrier said no raises with its words;+ # anything else falls back to the generic line.def refund@outcome = shipping_label.provider.refund_label(shipping_label).to_sreturn if %w[refunded refund_requested].include?(@outcome)failure(shipping_label, Spree.t('shipping_labels.errors.refund_failed'))+ rescue Spree::Core::LabelRefundRefused => e+ failure(shipping_label, e.message)enddef record_outcomediff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 669e671d19..8f704ceac0 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -828,6 +828,11 @@ module Spree# instead of being sent to check a connection that is fine.class LabelPurchaseRefused < RuntimeError; end+ # A carrier refusing to void a label, in its own words — a parcel it has+ # already collected, a label past its void window. Raised by a provider+ # when it knows why, so the merchant reads the reason.+ class LabelRefundRefused < RuntimeError; end+# A label the carrier refused to refund while a parcel was being# cancelled. The cancellation proceeds; the postage is the merchant's to# chase, so it is reported rather than dropped.diff --git a/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rb b/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rbindex 2352c52490..ee8ddd5826 100644--- a/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rb+++ b/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rb@@ -76,5 +76,17 @@ module Spreeexpect(subject.call(shipping_label: label)).to be_failureend+ # "The carrier refused" is not useful when the carrier said exactly why.+ it 'reports the carrier own reason when the provider knows it' do+ allow(label).to receive(:provider).and_return(provider)+ allow(provider).to receive(:refund_label).+ and_raise(Spree::Core::LabelRefundRefused, 'Unable to request refund. The parcel has been shipped.')++ result = subject.call(shipping_label: label)++ expect(result).to be_failure+ expect(result.error.to_s).to eq('Unable to request refund. The parcel has been shipped.')+ expect(label.reload).to be_purchased+ endendenddiff --git a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rbindex 70960bc872..8e9633271c 100644--- a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb+++ b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb@@ -87,11 +87,19 @@ module SpreeEasyPost# @param shipping_label [Spree::ShippingLabel]# @return [String, false]def refund_label(shipping_label)- integration = integration_for(shipping_label.owner)+ # The account that sold the label, not whichever one the parcel's+ # current method points at — a rerouted parcel still owes its postage+ # back to the carrier it was bought from.+ integration = shipping_label.integration || integration_for(shipping_label.owner)return false if integration.nil? || shipping_label.external_id.blank?shipment = integration.client.shipment.refund(shipping_label.external_id)refund_outcome(shipment.try(:refund_status))+ rescue EasyPost::Errors::EasyPostError => e+ # A parcel already in the carrier's hands, a label too old to void:+ # EasyPost says which, and only the merchant can act on it.+ report(e, shipping_label.owner)+ raise Spree::Core::LabelRefundRefused, e.messagerescue StandardError => ereport(e, shipping_label.owner)false