Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions testing/lib/workarea/testing/factories/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,42 @@ def create_placed_order(overrides = {})
)
)

# In some environments, the first pass of setting a shipping service can
# persist without the base shipping price adjustment, which then causes
# `Checkout#shippable?`/`Checkout#place_order` to fail. Re-applying the
# currently selected option ensures `Shipping#base_price` is set.
if order.requires_shipping?
shipping = checkout.shippings.first

if shipping&.shipping_service.present?
option = Workarea::Checkout::ShippingOptions
.new(order, shipping)
.find_valid(shipping.shipping_service.name)

shipping.set_shipping_service(option.to_h) if option.present?
end
end

unless checkout.place_order
raise(
UnplacedOrderError,
'failed placing the order in the create_placed_order factory'
)
shipping_errors = checkout
.shippings
.map { |s| s.errors.full_messages }
.flatten
.presence

message = [
'failed placing the order in the create_placed_order factory',
"complete?=#{checkout.complete?}",
"shippable?=#{checkout.shippable?}",
"payable?=#{checkout.payable?}",
("shipping_errors=#{shipping_errors.inspect}" if shipping_errors.present?),
("payment_errors=#{checkout.payment.errors.full_messages.inspect}" if checkout.payment&.errors&.any?)
].compact.join(' ')

raise(UnplacedOrderError, message)
end

forced_attrs = overrides.slice(:placed_at, :update_at, :total_price)
forced_attrs = overrides.slice(:placed_at, :updated_at, :total_price)
order.update_attributes!(forced_attrs)
order
end
Expand Down
Loading