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.

Give back the postage when a return is abandoned

Damian Legawiecb3464eeece41
diff --git a/packages/dashboard/src/components/spree/orders/shipping-label-row.tsx b/packages/dashboard/src/components/spree/orders/shipping-label-row.tsx
index ddf35be288..0f0e93dc10 100644
--- a/packages/dashboard/src/components/spree/orders/shipping-label-row.tsx
+++ b/packages/dashboard/src/components/spree/orders/shipping-label-row.tsx
@@ -37,7 +37,15 @@ export function ShippingLabelRow({
const confirm = useConfirm()
const { token } = useAuth()
- const refundable = label.source === 'purchased' && label.status === 'purchased'
+ // Mirrors Spree::ShippingLabel#refundable? — a label the carrier is still
+ // deciding on stays askable, because re-filing is the only way to re-drive a
+ // refund whose confirmation never arrived.
+ const refundable = label.source === 'purchased' && label.status !== 'refunded'
+ // A refund already filed and not yet settled: the same action re-files it.
+ const refundPending = label.status === 'refund_requested'
+ const refundLabelKey = refundPending
+ ? 'admin.orders.detail.fulfillments.retry_refund_label'
+ : 'admin.orders.detail.fulfillments.refund_label'
const deletable = label.source === 'uploaded'
async function print() {
@@ -95,14 +103,14 @@ export function ShippingLabelRow({
await confirm({
message: t('admin.orders.detail.fulfillments.confirm_refund_label'),
variant: 'destructive',
- confirmLabel: t('admin.orders.detail.fulfillments.refund_label'),
+ confirmLabel: t(refundLabelKey),
})
) {
onRefund()
}
}}
>
- {t('admin.orders.detail.fulfillments.refund_label')}
+ {t(refundLabelKey)}
</Button>
)}
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index c93a6d3b99..56ffb659ef 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -2780,6 +2780,7 @@
"print_packing_slip": "طباعة قائمة التعبئة",
"rates_pending_origin": "احفظ مصدر الشحن الجديد لعرض طرق التوصيل المتاحة منه.",
"refund_label": "استرداد الملصق",
+ "retry_refund_label": "إعادة محاولة الاسترداد",
"ship_to": "الشحن إلى",
"ships_from": "الشحن من",
"split": "تقسيم",
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 10c7cabe81..826ce06c98 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -2762,6 +2762,7 @@
"print_packing_slip": "Lieferschein drucken",
"rates_pending_origin": "Speichern Sie den neuen Ursprung, um die dort verfügbaren Versandarten zu sehen.",
"refund_label": "Etikett erstatten",
+ "retry_refund_label": "Erstattung erneut anfordern",
"ship_to": "Lieferadresse",
"ships_from": "Versand ab",
"split": "Aufteilen",
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index aa58e2edd2..648d743c03 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -2762,6 +2762,7 @@
"print_packing_slip": "Print packing slip",
"rates_pending_origin": "Save the new origin to see the delivery methods it offers.",
"refund_label": "Refund label",
+ "retry_refund_label": "Retry refund",
"ship_to": "Ship to",
"ships_from": "Ships from",
"split": "Split",
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index b5c3c7eaef..929993a6f8 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -2762,6 +2762,7 @@
"print_packing_slip": "Imprimer le bordereau de colisage",
"rates_pending_origin": "Enregistrez la nouvelle origine pour voir les modes de livraison proposés.",
"refund_label": "Rembourser l'étiquette",
+ "retry_refund_label": "Relancer le remboursement",
"ship_to": "Livrer à",
"ships_from": "Expédié depuis",
"split": "Diviser",
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index b5b8670548..929cf01245 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -2782,6 +2782,7 @@
"print_packing_slip": "Drukuj list przewozowy",
"rates_pending_origin": "Zapisz nowe miejsce wysyłki, aby zobaczyć dostępne dla niego sposoby dostawy.",
"refund_label": "Zwrot za etykietę",
+ "retry_refund_label": "Ponów zwrot kosztów",
"ship_to": "Adres dostawy",
"ships_from": "Wysyłka z",
"split": "Podziel",
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 9822da97b4..a1fa921650 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -2755,6 +2755,7 @@
"print_packing_slip": "打印装箱单",
"rates_pending_origin": "保存新的发货地后即可查看该地可用的配送方式。",
"refund_label": "退还面单",
+ "retry_refund_label": "重试退款",
"ship_to": "收货地址",
"ships_from": "发货地",
"split": "拆分",
diff --git a/spree/core/app/models/spree/delivery.rb b/spree/core/app/models/spree/delivery.rb
index 5932c22e5d..9324550c8d 100644
--- a/spree/core/app/models/spree/delivery.rb
+++ b/spree/core/app/models/spree/delivery.rb
@@ -61,9 +61,10 @@ module Spree
# What changes when a merchant corrects the tracking number: to the
# carrier a different number is a different parcel, so the journey starts
# over and everything belonging to the old number goes with it — its
- # status, its arrival, and the carrier and link that described it.
- # Otherwise a UPS badge and a UPS page survive onto a FedEx number, and a
- # stale arrival keeps dating the returns window.
+ # status, its arrival and ETA, the scans it collected, and the carrier and
+ # link that described it. Otherwise a UPS badge and a UPS page survive
+ # onto a FedEx number, a stale arrival keeps dating the returns window,
+ # and the new parcel shows the old one's scan history.
#
# @param attributes [Hash] the merchant's edit
# @return [Hash] the edit, widened when the number really changed
@@ -71,7 +72,9 @@ module Spree
number = attributes[:tracking_number] || attributes['tracking_number']
return attributes if number.blank? || number.to_s.squish == tracking_number
- corrected = attributes.merge(status: 'pending', delivered_at: nil)
+ corrected = attributes.merge(
+ status: 'pending', delivered_at: nil, estimated_delivery_at: nil, details: nil
+ )
corrected[:carrier] = nil if corrected[:carrier].blank? && corrected['carrier'].blank?
corrected[:tracking_url] = nil if corrected[:tracking_url].blank? && corrected['tracking_url'].blank?
corrected
diff --git a/spree/core/app/services/spree/fulfillments/stand_down_provider.rb b/spree/core/app/services/spree/fulfillments/stand_down_provider.rb
index 82d858eb41..f5a84b6291 100644
--- a/spree/core/app/services/spree/fulfillments/stand_down_provider.rb
+++ b/spree/core/app/services/spree/fulfillments/stand_down_provider.rb
@@ -14,24 +14,33 @@ module Spree
# @param fulfillment [Spree::Fulfillment]
# @return [Spree::ServiceModule::Result]
def call(fulfillment:)
- fulfillment.shipping_labels.active.each do |shipping_label|
+ self.class.refund_labels(fulfillment)
+ fulfillment.provider.cancel_fulfillment(fulfillment)
+ success(fulfillment)
+ end
+
+ # Gives back the postage on anything that is no longer travelling.
+ # Owner-agnostic because a canceled return is the same problem as a
+ # canceled parcel: the label is live, the merchant has paid for it, and
+ # whoever holds it could still ship on it.
+ #
+ # @param owner [Spree::Fulfillment, Spree::Return]
+ def self.refund_labels(owner)
+ owner.shipping_labels.active.each do |shipping_label|
next unless shipping_label.refundable?
result = Spree.shipping_label_refund_workflow.call(shipping_label: shipping_label)
next if result.success?
- # The goods are not going out either way, so a carrier that refuses
- # the refund never blocks the cancellation — but the merchant is out
- # of pocket, so it is reported rather than swallowed.
+ # The goods are not moving either way, so a carrier that refuses the
+ # refund never blocks the cancellation — but the merchant is out of
+ # pocket, so it is reported rather than swallowed.
Rails.error.report(
Spree::Core::LabelRefundFailed.new(result.error.to_s),
- context: { shipping_label_id: shipping_label.id, fulfillment_id: fulfillment.id },
+ context: { shipping_label_id: shipping_label.id, owner_type: owner.class.name, owner_id: owner.id },
source: 'spree.fulfillments.stand_down'
)
end
-
- fulfillment.provider.cancel_fulfillment(fulfillment)
- success(fulfillment)
end
end
end
diff --git a/spree/core/app/workflows/spree/returns/cancel.rb b/spree/core/app/workflows/spree/returns/cancel.rb
index 53697be7cc..d642ce940f 100644
--- a/spree/core/app/workflows/spree/returns/cancel.rb
+++ b/spree/core/app/workflows/spree/returns/cancel.rb
@@ -21,6 +21,11 @@ module Spree
step :mark_canceled
end
+ # Carrier I/O, after the status is committed: a customer who abandons
+ # a return must not be left holding live prepaid postage the merchant
+ # is still paying for.
+ external_step :refund_prepaid_label
+
run_hooks :after_cancel
return_record.publish_event('return.canceled')
success(return_record.reload)
@@ -34,6 +39,10 @@ module Spree
failure(return_record, :not_cancellable)
end
+ def refund_prepaid_label
+ Spree::Fulfillments::StandDownProvider.refund_labels(return_record)
+ end
+
def mark_canceled
memo = [return_record.memo, reason].compact_blank.join("\n")
return_record.update!(status: 'canceled', canceled_at: Time.current, memo: memo.presence)
diff --git a/spree/core/spec/models/spree/delivery_spec.rb b/spree/core/spec/models/spree/delivery_spec.rb
index 694da90e43..fafa4b93f0 100644
--- a/spree/core/spec/models/spree/delivery_spec.rb
+++ b/spree/core/spec/models/spree/delivery_spec.rb
@@ -139,7 +139,8 @@ RSpec.describe Spree::Delivery, type: :model do
let(:delivery) do
create(:delivery, owner: fulfillment, store: store, tracking_number: '1Z_OLD',
carrier: 'ups', tracking_url: 'https://ups.example/1Z_OLD',
- status: 'delivered', delivered_at: 2.days.ago)
+ status: 'delivered', delivered_at: 2.days.ago,
+ estimated_delivery_at: 1.day.ago, details: { 'scans' => ['Left facility'] })
end
# To a carrier a different number is a different parcel, so nothing about
@@ -152,6 +153,9 @@ RSpec.describe Spree::Delivery, type: :model do
expect(corrected[:delivered_at]).to be_nil
expect(corrected[:carrier]).to be_nil
expect(corrected[:tracking_url]).to be_nil
+ # The old parcel's ETA and scans describe a journey this number never made.
+ expect(corrected[:estimated_delivery_at]).to be_nil
+ expect(corrected[:details]).to be_nil
end
it 'leaves an edit that is not a correction alone' do
diff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
index 827e721c2d..a3c5b510e7 100644
--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
@@ -314,5 +314,29 @@ RSpec.describe 'Spree::Returns workflows' do
expect(create_return).to be_success
end
+
+ # A customer who abandons a return must not be left holding live prepaid
+ # postage: the label still works and the merchant is still paying for it.
+ it 'gives back the postage on a prepaid label' do
+ return_record = create_return.value
+ label = create(:shipping_label, owner: return_record, store: store)
+ allow(Spree.shipping_label_refund_workflow).to receive(:call).and_call_original
+
+ Spree::Returns::Cancel.call(return_record: return_record)
+
+ expect(Spree.shipping_label_refund_workflow).to have_received(:call).
+ with(shipping_label: label)
+ end
+
+ it 'cancels even when the carrier refuses the refund' do
+ return_record = create_return.value
+ create(:shipping_label, owner: return_record, store: store)
+ allow(Spree.shipping_label_refund_workflow).to receive(:call).
+ and_return(Spree::ServiceModule::Result.new(false, nil, 'carrier said no'))
+ allow(Rails.error).to receive(:report)
+
+ expect(Spree::Returns::Cancel.call(return_record: return_record)).to be_success
+ expect(Rails.error).to have_received(:report)
+ end
end
end