Created
November 9, 2012 17:09
-
-
Save BDQ/4046892 to your computer and use it in GitHub Desktop.
Spree API patches to allow 'checkout'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#order model decorator | |
Order.class_eval do | |
accepts_nested_attributes_for :line_items, :allow_destroy => true | |
def self.build_from_api(user, params) | |
order = create | |
params[:line_items_attributes].each do |line_item| | |
order.add_variant(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity]) | |
end | |
if params.key?(:email) | |
password_seed = SecureRandom.base64(28) | |
password = (0...20).map{ password_seed[rand(password_seed.length)] }.join | |
user = Spree::User.find_or_create_by_email(params[:email], | |
:login => params[:email], | |
:password => password, | |
:password_confirmation => password) | |
end | |
order.user = user | |
order.email = user.email | |
order.build_ship_address(params['ship_address_attributes']) if params.key? 'ship_address_attributes' | |
order.build_bill_address(params['bill_address_attributes']) if params.key? 'bill_address_attributes' | |
if order.line_items.present? && order.ship_address.present? && order.bill_address.present? && order.valid? | |
order.shipping_method = order.available_shipping_methods(:front_end).first | |
until order.payment? | |
order.next! | |
end | |
end | |
order | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#api controller decorator | |
Spree::Api::V1::PaymentsController.class_eval do | |
def create | |
# one of my uglier hacks to fix issue where failed | |
# payments are reverting to 'checkout' state instead | |
# of correctly staying 'failed' due to a ROLLBACK | |
# cause unknown (me think's core issues). | |
# | |
@bad_payments = @order.payments.select &:checkout? | |
if @bad_payments.present? | |
@bad_payments.each {|p| p.destroy } | |
@order.reload | |
end | |
# build as normal | |
@payment = @order.payments.build(params[:payment]) | |
if @payment.save | |
# try to progress order to completed state. | |
if @order.next | |
@order.reload #gets most uptodate payment_state | |
end | |
render :create, :status => 201 | |
else | |
invalid_resource!(@payment) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Post to OrderController with everything except the payment.
Then post to Payment