From d37b7bdd491a57ba4eaebbd36b48c4b370cd3625 Mon Sep 17 00:00:00 2001 From: vassalloandrea Date: Wed, 15 Sep 2021 14:39:28 +0200 Subject: [PATCH] Update the express checkout complete respose Allow to respond with the solidus order on the express checkout complete action if the solidus_api are enabled. This is needed since the express callbacks controller create action updates the Solidus order information and those information could be useful to update the custom frontend without making another API call to Solidus. --- .../express_callbacks_controller.rb | 14 +++++++++++++- .../checkouts_controller_spec.rb | 12 ++++++------ .../express_callbacks_controller_spec.rb | 16 +++++++++++++++- spec/support/solidus.rb | 5 +++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/controllers/solidus_afterpay/express_callbacks_controller.rb b/app/controllers/solidus_afterpay/express_callbacks_controller.rb index a2aa076..ee876e7 100644 --- a/app/controllers/solidus_afterpay/express_callbacks_controller.rb +++ b/app/controllers/solidus_afterpay/express_callbacks_controller.rb @@ -24,7 +24,7 @@ def create order.next! order.next! - render json: { redirect_url: checkout_state_url(order.state) }, status: :ok + SolidusAfterpay.config.use_solidus_api ? solidus_api_response : solidus_frontend_response end def update @@ -57,5 +57,17 @@ def order def payment_method @payment_method ||= SolidusAfterpay::PaymentMethod.active.find(params[:payment_method_id]) end + + def solidus_api_response + respond_with( + order.reload, + status: :ok, + default_template: 'spree/api/orders/show' + ) + end + + def solidus_frontend_response + render json: { redirect_url: checkout_state_url(order.state) }, status: :ok + end end end diff --git a/spec/requests/solidus_afterpay/checkouts_controller_spec.rb b/spec/requests/solidus_afterpay/checkouts_controller_spec.rb index 3d26222..53d04f5 100644 --- a/spec/requests/solidus_afterpay/checkouts_controller_spec.rb +++ b/spec/requests/solidus_afterpay/checkouts_controller_spec.rb @@ -50,11 +50,11 @@ end it 'returns the order_token' do - expect(JSON.parse(response.body)['token']).to eq(order_token) + expect(json_response['token']).to eq(order_token) end it 'returns the correct params' do - expect(JSON.parse(response.body)).to include('token', 'expires', 'redirectCheckoutUrl') + expect(json_response).to include('token', 'expires', 'redirectCheckoutUrl') end context 'when no redirect URLs are passed as params' do @@ -140,7 +140,7 @@ end it 'returns a resource not found error message' do - expect(JSON.parse(response.body)['error']).to eq('The resource you were looking for could not be found.') + expect(json_response['error']).to eq('The resource you were looking for could not be found.') end end @@ -154,7 +154,7 @@ end it 'returns a resource not found error message' do - expect(JSON.parse(response.body)['error']).to eq('The resource you were looking for could not be found.') + expect(json_response['error']).to eq('The resource you were looking for could not be found.') end end @@ -202,11 +202,11 @@ end it 'returns a resource not found error message' do - expect(JSON.parse(response.body)['error']).to eq(gateway_response_message) + expect(json_response['error']).to eq(gateway_response_message) end it 'returns the error_code' do - expect(JSON.parse(response.body)['errorCode']).to eq(gateway_response_error_code) + expect(json_response['errorCode']).to eq(gateway_response_error_code) end end end diff --git a/spec/requests/solidus_afterpay/express_callbacks_controller_spec.rb b/spec/requests/solidus_afterpay/express_callbacks_controller_spec.rb index 34c30ff..8c36498 100644 --- a/spec/requests/solidus_afterpay/express_callbacks_controller_spec.rb +++ b/spec/requests/solidus_afterpay/express_callbacks_controller_spec.rb @@ -200,7 +200,11 @@ end end - context 'when the use solidus api config is set to true', use_solidus_api: true do + context( + 'when the use solidus api config is set to true', + use_solidus_api: true, + vcr: 'retrieve_configuration/valid' + ) do context 'when the user is not authorized' do it 'returns 401 status code' do request @@ -224,6 +228,16 @@ request expect(response).to have_http_status(:ok) end + + # rubocop:disable RSpec/MultipleExpectations + it 'returns the order data' do + request + expect(json_response['id']).to eq(order.id) + expect(json_response['number']).to eq(order.number) + expect(json_response['state']).to eq('confirm') + expect(json_response['payment_methods'].count).to eq(1) + end + # rubocop:enable RSpec/MultipleExpectations end end end diff --git a/spec/support/solidus.rb b/spec/support/solidus.rb index 3d6b3db..47d7864 100644 --- a/spec/support/solidus.rb +++ b/spec/support/solidus.rb @@ -1 +1,6 @@ require 'spree/testing_support/order_walkthrough' +require 'spree/api/testing_support/helpers' + +RSpec.configure do |config| + config.include Spree::Api::TestingSupport::Helpers, type: :request +end