camaleon_ecommerce 0.0.4 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/plugins/ecommerce/admin.js +0 -33
  3. data/app/assets/javascripts/plugins/ecommerce/cart.js +83 -200
  4. data/app/assets/javascripts/plugins/ecommerce/fix_form.js +1 -4
  5. data/app/assets/stylesheets/plugins/ecommerce/checkout.css.scss +46 -0
  6. data/app/assets/stylesheets/plugins/ecommerce/front.scss +2 -30
  7. data/app/controllers/plugins/ecommerce/admin/coupons_controller.rb +2 -2
  8. data/app/controllers/plugins/ecommerce/admin/orders_controller.rb +45 -34
  9. data/app/controllers/plugins/ecommerce/admin/payment_methods_controller.rb +4 -3
  10. data/app/controllers/plugins/ecommerce/admin/prices_controller.rb +5 -5
  11. data/app/controllers/plugins/ecommerce/admin/settings_controller.rb +0 -2
  12. data/app/controllers/plugins/ecommerce/admin/shipping_methods_controller.rb +3 -3
  13. data/app/controllers/plugins/ecommerce/admin/tax_rates_controller.rb +2 -2
  14. data/app/controllers/plugins/ecommerce/admin_controller.rb +1 -1
  15. data/app/controllers/plugins/ecommerce/front/checkout_controller.rb +162 -110
  16. data/app/controllers/plugins/ecommerce/front/orders_controller.rb +7 -234
  17. data/app/controllers/plugins/ecommerce/front_controller.rb +42 -3
  18. data/app/decorators/plugins/ecommerce/cart_decorator.rb +27 -0
  19. data/app/{models → decorators}/plugins/ecommerce/coupon_decorator.rb +1 -1
  20. data/app/decorators/plugins/ecommerce/order_decorator.rb +22 -0
  21. data/app/decorators/plugins/ecommerce/product_decorator.rb +88 -0
  22. data/app/decorators/plugins/ecommerce/product_item_decorator.rb +18 -0
  23. data/app/helpers/plugins/ecommerce/ecommerce_email_helper.rb +91 -28
  24. data/app/helpers/plugins/ecommerce/ecommerce_helper.rb +30 -136
  25. data/app/helpers/plugins/ecommerce/ecommerce_payment_helper.rb +14 -20
  26. data/app/models/plugins/ecommerce/cart.rb +139 -15
  27. data/app/models/plugins/ecommerce/coupon.rb +33 -7
  28. data/app/models/plugins/ecommerce/order.rb +47 -31
  29. data/app/models/plugins/ecommerce/{product.rb → product_item.rb} +11 -5
  30. data/app/views/camaleon_cms/html_mailer/order_canceled.html.erb +2 -0
  31. data/app/views/camaleon_cms/html_mailer/order_confirmed.html.erb +2 -0
  32. data/app/views/camaleon_cms/html_mailer/order_received_admin.html.erb +1 -1
  33. data/app/views/camaleon_cms/html_mailer/order_shipped.html.erb +2 -0
  34. data/app/views/plugins/ecommerce/admin/coupons/form.html.erb +10 -10
  35. data/app/views/plugins/ecommerce/admin/coupons/index.html.erb +1 -1
  36. data/app/views/plugins/ecommerce/admin/orders/form.html.erb +5 -49
  37. data/app/views/plugins/ecommerce/admin/orders/index.html.erb +140 -80
  38. data/app/views/plugins/ecommerce/admin/orders/show.html.erb +7 -169
  39. data/app/views/plugins/ecommerce/admin/payment_methods/form.html.erb +30 -3
  40. data/app/views/plugins/ecommerce/admin/payment_methods/index.html.erb +1 -3
  41. data/app/views/plugins/ecommerce/admin/prices/index.html.erb +0 -2
  42. data/app/views/plugins/ecommerce/admin/prices/show.html.erb +0 -2
  43. data/app/views/plugins/ecommerce/admin/products/index.html.erb +1 -1
  44. data/app/views/plugins/ecommerce/admin/settings/index.html.erb +0 -1
  45. data/app/views/plugins/ecommerce/admin/tax_rates/form.html.erb +1 -1
  46. data/app/views/plugins/ecommerce/front/checkout/cart_index.html.erb +46 -57
  47. data/app/views/plugins/ecommerce/front/checkout/index.html.erb +34 -158
  48. data/app/views/plugins/ecommerce/front/login.html.erb +67 -0
  49. data/app/views/plugins/ecommerce/front/orders/index.html.erb +36 -44
  50. data/app/views/plugins/ecommerce/front/orders/show.html.erb +121 -67
  51. data/app/views/plugins/ecommerce/partials/_cart_widget.html.erb +38 -0
  52. data/app/views/plugins/ecommerce/partials/checkout/_details.html.erb +35 -0
  53. data/app/views/plugins/ecommerce/partials/checkout/_payments.html.erb +123 -0
  54. data/app/views/plugins/ecommerce/partials/checkout/_products_detail.html.erb +49 -0
  55. data/app/views/plugins/ecommerce/partials/checkout/_user_info.html.erb +93 -0
  56. data/app/views/post_types/commerce.html.erb +22 -0
  57. data/app/views/post_types/commerce/_product_widget.html.erb +13 -0
  58. data/app/views/post_types/commerce/category.html.erb +23 -0
  59. data/app/views/post_types/commerce/single.html.erb +91 -0
  60. data/config/custom_models.rb +24 -57
  61. data/config/locales/en.yml +192 -0
  62. data/config/locales/es.yml +405 -0
  63. data/config/routes.rb +26 -16
  64. data/db/migrate/20160502221327_add_cama_ecommerce_structure_plugin.rb +12 -0
  65. data/db/migrate/20160504162311_move_featured_attribute_of_products.rb +15 -0
  66. data/db/migrate/20160527184747_add_new_cart_structure.rb +21 -0
  67. data/lib/ecommerce/version.rb +1 -1
  68. metadata +42 -19
  69. data/app/models/plugins/ecommerce/order_decorator.rb +0 -54
  70. data/app/views/layouts/plugins/ecommerce/mailer.html.erb +0 -22
  71. data/app/views/plugins/ecommerce/admin/index.html.erb +0 -2
  72. data/app/views/plugins/ecommerce/front/_post_list_item.html.erb +0 -18
  73. data/app/views/plugins/ecommerce/front/index.html.erb +0 -2
  74. data/app/views/plugins/ecommerce/front/list_products.html.erb +0 -34
  75. data/app/views/plugins/ecommerce/front/orders/pay_by_bank_transfer.html.erb +0 -67
  76. data/app/views/plugins/ecommerce/front/orders/pay_by_credit_card.html.erb +0 -131
  77. data/app/views/plugins/ecommerce/front/orders/pay_by_credit_card_authorize_net.erb +0 -150
  78. data/app/views/plugins/ecommerce/front/orders/select_payment.html.erb +0 -70
  79. data/app/views/plugins/ecommerce/front/product.html.erb +0 -105
  80. data/app/views/plugins/ecommerce/layouts/_ecommerce.html.erb +0 -41
  81. data/app/views/plugins/ecommerce/partials/_form_address.html.erb +0 -84
  82. data/app/views/plugins/ecommerce/partials/_table_order_products.html.erb +0 -85
  83. data/config/locales/translation.yml +0 -371
@@ -7,16 +7,15 @@
7
7
  See the GNU Affero General Public License (GPLv3) for more details.
8
8
  =end
9
9
  class Plugins::Ecommerce::Admin::OrdersController < Plugins::Ecommerce::AdminController
10
- before_action :set_order, only: ['show', 'edit', 'update', 'destroy']
10
+ before_action :set_order, except: [:index, :new] #, only: ['show', 'edit', 'update', 'destroy']
11
+ before_action :set_order_bread
11
12
 
12
13
  def index
13
14
  orders = current_site.orders
14
15
  if params[:q].present?
15
- #orders = orders.where(params[:q].strip_stopwords2(I18n.locale).split(" ").map{|text| "posts.title LIKE '%#{text}%'" }.join(" OR "))
16
16
  orders = orders.where(slug: params[:q])
17
17
  end
18
18
  if params[:c].present?
19
- #orders = orders.where(user_id: User.joins(:metas).where(usermeta: {key: ['first_name','last_name']}).where("usermeta.value LIKE ?","%#{params[:c]}%").pluck(:id))
20
19
  orders = orders.joins(:details).where("plugins_order_details.customer LIKE ?", "%#{params[:c]}%")
21
20
  end
22
21
  if params[:e].present?
@@ -28,14 +27,12 @@ class Plugins::Ecommerce::Admin::OrdersController < Plugins::Ecommerce::AdminCon
28
27
  if params[:s].present?
29
28
  orders = orders.where(status: params[:s])
30
29
  end
31
-
32
- orders = orders.order('updated_at DESC')
33
30
  @orders = orders.paginate(:page => params[:page], :per_page => current_site.admin_per_page)
34
31
  end
35
32
 
36
33
  def show
37
- admin_breadcrumb_add("#{t('plugin.ecommerce.table.details')}")
38
34
  @order = @order.decorate
35
+ add_breadcrumb("#{t('plugin.ecommerce.details_order', default: 'Order details')} - #{@order.slug}")
39
36
  end
40
37
 
41
38
  def new
@@ -44,53 +41,67 @@ class Plugins::Ecommerce::Admin::OrdersController < Plugins::Ecommerce::AdminCon
44
41
  end
45
42
 
46
43
  def edit
47
- admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
44
+ add_breadcrumb("#{t('camaleon_cms.admin.button.edit', default: 'Edit')}")
48
45
  render 'form'
49
46
  end
50
47
 
51
48
  def update
52
- @order.details.update(@order.details.attributes.merge(params[:order][:details]))
53
- @order.set_meta("billing_address", @order.get_meta("billing_address", {}).merge(params[:order][:billing_address]))
54
- @order.set_meta("shipping_address", @order.get_meta("shipping_address", {}).merge(params[:order][:shipping_address]))
55
- flash[:notice] = "#{t('plugin.ecommerce.message.order', status: "#{t('plugin.ecommerce.message.updated')}")}"
49
+ @order.set_meta("billing_address", params[:order][:billing_address])
50
+ @order.set_meta("shipping_address", params[:order][:shipping_address])
51
+ @order.set_metas(params[:metas])
52
+ flash[:notice] = "#{t('plugin.ecommerce.message.order_updated', default: 'Order Updated')}"
56
53
  redirect_to action: :show, id: params[:id]
57
54
  end
58
55
 
56
+ def destroy
57
+ if @order.destroy
58
+ flash[:notice] = "#{t('plugin.ecommerce.message.order_destroyed', default: 'Order Destroyed')}"
59
+ else
60
+ flash[:error] = "#{t('plugin.ecommerce.message.order_no_destroyed', default: 'Occurred some problems destroying the order')}"
61
+ end
62
+ redirect_to action: :index
63
+ end
64
+
59
65
  # accepted order
60
- def accepted
61
- @order = current_site.orders.find_by_slug(params[:order_id])
66
+ def mark_accepted
67
+ @order.accepted!
62
68
  r = {order: @order}; hooks_run('plugin_ecommerce_before_accepted_order', r)
63
- @order.update({status: 'accepted'})
64
- @order.details.update({accepted_at: Time.now})
65
- message = "#{t('plugin.ecommerce.message.order', status: "#{t('plugin.ecommerce.message.accepted')}")}"
69
+ message = "#{t('plugin.ecommerce.message.order_accepted', default: 'Order Accepted')}"
66
70
  r = {order: @order, message: message}; hooks_run('plugin_ecommerce_after_accepted_order', r)
67
- flash[:info] = r[:message]
68
- redirect_to action: :show, id: params[:order_id]
71
+ flash[:notice] = r[:message]
72
+ redirect_to action: :index
73
+ end
74
+
75
+ def mark_bank_confirmed
76
+ @order.bank_confirmed!
77
+ commerce_send_order_received_email(@order, true)
78
+ flash[:notice] = "#{t('plugin.ecommerce.message.order_bank_confirmed', default: 'Pay Bank Confirmed')}"
79
+ redirect_to action: :index
69
80
  end
70
81
 
71
82
  # shipped order
72
- def shipped
73
- @order = current_site.orders.find_by_slug(params[:order_id])
74
- @order.update({status: 'shipped'})
75
- @order.details.update({shipped_at: Time.now})
76
- code = params[:payment][:consignment_number]
77
- @order.set_meta("payment", @order.get_meta("payment", {}).merge({consignment_number: code}))
78
- flash[:info] = "#{t('plugin.ecommerce.message.order', status: "#{t('plugin.ecommerce.message.shipped')}")}"
79
- redirect_to action: :show, id: params[:order_id]
83
+ def mark_shipped
84
+ @order.shipped!(params[:consignment_number])
85
+ cama_send_email(@order.user.email, t('plugin.ecommerce.mail.order_shipped.subject'), {template_name: 'order_shipped', extra_data: {order: @order, consignment_number: params[:consignment_number]}})
86
+ flash[:notice] = "#{t('plugin.ecommerce.message.order_shipped', default: 'Order Shipped')}"
87
+ redirect_to action: :index
80
88
  end
81
89
 
82
- # closed order
83
- def closed
84
- @order = current_site.orders.find_by_slug(params[:order_id])
85
- @order.update({status: 'closed'})
86
- @order.details.update({closed_at: Time.now})
87
- flash[:info] = "#{t('plugin.ecommerce.message.order', status: "#{t('plugin.ecommerce.message.closed')}")}"
88
- redirect_to action: :show, id: params[:order_id]
90
+ def mark_canceled
91
+ @order.canceled!
92
+ @order.set_meta('description', params[:description])
93
+ cama_send_email(@order.user.email, t('plugin.ecommerce.mail.order_canceled.subject'), {template_name: 'order_canceled', extra_data: {order: @order}, description: params[:description]})
94
+ flash[:notice] = "#{t('plugin.ecommerce.message.order_canceled', default: 'Order canceled')}"
95
+ redirect_to action: :index
89
96
  end
90
97
 
91
98
  private
92
99
  def set_order
93
- @order = current_site.orders.find_by_slug(params[:id])
100
+ @order = current_site.orders.find_by_slug(params[:id] || params[:order_id])
101
+ end
102
+
103
+ def set_order_bread
104
+ add_breadcrumb I18n.t("plugin.ecommerce.orders", default: 'Orders'), admin_plugins_ecommerce_orders_path
94
105
  end
95
106
 
96
107
  end
@@ -15,17 +15,18 @@ class Plugins::Ecommerce::Admin::PaymentMethodsController < Plugins::Ecommerce::
15
15
 
16
16
  def new
17
17
  @payment_method = current_site.payment_methods.new
18
- admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
18
+ @payment_method.status = 1
19
+ add_breadcrumb("#{t('plugin.ecommerce.new')}")
19
20
  render 'form'
20
21
  end
21
22
 
22
23
  def show
23
- admin_breadcrumb_add("#{t('plugin.ecommerce.table.details')}")
24
+ add_breadcrumb("#{t('plugin.ecommerce.table.details')}")
24
25
  @payment_method = @payment_method.decorate
25
26
  end
26
27
 
27
28
  def edit
28
- admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
29
+ add_breadcrumb("#{t('camaleon_cms.admin.button.edit')}")
29
30
  render 'form'
30
31
  end
31
32
 
@@ -10,13 +10,13 @@ class Plugins::Ecommerce::Admin::PricesController < Plugins::Ecommerce::AdminCon
10
10
  before_action :set_shipping_method
11
11
 
12
12
  def index
13
- admin_breadcrumb_add("#{t('plugin.ecommerce.product.price')}")
13
+ add_breadcrumb("#{t('plugin.ecommerce.product.price')}")
14
14
  end
15
15
 
16
16
  def new
17
17
  @price = {}
18
- admin_breadcrumb_add("#{t('plugin.ecommerce.product.price')}", admin_plugins_ecommerce_shipping_method_prices_path( params[:shipping_method_id] ))
19
- admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
18
+ add_breadcrumb("#{t('plugin.ecommerce.product.price')}", admin_plugins_ecommerce_shipping_method_prices_path( params[:shipping_method_id] ))
19
+ add_breadcrumb("#{t('plugin.ecommerce.new')}")
20
20
  render 'form'
21
21
  end
22
22
 
@@ -25,8 +25,8 @@ class Plugins::Ecommerce::Admin::PricesController < Plugins::Ecommerce::AdminCon
25
25
 
26
26
  def edit
27
27
  @price = @prices[params[:id].to_sym] || {}
28
- admin_breadcrumb_add("#{t('plugin.ecommerce.product.price')}", admin_plugins_ecommerce_shipping_method_prices_path( params[:shipping_method_id] ))
29
- admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
28
+ add_breadcrumb("#{t('plugin.ecommerce.product.price')}", admin_plugins_ecommerce_shipping_method_prices_path( params[:shipping_method_id] ))
29
+ add_breadcrumb("#{t('camaleon_cms.admin.button.edit')}")
30
30
  render 'form'
31
31
  end
32
32
 
@@ -17,6 +17,4 @@ class Plugins::Ecommerce::Admin::SettingsController < Plugins::Ecommerce::AdminC
17
17
  flash[:notice] = t('camaleon_cms.admin.post_type.message.updated')
18
18
  redirect_to action: :index
19
19
  end
20
-
21
- # http://finance.yahoo.com/d/quotes.csv?e=.csv&f=c4l1&s=EURUSD=X
22
20
  end
@@ -15,16 +15,16 @@ class Plugins::Ecommerce::Admin::ShippingMethodsController < Plugins::Ecommerce:
15
15
 
16
16
  def new
17
17
  @shipping_method = current_site.shipping_methods.new
18
- admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
18
+ add_breadcrumb("#{t('plugin.ecommerce.new')}")
19
19
  render 'form'
20
20
  end
21
21
 
22
22
  def show
23
- admin_breadcrumb_add("#{t('plugin.ecommerce.table.details')}")
23
+ add_breadcrumb("#{t('plugin.ecommerce.table.details')}")
24
24
  end
25
25
 
26
26
  def edit
27
- admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
27
+ add_breadcrumb("#{t('camaleon_cms.admin.button.edit')}")
28
28
  render 'form'
29
29
  end
30
30
 
@@ -15,7 +15,7 @@ class Plugins::Ecommerce::Admin::TaxRatesController < Plugins::Ecommerce::AdminC
15
15
 
16
16
  def new
17
17
  @tax_rate = current_site.tax_rates.new
18
- admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
18
+ add_breadcrumb("#{t('plugin.ecommerce.new')}")
19
19
  render 'form'
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ class Plugins::Ecommerce::Admin::TaxRatesController < Plugins::Ecommerce::AdminC
23
23
  end
24
24
 
25
25
  def edit
26
- admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
26
+ add_breadcrumb("#{t('camaleon_cms.admin.button.edit')}")
27
27
  render 'form'
28
28
  end
29
29
 
@@ -8,7 +8,6 @@
8
8
  =end
9
9
  class Plugins::Ecommerce::AdminController < CamaleonCms::Apps::PluginsAdminController
10
10
  before_action :verify_ecommerce_permission
11
- add_breadcrumb I18n.t("plugin.ecommerce.e_commerce")
12
11
  def index
13
12
  # here your actions for admin panel
14
13
  end
@@ -16,5 +15,6 @@ class Plugins::Ecommerce::AdminController < CamaleonCms::Apps::PluginsAdminContr
16
15
  private
17
16
  def verify_ecommerce_permission
18
17
  authorize! :posts, get_commerce_post_type
18
+ add_breadcrumb I18n.t("plugin.ecommerce.e_commerce", default: 'Ecommerce'), admin_plugins_ecommerce_index_path
19
19
  end
20
20
  end
@@ -7,160 +7,212 @@
7
7
  See the GNU Affero General Public License (GPLv3) for more details.
8
8
  =end
9
9
  class Plugins::Ecommerce::Front::CheckoutController < Plugins::Ecommerce::FrontController
10
-
10
+ before_action :commerce_authenticate
11
11
  before_action :set_cart
12
+ before_action :set_payment, only: [:pay_by_stripe, :pay_by_bank_transfer, :pay_by_credit_card, :pay_by_authorize_net, :pay_by_paypal]
12
13
 
13
14
  def index
14
- if @cart.products.size > 0
15
- @products = @cart.products
16
- else
17
- flash[:notice] = "Not found products."
18
- redirect_to action: :cart_index
15
+ unless @cart.product_items.count > 0
16
+ flash[:notice] = t('plugins.ecommerce.messages.cart_no_products', default: 'Not exist products in your cart')
17
+ return redirect_to action: :cart_index
19
18
  end
20
- @ecommerce_bredcrumb << ["Checkout"]
19
+ @ecommerce_bredcrumb << [t('plugins.ecommerce.messages.checkout', default: 'Checkout')]
21
20
  end
22
21
 
23
- def processing
24
-
25
-
26
- @products = @cart.products
27
-
28
- total_weight = 0
29
- tax_total = 0
30
- sub_total = 0
31
-
32
- pay_status = 'unpaid'
22
+ def step_address
23
+ @cart.set_meta("billing_address", params[:order][:billing_address])
24
+ @cart.set_meta("shipping_address", params[:order][:shipping_address])
25
+ render nothing: true
26
+ end
33
27
 
34
- @products.each do |product|
35
- product = product.decorate
36
- product_options = @cart.get_option("product_#{product.id}")
37
- price = product_options[:price].to_f
38
- qty = product_options[:qty].to_f
39
- qty_real = product.get_field_value('ecommerce_qty').to_f
40
- if qty_real < 1
41
- @cart.delete_option("product_#{product.id}") if qty < 1
42
- else
43
- qty = qty_real if qty > qty_real
44
- tax_product = product_options[:tax].to_f
45
- tax_total += tax_product * qty
46
- total_weight += product_options[:weight].to_f * product_options[:qty].to_f
47
- sub_total += price * qty
48
- product.update_field_value('ecommerce_qty', (qty_real - qty).to_i)
49
- end
28
+ def step_shipping
29
+ @cart.update_column(:shipping_method_id, params[:shipping_method])
30
+ if params[:next_step].present?
31
+ render partial: plugin_view('partials/checkout/payments'), layout: false
32
+ else
33
+ render partial: plugin_view('partials/checkout/products_detail'), layout: false
50
34
  end
35
+ end
51
36
 
52
- shipping_method = current_site.shipping_methods.find(params[:order][:payment][:shipping_method])
53
- weight_price = shipping_method.get_price_from_weight(total_weight)
54
-
55
- total = sub_total + tax_total + weight_price
56
-
57
- payment_amount = total
58
-
59
- coupon_total = ''
60
- coupon_amount = 0
61
- if params[:order][:payment][:coupon_code].present?
62
- coupon = current_site.coupons.find_valid_by_code(params[:order][:payment][:coupon_code])
63
- if coupon.present?
64
- coupon = coupon.decorate
65
- coupon_total = coupon.the_amount
66
- opts = coupon.options
67
-
68
- case opts[:discount_type]
69
- when 'free_ship'
70
- pay_status = 'received'
71
- coupon_amount = payment_amount
72
- when 'percent'
73
- coupon_amount = sub_total * opts[:amount].to_f / 100
74
- when 'money'
75
- coupon_amount = opts[:amount].to_f
76
- end
77
- payment_amount = payment_amount - coupon_amount
37
+ # free carts
38
+ def complete_free_order
39
+ if @cart.free_cart?
40
+ errors = ecommerce_verify_cart_errors(@cart)
41
+ if errors.present?
42
+ flash[:error] = errors.join('<br>')
43
+ redirect_to :back
44
+ else
45
+ @cart.set_meta('free_order', true)
46
+ mark_order_like_received(@cart)
47
+ redirect_to plugins_ecommerce_orders_path
78
48
  end
79
- end
80
-
81
- payment_amount = 0 if payment_amount < 0
82
-
83
- order_id = Time.now.to_i
84
- @order = current_site.orders.set_user(current_user).create(name: "Order #{order_id}", slug: order_id, status: pay_status)
85
- details = params[:order][:details]
86
- details[:received_at] = Time.now
87
- @order.create_details(details)
88
- @order.set_meta("products", @cart.options)
89
- @order.set_meta("details", params[:order][:details])
90
- @order.set_meta("billing_address", params[:order][:billing_address])
91
- @order.set_meta("shipping_address", params[:order][:shipping_address])
92
- total = sub_total + tax_total + weight_price
93
- @order.set_meta("payment", params[:order][:payment].merge({
94
- amount: payment_amount,
95
- currency_code: current_site.currency_code,
96
- total: total,
97
- sub_total: sub_total,
98
- tax_total: tax_total,
99
- weight_price: weight_price,
100
- coupon: coupon_total,
101
- coupon_amount: coupon_amount
102
- }))
103
-
104
- @cart.destroy
105
-
106
- if payment_amount > 0
107
- redirect_to plugins_ecommerce_order_select_payment_path(order: @order.slug)
108
49
  else
109
- flash[:notice] = "Saved Orders."
110
- redirect_to plugins_ecommerce_orders_path
50
+ flash[:error] = "Invalid complete payment"
51
+ redirect_to :back
111
52
  end
112
53
  end
113
54
 
114
55
  def cart_index
115
- @products = @cart.products
116
- @ecommerce_bredcrumb << ["Shopping Cart"]
56
+ @products = @cart.product_items.decorate
57
+ @ecommerce_bredcrumb << [t('plugins.ecommerce.messages.shopping_cart', default: 'Shopping cart')]
58
+ end
59
+
60
+ def res_coupon
61
+ code = params[:code].to_s.downcase
62
+ res = @cart.discount_for(code)
63
+ if res[:error].present?
64
+ render inline: commerce_coupon_error_message(res[:error], res[:coupon]), status: 500
65
+ else
66
+ @cart.update_column(:coupon, code)
67
+ render partial: plugin_view('partials/checkout/products_detail'), layout: false
68
+ end
117
69
  end
118
70
 
119
71
  # params[cart]: product_id, qty
120
72
  def cart_add
121
73
  data = params[:cart]
122
- product_id = data[:product_id]
123
- @cart.add_product(product_id)
124
- @cart.set_option("product_#{product_id}", e_add_data_product(data, product_id))
125
- flash[:notice] = t('plugin.ecommerce.msg.added_product_in_cart')
74
+ qty = data[:qty].to_f rescue 0
75
+ product = current_site.products.find(data[:product_id]).decorate
76
+ unless product.can_added?(qty)
77
+ flash[:error] = t('plugins.ecommerce.messages.not_enough_product_qty', product: product.the_title, qty: product.the_qty_real, default: 'There is not enough products "%{product}" (%{qty})')
78
+ return redirect_to :back
79
+ end
80
+ @cart.add_product(product, qty)
81
+ flash[:notice] = t('plugins.ecommerce.messages.added_product_in_cart', default: 'Product added into cart')
126
82
  redirect_to action: :cart_index
127
83
  end
128
84
 
129
85
  def cart_update
86
+ errors = []
130
87
  params[:products].each do |data|
131
- product_id = data[:product_id]
132
- @cart.set_option("product_#{product_id}", e_add_data_product(data, product_id))
88
+ product = @cart.products.find(data[:product_id]).decorate
89
+ qty = data[:qty].to_f
90
+ if product.can_added?(qty)
91
+ @cart.add_product(product, qty)
92
+ else
93
+ errors << t('plugins.ecommerce.messages.not_enough_product_qty', product: product.the_title, qty: product.the_qty_real, default: 'There is not enough products "%{product}" (%{qty})')
94
+ end
133
95
  end
134
- flash[:notice] = "Updated product in Cart."
96
+ flash[:error] = errors.join('<br>') if errors.present?
97
+ flash[:notice] = t('plugins.ecommerce.messages.cart_updated', default: 'Shopping cart updated') unless errors.present?
135
98
  redirect_to action: :cart_index
136
99
  end
137
100
 
138
101
  def cart_remove
139
102
  @cart.remove_product(params[:product_id])
140
- @cart.delete_option("product_#{params[:product_id]}")
141
- flash[:notice] = "Deleted product from the Cart."
103
+ flash[:notice] = t('plugins.ecommerce.messages.cart_deleted', default: 'Product removed from your shopping cart')
142
104
  redirect_to action: :cart_index
143
105
  end
144
106
 
145
- def orders
146
- render json: current_site.orders.set_user(current_user)
107
+ def cancel_order
108
+ # @cart = current_site.orders.find_by_slug(params[:order])
109
+ @cart.update({status: 'canceled', kind: 'order', closed_at: Time.now})
110
+ flash[:notice] = t('plugins.ecommerce.messages.canceled_order', default: "Canceled Order")
111
+ redirect_to plugins_ecommerce_orders_url
147
112
  end
148
113
 
149
- private
150
- def set_cart
151
- if signin?
152
- @cart = current_site.carts.set_user(current_user).first_or_create(name: "Cart by #{current_user.id}")
114
+ # pay by stripe
115
+ def pay_by_stripe
116
+ require 'stripe'
117
+ Stripe.api_key = @payment.options[:stripe_id]
118
+ customer = Stripe::Customer.create(:email => params[:stripeEmail], :source => params[:stripeToken])
119
+ begin
120
+ charge = Stripe::Charge.create(
121
+ :customer => customer.id,
122
+ :amount => commerce_to_cents(@cart.total_amount),
123
+ :description => "Payment Products: #{@cart.products_title}",
124
+ :currency => commerce_current_currency
125
+ )
126
+ @cart.set_meta("payment_data", params)
127
+ mark_order_like_received(@cart)
128
+ redirect_to plugins_ecommerce_orders_url
129
+ rescue Stripe::CardError => e
130
+ flash[:error] = e.message
131
+ flash[:payment_error] = true
132
+ redirect_to :back
133
+ rescue => e
134
+ flash[:error] = e.message
135
+ redirect_to :back
136
+ end
137
+ end
138
+
139
+ def pay_by_bank_transfer
140
+ @cart.set_meta("payment_data", params[:details])
141
+ mark_order_like_received(@cart, 'bank_pending')
142
+ redirect_to plugins_ecommerce_orders_url
143
+ end
144
+
145
+ def pay_by_authorize_net
146
+ res = payment_pay_by_credit_card_authorize_net(@cart, @payment)
147
+ if res[:error].present?
148
+ flash[:error] = res[:error]
149
+ flash[:payment_error] = true
150
+ redirect_to :back
153
151
  else
154
- cookies[:return_to] = request.referer
155
- redirect_to cama_admin_login_path
152
+ mark_order_like_received(@cart)
153
+ redirect_to plugins_ecommerce_orders_url
156
154
  end
157
155
  end
158
156
 
157
+ def success_paypal
158
+ # @cart = current_site.carts.find_by_slug(params[:order])
159
+ @cart.set_meta('payment_data', {token: params[:token], PayerID: params[:PayerID]})
160
+ mark_order_like_received(@cart)
161
+ redirect_to plugins_ecommerce_orders_url
162
+ end
159
163
 
160
- def process_pay(data = {})
164
+ def cancel_paypal
165
+ # @cart = current_site.orders.find_by_slug(params[:order])
166
+ redirect_to plugins_ecommerce_orders_url
167
+ end
161
168
 
169
+ def pay_by_paypal
170
+ billing_address = @cart.get_meta("billing_address")
171
+ ActiveMerchant::Billing::Base.mode = @payment.options[:paypal_sandbox].to_s.to_bool ? :test : :production
172
+ paypal_options = {
173
+ :login => @payment.options[:paypal_login],
174
+ :password => @payment.options[:paypal_password],
175
+ :signature => @payment.options[:paypal_signature]
176
+ }
177
+ @gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
178
+ @options = {
179
+ brand_name: current_site.name,
180
+ items: [{number: @cart.slug, name: "Buy Products from #{current_site.the_title}: #{@cart.products_title}", amount: commerce_to_cents(@cart.total_amount)}],
181
+ :order_id => @cart.slug,
182
+ :currency => current_site.currency_code,
183
+ :email => @cart.user.email,
184
+ :billing_address => {:name => "#{billing_address[:first_name]} #{billing_address[:last_name]}",
185
+ :address1 => billing_address[:address1],
186
+ :address2 => billing_address[:address2],
187
+ :city => billing_address[:city],
188
+ :state => billing_address[:state],
189
+ :country => billing_address[:country],
190
+ :zip => billing_address[:zip]
191
+ },
192
+ :description => "Buy Products from #{current_site.the_title}: #{@cart.total_amount}",
193
+ :ip => request.remote_ip,
194
+ :return_url => plugins_ecommerce_checkout_success_paypal_url(order: @cart.slug),
195
+ :cancel_return_url => plugins_ecommerce_checkout_cancel_paypal_url(order: @cart.slug)
196
+ }
197
+ response = @gateway.setup_purchase(commerce_to_cents(@cart.total_amount), @options)
198
+ redirect_to @gateway.redirect_url_for(response.token)
199
+ end
162
200
 
201
+ private
202
+ def set_cart
203
+ @cart = current_site.carts.set_user(current_user).first_or_create(name: "Cart by #{current_user.id}").decorate
163
204
  end
164
205
 
206
+ def commerce_to_cents(money)
207
+ (money*100).round
208
+ end
209
+
210
+ def set_bread
211
+ @ecommerce_bredcrumb << [t('plugins.ecommerce.messages.checkout', default: 'Checkout'), url_for(action: :cart_index)]
212
+ end
165
213
 
214
+ def set_payment
215
+ @payment = current_site.payment_methods.actives.where(id: params[:payment][:payment_id]).first
216
+ @cart.update_column(:payment_method_id, @payment.id)
217
+ end
166
218
  end