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
Add Cart endpoint and refactor Customer namespace in API v3
Damian Legawiec9ff00c57d36d
diff --git a/api/app/controllers/spree/api/v3/store/addresses_controller.rb b/api/app/controllers/spree/api/v3/store/addresses_controller.rbdeleted file mode 100644index 17397a5deb..0000000000--- a/api/app/controllers/spree/api/v3/store/addresses_controller.rb+++ /dev/null@@ -1,64 +0,0 @@-module Spree- module Api- module V3- module Store- class AddressesController < ResourceController- prepend_before_action :require_authentication!-- # POST /api/v3/store/customers/me/addresses- def create- result = Spree.address_create_service.call(- address_params: permitted_params,- user: current_user- )-- if result.success?- render json: serialize_resource(result.value), status: :created- else- render_errors(result.value.errors)- end- end-- # PATCH /api/v3/store/customers/me/addresses/:id- def update- result = Spree.address_update_service.call(- address: @resource,- address_params: permitted_params- )-- if result.success?- render json: serialize_resource(result.value)- else- render_errors(result.value.errors)- end- end-- protected-- def set_parent- @parent = current_user- end-- def parent_association- :addresses- end-- def model_class- Spree::Address- end-- def serializer_class- Spree.api.address_serializer- end-- def permitted_params- params.require(:address).permit(- :firstname, :lastname, :address1, :address2, :city,- :zipcode, :phone, :company, :country_iso, :state_abbr, :state_name- )- end- end- end- end- end-enddiff --git a/api/app/controllers/spree/api/v3/store/cart_controller.rb b/api/app/controllers/spree/api/v3/store/cart_controller.rbnew file mode 100644index 0000000000..2641a8e5d3--- /dev/null+++ b/api/app/controllers/spree/api/v3/store/cart_controller.rb@@ -0,0 +1,63 @@+module Spree+ module Api+ module V3+ module Store+ class CartController < Store::BaseController+ # GET /api/v3/store/cart+ # Returns current incomplete order (cart)+ # Returns 404 if no cart exists - use POST /orders to create one+ # Authorize via order_token param or JWT Bearer token+ def show+ @cart = find_cart++ if @cart.nil?+ render_error(+ code: ERROR_CODES[:not_found],+ message: 'No cart found. Create one with POST /orders',+ status: :not_found+ )+ return+ end++ render json: serialize_resource(@cart)+ end++ protected++ def serializer_class+ Spree.api.order_serializer+ end++ private++ def find_cart+ # Try order_token first (guest checkout)+ if params[:order_token].present?+ return current_store.orders+ .incomplete+ .find_by(token: params[:order_token])+ end++ # Then try JWT authenticated user+ if current_user.present?+ return current_store.orders+ .incomplete+ .where(user: current_user)+ .order(created_at: :desc)+ .first+ end++ nil+ end++ def serialize_resource(resource)+ result = serializer_class.new(resource, params: serializer_params).to_h+ # Always include order token for cart (needed for guest checkout)+ result[:token] = resource.token if resource.token.present?+ result+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb b/api/app/controllers/spree/api/v3/store/credit_cards_controller.rbdeleted file mode 100644index 7da2f0da8a..0000000000--- a/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb+++ /dev/null@@ -1,29 +0,0 @@-module Spree- module Api- module V3- module Store- class CreditCardsController < ResourceController- prepend_before_action :require_authentication!-- protected-- def set_parent- @parent = current_user- end-- def parent_association- :credit_cards- end-- def model_class- Spree::CreditCard- end-- def serializer_class- Spree.api.credit_card_serializer- end- end- end- end- end-enddiff --git a/api/app/controllers/spree/api/v3/store/customer/account_controller.rb b/api/app/controllers/spree/api/v3/store/customer/account_controller.rbnew file mode 100644index 0000000000..713ae4c567--- /dev/null+++ b/api/app/controllers/spree/api/v3/store/customer/account_controller.rb@@ -0,0 +1,37 @@+module Spree+ module Api+ module V3+ module Store+ module Customer+ class AccountController < Store::BaseController+ prepend_before_action :require_authentication!++ # GET /api/v3/store/customer+ def show+ render json: serialize_resource(current_user)+ end++ # PATCH /api/v3/store/customer+ def update+ if current_user.update(permitted_params)+ render json: serialize_resource(current_user)+ else+ render_errors(current_user.errors)+ end+ end++ protected++ def serializer_class+ Spree.api.user_serializer+ end++ def permitted_params+ params.require(:user).permit(Spree::PermittedAttributes.user_attributes)+ end+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v3/store/customer/addresses_controller.rb b/api/app/controllers/spree/api/v3/store/customer/addresses_controller.rbnew file mode 100644index 0000000000..6a1e14f670--- /dev/null+++ b/api/app/controllers/spree/api/v3/store/customer/addresses_controller.rb@@ -0,0 +1,66 @@+module Spree+ module Api+ module V3+ module Store+ module Customer+ class AddressesController < ResourceController+ prepend_before_action :require_authentication!++ # POST /api/v3/store/customer/addresses+ def create+ result = Spree.address_create_service.call(+ address_params: permitted_params,+ user: current_user+ )++ if result.success?+ render json: serialize_resource(result.value), status: :created+ else+ render_errors(result.value.errors)+ end+ end++ # PATCH /api/v3/store/customer/addresses/:id+ def update+ result = Spree.address_update_service.call(+ address: @resource,+ address_params: permitted_params+ )++ if result.success?+ render json: serialize_resource(result.value)+ else+ render_errors(result.value.errors)+ end+ end++ protected++ def set_parent+ @parent = current_user+ end++ def parent_association+ :addresses+ end++ def model_class+ Spree::Address+ end++ def serializer_class+ Spree.api.address_serializer+ end++ def permitted_params+ params.require(:address).permit(+ :firstname, :lastname, :address1, :address2, :city,+ :zipcode, :phone, :company, :country_iso, :state_abbr, :state_name+ )+ end+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb b/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rbnew file mode 100644index 0000000000..35b72f0454--- /dev/null+++ b/api/app/controllers/spree/api/v3/store/customer/credit_cards_controller.rb@@ -0,0 +1,31 @@+module Spree+ module Api+ module V3+ module Store+ module Customer+ class CreditCardsController < ResourceController+ prepend_before_action :require_authentication!++ protected++ def set_parent+ @parent = current_user+ end++ def parent_association+ :credit_cards+ end++ def model_class+ Spree::CreditCard+ end++ def serializer_class+ Spree.api.credit_card_serializer+ end+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v3/store/customer/payment_sources_controller.rb b/api/app/controllers/spree/api/v3/store/customer/payment_sources_controller.rbnew file mode 100644index 0000000000..1eede5520a--- /dev/null+++ b/api/app/controllers/spree/api/v3/store/customer/payment_sources_controller.rb@@ -0,0 +1,31 @@+module Spree+ module Api+ module V3+ module Store+ module Customer+ class PaymentSourcesController < ResourceController+ prepend_before_action :require_authentication!++ protected++ def set_parent+ @parent = current_user+ end++ def parent_association+ :wallet_payment_sources+ end++ def model_class+ Spree::WalletPaymentSource+ end++ def serializer_class+ Spree.api.wallet_payment_source_serializer+ end+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v3/store/customers_controller.rb b/api/app/controllers/spree/api/v3/store/customers_controller.rbdeleted file mode 100644index 68f184401c..0000000000--- a/api/app/controllers/spree/api/v3/store/customers_controller.rb+++ /dev/null@@ -1,35 +0,0 @@-module Spree- module Api- module V3- module Store- class CustomersController < Store::BaseController- prepend_before_action :require_authentication!-- # GET /api/v3/store/customers/me- def show- render json: serialize_resource(current_user)- end-- # PATCH /api/v3/store/customers/me- def update- if current_user.update(permitted_params)- render json: serialize_resource(current_user)- else- render_errors(current_user.errors)- end- end-- protected-- def serializer_class- Spree.api.user_serializer- end-- def permitted_params- params.require(:user).permit(Spree::PermittedAttributes.user_attributes)- end- end- end- end- end-enddiff --git a/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb b/api/app/controllers/spree/api/v3/store/payment_sources_controller.rbdeleted file mode 100644index 15116cc22e..0000000000--- a/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb+++ /dev/null@@ -1,25 +0,0 @@-module Spree- module Api- module V3- module Store- class PaymentSourcesController < ResourceController- prepend_before_action :require_authentication!-- protected-- def scope- current_user.payment_sources- end-- def model_class- Spree::PaymentSource- end-- def serializer_class- Spree.api.payment_source_serializer- end- end- end- end- end-enddiff --git a/api/config/routes.rb b/api/config/routes.rbindex eab4a2e72a..fab2d42799 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -25,7 +25,10 @@ Spree::Core::Engine.add_routes doresources :products, only: [:index], controller: 'taxons/products'end- # Orders+ # Cart - alias for current incomplete order (creates if none exists)+ get 'cart', to: 'cart#show'++ # Orders - all orders (complete and incomplete)resources :orders domember do# State transitions@@ -44,11 +47,13 @@ Spree::Core::Engine.add_routes doend# Customer- get 'customers/me', to: 'customers#show'- patch 'customers/me', to: 'customers#update'- resources :addresses, only: [:index, :show, :create, :update, :destroy], path: 'customers/me/addresses'- resources :credit_cards, only: [:index, :show, :destroy], path: 'customers/me/credit_cards'- resources :payment_sources, only: [:index, :show, :destroy], path: 'customers/me/payment_sources'+ namespace :customer, path: 'customer' do+ get '/', to: 'account#show'+ patch '/', to: 'account#update'+ resources :addresses, only: [:index, :show, :create, :update, :destroy]+ resources :credit_cards, only: [:index, :show, :destroy]+ resources :payment_sources, only: [:index, :show, :destroy]+ end# Wishlistsresources :wishlists dodiff --git a/api/spec/controllers/spree/api/v3/store/customer/account_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customer/account_controller_spec.rbnew file mode 100644index 0000000000..261ef54938--- /dev/null+++ b/api/spec/controllers/spree/api/v3/store/customer/account_controller_spec.rb@@ -0,0 +1,117 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Store::Customer::AccountController, type: :controller do+ render_views++ include_context 'API v3 Store'++ before do+ request.headers['X-Spree-Api-Key'] = api_key.token+ request.headers['Authorization'] = "Bearer #{jwt_token}"+ end++ describe 'GET #show' do+ it 'returns the current user' do+ get :show++ expect(response).to have_http_status(:ok)+ expect(json_response['id']).to eq(user.prefix_id)+ expect(json_response['email']).to eq(user.email)+ end++ it 'returns user attributes' do+ get :show++ expect(json_response).to include('id', 'email', 'first_name', 'last_name')+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :show++ expect(response).to have_http_status(:unauthorized)+ expect(json_response['error']['code']).to eq('authentication_required')+ end+ end++ context 'with invalid token' do+ before { request.headers['Authorization'] = 'Bearer invalid' }++ it 'returns unauthorized' do+ get :show++ expect(response).to have_http_status(:unauthorized)+ expect(json_response['error']['code']).to eq('authentication_required')+ end+ end++ context 'with expired token' do+ let(:expired_token) { Spree::Api::V3::TestingSupport.generate_jwt(user, expiration: -1.hour.to_i) }++ before { request.headers['Authorization'] = "Bearer #{expired_token}" }++ it 'returns unauthorized' do+ get :show++ expect(response).to have_http_status(:unauthorized)+ expect(json_response['error']['code']).to eq('authentication_required')+ end+ end+ end++ describe 'PATCH #update' do+ it 'updates the current user first name' do+ patch :update, params: { user: { first_name: 'Updated' } }++ expect(response).to have_http_status(:ok)+ expect(user.reload.first_name).to eq('Updated')+ end++ it 'updates multiple fields' do+ patch :update, params: { user: { first_name: 'John', last_name: 'Doe' } }++ expect(response).to have_http_status(:ok)+ user.reload+ expect(user.first_name).to eq('John')+ expect(user.last_name).to eq('Doe')+ end++ it 'returns updated user data' do+ patch :update, params: { user: { first_name: 'Updated' } }++ expect(json_response['first_name']).to eq('Updated')+ end++ context 'validation errors' do+ it 'returns errors for blank email' do+ patch :update, params: { user: { email: '' } }++ expect(response).to have_http_status(:unprocessable_entity)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['details']['email']).to be_present+ end++ it 'returns errors for duplicate email' do+ other_user = create(:user)+ patch :update, params: { user: { email: other_user.email } }++ expect(response).to have_http_status(:unprocessable_entity)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['details']['email']).to be_present+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ patch :update, params: { user: { first_name: 'Updated' } }++ expect(response).to have_http_status(:unauthorized)+ expect(json_response['error']['code']).to eq('authentication_required')+ end+ end+ end+enddiff --git a/api/spec/controllers/spree/api/v3/store/customer/addresses_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customer/addresses_controller_spec.rbnew file mode 100644index 0000000000..a65253d5bd--- /dev/null+++ b/api/spec/controllers/spree/api/v3/store/customer/addresses_controller_spec.rb@@ -0,0 +1,209 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Store::Customer::AddressesController, type: :controller do+ render_views++ include_context 'API v3 Store'++ let(:country) { create(:country) }+ let(:state) { create(:state, country: country) }+ let!(:address) { create(:address, user: user, country: country, state: state) }++ before do+ request.headers['X-Spree-Api-Key'] = api_key.token+ request.headers['Authorization'] = "Bearer #{jwt_token}"+ end++ describe 'GET #index' do+ it 'returns the user addresses' do+ get :index++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].length).to eq(user.addresses.count)+ end++ it 'returns address attributes' do+ get :index++ address_data = json_response['data'].first+ expect(address_data).to include('id', 'firstname', 'lastname', 'address1', 'city', 'zipcode')+ end++ it 'only returns addresses belonging to the current user' do+ other_user = create(:user)+ other_address = create(:address, user: other_user)++ get :index++ ids = json_response['data'].map { |a| a['id'] }+ expect(ids).to include(address.prefix_id)+ expect(ids).not_to include(other_address.prefix_id)+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :index++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'GET #show' do+ it 'returns the address' do+ get :show, params: { id: address.prefix_id }++ expect(response).to have_http_status(:ok)+ expect(json_response['id']).to eq(address.prefix_id)+ end++ it 'returns address attributes' do+ get :show, params: { id: address.prefix_id }++ expect(json_response).to include('id', 'firstname', 'lastname', 'address1', 'city', 'zipcode')+ end++ context 'when address belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_address) { create(:address, user: other_user) }++ it 'returns not found' do+ get :show, params: { id: other_address.prefix_id }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :show, params: { id: address.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'POST #create' do+ let(:address_params) do+ {+ firstname: 'John',+ lastname: 'Doe',+ address1: '123 Main St',+ city: 'New York',+ zipcode: '10001',+ phone: '555-1234',+ country_iso: country.iso,+ state_abbr: state.abbr+ }+ end++ it 'creates a new address' do+ expect {+ post :create, params: { address: address_params }+ }.to change { user.addresses.count }.by(1)++ expect(response).to have_http_status(:created)+ end++ it 'returns the created address' do+ post :create, params: { address: address_params }++ expect(json_response['firstname']).to eq('John')+ expect(json_response['lastname']).to eq('Doe')+ expect(json_response['address1']).to eq('123 Main St')+ end++ context 'with invalid params' do+ it 'returns validation errors for missing firstname' do+ post :create, params: { address: address_params.except(:firstname) }++ expect(response).to have_http_status(:unprocessable_entity)+ expect(json_response['error']['code']).to eq('validation_error')+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ post :create, params: { address: address_params }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'PATCH #update' do+ it 'updates the address' do+ patch :update, params: { id: address.prefix_id, address: { firstname: 'Updated' } }++ expect(response).to have_http_status(:ok)+ expect(address.reload.firstname).to eq('Updated')+ end++ it 'returns the updated address' do+ patch :update, params: { id: address.prefix_id, address: { firstname: 'Updated' } }++ expect(json_response['firstname']).to eq('Updated')+ end++ context 'when address belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_address) { create(:address, user: other_user) }++ it 'returns not found' do+ patch :update, params: { id: other_address.prefix_id, address: { firstname: 'Hacker' } }++ expect(response).to have_http_status(:not_found)+ expect(other_address.reload.firstname).not_to eq('Hacker')+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ patch :update, params: { id: address.prefix_id, address: { firstname: 'Updated' } }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'DELETE #destroy' do+ it 'deletes the address' do+ expect {+ delete :destroy, params: { id: address.prefix_id }+ }.to change { user.addresses.count }.by(-1)++ expect(response).to have_http_status(:no_content)+ end++ context 'when address belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_address) { create(:address, user: other_user) }++ it 'returns not found' do+ expect {+ delete :destroy, params: { id: other_address.prefix_id }+ }.not_to change { Spree::Address.count }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ delete :destroy, params: { id: address.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end+enddiff --git a/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rbnew file mode 100644index 0000000000..bdf901cdc2--- /dev/null+++ b/api/spec/controllers/spree/api/v3/store/customer/credit_cards_controller_spec.rb@@ -0,0 +1,114 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Store::Customer::CreditCardsController, type: :controller do+ render_views++ include_context 'API v3 Store'++ let!(:credit_card) { create(:credit_card, user: user) }++ before do+ request.headers['X-Spree-Api-Key'] = api_key.token+ request.headers['Authorization'] = "Bearer #{jwt_token}"+ end++ describe 'GET #index' do+ it 'returns the user credit cards' do+ get :index++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].length).to eq(user.credit_cards.count)+ end++ it 'returns credit card attributes' do+ get :index++ card_data = json_response['data'].first+ expect(card_data).to include('id', 'last_digits', 'month', 'year', 'cc_type')+ end++ it 'only returns credit cards belonging to the current user' do+ other_user = create(:user)+ other_card = create(:credit_card, user: other_user)++ get :index++ ids = json_response['data'].map { |c| c['id'] }+ expect(ids).to include(credit_card.prefix_id)+ expect(ids).not_to include(other_card.prefix_id)+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :index++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'GET #show' do+ it 'returns the credit card' do+ get :show, params: { id: credit_card.prefix_id }++ expect(response).to have_http_status(:ok)+ expect(json_response['id']).to eq(credit_card.prefix_id)+ end++ context 'when credit card belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_card) { create(:credit_card, user: other_user) }++ it 'returns not found' do+ get :show, params: { id: other_card.prefix_id }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :show, params: { id: credit_card.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'DELETE #destroy' do+ it 'deletes the credit card' do+ expect {+ delete :destroy, params: { id: credit_card.prefix_id }+ }.to change { user.credit_cards.count }.by(-1)++ expect(response).to have_http_status(:no_content)+ end++ context 'when credit card belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_card) { create(:credit_card, user: other_user) }++ it 'returns not found' do+ expect {+ delete :destroy, params: { id: other_card.prefix_id }+ }.not_to change { Spree::CreditCard.count }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ delete :destroy, params: { id: credit_card.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end+enddiff --git a/api/spec/controllers/spree/api/v3/store/customer/payment_sources_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customer/payment_sources_controller_spec.rbnew file mode 100644index 0000000000..63e64cbf65--- /dev/null+++ b/api/spec/controllers/spree/api/v3/store/customer/payment_sources_controller_spec.rb@@ -0,0 +1,118 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Store::Customer::PaymentSourcesController, type: :controller do+ render_views++ include_context 'API v3 Store'++ let(:credit_card) { create(:credit_card, user: user) }+ let!(:payment_source) { create(:wallet_payment_source, user: user, payment_source: credit_card) }++ before do+ request.headers['X-Spree-Api-Key'] = api_key.token+ request.headers['Authorization'] = "Bearer #{jwt_token}"+ end++ describe 'GET #index' do+ it 'returns the user wallet payment sources' do+ get :index++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].length).to eq(user.wallet_payment_sources.count)+ end++ it 'returns payment source attributes' do+ get :index++ source_data = json_response['data'].first+ expect(source_data).to include('id', 'default')+ end++ it 'only returns payment sources belonging to the current user' do+ other_user = create(:user)+ other_card = create(:credit_card, user: other_user)+ other_source = create(:wallet_payment_source, user: other_user, payment_source: other_card)++ get :index++ ids = json_response['data'].map { |s| s['id'] }+ expect(ids).to include(payment_source.prefix_id)+ expect(ids).not_to include(other_source.prefix_id)+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :index++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'GET #show' do+ it 'returns the payment source' do+ get :show, params: { id: payment_source.prefix_id }++ expect(response).to have_http_status(:ok)+ expect(json_response['id']).to eq(payment_source.prefix_id)+ end++ context 'when payment source belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_card) { create(:credit_card, user: other_user) }+ let(:other_source) { create(:wallet_payment_source, user: other_user, payment_source: other_card) }++ it 'returns not found' do+ get :show, params: { id: other_source.prefix_id }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ get :show, params: { id: payment_source.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end++ describe 'DELETE #destroy' do+ it 'deletes the payment source' do+ expect {+ delete :destroy, params: { id: payment_source.prefix_id }+ }.to change { user.wallet_payment_sources.count }.by(-1)++ expect(response).to have_http_status(:no_content)+ end++ context 'when payment source belongs to another user' do+ let(:other_user) { create(:user) }+ let(:other_card) { create(:credit_card, user: other_user) }+ let(:other_source) { create(:wallet_payment_source, user: other_user, payment_source: other_card) }++ it 'returns not found' do+ expect {+ delete :destroy, params: { id: other_source.prefix_id }+ }.not_to change { Spree::WalletPaymentSource.count }++ expect(response).to have_http_status(:not_found)+ end+ end++ context 'without authentication' do+ before { request.headers['Authorization'] = nil }++ it 'returns unauthorized' do+ delete :destroy, params: { id: payment_source.prefix_id }++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end+enddiff --git a/sdk/src/client.ts b/sdk/src/client.tsindex f5cc2a93b4..b5566a6510 100644--- a/sdk/src/client.ts+++ b/sdk/src/client.ts@@ -157,15 +157,13 @@ export class SpreeClient {* Register a new customer account*/register: (params: RegisterParams): Promise<AuthTokens> =>- this.request<AuthTokens>('POST', '/auth/register', { body: params }),+ this.request<AuthTokens>('POST', '/auth/register', { body: { user: params } }),/**- * Refresh access token using refresh token+ * Refresh access token (requires valid Bearer token)*/- refresh: (refreshToken: string): Promise<AuthTokens> =>- this.request<AuthTokens>('POST', '/auth/refresh', {- body: { refresh_token: refreshToken },- }),+ refresh: (options: RequestOptions): Promise<AuthTokens> =>+ this.request<AuthTokens>('POST', '/auth/refresh', options),};// ============================================@@ -326,7 +324,26 @@ export class SpreeClient {};// ============================================- // Orders (Cart & Checkout)+ // Cart (convenience wrapper for current incomplete order)+ // ============================================++ readonly cart = {+ /**+ * Get current cart (returns null if none exists)+ * Pass orderToken for guest checkout, or use JWT for authenticated users+ */+ get: (options?: RequestOptions): Promise<StoreOrder & { token: string }> =>+ this.request<StoreOrder & { token: string }>('GET', '/cart', options),++ /**+ * Create a new cart (alias for orders.create)+ */+ create: (options?: RequestOptions): Promise<StoreOrder & { token: string }> =>+ this.request<StoreOrder & { token: string }>('POST', '/orders', options),+ };++ // ============================================+ // Orders (all orders - complete and incomplete)// ============================================readonly orders = {@@ -521,7 +538,7 @@ export class SpreeClient {* Get current customer profile*/get: (options?: RequestOptions): Promise<StoreUser> =>- this.request<StoreUser>('GET', '/customers/me', options),+ this.request<StoreUser>('GET', '/customer', options),/*** Update current customer profile@@ -530,7 +547,7 @@ export class SpreeClient {params: { first_name?: string; last_name?: string; email?: string },options?: RequestOptions): Promise<StoreUser> =>- this.request<StoreUser>('PATCH', '/customers/me', {+ this.request<StoreUser>('PATCH', '/customer', {...options,body: { user: params },}),@@ -548,7 +565,7 @@ export class SpreeClient {): Promise<PaginatedResponse<StoreAddress>> =>this.request<PaginatedResponse<StoreAddress>>('GET',- '/customers/me/addresses',+ '/customer/addresses',{ ...options, params: params as Record<string, string | number | undefined> }),@@ -556,7 +573,7 @@ export class SpreeClient {* Get an address by ID*/get: (id: string, options?: RequestOptions): Promise<StoreAddress> =>- this.request<StoreAddress>('GET', `/customers/me/addresses/${id}`, options),+ this.request<StoreAddress>('GET', `/customer/addresses/${id}`, options),/*** Create an address@@ -565,7 +582,7 @@ export class SpreeClient {address: Omit<StoreAddress, 'id'>,options?: RequestOptions): Promise<StoreAddress> =>- this.request<StoreAddress>('POST', '/customers/me/addresses', {+ this.request<StoreAddress>('POST', '/customer/addresses', {...options,body: { address },}),@@ -578,7 +595,7 @@ export class SpreeClient {address: Partial<Omit<StoreAddress, 'id'>>,options?: RequestOptions): Promise<StoreAddress> =>- this.request<StoreAddress>('PATCH', `/customers/me/addresses/${id}`, {+ this.request<StoreAddress>('PATCH', `/customer/addresses/${id}`, {...options,body: { address },}),@@ -587,7 +604,7 @@ export class SpreeClient {* Delete an address*/delete: (id: string, options?: RequestOptions): Promise<void> =>- this.request<void>('DELETE', `/customers/me/addresses/${id}`, options),+ this.request<void>('DELETE', `/customer/addresses/${id}`, options),},};diff --git a/sdk/src/types/index.ts b/sdk/src/types/index.tsindex 87bf0865bb..ece03c4160 100644--- a/sdk/src/types/index.ts+++ b/sdk/src/types/index.ts@@ -24,10 +24,13 @@ export interface ErrorResponse {// Auth typesexport interface AuthTokens {- access_token: string;- refresh_token: string;- token_type: 'Bearer';- expires_in: number;+ token: string;+ user: {+ id: string;+ email: string;+ first_name: string | null;+ last_name: string | null;+ };}export interface LoginCredentials {