Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace order simulator with something simpler #82

Merged
merged 2 commits into from
Aug 4, 2020

Conversation

seand7565
Copy link
Contributor

No description provided.

@seand7565 seand7565 force-pushed the fix_order_simulator branch 4 times, most recently from eeea785 to 1978e5d Compare July 30, 2020 20:55
@seand7565 seand7565 changed the title First pass at transaction rollbacks to replace order simulator Replace order simulator with something simpler Jul 31, 2020
@seand7565 seand7565 marked this pull request as ready for review July 31, 2020 12:29
@seand7565 seand7565 requested a review from elia July 31, 2020 12:29
Copy link
Member

@elia elia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

Just a suggestion for extracting some of the inline controller code.

Comment on lines 11 to 41
@order.transaction do
build_address(params[:address])
@order.ensure_updated_shipments
@order.email = "[email protected]" unless @order.email
@order.contents.advance
@paypal_order = SolidusPaypalCommercePlatform::PaypalOrder.new(@order).to_replace_json
raise ActiveRecord::Rollback
end

render json: @paypal_order, status: :ok
end

private

def build_address(address)
country = ::Spree::Country.find_by(iso: address[:country_code])
# Also adds fake information for a few fields, so validations can run
new_ship_address = ::Spree::Address.new(
city: address[:city],
state: country.states.find_by(abbr: address[:state]),
state_name: address[:state],
zipcode: address[:postal_code],
country: country,
address1: "123 Fake St.",
phone: "123456789",
firstname: "Fake"
)

@order.update(ship_address: new_ship_address)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about moving most of this to an external object and keep the controller a little slender?

Ideally the action should look more or less like this:

@paypal_order = SolidusPaypalCommercePlatform::PaypalOrder.new(@order)

render json: @paypal_order.to_replace_json(address: params[:address])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the address update functionality to SPCP::PayPalAddress, since its responsibility is to format paypal addresses to Spree addresses. How does that look?

We were updating only the address when PayPal returns the users address,
but we also need to make sure that the shipments are relevant to the
new address also. This makes sure the shipments are regenerated when
the address changes.
Previously when we got a partial address from PayPal, we would dup
the order, and use it in its unsaved state to simulate the new
address, so nothing would be actually changed or added to the DB.
However, this does not play well with a lot of Solidus methods,
like promotion activation (because it calls `create!` which doesn't
work with unsaved objects). It was also becoming a bit too complex.

This PR moves to simplify all of that by just wrapping the address
change in an ActiveRecord transaction and rolling it back after we
get the data we need to send back to PayPal.

In essence, this will change the orders ship_address, generate new
shipments, and format all of that to be sent to PayPal, and then undo
all of the actions it just took to generate that data, leaving the order
untouched at the end.
@seand7565 seand7565 force-pushed the fix_order_simulator branch from 1978e5d to 451ea98 Compare August 4, 2020 13:02
@seand7565 seand7565 requested a review from elia August 4, 2020 13:08
Copy link
Member

@elia elia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@elia elia merged commit f5b4796 into solidusio:master Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants