Class: Effective::OrdersController

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

Instance Method Summary collapse

Methods included from Providers::StripeConnect

#stripe_connect_redirect_uri

Methods included from Providers::Stripe

#stripe_charge

Methods included from Providers::Refund

#refund, #refund_params

Methods included from Providers::Pretend

#pretend

Methods included from Providers::Paypal

#paypal_postback

Methods included from Providers::Moneris

#moneris_postback

Methods included from Providers::MarkAsPaid

#mark_as_paid, #mark_as_paid_params

Methods included from Providers::Free

#free, #free_params

Methods included from Providers::Cheque

#pay_by_cheque

Methods included from Providers::Ccbill

#ccbill_postback

Methods included from Providers::AppCheckout

#app_checkout

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

#bulk_send_buyer_receiptObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/effective/orders_controller.rb', line 170

def bulk_send_buyer_receipt
  @orders = Effective::Order.purchased.where(id: params[:ids])

  begin
    EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))

    @orders.each do |order|
      next unless (EffectiveOrders.authorize!(self, :show, order) rescue false)

      order.send_order_receipt_to_buyer!
    end

    render json: { status: 200, message: "Successfully sent #{@orders.length} receipt emails"}
  rescue => e
    render json: { status: 500, message: "Bulk send buyer receipt error: #{e.message}" }
  end
end

#createObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/effective/orders_controller.rb', line 50

def create
  @order ||= Effective::Order.new(item: current_cart, user: current_user)
  EffectiveOrders.authorize!(self, :create, @order)

  @order.assign_attributes(checkout_params) if params[:effective_order]

  Effective::Order.transaction do
    begin
      @order.save!
      set_redirect_cookie!
      redirect_to(effective_orders.order_path(@order)) and return
    rescue => e
      raise ActiveRecord::Rollback
    end
  end

  flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
  render :new
end

#declinedObject



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

def declined
  @order = Effective::Order.find(params[:id])
  EffectiveOrders.authorize!(self, :show, @order)

  clear_redirect_cookie!

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

#editObject



70
71
72
73
# File 'app/controllers/effective/orders_controller.rb', line 70

def edit
  @order ||= Effective::Order.find(params[:id])
  EffectiveOrders.authorize!(self, :edit, @order)
end

#indexObject



103
104
105
106
107
108
# File 'app/controllers/effective/orders_controller.rb', line 103

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

  EffectiveOrders.authorize!(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



112
113
114
115
# File 'app/controllers/effective/orders_controller.rb', line 112

def my_purchases
  @orders = Effective::Order.deep.purchased_by(current_user)
  EffectiveOrders.authorize!(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



118
119
120
121
# File 'app/controllers/effective/orders_controller.rb', line 118

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

#newObject

This is the entry point for any Checkout button



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

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

  EffectiveOrders.authorize!(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
    redirect_to(effective_orders.cart_path)
    return
  end

  @order.errors.clear
  @order.billing_address.errors.clear if @order.billing_address
  @order.shipping_address.errors.clear if @order.shipping_address
end

#purchasedObject

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/effective/orders_controller.rb', line 124

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

  clear_redirect_cookie!

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

  EffectiveOrders.authorize!(self, :show, @order)

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

#send_buyer_receiptObject



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

def send_buyer_receipt
  @order = Effective::Order.find(params[:id])
  EffectiveOrders.authorize!(self, :show, @order)

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

  if respond_to?(:redirect_back)
    redirect_back(fallback_location: effective_orders.order_path(@order))
  elsif request.referrer.present?
    redirect_to :back
  else
    redirect_to effective_orders.order_path(@order)
  end
end

#showObject



96
97
98
99
100
101
# File 'app/controllers/effective/orders_controller.rb', line 96

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

  @page_title ||= ((@order.user == current_user && !@order.purchased?) ? 'Checkout' : @order.to_s)
end

#updateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/effective/orders_controller.rb', line 75

def update
  @order ||= Effective::Order.find(params[:id])
  EffectiveOrders.authorize!(self, :update, @order)

  @order.assign_attributes(checkout_params)

  Effective::Order.transaction do
    begin
      @order.save!
      set_redirect_cookie!

      redirect_to(effective_orders.order_path(@order)) and return
    rescue => e
      raise ActiveRecord::Rollback
    end
  end

  flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
  render :edit
end