We use this but needs a lot of cleanup.
[17:16:51] $ cat app/controllers/coupon_controller.rb
class CouponController < Spree::Api::V1::BaseController
# before_filter :authorize_read!
def show
end
def search
@orders = Order.ransack(params[:q]).
result.page(params[:page])
render :index
end
def check
@coupon_code = params[:id]
if Spree::Promotion.exists?(:code => @coupon_code)
puts 'code:'+@coupon_code
render :text => "OK", :status => 200
else
puts 'code failed:'+@coupon_code
render :text => "FAIL", :status => 422
end
end
def update
@order = Order.find_by_number!(params[:id])
if @order.update_attributes(params[:order])
if @order.coupon_code.present?
if apply_coupon_code
# flash[:notice] = t(:coupon_code_applied)
else
# flash[:error] = t(:promotion_not_found)
render respond_with(@order) and return
end
end
@order.line_items = @order.line_items.select {|li| li.quantity > 0 }
fire_event('spree.order.contents_changed')
respond_with(@order) { |format| format.html { redirect_to cart_path } }
else
respond_with(@order)
end
end
def apply_coupon_code
return if @order.coupon_code.blank?
if Spree::Promotion.exists?(:code => @order.coupon_code)
fire_event('spree.checkout.coupon_code_added', :coupon_code => @order.coupon_code)
true
end
end
end
17:23:41] $ cat app/controllers/spree/api/v1/orders_controller_decorator.rb
Spree::Api::V1::OrdersController.class_eval do
# include Spree::Core::ControllerHelpers
def update
# @order = Order.find_by_number!(params[:id])
puts "coupon: step 1"
if @order.update_attributes(params[:order])
puts "coupon: step 2"
if @order.coupon_code.present?
puts "coupon: step 3"
if apply_coupon_code
puts "coupon: step 4a"
# flash[:notice] = t(:coupon_code_applied)
else
puts "coupon: step 4b"
#flash[:error] = t(:promotion_not_found)
#render :edit and return
end
end
@order.line_items = @order.line_items.select {|li| li.quantity > 0 }
fire_event('spree.order.contents_changed')
puts "coupon: step 5"
render :show, :status => 200
else
render :show, :status => 422
end
end
def apply_coupon_code
puts "coupon: step 6"
return if @order.coupon_code.blank?
if Spree::Promotion.exists?(:code => @order.coupon_code)
puts "coupon: step 7"
fire_event('spree.checkout.coupon_code_added', :coupon_code => @order.coupon_code)
true
end
end
def fire_event(name, extra_payload = {})
ActiveSupport::Notifications.instrument(name, default_notification_payload.merge(extra_payload))
end
# Creates the hash that is sent as the payload for all notifications. Specific notifications will
# add additional keys as appropriate. Override this method if you need additional data when
# responding to a notification
def default_notification_payload
{:user => try_spree_current_user, :order => @order}
end
def try_spree_current_user
respond_to?(:spree_current_user) ? spree_current_user : nil
end
end
cat app/models/spree/order_decorator.rb
Spree::Order.class_eval do
checkout_flow do
# go_to_state :address, :if => lambda { |order| order.payment_required? }
go_to_state :address
go_to_state :payment, :if => lambda { |order| order.payment_required? }
go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
go_to_state :complete
end
# If true, causes the payment step to happen during the checkout process
def payment_required?
return false
end
# If true, causes the confirmation step to happen during the checkout process
def confirmation_required?
return false
end
# Finalizes an in progress order after checkout is complete.
# Called after transition to complete state when payments will
# have been processed
def finalize!
touch :completed_at
InventoryUnit.assign_opening_inventory(self)
# lock all adjustments (coupon promotions, etc.)
adjustments.each { |adjustment| adjustment.update_column('locked', true) }
# update payment and shipment(s) states, and save
updater = OrderUpdater.new(self)
updater.update_payment_state
shipments.each { |shipment| shipment.update!(self) }
updater.update_shipment_state
save
#FIXME do not send the order confirmation to the user
# deliver_order_confirmation_email
self.state_changes.create({
:previous_state => 'cart',
:next_state => 'complete',
:name => 'order' ,
:user_id => self.user_id
}, :without_protection => true)
end
def deliver_order_confirmation_email
begin
OrderMailer.confirm_email(self).deliver
rescue Exception => e
logger.error(e.backtrace * "\n")
end
end
end