Class: SpreeUberDirect::Quote

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_uber_direct/quote.rb

Overview

Requests a live Uber Direct delivery-fee quote for an order — the storefront-facing half of this extension (called from Spree::Calculator::Shipping::UberDirectQuote during checkout, M3) and again at order.completed if the original quote expired (see DeliveryDispatcher, M4).

order.total at whichever moment this is called is what's sent as manifest_total_value — Spree recalculates on every checkout step already, and Uber isn't in the money path (Spree's own payment method still charges the customer; Uber only uses this for delivery-fee/ insurance math) — same rationale SpreeDoordash::Quote documents for its own order_value field.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callObject



17
# File 'app/services/spree_uber_direct/quote.rb', line 17

def self.call(...) = new.call(...)

Instance Method Details

#call(order) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/spree_uber_direct/quote.rb', line 19

def call(order)
  return nil unless order.ship_address
  # Same real bug DoorDash's own Quote hit live: with no phone on the
  # ship address, the request is doomed before it's even sent —
  # Uber's own schema requires `^\+[0-9]+$` on both phone fields.
  # Spree::Config[:address_requires_phone] (spree_host) should make
  # this unreachable in the normal storefront checkout flow, but this
  # guard stays as defense in depth for any other path that can create
  # an order (admin, API, migrated data).
  return nil if order.ship_address.phone.blank?

  stock_location = pickup_location_for(order)
  return nil unless stock_location&.phone.present?

  client = SpreeUberDirect::Client.for_store(order.store || Spree::Store.default)
  response = client.create_quote(build_payload(order, stock_location))
  mapping = persist_quote!(order, response)

  Result.new(
    fee_cents: mapping.quoted_fee_cents,
    currency: mapping.currency,
    external_quote_id: mapping.external_quote_id,
    expires_at: mapping.quote_expires_at
  )
rescue SpreeUberDirect::Client::MissingCredentialsError, SpreeUberDirect::RequestError => e
  # Unserviceable address, no credential connected, Uber-side rejection
  # — none of these should ever raise into checkout. The calculator
  # (M3) treats a nil Result as "this rate isn't available," Spree's
  # normal shape for "can't quote this," same as
  # Spree::Calculator::Shipping::DoordashQuote's own precedent.
  Rails.logger.info("[SpreeUberDirect] quote skipped for order #{order.number}: #{e.message}")
  nil
end