Class: Effective::OrdersController

Inherits:
ApplicationController
  • Object
show all
Includes:
Providers::Moneris, Providers::Paypal, 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::Paypal

#paypal_postback

Methods included from Providers::Moneris

#moneris_postback

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 34

def create
  @order = Order.new(current_cart, current_user)
  @order.attributes = order_params
  @order.shipping_address = @order.billing_address if @order.shipping_address_same_as_billing?

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

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

      if @order.save_shipping_address? && @order.user.respond_to?(:shipping_address) && @order.shipping_address.try(:empty?) == false
        @order.user.shipping_address = @order.shipping_address
      end

      @order.save!

      if @order.total == 0 && EffectiveOrders.allow_free_orders
        order_purchased('zero-dollar order')
      else
        redirect_to(effective_orders.order_path(@order))
      end

      return
    rescue => e
      Rails.logger.info e.message
      flash[:danger] = "An error has ocurred. Please try again. Message: #{e.message}"
      raise ActiveRecord::Rollback
    end
  end

  render :action => :new
end

#declinedObject

An error has occurred, please try again



107
108
109
110
# File 'app/controllers/effective/orders_controller.rb', line 107

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

#indexObject



81
82
83
# File 'app/controllers/effective/orders_controller.rb', line 81

def index
  redirect_to effective_orders.my_purchases_path
end

#my_purchasesObject

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



87
88
89
90
91
# File 'app/controllers/effective/orders_controller.rb', line 87

def my_purchases
  @orders = Order.purchased_by(current_user)

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

#my_salesObject

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



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

def my_sales
  @order_items = OrderItem.sold_by(current_user)

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

#newObject

This is the entry point for the “Checkout” buttons



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/effective/orders_controller.rb', line 16

def new
  @order ||= Order.new(current_cart, 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
  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
  end
end

#pretend_purchaseObject



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

def pretend_purchase
  if Rails.env.development? || EffectiveOrders.allow_pretend_purchase_in_production
    @order = Order.find(params[:id])
    EffectiveOrders.authorized?(self, :update, @order)
    order_purchased('for pretend', params[:purchased_redirect_url], params[:declined_redirect_url])
  end
end

#purchasedObject

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



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

def purchased # Thank You!
  @order = Order.find(params[:id])
  EffectiveOrders.authorized?(self, :show, @order)
end

#resend_buyer_receiptObject



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

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

  if (Effective::OrdersMailer.order_receipt_to_buyer(@order).deliver rescue false)
    flash[:success] = "Successfully resent order receipt to #{@order.user.email}"
  else
    flash[:danger] = "Unable to send order receipt"
  end

  begin
    redirect_to :back
  rescue => e
    redirect_to effective_orders.admin_orders_path
  end
end

#showObject



70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/effective/orders_controller.rb', line 70

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

  if @order.purchased? == false
    @page_title = 'Checkout'
    render(:checkout) and return
  end

end