camaleon_ecommerce 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/plugins/ecommerce/admin.js +33 -0
  6. data/app/assets/javascripts/plugins/ecommerce/cart.js +207 -0
  7. data/app/assets/javascripts/plugins/ecommerce/fix_form.js +10 -0
  8. data/app/assets/javascripts/plugins/ecommerce/jquery.creditCardValidator.js +208 -0
  9. data/app/assets/stylesheets/plugins/ecommerce/admin.scss +122 -0
  10. data/app/assets/stylesheets/plugins/ecommerce/front.scss +36 -0
  11. data/app/controllers/plugins/ecommerce/admin/coupons_controller.rb +61 -0
  12. data/app/controllers/plugins/ecommerce/admin/orders_controller.rb +96 -0
  13. data/app/controllers/plugins/ecommerce/admin/payment_methods_controller.rb +90 -0
  14. data/app/controllers/plugins/ecommerce/admin/prices_controller.rb +70 -0
  15. data/app/controllers/plugins/ecommerce/admin/settings_controller.rb +22 -0
  16. data/app/controllers/plugins/ecommerce/admin/shipping_methods_controller.rb +62 -0
  17. data/app/controllers/plugins/ecommerce/admin/tax_rates_controller.rb +61 -0
  18. data/app/controllers/plugins/ecommerce/admin_controller.rb +20 -0
  19. data/app/controllers/plugins/ecommerce/front/checkout_controller.rb +166 -0
  20. data/app/controllers/plugins/ecommerce/front/orders_controller.rb +255 -0
  21. data/app/controllers/plugins/ecommerce/front_controller.rb +18 -0
  22. data/app/helpers/plugins/ecommerce/ecommerce_email_helper.rb +46 -0
  23. data/app/helpers/plugins/ecommerce/ecommerce_functions_helper.rb +49 -0
  24. data/app/helpers/plugins/ecommerce/ecommerce_helper.rb +240 -0
  25. data/app/helpers/plugins/ecommerce/ecommerce_payment_helper.rb +67 -0
  26. data/app/models/plugins/ecommerce/cart.rb +40 -0
  27. data/app/models/plugins/ecommerce/coupon.rb +25 -0
  28. data/app/models/plugins/ecommerce/coupon_decorator.rb +60 -0
  29. data/app/models/plugins/ecommerce/order.rb +65 -0
  30. data/app/models/plugins/ecommerce/order_decorator.rb +54 -0
  31. data/app/models/plugins/ecommerce/order_detail.rb +13 -0
  32. data/app/models/plugins/ecommerce/payment_method.rb +29 -0
  33. data/app/models/plugins/ecommerce/product.rb +14 -0
  34. data/app/models/plugins/ecommerce/shipping_method.rb +26 -0
  35. data/app/models/plugins/ecommerce/tax_rate.rb +18 -0
  36. data/app/views/camaleon_cms/html_mailer/order_received.html.erb +3 -0
  37. data/app/views/camaleon_cms/html_mailer/order_received_admin.html.erb +3 -0
  38. data/app/views/camaleon_cms/html_mailer/recovery_cart.html.erb +4 -0
  39. data/app/views/layouts/plugins/ecommerce/mailer.html.erb +22 -0
  40. data/app/views/plugins/ecommerce/admin/coupons/form.html.erb +73 -0
  41. data/app/views/plugins/ecommerce/admin/coupons/index.html.erb +50 -0
  42. data/app/views/plugins/ecommerce/admin/index.html.erb +2 -0
  43. data/app/views/plugins/ecommerce/admin/orders/form.html.erb +64 -0
  44. data/app/views/plugins/ecommerce/admin/orders/index.html.erb +90 -0
  45. data/app/views/plugins/ecommerce/admin/orders/show.html.erb +173 -0
  46. data/app/views/plugins/ecommerce/admin/payment_methods/form.html.erb +115 -0
  47. data/app/views/plugins/ecommerce/admin/payment_methods/index.html.erb +43 -0
  48. data/app/views/plugins/ecommerce/admin/payment_methods/show.html.erb +70 -0
  49. data/app/views/plugins/ecommerce/admin/prices/form.html.erb +49 -0
  50. data/app/views/plugins/ecommerce/admin/prices/index.html.erb +39 -0
  51. data/app/views/plugins/ecommerce/admin/prices/show.html.erb +19 -0
  52. data/app/views/plugins/ecommerce/admin/products/index.html.erb +114 -0
  53. data/app/views/plugins/ecommerce/admin/settings/index.html.erb +25 -0
  54. data/app/views/plugins/ecommerce/admin/shipping_methods/form.html.erb +39 -0
  55. data/app/views/plugins/ecommerce/admin/shipping_methods/index.html.erb +41 -0
  56. data/app/views/plugins/ecommerce/admin/shipping_methods/show.html.erb +22 -0
  57. data/app/views/plugins/ecommerce/admin/tax_rates/form.html.erb +36 -0
  58. data/app/views/plugins/ecommerce/admin/tax_rates/index.html.erb +41 -0
  59. data/app/views/plugins/ecommerce/front/_post_list_item.html.erb +18 -0
  60. data/app/views/plugins/ecommerce/front/checkout/cart_index.html.erb +59 -0
  61. data/app/views/plugins/ecommerce/front/checkout/index.html.erb +166 -0
  62. data/app/views/plugins/ecommerce/front/index.html.erb +2 -0
  63. data/app/views/plugins/ecommerce/front/list_products.html.erb +34 -0
  64. data/app/views/plugins/ecommerce/front/orders/index.html.erb +46 -0
  65. data/app/views/plugins/ecommerce/front/orders/pay_by_bank_transfer.html.erb +67 -0
  66. data/app/views/plugins/ecommerce/front/orders/pay_by_credit_card.html.erb +131 -0
  67. data/app/views/plugins/ecommerce/front/orders/pay_by_credit_card_authorize_net.erb +150 -0
  68. data/app/views/plugins/ecommerce/front/orders/select_payment.html.erb +70 -0
  69. data/app/views/plugins/ecommerce/front/orders/show.html.erb +76 -0
  70. data/app/views/plugins/ecommerce/front/product.html.erb +105 -0
  71. data/app/views/plugins/ecommerce/layouts/_ecommerce.html.erb +41 -0
  72. data/app/views/plugins/ecommerce/partials/_form_address.html.erb +84 -0
  73. data/app/views/plugins/ecommerce/partials/_table_order_products.html.erb +85 -0
  74. data/config/camaleon_plugin.json +48 -0
  75. data/config/currency.json +154 -0
  76. data/config/currency_en.json +154 -0
  77. data/config/currency_es.json +154 -0
  78. data/config/custom_models.rb +64 -0
  79. data/config/locales/readme.txt +1 -0
  80. data/config/locales/translation.yml +371 -0
  81. data/config/routes.rb +57 -0
  82. data/lib/camaleon_ecommerce.rb +4 -0
  83. data/lib/ecommerce.rb +4 -0
  84. data/lib/ecommerce/engine.rb +6 -0
  85. data/lib/ecommerce/version.rb +3 -0
  86. data/lib/tasks/ecommerce_tasks.rake +4 -0
  87. metadata +185 -0
@@ -0,0 +1,22 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::Admin::SettingsController < Plugins::Ecommerce::AdminController
10
+ add_breadcrumb I18n.t("plugin.ecommerce.e_commerce")
11
+ def index
12
+ @setting = current_site.get_meta("_setting_ecommerce", {})
13
+ end
14
+
15
+ def saved
16
+ current_site.set_meta('_setting_ecommerce', params[:setting])
17
+ flash[:notice] = t('camaleon_cms.admin.post_type.message.updated')
18
+ redirect_to action: :index
19
+ end
20
+
21
+ # http://finance.yahoo.com/d/quotes.csv?e=.csv&f=c4l1&s=EURUSD=X
22
+ end
@@ -0,0 +1,62 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::Admin::ShippingMethodsController < Plugins::Ecommerce::AdminController
10
+ before_action :set_order, only: ['show','edit','update','destroy']
11
+
12
+ def index
13
+ @shipping_methods = current_site.shipping_methods.paginate(:page => params[:page], :per_page => current_site.admin_per_page)
14
+ end
15
+
16
+ def new
17
+ @shipping_method = current_site.shipping_methods.new
18
+ admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
19
+ render 'form'
20
+ end
21
+
22
+ def show
23
+ admin_breadcrumb_add("#{t('plugin.ecommerce.table.details')}")
24
+ end
25
+
26
+ def edit
27
+ admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
28
+ render 'form'
29
+ end
30
+
31
+ def create
32
+ data = params[:plugins_ecommerce_shipping_method]
33
+ @shipping_method = current_site.shipping_methods.new(data)
34
+ if @shipping_method.save
35
+ @shipping_method.set_meta('_default',params[:options])
36
+ flash[:notice] = t('camaleon_cms.admin.post_type.message.created')
37
+ redirect_to action: :index
38
+ else
39
+ render 'form'
40
+ end
41
+ end
42
+
43
+ def update
44
+ data = params[:plugins_ecommerce_shipping_method]
45
+ if @shipping_method.update(data)
46
+ @shipping_method.set_meta('_default',params[:options])
47
+ flash[:notice] = t('camaleon_cms.admin.post_type.message.updated')
48
+ redirect_to action: :index
49
+ else
50
+ render 'form'
51
+ end
52
+ end
53
+
54
+
55
+
56
+
57
+ private
58
+ def set_order
59
+ @shipping_method = current_site.shipping_methods.find(params[:id])
60
+ end
61
+
62
+ end
@@ -0,0 +1,61 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::Admin::TaxRatesController < Plugins::Ecommerce::AdminController
10
+ before_action :set_order, only: ['show','edit','update','destroy']
11
+
12
+ def index
13
+ @tax_rates = current_site.tax_rates.paginate(:page => params[:page], :per_page => current_site.admin_per_page)
14
+ end
15
+
16
+ def new
17
+ @tax_rate = current_site.tax_rates.new
18
+ admin_breadcrumb_add("#{t('plugin.ecommerce.new')}")
19
+ render 'form'
20
+ end
21
+
22
+ def show
23
+ end
24
+
25
+ def edit
26
+ admin_breadcrumb_add("#{t('camaleon_cms.admin.button.edit')}")
27
+ render 'form'
28
+ end
29
+
30
+ def create
31
+ data = params[:plugins_ecommerce_tax_rate]
32
+ @tax_rate = current_site.tax_rates.new(data)
33
+ if @tax_rate.save
34
+ @tax_rate.set_meta('_default', params[:options])
35
+ flash[:notice] = t('camaleon_cms.admin.post_type.message.created')
36
+ redirect_to action: :index
37
+ else
38
+ render 'form'
39
+ end
40
+ end
41
+
42
+ def update
43
+ data = params[:plugins_ecommerce_tax_rate]
44
+ if @tax_rate.update(data)
45
+ @tax_rate.set_meta('_default', params[:options])
46
+ flash[:notice] = t('camaleon_cms.admin.post_type.message.updated')
47
+ redirect_to action: :index
48
+ else
49
+ render 'form'
50
+ end
51
+ end
52
+
53
+
54
+
55
+
56
+ private
57
+ def set_order
58
+ @tax_rate = current_site.tax_rates.find(params[:id])
59
+ end
60
+
61
+ end
@@ -0,0 +1,20 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::AdminController < CamaleonCms::Apps::PluginsAdminController
10
+ before_action :verify_ecommerce_permission
11
+ add_breadcrumb I18n.t("plugin.ecommerce.e_commerce")
12
+ def index
13
+ # here your actions for admin panel
14
+ end
15
+
16
+ private
17
+ def verify_ecommerce_permission
18
+ authorize! :posts, get_commerce_post_type
19
+ end
20
+ end
@@ -0,0 +1,166 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::Front::CheckoutController < Plugins::Ecommerce::FrontController
10
+
11
+ before_action :set_cart
12
+
13
+ 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
19
+ end
20
+ @ecommerce_bredcrumb << ["Checkout"]
21
+ end
22
+
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'
33
+
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
50
+ end
51
+
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
78
+ 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
+ else
109
+ flash[:notice] = "Saved Orders."
110
+ redirect_to plugins_ecommerce_orders_path
111
+ end
112
+ end
113
+
114
+ def cart_index
115
+ @products = @cart.products
116
+ @ecommerce_bredcrumb << ["Shopping Cart"]
117
+ end
118
+
119
+ # params[cart]: product_id, qty
120
+ def cart_add
121
+ 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')
126
+ redirect_to action: :cart_index
127
+ end
128
+
129
+ def cart_update
130
+ 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))
133
+ end
134
+ flash[:notice] = "Updated product in Cart."
135
+ redirect_to action: :cart_index
136
+ end
137
+
138
+ def cart_remove
139
+ @cart.remove_product(params[:product_id])
140
+ @cart.delete_option("product_#{params[:product_id]}")
141
+ flash[:notice] = "Deleted product from the Cart."
142
+ redirect_to action: :cart_index
143
+ end
144
+
145
+ def orders
146
+ render json: current_site.orders.set_user(current_user)
147
+ end
148
+
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}")
153
+ else
154
+ cookies[:return_to] = request.referer
155
+ redirect_to cama_admin_login_path
156
+ end
157
+ end
158
+
159
+
160
+ def process_pay(data = {})
161
+
162
+
163
+ end
164
+
165
+
166
+ end
@@ -0,0 +1,255 @@
1
+ =begin
2
+ Camaleon CMS is a content management system
3
+ Copyright (C) 2015 by Owen Peredo Diaz
4
+ Email: owenperedo@gmail.com
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7
+ See the GNU Affero General Public License (GPLv3) for more details.
8
+ =end
9
+ class Plugins::Ecommerce::Front::OrdersController < Plugins::Ecommerce::FrontController
10
+
11
+
12
+ def index
13
+ @ecommerce_bredcrumb << ["Orders"]
14
+ @orders = current_site.orders.set_user(current_user).all
15
+ end
16
+
17
+ def show
18
+ @order = current_site.orders.find_by_slug(params[:order]).decorate
19
+ @ecommerce_bredcrumb << ["Orders", url_for(action: :index)]
20
+ @ecommerce_bredcrumb << ["Detail Order: #{params[:order]}"]
21
+ end
22
+
23
+ def res_coupon
24
+ coupon = current_site.coupons.find_by_slug(params[:code].to_s.parameterize)
25
+ error = false
26
+ if coupon.nil?
27
+ error = 'Not Found Coupon'
28
+ elsif "#{coupon.options[:expirate_date]} 23:59:59".to_time.to_i < Time.now.to_i
29
+ error = 'Coupon Expired'
30
+ elsif coupon.status != '1'
31
+ error = 'Coupon not active'
32
+ end
33
+ if error
34
+ render json: {error: error}
35
+ else
36
+ coupon = coupon.decorate
37
+ render json: {data: {text: "#{coupon.the_amount}", options: coupon.options, code: coupon.slug, current_unit: current_site.current_unit}}
38
+ end
39
+ end
40
+
41
+ def select_payment
42
+ @order = current_site.orders.find_by_slug(params[:order])
43
+ if params[:cancel].present?
44
+ @order.update({status: 'canceled'})
45
+ @order.details.update({closed_at: Time.now})
46
+ flash[:notice] = "Canceled Order"
47
+ redirect_to action: :index
48
+ end
49
+ @ecommerce_bredcrumb << ["Orders", url_for(action: :index)]
50
+ @ecommerce_bredcrumb << ["Payment Order: #{params[:order]}"]
51
+ end
52
+
53
+ def set_select_payment
54
+ @order = current_site.orders.find_by_slug(params[:order])
55
+ @order.set_meta("payment", @order.get_meta("payment", {}).merge(params[:payment] || {}))
56
+ redirect_to plugins_ecommerce_order_pay_path(order: @order.slug)
57
+ end
58
+
59
+ def pay
60
+ @order = current_site.orders.find_by_slug(params[:order])
61
+ @ecommerce_bredcrumb << ["Orders", url_for(action: :index)]
62
+ if @order.get_meta("payment", {})[:type] == 'paypal'
63
+ pay_by_paypal
64
+ elsif @order.get_meta("payment", {})[:type] == 'credit_card'
65
+ @payment_methods = current_site.payment_methods.find(@order.get_meta("payment", {})[:payment_id])
66
+ @ecommerce_bredcrumb << ["Payment by Credit Card"]
67
+ render 'pay_by_credit_card'
68
+ elsif @order.get_meta("payment", {})[:type] == 'authorize_net'
69
+ @payment_methods = current_site.payment_methods.find(@order.get_meta("payment", {})[:payment_id])
70
+ @ecommerce_bredcrumb << ['Payment by Credit Card']
71
+ render 'pay_by_credit_card_authorize_net'
72
+ else
73
+ @payment_methods = current_site.payment_methods.find(@order.get_meta("payment", {})[:payment_id])
74
+ @ecommerce_bredcrumb << ["Payment by Bank Transfer"]
75
+ render 'pay_by_bank_transfer'
76
+ end
77
+ end
78
+
79
+ def pay_by_bank_transfer
80
+ @order = current_site.orders.find_by_slug(params[:order])
81
+ @order.set_meta("pay_bank_transfer", params[:details])
82
+ mark_order_like_received(@order)
83
+ flash[:notice] = "Updated Pay"
84
+ redirect_to action: :index
85
+ end
86
+
87
+ def pay_by_credit_card
88
+ @order = current_site.orders.find_by_slug(params[:order])
89
+ res = pay_by_credit_card_run
90
+ if res[:error].present?
91
+ @error = res[:error]
92
+ @payment_methods = current_site.payment_methods.find(@order.get_meta("payment", {})[:payment_id])
93
+ render 'pay_by_credit_card'
94
+ else
95
+ @order.set_meta('pay_credit_card', params)
96
+ mark_order_like_received(@order)
97
+ flash[:notice] = 'Updated Pay'
98
+ redirect_to action: :index
99
+ end
100
+ end
101
+
102
+ def pay_by_credit_card_authorize_net
103
+ @order = current_site.orders.find_by_slug(params[:order])
104
+ res = payment_pay_by_credit_card_authorize_net(@order)
105
+ if res[:error].present?
106
+ @error = res[:error]
107
+ @payment_methods = current_site.payment_methods.find(@order.get_meta("payment", {})[:payment_id])
108
+ render 'pay_by_credit_card_authorize_net'
109
+ else
110
+ flash[:notice] = 'Updated Pay'
111
+ redirect_to action: :index
112
+ end
113
+ end
114
+
115
+ def success
116
+ @order = current_site.orders.find_by_slug(params[:order])
117
+ @order.set_meta('pay_paypal', {token: params[:token], PayerID: params[:PayerID]})
118
+ mark_order_like_received(@order)
119
+ flash[:notice] = 'Updated Pay'
120
+ redirect_to action: :index
121
+ end
122
+
123
+ def cancel
124
+ #@order = current_site.orders.find_by_slug(params[:order])
125
+ flash[:notice] = 'Cancel Pay by Paypal'
126
+ redirect_to action: :index
127
+ end
128
+
129
+ private
130
+
131
+
132
+ def pay_by_credit_card_run
133
+ payment = @order.get_meta("payment", {})
134
+ billing_address = @order.get_meta("billing_address")
135
+ details = @order.get_meta("details")
136
+ @payment_method = current_site.payment_methods.find(payment[:payment_id])
137
+
138
+ @params = {
139
+ :order_id => @order.slug,
140
+ :currency => current_site.currency_code,
141
+ :email => details[:email],
142
+ :billing_address => {:name => "#{billing_address[:first_name]} #{billing_address[:last_name]}",
143
+ :address1 => billing_address[:address1],
144
+ :address2 => billing_address[:address2],
145
+ :city => billing_address[:city],
146
+ :state => billing_address[:state],
147
+ :country => billing_address[:country],
148
+ :zip => billing_address[:zip]
149
+ },
150
+ :description => 'Buy Products',
151
+ :ip => request.remote_ip
152
+ }
153
+
154
+ @amount = to_cents(payment[:amount].to_f)
155
+
156
+ paypal_options = {
157
+ :login => @payment_method.options[:cc_paypal_login],
158
+ :password => @payment_method.options[:cc_paypal_password],
159
+ :signature => @payment_method.options[:cc_paypal_signature]
160
+ }
161
+
162
+ ActiveMerchant::Billing::Base.mode = @payment_method.options[:cc_paypal_sandbox].to_s.to_bool ? :test : :production
163
+ @gateway = ActiveMerchant::Billing::PaypalGateway.new(paypal_options)
164
+
165
+ @credit_card = ActiveMerchant::Billing::CreditCard.new(
166
+ :first_name => params[:firstName],
167
+ :last_name => params[:lastName],
168
+ :number => params[:cardNumber],
169
+ :month => params[:expMonth],
170
+ :year => "20#{params[:expYear]}",
171
+ :verification_value => params[:cvCode])
172
+
173
+ if @credit_card.validate.empty?
174
+ puts "--#{@params.inspect}--"
175
+ response = @gateway.verify(@credit_card, @params)
176
+ #response = @gateway.purchase(@amount, @credit_card, @params)
177
+ if response.success?
178
+ return {success: 'Paid Correct'} #puts "Successfully charged $#{sprintf("%.2f", @amount / 100)} to the credit card #{@credit_card.display_number}"
179
+ else
180
+ return {error: response.message} #raise StandardError, response.message
181
+ end
182
+ else
183
+ return {error: "Credit Card Invalid"}
184
+ end
185
+ end
186
+
187
+ def pay_by_paypal
188
+ payment = @order.get_meta("payment", {})
189
+ billing_address = @order.get_meta("billing_address")
190
+ details = @order.get_meta("details")
191
+ @payment_method = current_site.payment_methods.find(payment[:payment_id])
192
+
193
+ ActiveMerchant::Billing::Base.mode = @payment_method.options[:paypal_sandbox].to_s.to_bool ? :test : :production
194
+ paypal_options = {
195
+ :login => @payment_method.options[:paypal_login],
196
+ :password => @payment_method.options[:paypal_password],
197
+ :signature => @payment_method.options[:paypal_signature]
198
+ }
199
+
200
+ @gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
201
+
202
+ #subtotal, shipping, total, tax = get_totals(payment)
203
+ @options = {
204
+ brand_name: current_site.name,
205
+
206
+ #allow_guest_checkout: true,
207
+ #items: get_items(@order.meta[:products]),
208
+
209
+ items: [{number: @order.slug, name: 'Buy Products', amount: to_cents(payment[:amount].to_f)}],
210
+ :order_id => @order.slug,
211
+ :currency => current_site.currency_code,
212
+ :email => details[:email],
213
+ :billing_address => {:name => "#{billing_address[:first_name]} #{billing_address[:last_name]}",
214
+ :address1 => billing_address[:address1],
215
+ :address2 => billing_address[:address2],
216
+ :city => billing_address[:city],
217
+ :state => billing_address[:state],
218
+ :country => billing_address[:country],
219
+ :zip => billing_address[:zip]
220
+ },
221
+ :description => 'Buy Products',
222
+ :ip => request.remote_ip,
223
+ :return_url => plugins_ecommerce_order_success_url(order: @order.slug),
224
+ :cancel_return_url => plugins_ecommerce_order_cancel_url(order: @order.slug)
225
+ }
226
+
227
+ response = @gateway.setup_purchase(to_cents(payment[:amount].to_f), @options)
228
+
229
+ redirect_to @gateway.redirect_url_for(response.token)
230
+ end
231
+
232
+ def get_items(products)
233
+ products.collect do |key, product|
234
+ {
235
+ :name => product[:product_title],
236
+ :number => product[:product_id],
237
+ :quantity => product[:qty],
238
+ :amount => to_cents(product[:price].to_f),
239
+ }
240
+ end
241
+ end
242
+
243
+ def get_totals(payment)
244
+ tax = payment[:tax_total].to_f
245
+ subtotal = payment[:sub_total].to_f
246
+ shipping = payment[:weight_price].to_f + payment[:sub_total].to_f
247
+ total = subtotal + shipping
248
+ return subtotal, shipping, total, tax
249
+ end
250
+
251
+ def to_cents(money)
252
+ (money*100).round
253
+ end
254
+
255
+ end