Class: Effective::OrdersController

Inherits:
ApplicationController
  • Object
show all
Includes:
Providers::AppCheckout, Providers::Ccbill, Providers::Cheque, Providers::Moneris, Providers::Paypal, Providers::Pretend, Providers::Stripe, Providers::StripeConnect, EffectiveCartsHelper
Defined in:
app/controllers/effective/orders_controller.rb

Instance Method Summary collapse

Methods included from Providers::Pretend

#pretend_purchase

Methods included from Providers::AppCheckout

#app_checkout

Methods included from Providers::Ccbill

#ccbill_postback

Methods included from Providers::StripeConnect

#stripe_connect_redirect_uri

Methods included from Providers::Stripe

#stripe_charge

Methods included from Providers::Paypal

#paypal_postback

Methods included from Providers::Moneris

#moneris_postback

Methods included from Providers::Cheque

#pay_by_cheque

Methods included from EffectiveCartsHelper

#current_cart, #link_to_add_to_cart, #link_to_checkout, #link_to_current_cart, #link_to_empty_cart, #link_to_remove_from_cart, #render_cart, #render_purchasables

Instance Method Details

#createObject



67
68
69
70
# File 'app/controllers/effective/orders_controller.rb', line 67

def create
  @order ||= Effective::Order.new(current_cart, user: current_user)
  save_order_and_redirect_to_step2
end

#declinedObject

An error has occurred, please try again



174
175
176
177
178
179
# File 'app/controllers/effective/orders_controller.rb', line 174

def declined # An error occurred!
  @order = Effective::Order.find(params[:id])
  EffectiveOrders.authorized?(self, :show, @order)

  redirect_to(effective_orders.order_path(@order)) unless @order.declined?
end

#editObject



59
60
61
62
63
64
65
# File 'app/controllers/effective/orders_controller.rb', line 59

def edit
  @order ||= Effective::Order.find(params[:id])

  EffectiveOrders.authorized?(self, :edit, @order)

  render :checkout_step1
end

#indexObject



135
136
137
138
139
140
# File 'app/controllers/effective/orders_controller.rb', line 135

def index
  @orders = Effective::Order.purchased_by(current_user)
  @pending_orders = Effective::Order.pending.where(user: current_user)

  EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
end

#my_purchasesObject

Basically an index page. Purchases is an Order History page. List of purchased orders



144
145
146
147
148
# File 'app/controllers/effective/orders_controller.rb', line 144

def my_purchases
  @orders = Effective::Order.purchased_by(current_user)

  EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
end

#my_salesObject

Sales is a list of what products beign sold by me have been purchased



151
152
153
154
# File 'app/controllers/effective/orders_controller.rb', line 151

def my_sales
  @order_items = Effective::OrderItem.sold_by(current_user)
  EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
end

#newObject

This is the entry point for the “Checkout” buttons



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/effective/orders_controller.rb', line 33

def new
  @order ||= Effective::Order.new(current_cart, user: current_user)

  EffectiveOrders.authorized?(self, :new, @order)

  # We're only going to check for a subset of errors on this step,
  # with the idea that we don't want to create an Order object if the Order is totally invalid
  @order.valid?

  if @order.errors[:order_items].present?
    flash[:danger] = @order.errors[:order_items].first
    redirect_to(effective_orders.cart_path)
    return
  elsif @order.errors[:total].present?
    flash[:danger] = @order.errors[:total].first.gsub(EffectiveOrders.minimum_charge.to_i.to_s, view_context.price_to_currency(EffectiveOrders.minimum_charge.to_i))
    redirect_to(effective_orders.cart_path)
    return
  end

  @order.errors.clear
  @order.billing_address.try(:errors).try(:clear)
  @order.shipping_address.try(:errors).try(:clear)

  render :checkout_step1
end

#purchasedObject

Thank you for Purchasing this Order. This is where a successfully purchased order ends up



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/effective/orders_controller.rb', line 157

def purchased # Thank You!
  @order = if params[:id].present?
    Effective::Order.find(params[:id])
  elsif current_user.present?
    Effective::Order.purchased_by(current_user).first
  end

  if @order.blank?
    redirect_to(effective_orders.my_purchases_path) and return
  end

  EffectiveOrders.authorized?(self, :show, @order)

  redirect_to(effective_orders.order_path(@order)) unless @order.purchased?
end

#resend_buyer_receiptObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/effective/orders_controller.rb', line 181

def resend_buyer_receipt
  @order = Effective::Order.find(params[:id])
  EffectiveOrders.authorized?(self, :show, @order)

  if @order.send_order_receipt_to_buyer!
    flash[:success] = "Successfully sent receipt to #{@order.user.email}"
  else
    flash[:danger] = "Unable to send receipt."
  end

  redirect_to(request.referer.present? ? :back : effective_orders.order_path(@order))
end

#save_order_and_redirect_to_step2Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/effective/orders_controller.rb', line 78

def save_order_and_redirect_to_step2
  (redirect_to effective_orders.cart_path and return) if (@order.blank? || current_user.blank?)

  @order.attributes = order_params
  @order.user_id = current_user.id

  EffectiveOrders.authorized?(self, (@order.persisted? ? :update : :create), @order)

  @order.valid?  # This makes sure the correct shipping_address is copied from billing_address if shipping_address_same_as_billing

  Effective::Order.transaction do
    begin
      if @order.save_billing_address? && @order.user.respond_to?(:billing_address=) && @order.billing_address.present?
        @order.user.billing_address = @order.billing_address
      end

      if @order.save_shipping_address? && @order.user.respond_to?(:shipping_address=) && @order.shipping_address.present?
        @order.user.shipping_address = @order.shipping_address
      end

      @order.save!

      if @order.total == 0 && EffectiveOrders.allow_free_orders
        order_purchased(details: 'automatic purchase of free order', provider: 'free', card: 'none')
      else
        redirect_to(effective_orders.order_path(@order))  # This goes to checkout_step2
      end

      return true
    rescue => e
      Rails.logger.info e.message
      flash.now[:danger] = "Unable to save order: #{@order.errors.full_messages.to_sentence}.  Please try again."
      raise ActiveRecord::Rollback
    end
  end

  # Fall back to checkout step 1
  render :checkout_step1
end

#showObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/effective/orders_controller.rb', line 118

def show
  @order = Effective::Order.find(params[:id])
  EffectiveOrders.authorized?(self, :show, @order)

  @page_title ||= (
    if @order.purchased?
      'Receipt'
    elsif @order.user != current_user
      @order.pending? ? "Pending Order ##{@order.to_param}" : "Order ##{@order.to_param}"
    else
      'Checkout'
    end
  )

  render(:checkout_step2) if @order.purchased? == false && @order.user == current_user
end

#updateObject

If there is an existing order, it will be posted to the /update action, instead of /create



73
74
75
76
# File 'app/controllers/effective/orders_controller.rb', line 73

def update
  @order ||= Effective::Order.find(params[:id])
  save_order_and_redirect_to_step2
end