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.

Refund a label through the carrier that sold it

Damian Legawiec1fcd8a1844d7
diff --git a/packages/dashboard/src/components/spree/orders/fulfillment-label-upload-dialog.tsx b/packages/dashboard/src/components/spree/orders/fulfillment-label-upload-dialog.tsx
index 1a2e8d5988..58bb65f201 100644
--- a/packages/dashboard/src/components/spree/orders/fulfillment-label-upload-dialog.tsx
+++ b/packages/dashboard/src/components/spree/orders/fulfillment-label-upload-dialog.tsx
@@ -7,6 +7,15 @@ import {
} from '@spree/dashboard-core'
import {
Button,
+ Combobox,
+ ComboboxButtonTrigger,
+ ComboboxContent,
+ ComboboxEmpty,
+ ComboboxItem,
+ ComboboxList,
+ ComboboxSearch,
+ ComboboxTriggerPlaceholder,
+ ComboboxValue,
Dialog,
DialogBody,
DialogContent,
@@ -24,10 +33,11 @@ import {
InputGroupText,
} from '@spree/dashboard-ui'
import { useState } from 'react'
-import { useForm } from 'react-hook-form'
+import { Controller, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { z } from 'zod'
import { useFulfillmentActions } from '../../../hooks/use-fulfillments'
+import { useTrackingCarriers } from '../../../hooks/use-tracking-carriers'
/** What a carrier label plausibly arrives as — mirrors the server allowlist. */
const LABEL_ACCEPT = 'application/pdf,image/png,text/plain'
@@ -60,6 +70,18 @@ export function FulfillmentLabelUploadDialog({
}) {
const { t } = useTranslation()
const { buyLabel } = useFulfillmentActions(orderId)
+ const { data: carriersData } = useTrackingCarriers(open)
+
+ // The registry's carriers, stored by key and shown by name. Anything it
+ // does not know is still typeable — a forwarder's own name is a legal
+ // carrier, and an empty value asks the server to detect one.
+ const carrierOptions = (carriersData?.data ?? []).map((carrier) => ({
+ value: carrier.id,
+ label: carrier.name,
+ }))
+
+ const carrierLabel = (value: string) =>
+ carrierOptions.find((option) => option.value === value)?.label ?? value
const [file, setFile] = useState<FileUploadValue>(EMPTY_FILE_UPLOAD_VALUE)
// The signed id does not exist until the upload resolves, so saving during
@@ -168,10 +190,40 @@ export function FulfillmentLabelUploadDialog({
<FieldLabel htmlFor="label-carrier">
{t('admin.orders.detail.fulfillments.carrier_label')}
</FieldLabel>
- <Input
- id="label-carrier"
- placeholder={t('admin.orders.detail.fulfillments.carrier_auto')}
- {...form.register('carrier')}
+ <Controller
+ control={form.control}
+ name="carrier"
+ render={({ field }) => (
+ <Combobox
+ items={carrierOptions.map((option) => option.value)}
+ value={field.value}
+ onValueChange={(value: string | null) => field.onChange(value ?? '')}
+ itemToStringLabel={carrierLabel}
+ >
+ <ComboboxButtonTrigger id="label-carrier" onBlur={field.onBlur}>
+ {field.value ? (
+ <ComboboxValue />
+ ) : (
+ <ComboboxTriggerPlaceholder>
+ {t('admin.orders.detail.fulfillments.carrier_auto')}
+ </ComboboxTriggerPlaceholder>
+ )}
+ </ComboboxButtonTrigger>
+ <ComboboxContent>
+ <ComboboxSearch
+ placeholder={t('admin.orders.detail.fulfillments.carrier_search')}
+ />
+ <ComboboxEmpty>{t('admin.common.no_results')}</ComboboxEmpty>
+ <ComboboxList>
+ {(value: string) => (
+ <ComboboxItem key={value} value={value}>
+ {carrierLabel(value)}
+ </ComboboxItem>
+ )}
+ </ComboboxList>
+ </ComboboxContent>
+ </Combobox>
+ )}
/>
</Field>
</DialogBody>
diff --git a/spree/core/app/models/spree/shipping_label.rb b/spree/core/app/models/spree/shipping_label.rb
index 4b41cde3af..239c6550b6 100644
--- a/spree/core/app/models/spree/shipping_label.rb
+++ b/spree/core/app/models/spree/shipping_label.rb
@@ -65,6 +65,21 @@ module Spree
source == 'uploaded'
end
+ # The provider that sold this label, which is the only one that can refund
+ # it. Resolved from the connected account it was bought through rather
+ # than from the parcel's current delivery method: a merchant who reroutes
+ # a parcel after buying postage still has to give that postage back to the
+ # carrier who sold it.
+ #
+ # @return [Spree::FulfillmentProvider::Base]
+ def provider
+ klass = integration && Spree.fulfillment_providers.find do |candidate|
+ candidate.integration_class.to_s == integration.type
+ end
+
+ klass ? klass.new : owner.provider
+ end
+
# @return [Boolean] whether a refund can still be asked of the carrier.
# A label the carrier is still deciding on counts: some settle refunds
# asynchronously and a re-ask is how a stuck one is re-driven.
diff --git a/spree/core/app/workflows/spree/shipping_labels/refund.rb b/spree/core/app/workflows/spree/shipping_labels/refund.rb
index 7fea852d45..126f70c35a 100644
--- a/spree/core/app/workflows/spree/shipping_labels/refund.rb
+++ b/spree/core/app/workflows/spree/shipping_labels/refund.rb
@@ -46,7 +46,7 @@ module Spree
end
def refund
- @outcome = shipping_label.owner.provider.refund_label(shipping_label).to_s
+ @outcome = shipping_label.provider.refund_label(shipping_label).to_s
return if %w[refunded refund_requested].include?(@outcome)
diff --git a/spree/core/spec/models/spree/shipping_label_spec.rb b/spree/core/spec/models/spree/shipping_label_spec.rb
index d10cf34a3d..c8b69a3322 100644
--- a/spree/core/spec/models/spree/shipping_label_spec.rb
+++ b/spree/core/spec/models/spree/shipping_label_spec.rb
@@ -82,4 +82,35 @@ RSpec.describe Spree::ShippingLabel, type: :model do
end
end
end
+ # A merchant who reroutes a parcel after buying postage still owes that
+ # postage back to the carrier who sold it, not to whoever carries the parcel
+ # now — and the Manual provider refunds nothing at all.
+ describe '#provider' do
+ # A carrier gem registers a provider that names the integration holding
+ # its credentials; the label resolves back through that pairing.
+ let(:selling_provider) do
+ Class.new(Spree::FulfillmentProvider::Base) do
+ def self.name = 'SpecSellingProvider'
+ def self.integration_class = 'Spree::Integration'
+ def self.generates_labels? = true
+ end
+ end
+
+ it 'is the one behind the integration that sold the label' do
+ allow(Spree).to receive(:fulfillment_providers).and_return([selling_provider])
+ # Stood in rather than created: the only concrete integration in the
+ # suite belongs to a provider gem core does not load.
+ integration = instance_double(Spree::Integration, type: 'Spree::Integration')
+ label = build(:shipping_label, owner: fulfillment)
+ allow(label).to receive(:integration).and_return(integration)
+
+ expect(label.provider).to be_a(selling_provider)
+ end
+
+ it 'falls back to the parcel own provider for an uploaded label' do
+ label = build(:shipping_label, :uploaded, owner: fulfillment, integration: nil)
+
+ expect(label.provider.class).to eq(fulfillment.provider.class)
+ end
+ end
end
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 71c12d57b9..2352c52490 100644
--- a/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rb
+++ b/spree/core/spec/workflows/spree/shipping_labels/refund_spec.rb
@@ -59,6 +59,7 @@ module Spree
expect(label.reload).to be_purchased
end
+
it 'refuses an uploaded label' do
# Its own parcel: one live label per parcel is a database constraint,
# and this fulfillment already carries the purchased one above.