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



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

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



170
171
172
173
174
175
# File 'app/controllers/effective/orders_controller.rb', line 170

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



51
52
53
54
55
56
57
# File 'app/controllers/effective/orders_controller.rb', line 51

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

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

  render :checkout_step1
end

#indexObject



131
132
133
134
135
136
# File 'app/controllers/effective/orders_controller.rb', line 131

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



140
141
142
143
144
# File 'app/controllers/effective/orders_controller.rb', line 140

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



147
148
149
150
# File 'app/controllers/effective/orders_controller.rb', line 147

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/effective/orders_controller.rb', line 25

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/effective/orders_controller.rb', line 153

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



177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/effective/orders_controller.rb', line 177

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



70
71
72
73
74
75
76
77
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
# File 'app/controllers/effective/orders_controller.rb', line 70

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

  if EffectiveOrders.require_shipping_address
    if @order.shipping_address_same_as_billing? && @order.billing_address.present?
      @order.shipping_address = @order.billing_address
    end
  end

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

  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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/effective/orders_controller.rb', line 114

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



65
66
67
68
# File 'app/controllers/effective/orders_controller.rb', line 65

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