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
Make customs paperwork reachable
Damian Legawiecd6b68288c077
diff --git a/packages/admin-sdk/src/types/generated/Return.ts b/packages/admin-sdk/src/types/generated/Return.tsindex 59fe991cf8..d44b381569 100644--- a/packages/admin-sdk/src/types/generated/Return.ts+++ b/packages/admin-sdk/src/types/generated/Return.ts@@ -1,4 +1,4 @@-// Typelizer digest b9ec1c68e3b89c84b48a09c59725c416+// Typelizer digest 08ccffa55aaeaff3bc54fdacdbb4fe6f//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {ReturnReason, ReturnLineItem, Order, StockLocation, Refund, ShippingLabel, Delivery} from '@/types'@@ -28,6 +28,7 @@ interface Return {order?: Order;stock_location?: StockLocation;refunds?: Array<Refund>;+ documents: Array<{ kind: string; url: string }>;labels: Array<ShippingLabel>;deliveries: Array<Delivery>;}diff --git a/packages/dashboard/src/components/spree/order-returns-card.tsx b/packages/dashboard/src/components/spree/order-returns-card.tsxindex 7fe14e9b92..a170ede1c7 100644--- a/packages/dashboard/src/components/spree/order-returns-card.tsx+++ b/packages/dashboard/src/components/spree/order-returns-card.tsx@@ -50,6 +50,7 @@ import i18n from 'i18next'import { useState } from 'react'import { useTranslation } from 'react-i18next'import { useOrderReturns, useReturnActions } from '../../hooks/use-returns'+import { ShippingDocuments } from './orders/shipping-documents'import { ShippingLabelRow } from './orders/shipping-label-row'import { CreateReturnDialog, fulfilledUnits } from './post-sale-create-dialogs'@@ -285,12 +286,15 @@ function ReturnLabel({if (activeLabel) {return (- <ShippingLabelRow- label={activeLabel}- isRefunding={isRefunding}- onRefund={() => onRefund(activeLabel.id)}- onDelete={() => onDelete(activeLabel.id)}- />+ <>+ <ShippingLabelRow+ label={activeLabel}+ isRefunding={isRefunding}+ onRefund={() => onRefund(activeLabel.id)}+ onDelete={() => onDelete(activeLabel.id)}+ />+ <ShippingDocuments documents={returnRecord.documents} />+ </>)}diff --git a/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx b/packages/dashboard/src/components/spree/orders/fulfillments-card.tsxindex 4133267022..d3cf772ff3 100644--- a/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx+++ b/packages/dashboard/src/components/spree/orders/fulfillments-card.tsx@@ -63,6 +63,7 @@ import { FulfillmentEditDialog } from './fulfillment-edit-dialog'import { FulfillmentFulfillForm } from './fulfillment-fulfill-form'import { FulfillmentItemList } from './fulfillment-item-list'import { FulfillmentLabelUploadDialog } from './fulfillment-label-upload-dialog'+import { ShippingDocuments } from './shipping-documents'import { ShippingLabelRow } from './shipping-label-row'/**@@ -567,6 +568,8 @@ function FulfillmentRow({ order, fulfillment }: { order: Order; fulfillment: Ful/>)}+ {!fulfilling && <ShippingDocuments documents={fulfillment.documents} />}+{!fulfilling && (<FulfillmentDeliveriesorderId={orderId}diff --git a/packages/dashboard/src/components/spree/orders/shipping-documents.tsx b/packages/dashboard/src/components/spree/orders/shipping-documents.tsxnew file mode 100644index 0000000000..8c5cb27476--- /dev/null+++ b/packages/dashboard/src/components/spree/orders/shipping-documents.tsx@@ -0,0 +1,42 @@+import { CardContent } from '@spree/dashboard-ui'+import { FileTextIcon } from 'lucide-react'+import { useTranslation } from 'react-i18next'++/**+ * Customs paperwork the carrier produced beside the label — a commercial+ * invoice, a declaration — for parcels that cross a border.+ *+ * Owner-agnostic like the label row beside it: an export and a cross-border+ * return are declared the same way. The files are hosted by the carrier+ * rather than stored here, so these open in a new tab and a link can stop+ * working once the carrier expires it.+ */+export function ShippingDocuments({+ documents,+}: {+ documents?: Array<{ kind: string; url: string }>+}) {+ const { t } = useTranslation()++ if (!documents?.length) return null++ return (+ <CardContent className="flex flex-col gap-1.5 border-t pt-3">+ <p className="text-sm font-medium">{t('admin.orders.detail.fulfillments.documents_title')}</p>+ {documents.map((document) => (+ <a+ key={document.url}+ href={document.url}+ target="_blank"+ rel="noreferrer"+ className="flex items-center gap-2 text-sm text-primary hover:underline"+ >+ <FileTextIcon className="size-4 shrink-0" />+ {t(`admin.orders.detail.fulfillments.document_kinds.${document.kind}`, {+ defaultValue: document.kind.replace(/_/g, ' '),+ })}+ </a>+ ))}+ </CardContent>+ )+}diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.jsonindex ac2337ed09..19083830ed 100644--- a/packages/dashboard/src/locales/ar.json+++ b/packages/dashboard/src/locales/ar.json@@ -2751,6 +2751,15 @@"delete_label": "حذف الملصق","deliveries_title": "التتبع","delivery_method": "طريقة التوصيل",+ "document_kinds": {+ "certificate_of_origin": "شهادة المنشأ",+ "cn22": "نموذج جمركي CN22",+ "cn23": "نموذج جمركي CN23",+ "commercial_invoice": "فاتورة تجارية",+ "customs_form": "نموذج جمركي",+ "form": "مستند"+ },+ "documents_title": "المستندات الجمركية","edit_description": "غيّر مكان شحن هذه الشحنة وطريقة التوصيل التي تنقلها.","edit_title": "تعديل الشحنة","edit_tracking_title": "تعديل التتبع",diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.jsonindex 30fb1fe419..7ec18cca32 100644--- a/packages/dashboard/src/locales/de.json+++ b/packages/dashboard/src/locales/de.json@@ -2733,6 +2733,15 @@"delete_label": "Etikett löschen","deliveries_title": "Sendungsverfolgung","delivery_method": "Versandart",+ "document_kinds": {+ "certificate_of_origin": "Ursprungszeugnis",+ "cn22": "Zollformular CN22",+ "cn23": "Zollformular CN23",+ "commercial_invoice": "Handelsrechnung",+ "customs_form": "Zollformular",+ "form": "Dokument"+ },+ "documents_title": "Zolldokumente","edit_description": "Ändern Sie, von wo diese Sendung versandt wird und welche Versandart sie befördert.","edit_title": "Sendung bearbeiten","edit_tracking_title": "Sendungsverfolgung bearbeiten",diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex dc2063d1ef..843badb16f 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -2733,6 +2733,15 @@"delete_label": "Delete label","deliveries_title": "Tracking","delivery_method": "Delivery method",+ "document_kinds": {+ "certificate_of_origin": "Certificate of origin",+ "cn22": "Customs form CN22",+ "cn23": "Customs form CN23",+ "commercial_invoice": "Commercial invoice",+ "customs_form": "Customs form",+ "form": "Document"+ },+ "documents_title": "Customs documents","edit_description": "Change where this fulfillment ships from and which delivery method carries it.","edit_title": "Edit fulfillment","edit_tracking_title": "Edit tracking",diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex 702275f240..798ea52d86 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -2733,6 +2733,15 @@"delete_label": "Supprimer l'étiquette","deliveries_title": "Suivi","delivery_method": "Mode de livraison",+ "document_kinds": {+ "certificate_of_origin": "Certificat d'origine",+ "cn22": "Formulaire douanier CN22",+ "cn23": "Formulaire douanier CN23",+ "commercial_invoice": "Facture commerciale",+ "customs_form": "Formulaire douanier",+ "form": "Document"+ },+ "documents_title": "Documents douaniers","edit_description": "Modifiez le lieu d'expédition et le mode de livraison de cette expédition.","edit_title": "Modifier l'expédition","edit_tracking_title": "Modifier le suivi",diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.jsonindex 90dcdeeed9..85cc77ebef 100644--- a/packages/dashboard/src/locales/pl.json+++ b/packages/dashboard/src/locales/pl.json@@ -2753,6 +2753,15 @@"delete_label": "Usuń etykietę","deliveries_title": "Śledzenie","delivery_method": "Sposób dostawy",+ "document_kinds": {+ "certificate_of_origin": "Świadectwo pochodzenia",+ "cn22": "Formularz celny CN22",+ "cn23": "Formularz celny CN23",+ "commercial_invoice": "Faktura handlowa",+ "customs_form": "Formularz celny",+ "form": "Dokument"+ },+ "documents_title": "Dokumenty celne","edit_description": "Zmień miejsce wysyłki tej przesyłki oraz sposób dostawy, którym jest realizowana.","edit_title": "Edytuj przesyłkę","edit_tracking_title": "Edytuj śledzenie",diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.jsonindex 99b5ea82b2..0e0e42b4f4 100644--- a/packages/dashboard/src/locales/zh-CN.json+++ b/packages/dashboard/src/locales/zh-CN.json@@ -2726,6 +2726,15 @@"delete_label": "删除面单","deliveries_title": "物流跟踪","delivery_method": "配送方式",+ "document_kinds": {+ "certificate_of_origin": "原产地证书",+ "cn22": "报关单 CN22",+ "cn23": "报关单 CN23",+ "commercial_invoice": "商业发票",+ "customs_form": "报关单",+ "form": "文件"+ },+ "documents_title": "报关文件","edit_description": "修改此发货单的发货地以及承运的配送方式。","edit_title": "编辑发货单","edit_tracking_title": "编辑物流跟踪",diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rbindex e0a09d2cee..8fb55f1d37 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb@@ -92,8 +92,15 @@ module Spree:returnsend+ # `documents` resolves the provider through the returned units and+ # reads the labels it produced, so both are preloaded: without them+ # a page of returns is a query per row.def collection_includes- [:reason, :stock_location, { return_line_items: [:variant, :line_item] }]+ [+ :reason, :stock_location, :deliveries,+ { shipping_labels: { file_attachment: :blob } },+ { return_line_items: [:variant, :line_item, { fulfillment_item: :fulfillment }] }+ ]enddef permitted_paramsdiff --git a/spree/api/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/fulfillment_serializer.rbindex 4a37d7c9f3..60cfa61cec 100644--- a/spree/api/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb@@ -34,7 +34,7 @@ module Spree# Customs forms and other paperwork the provider produced beside the# label; the labels themselves are listed under +labels+.attribute :documents do |fulfillment|- fulfillment.provider.documents(fulfillment)+ fulfillment.provider.documents(fulfillment).map(&:as_json)end# Every label bought or uploaded for this parcel, refunded onesdiff --git a/spree/api/app/serializers/spree/api/v3/admin/return_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/return_serializer.rbindex 05ff29441c..54087f6e5e 100644--- a/spree/api/app/serializers/spree/api/v3/admin/return_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/return_serializer.rb@@ -7,6 +7,7 @@ module Spreeclass ReturnSerializer < V3::ReturnSerializertypelize memo: [:string, nullable: true],metadata: 'Record<string, unknown>',+ documents: "Array<{ kind: string; url: string }>",stock_location_id: [:string, nullable: true],created_by_id: [:string, nullable: true],refunded_total: :string,@@ -39,6 +40,12 @@ module Spreeone :stock_location, resource: proc { Spree.api.admin_stock_location_serializer }, if: proc { expand?('stock_location') }many :refunds, resource: proc { Spree.api.admin_refund_serializer }, if: proc { expand?('refunds') }+ # Customs paperwork the provider produced beside the label: a+ # cross-border return is declared like any other export.+ attribute :documents do |return_record|+ return_record.provider.documents(return_record).map(&:as_json)+ end+# The prepaid label for the parcel coming back, refunded ones# included — the postage history of the return.many :shipping_labels, key: :labels, resource: proc { Spree.api.admin_shipping_label_serializer }diff --git a/spree/api/spec/serializers/spree/api/v3/admin/documents_spec.rb b/spree/api/spec/serializers/spree/api/v3/admin/documents_spec.rbnew file mode 100644index 0000000000..83618f7824--- /dev/null+++ b/spree/api/spec/serializers/spree/api/v3/admin/documents_spec.rb@@ -0,0 +1,55 @@+require 'spec_helper'+require 'spree/testing_support/label_provider'++# Customs paperwork reaches the merchant through the provider, not a column,+# so what matters is that both owners of a label actually ask for it: an+# international return is declared like any other export, and for a long+# while only fulfillments exposed it.+RSpec.describe 'admin serializers expose provider documents' do+ let(:store) { @default_store }+ let(:carrier_documents) do+ [Spree::ShippingDocument.new(kind: 'commercial_invoice', url: 'https://carrier.example/invoice.pdf')]+ end++ # A real provider rather than a double: the serializers ask a provider for+ # more than its documents, and a double narrowed to one method only proves+ # the spec author guessed the calls right.+ let(:provider) do+ Spree::TestingSupport::LabelProvider.new.tap do |instance|+ paperwork = carrier_documents+ instance.define_singleton_method(:documents) { |_owner| paperwork }+ end+ end++ describe Spree::Api::V3::Admin::FulfillmentSerializer do+ let(:order) { create(:order_ready_to_ship, store: store) }+ let(:fulfillment) { order.fulfillments.first }++ it 'renders what the provider produced beside the label' do+ allow(fulfillment).to receive(:provider).and_return(provider)++ rendered = JSON.parse(described_class.new(fulfillment).serialize)++ expect(rendered['documents']).to eq([{ 'kind' => 'commercial_invoice', 'url' => 'https://carrier.example/invoice.pdf' }])+ end+ end++ describe Spree::Api::V3::Admin::ReturnSerializer do+ let(:return_record) { create(:return, order: create(:shipped_order, store: store)) }++ it 'renders the paperwork for the inbound parcel' do+ allow(return_record).to receive(:provider).and_return(provider)++ rendered = JSON.parse(described_class.new(return_record).serialize)++ expect(rendered['documents']).to eq([{ 'kind' => 'commercial_invoice', 'url' => 'https://carrier.example/invoice.pdf' }])+ end++ it 'is empty rather than absent when the provider produced none' do+ rendered = JSON.parse(described_class.new(return_record).serialize)++ expect(rendered).to have_key('documents')+ expect(rendered['documents']).to eq([])+ end+ end+enddiff --git a/spree/core/app/models/spree/fulfillment_provider/base.rb b/spree/core/app/models/spree/fulfillment_provider/base.rbindex f442e4f9e3..871214b505 100644--- a/spree/core/app/models/spree/fulfillment_provider/base.rb+++ b/spree/core/app/models/spree/fulfillment_provider/base.rb@@ -110,7 +110,7 @@ module Spree# is never reported here.## @param _owner [Spree::Fulfillment, Spree::Return]- # @return [Array<Hash>] `{ kind:, url: }` entries+ # @return [Array<Spree::ShippingDocument>]def documents(_owner)[]enddiff --git a/spree/core/app/models/spree/shipping_document.rb b/spree/core/app/models/spree/shipping_document.rbnew file mode 100644index 0000000000..df541612be--- /dev/null+++ b/spree/core/app/models/spree/shipping_document.rb@@ -0,0 +1,32 @@+module Spree+ # Paperwork a carrier produced beside the label — a commercial invoice, a+ # customs declaration — returned by a fulfillment provider's +documents+.+ #+ # Ephemeral, never persisted: the file stays hosted by the carrier and the+ # provider re-derives this on read. It is a typed object rather than a hash+ # so the contract has one definition, and so promoting it to a record later+ # (with a stored file, the way +Spree::ShippingLabel+ keeps its own) is a+ # change to this class rather than to every provider and serializer.+ #+ # +kind+ is the carrier's own word for the document and is deliberately not+ # validated against a list: what paperwork a border wants is not Spree's+ # vocabulary to fix, and an unknown kind renders humanised rather than+ # being dropped.+ class ShippingDocument+ include ActiveModel::Model+ include ActiveModel::Attributes++ attribute :kind, :string, default: 'form'+ attribute :url, :string++ validates :url, presence: true++ # Serializers render documents as plain objects; keeping the shape here+ # means the API contract does not depend on how a provider spells it.+ #+ # @return [Hash]+ def as_json(*)+ { kind: kind, url: url }+ end+ end+enddiff --git a/spree/core/spec/models/spree/shipping_document_spec.rb b/spree/core/spec/models/spree/shipping_document_spec.rbnew file mode 100644index 0000000000..b14d398ec8--- /dev/null+++ b/spree/core/spec/models/spree/shipping_document_spec.rb@@ -0,0 +1,19 @@+require 'spec_helper'++RSpec.describe Spree::ShippingDocument do+ it 'defaults to a generic kind when the carrier does not name one' do+ expect(described_class.new(url: 'https://carrier.example/doc.pdf').kind).to eq('form')+ end++ it 'requires somewhere to read the document' do+ expect(described_class.new(kind: 'cn23')).not_to be_valid+ end++ # The rendered shape is the API contract, so it is pinned here rather than+ # left to whatever a serializer happens to do with the object.+ it 'renders as the kind and the url' do+ document = described_class.new(kind: 'commercial_invoice', url: 'https://carrier.example/ci.pdf')++ expect(document.as_json).to eq(kind: 'commercial_invoice', url: 'https://carrier.example/ci.pdf')+ end+enddiff --git a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rbindex 5277763b47..7a086b53cf 100644--- a/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb+++ b/spree/providers/easypost/app/models/spree_easypost/fulfillment_provider.rb@@ -131,7 +131,7 @@ module SpreeEasyPost# itself is the Spree::ShippingLabel, never listed here.## @param owner [Spree::Fulfillment, Spree::Return]- # @return [Array<Hash>]+ # @return [Array<Spree::ShippingDocument>]def documents(owner)# Read from the loaded association rather than re-scoping it: this is# called once per parcel while serializing an order, and a fresh query@@ -141,7 +141,7 @@ module SpreeEasyPostArray(forms).filter_map do |form|next if form['url'].blank?- { kind: form['form_type'].presence || 'form', url: form['url'] }+ Spree::ShippingDocument.new(kind: form['form_type'].presence || 'form', url: form['url'])endenddiff --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 ed89312ddc..7af166eca7 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@@ -287,7 +287,11 @@ RSpec.describe SpreeEasyPost::FulfillmentProvider dometadata: { 'easypost_forms' => [{ 'form_type' => 'commercial_invoice', 'url' => 'https://forms.example/ci.pdf' }] })- expect(provider.documents(fulfillment)).to eq([{ kind: 'commercial_invoice', url: 'https://forms.example/ci.pdf' }])+ documents = provider.documents(fulfillment)++ expect(documents.map(&:kind)).to eq(['commercial_invoice'])+ expect(documents.map(&:url)).to eq(['https://forms.example/ci.pdf'])+ expect(documents).to all(be_a(Spree::ShippingDocument))endit 'is empty without a label' do