Skip to content

Commit

Permalink
Update the express checkout complete respose
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vassalloandrea committed Sep 15, 2021
1 parent 16c6e17 commit d37b7bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
14 changes: 13 additions & 1 deletion app/controllers/solidus_afterpay/express_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions spec/requests/solidus_afterpay/checkouts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/support/solidus.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d37b7bd

Please sign in to comment.