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.

Fix: EasyPost quotes every package of a split order with its own contents (#14779)

Damian Legawiec28c9ea26cd9b
diff --git a/docs/developer/how-to/custom-delivery-rate-provider.mdx b/docs/developer/how-to/custom-delivery-rate-provider.mdx
index 0f3adaba5f..cf7a7fd0d5 100644
--- a/docs/developer/how-to/custom-delivery-rate-provider.mdx
+++ b/docs/developer/how-to/custom-delivery-rate-provider.mdx
@@ -93,13 +93,19 @@ module SpreeAcmeCarrier
# One API call serves every delivery method sharing this provider within
# a request — carriers return all services in one response, so quoting
# five methods should not mean five round-trips.
+ #
+ # Key the cache on everything the request sends, not on the location and
+ # order: an order split into several packages from one stock location
+ # would otherwise get the first package's rates for every package.
+ # Digesting the payload itself keeps the key complete as the request grows.
def quotes_for(package)
- key = [:acme_quotes, store.id, package.stock_location.id, package.order.id]
- Spree::Current.provider_cache[key] ||= integration.client.rates(
- from: package.stock_location,
- to: package.order.ship_address,
- parcel: { weight: package.weight }
- )
+ request = {
+ from: package.stock_location.slice(:address1, :city, :zipcode, :country_id),
+ to: package.owner.ship_address.slice(:address1, :city, :zipcode, :country_id),
+ parcel: { weight: package.weight.to_f }
+ }
+ key = [:acme_quotes, store.id, Digest::SHA256.hexdigest(request.to_json)]
+ Spree::Current.provider_cache[key] ||= integration.client.rates(**request)
end
end
end
diff --git a/spree/providers/easypost/app/models/spree_easypost/delivery_rate_provider.rb b/spree/providers/easypost/app/models/spree_easypost/delivery_rate_provider.rb
index fe66a086d0..23b5a6c599 100644
--- a/spree/providers/easypost/app/models/spree_easypost/delivery_rate_provider.rb
+++ b/spree/providers/easypost/app/models/spree_easypost/delivery_rate_provider.rb
@@ -117,11 +117,16 @@ module SpreeEasyPost
# One shipment-create returns rates for every carrier and service, so all
# delivery methods sharing this provider within a request reuse it.
+ #
+ # Keyed by a digest of the exact payload sent: an order split into
+ # several packages from one stock location must quote each package's own
+ # parcel and customs contents, never the first package's cached rates.
+ # Anything that changes the request changes the key; identical requests
+ # still cost one API call.
def easypost_shipment(package)
- cache_key = [:easypost_shipment, store.id, package.stock_location.id, package.owner.id]
- Spree::Current.provider_cache[cache_key] ||= integration.client.shipment.create(
- **shipment_params(package)
- )
+ params = shipment_params(package)
+ cache_key = [:easypost_shipment, store.id, integration.id, Digest::SHA256.hexdigest(params.to_json)]
+ Spree::Current.provider_cache[cache_key] ||= integration.client.shipment.create(**params)
end
# Built by the shared helper so a label bought against this quote carries
diff --git a/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb b/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
index 9080186526..fd20e58f17 100644
--- a/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
+++ b/spree/providers/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb
@@ -244,6 +244,44 @@ RSpec.describe SpreeEasyPost::DeliveryRateProvider do
expect(shipment_service).to have_received(:create).once
expect(other_estimates.size).to eq(2)
end
+
+ # An order split into several packages from one stock location: each
+ # package ships its own parcel, so each must be quoted on its own. The
+ # cache used to key on the location and owner only, handing the second
+ # package the first one's rates.
+ describe 'split packages from one stock location' do
+ let(:order) { create(:order_with_line_items, store: store, line_items_count: 2) }
+ let(:fulfillment) { order.fulfillments.first }
+ let(:packages) do
+ order.line_items.first.variant.update!(weight: 1)
+ order.line_items.last.variant.update!(weight: 20)
+
+ fulfillment.to_package.contents.group_by(&:variant).values.map do |items|
+ Spree::Stock::Package.new(fulfillment.stock_location, items).tap { |split| split.owner = order }
+ end
+ end
+
+ it 'quotes each package with its own contents' do
+ allow(shipment_service).to receive(:create) do |params|
+ rate = double(carrier: 'UPS', service: 'Ground', rate: params[:parcel][:weight].to_s, currency: 'USD',
+ delivery_date: nil, id: "rate_#{params[:parcel][:weight]}", shipment_id: 'shp')
+ double(rates: [rate])
+ end
+
+ light, heavy = packages.sort_by(&:weight).map { |split| provider.estimates(split).first }
+
+ expect(shipment_service).to have_received(:create).twice
+ expect(light.cost).not_to eq(heavy.cost)
+ expect(light.metadata['easypost_rate_id']).not_to eq(heavy.metadata['easypost_rate_id'])
+ end
+
+ it 'still reuses the quote for the same package' do
+ packages.each { |split| provider.estimates(split) }
+ packages.each { |split| provider.estimates(split) }
+
+ expect(shipment_service).to have_received(:create).twice
+ end
+ end
end
end