Skip to content

Instantly share code, notes, and snippets.

@cmaujean
Created July 26, 2011 19:42
Show Gist options
  • Save cmaujean/1107785 to your computer and use it in GitHub Desktop.
Save cmaujean/1107785 to your computer and use it in GitHub Desktop.
State machine overrides
>> Gateway.current.payment_profiles_supported?
=> false
>> Gateway.all
=> [#<Gateway::AuthorizeNetCim id: 3, type: "Gateway::AuthorizeNetCim", name: "Credit Card", description: "Authorize.net CIM ", active: true, environment: "production", created_at: "2010-03-08 20:29:39", updated_at: "2011-07-26 18:27:11", deleted_at: nil, display_on: "">]
>> Gateway.current
=> #<PaymentMethod::Check id: 4, type: "PaymentMethod::Check", name: "COD", description: "Cash on delivery.", active: true, environment: "production", created_at: "2010-07-21 20:03:14", updated_at: "2010-10-15 09:09:59", deleted_at: nil, display_on: "">
>>
state_machine :initial => 'cart', :use_transactions => false do
event :next do
transition :from => 'cart', :to => 'address'
transition :from => 'address', :to => 'delivery'
transition :from => 'delivery', :to => 'payment'
transition :from => 'confirm', :to => 'complete'
# note: some payment methods will not support a confirm step
transition :from => 'payment', :to => 'confirm',
:if => Proc.new { Gateway.current && Gateway.current.payment_profiles_supported? }
transition :from => 'payment', :to => 'complete'
transition :from => 'complete', :to => 'pending'
transition :from => 'pending', :to => 'picking'
transition :from => 'picking', :to => 'picked'
end
event :cancel do
transition :to => 'canceled', :if => :allow_cancel?
end
event :return do
transition :to => 'returned', :from => 'awaiting_return'
end
event :resume do
transition :to => 'resumed', :from => 'canceled', :if => :allow_resume?
end
event :authorize_return do
transition :to => 'awaiting_return'
end
event :pending do
transition :to => 'pending', :from => ['picking', 'complete']
end
event :picking do
transition :to => 'picking', :from => ['pending', 'picked']
end
event :picked do
transition :to => 'picked', :from => ['picking']
end
before_transition :to => 'complete' do |order|
begin
order.process_payments!
rescue Spree::GatewayError
if Spree::Config[:allow_checkout_on_gateway_error]
true
else
false
end
end
end
after_transition :to => 'complete', :do => :finalize!
after_transition :to => 'delivery', :do => :create_tax_charge!
after_transition :to => 'payment', :do => :create_shipment!
after_transition :to => 'canceled', :do => :after_cancel
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment