Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
failure(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_s
return 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)
end
def record_outcome
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index 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.rb
index 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 Spree
expect(subject.call(shipping_label: label)).to be_failure
end
+ # "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
+ end
end
end
diff --git a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb
index 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.message
rescue StandardError => e
report(e, shipping_label.owner)
false