Class: Plugins::Ecommerce::Front::CheckoutController

Inherits:
Plugins::Ecommerce::FrontController
  • Object
show all
Defined in:
app/controllers/plugins/ecommerce/front/checkout_controller.rb

Instance Method Summary collapse

Instance Method Details

#cancel_orderObject



107
108
109
110
111
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 107

def cancel_order
  @cart.update({status: 'canceled', kind: 'order', closed_at: Time.now})
  flash[:cama_ecommerce][:notice] = t('plugins.ecommerce.messages.canceled_order', default: "Canceled Order")
  params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to plugins_ecommerce_orders_url)
end

#cancel_paypalObject



179
180
181
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 179

def cancel_paypal
  redirect_to plugins_ecommerce_orders_url
end

#cart_addObject

params: product_id, qty



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 66

def cart_add
  data = params[:cart]
  qty = data[:qty].to_f rescue 0
  product = current_site.products.find(data[:product_id]).decorate
  unless product.valid_variation?(params[:variation_id])
    flash[:cama_ecommerce][:error] = t('plugins.ecommerce.messages.missing_variation', default: 'Invalid Product Variation')
    return params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to action: :cart_index)
  end

  unless product.can_added?(qty, params[:variation_id])
    flash[:cama_ecommerce][:error] =  t('plugins.ecommerce.messages.not_enough_product_qty', product: product.the_variation_title(params[:variation_id]), qty: product.the_qty(params[:variation_id]), default: 'There is not enough products "%{product}" (Available %{qty})')
    return params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to request.referer)
  end
  @cart.add_product(product, qty, params[:variation_id])
  flash[:cama_ecommerce][:notice] = t('plugins.ecommerce.messages.added_product_in_cart', default: 'Product added into cart')
  params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to action: :cart_index)
end

#cart_indexObject



49
50
51
52
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 49

def cart_index
  @products = @cart.product_items.decorate
  @ecommerce_breadcrumb << [t('plugins.ecommerce.messages.shopping_cart', default: 'Shopping cart')]
end

#cart_removeObject



101
102
103
104
105
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 101

def cart_remove
  @cart.product_items.find(params[:product_item_id]).destroy
  flash[:cama_ecommerce][:notice] = t('plugins.ecommerce.messages.cart_deleted', default: 'Product removed from your shopping cart')
  params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to action: :cart_index)
end

#cart_updateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 84

def cart_update
  errors = []
  params[:product_items].each do |data|
    item =  @cart.product_items.find(data[:item_id])
    product = item.product.decorate
    qty = data[:qty].to_f
    if product.can_added?(qty, item.variation_id)
      @cart.add_product(product, qty, item.variation_id)
    else
      errors << t('plugins.ecommerce.messages.not_enough_product_qty', product: product.the_variation_title(item.variation_id), qty: product.the_qty(item.variation_id), default: 'There is not enough products "%{product}" (Available %{qty})')
    end
  end
  flash[:cama_ecommerce][:error] = errors.join('<br>') if errors.present?
  flash[:cama_ecommerce][:notice] = t('plugins.ecommerce.messages.cart_updated', default: 'Shopping cart updated') unless errors.present?
  params[:format] == 'json' ? render(json: flash.discard(:cama_ecommerce).to_hash) : (redirect_to action: :cart_index)
end

#complete_free_orderObject

free carts



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 30

def complete_free_order
  if @cart.free_cart?
    errors = ecommerce_verify_cart_errors(@cart)
    if errors.present?
      flash[:cama_ecommerce][:error] = errors.join('<br>')
      redirect_to request.referer
    else
      hooks_run('plugin_ecommerce_before_complete_free_order', @cart)
      @cart.set_meta('free_order', true)
      commerce_mark_cart_received(@cart)
      hooks_run('plugin_ecommerce_after_complete_free_order', @cart)
      redirect_to plugins_ecommerce_orders_path
    end
  else
    flash[:cama_ecommerce][:error] = "Invalid complete payment"
    redirect_to request.referer
  end
end

#indexObject



6
7
8
9
10
11
12
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 6

def index
  unless @cart.product_items.count > 0
    flash[:cama_ecommerce][:notice] = t('plugins.ecommerce.messages.cart_no_products', default: 'Not exist products in your cart')
    return redirect_to action: :cart_index
  end
  @ecommerce_breadcrumb << [t('plugins.ecommerce.messages.checkout', default: 'Checkout')]
end

#pay_by_authorize_netObject



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

def pay_by_authorize_net
  res = Plugins::Ecommerce::CartService.new(current_site, @cart).
    pay_with_authorize_net(payment_method: @payment, ip: request.remote_ip,
      first_name: params[:firstName],
      last_name: params[:lastName],
      number: params[:cardNumber],
      exp_month: params[:expMonth],
      exp_year: params[:expYear],
      cvc: params[:cvCode],
    )
  if res[:error].present?
    flash[:cama_ecommerce][:error] = res[:error]
    flash[:payment_error] = true
    redirect_to request.referer
  else
    commerce_mark_cart_received(@cart)
    redirect_to plugins_ecommerce_orders_url
  end
end

#pay_by_bank_transferObject



131
132
133
134
135
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 131

def pay_by_bank_transfer
  @cart.set_meta("payment_data", params[:details])
  commerce_mark_cart_received(@cart, 'bank_pending')
  redirect_to plugins_ecommerce_orders_url
end

#pay_by_on_deliveryObject



137
138
139
140
141
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 137

def pay_by_on_delivery
  @cart.set_meta("payment_data", params[:details])
  commerce_mark_cart_received(@cart, 'on_delivery')
  redirect_to plugins_ecommerce_orders_url
end

#pay_by_paypalObject



183
184
185
186
187
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 183

def pay_by_paypal
  result = Plugins::Ecommerce::CartService.new(current_site, @cart).
    pay_with_paypal(ip: request.remote_ip, return_url: plugins_ecommerce_checkout_success_paypal_url(order: @cart.slug), cancel_return_url: plugins_ecommerce_checkout_cancel_paypal_url(order: @cart.slug))
  redirect_to result[:redirect_url]
end

#pay_by_stripeObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 113

def pay_by_stripe
  result = Plugins::Ecommerce::CartService.new(current_site, @cart).
    pay_with_stripe(payment_method: @payment,
      email: params[:stripeEmail],
      stripe_token: params[:stripeToken],
    )
  if result[:error].present?
    flash[:cama_ecommerce][:error] = result[:error]
    if result[:payment_error]
      flash[:payment_error] = true
    end
    redirect_to request.referer
  else
    commerce_mark_cart_received(@cart)
    redirect_to plugins_ecommerce_orders_url
  end
end

#res_couponObject



54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 54

def res_coupon
  code = params[:code].to_s.downcase
  res = @cart.discount_for(code)
  if res[:error].present?
    render inline: commerce_coupon_error_message(res[:error], res[:coupon]), status: 500
  else
    @cart.update_column(:coupon, code)
    render partial: plugin_view('partials/checkout/products_detail'), layout: false
  end
end

#step_addressObject



14
15
16
17
18
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 14

def step_address
  @cart.set_meta("billing_address", params[:order][:billing_address])
  @cart.set_meta("shipping_address", params[:order][:shipping_address])
  render inline: ''
end

#step_shippingObject



20
21
22
23
24
25
26
27
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 20

def step_shipping
  @cart.update_column(:shipping_method_id, params[:shipping_method])
  if params[:next_step].present?
    render partial: plugin_view('partials/checkout/payments'), layout: false
  else
    render partial: plugin_view('partials/checkout/products_detail'), layout: false
  end
end

#success_paypalObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/plugins/ecommerce/front/checkout_controller.rb', line 163

def success_paypal
  response = @cart.paypal_gateway.purchase(Plugins::Ecommerce::UtilService.ecommerce_money_to_cents(@cart.total_amount), {
    :ip => request.remote_ip,
    :token => params[:token],
    :payer_id => params[:PayerID]
  })

  if response.success?
    @cart.set_meta('payment_data', {token: params[:token], PayerID: params[:PayerID], ip: request.remote_ip})
    commerce_mark_cart_received(@cart)
  else
    flash[:cama_ecommerce][:error] = response.message
  end
  redirect_to plugins_ecommerce_orders_url
end