Class: Spree::Api::V3::Store::CartsController

Inherits:
ResourceController show all
Includes:
CartResolvable, OrderLock
Defined in:
app/controllers/spree/api/v3/store/carts_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods inherited from ResourceController

#index

Methods included from ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#associateObject

PATCH /api/v3/store/carts/:id/associate Associates a guest cart with the currently authenticated user Requires: JWT authentication + cart ID in URL



87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 87

def associate
  @cart = find_cart_for_association

  result = Spree.cart_associate_service.call(guest_order: @cart, user: current_user, guest_only: true)

  if result.success?
    render_cart
  else
    render_service_error(result.error.to_s)
  end
end

#completeObject

POST /api/v3/store/carts/:id/complete Completes the checkout — returns Order (not Cart). Idempotent: if the cart is already completed, falls back to the orders scope and returns the completed order.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 103

def complete
  find_cart!

  result = Spree::Dependencies.carts_complete_service.constantize.call(cart: @cart)

  if result.success?
    @cart = result.value
    render_order
  else
    render_service_error(
      result.error.to_s.presence || 'Could not complete checkout',
      code: ERROR_CODES[:cart_cannot_complete]
    )
  end
rescue ActiveRecord::RecordNotFound
  @cart = current_store.orders.complete.find_by_prefix_id!(params[:id])
  authorize!(:show, @cart, cart_token)

  render_order
end

#createObject

POST /api/v3/store/carts Creates a new shopping cart (order) Can be created by guests or authenticated customers



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 32

def create
  result = Spree::Carts::Create.call(
    params: permitted_params.merge(
      user: current_user,
      store: current_store,
      currency: current_currency,
      locale: current_locale
    )
  )

  if result.success?
    @cart = result.value
    render_cart(status: :created)
  else
    render_service_error(result.error.to_s)
  end
end

#destroyObject

DELETE /api/v3/store/carts/:id Deletes/abandons the cart



72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 72

def destroy
  find_cart!

  result = Spree.cart_destroy_service.call(order: @cart)

  if result.success?
    head :no_content
  else
    render_service_error(result.error.to_s)
  end
end

#showObject

GET /api/v3/store/carts/:id Returns cart by prefixed ID Auto-advances the checkout state machine so that shipments and payment requirements are up-to-date (temporary until Spree 6 removes the state machine).



17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 17

def show
  @cart = find_cart

  if @cart.ship_address_id.present? && @cart.shipments.empty?
    ActiveRecord::Base.connected_to(role: :writing) do
      with_order_lock { Spree::Checkout::Advance.call(order: @cart) }
    end
  end

  render_cart
end

#updateObject

PATCH /api/v3/store/carts/:id Updates cart info (email, addresses, special instructions). Auto-advances to the next checkout step when possible.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 53

def update
  find_cart!

  with_order_lock do
    result = Spree::Carts::Update.call(
      cart: @cart,
      params: permitted_params
    )

    if result.success?
      render_cart
    else
      render_service_error(result.error, code: ERROR_CODES[:validation_error])
    end
  end
end