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_receipt ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/effective/orders_controller.rb', line 163

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

#create ⇒ Object



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

def create
  @order ||= Effective::Order.new(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!
      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

#declined ⇒ Object



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

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

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

#edit ⇒ Object



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

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

#index ⇒ Object



100
101
102
103
104
105
# File 'app/controllers/effective/orders_controller.rb', line 100

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_purchases ⇒ Object

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



109
110
111
112
# File 'app/controllers/effective/orders_controller.rb', line 109

def my_purchases
  @orders = Effective::Order.deep.purchased_by(current_user)
  EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
end

#my_sales ⇒ Object

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



115
116
117
118
# File 'app/controllers/effective/orders_controller.rb', line 115

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

#new ⇒ Object

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(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

#purchased ⇒ Object

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



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

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_orders_path) and return
  end

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

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

#send_buyer_receipt ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/effective/orders_controller.rb', line 144

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

#show ⇒ Object



93
94
95
96
97
98
# File 'app/controllers/effective/orders_controller.rb', line 93

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

#update ⇒ Object



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

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!
      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