effective_orders 2.2.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +124 -84
  4. data/app/assets/javascripts/effective_orders/customers.js.coffee +39 -0
  5. data/app/assets/javascripts/effective_orders/providers/{stripe_charges.js.coffee → stripe.js.coffee} +15 -13
  6. data/app/assets/javascripts/effective_orders/subscriptions.js.coffee +73 -0
  7. data/app/assets/stylesheets/effective_orders.scss +2 -1
  8. data/app/assets/stylesheets/effective_orders/_order.scss +16 -8
  9. data/app/assets/stylesheets/effective_orders/_subscriptions.scss +14 -0
  10. data/app/controllers/admin/customers_controller.rb +11 -8
  11. data/app/controllers/admin/order_items_controller.rb +4 -8
  12. data/app/controllers/admin/orders_controller.rb +133 -87
  13. data/app/controllers/effective/carts_controller.rb +18 -8
  14. data/app/controllers/effective/concerns/purchase.rb +39 -0
  15. data/app/controllers/effective/customers_controller.rb +43 -0
  16. data/app/controllers/effective/orders_controller.rb +73 -119
  17. data/app/controllers/effective/providers/app_checkout.rb +3 -1
  18. data/app/controllers/effective/providers/ccbill.rb +4 -6
  19. data/app/controllers/effective/providers/cheque.rb +20 -11
  20. data/app/controllers/effective/providers/free.rb +33 -0
  21. data/app/controllers/effective/providers/mark_as_paid.rb +33 -0
  22. data/app/controllers/effective/providers/moneris.rb +9 -17
  23. data/app/controllers/effective/providers/paypal.rb +4 -6
  24. data/app/controllers/effective/providers/pretend.rb +4 -4
  25. data/app/controllers/effective/providers/refund.rb +39 -0
  26. data/app/controllers/effective/providers/stripe.rb +19 -40
  27. data/app/controllers/effective/providers/stripe_connect.rb +2 -6
  28. data/app/controllers/effective/webhooks_controller.rb +44 -95
  29. data/app/datatables/effective_customers_datatable.rb +21 -29
  30. data/app/datatables/effective_order_items_datatable.rb +77 -79
  31. data/app/datatables/effective_orders_datatable.rb +67 -57
  32. data/app/helpers/effective_carts_helper.rb +17 -14
  33. data/app/helpers/effective_orders_helper.rb +40 -56
  34. data/app/helpers/effective_paypal_helper.rb +3 -3
  35. data/app/helpers/effective_stripe_helper.rb +47 -18
  36. data/app/helpers/effective_subscriptions_helper.rb +79 -0
  37. data/app/mailers/effective/orders_mailer.rb +125 -2
  38. data/app/models/concerns/acts_as_purchasable.rb +23 -33
  39. data/app/models/concerns/acts_as_subscribable.rb +68 -0
  40. data/app/models/concerns/acts_as_subscribable_buyer.rb +22 -0
  41. data/app/models/effective/cart.rb +53 -24
  42. data/app/models/effective/cart_item.rb +6 -12
  43. data/app/models/effective/customer.rb +51 -54
  44. data/app/models/effective/order.rb +160 -147
  45. data/app/models/effective/order_item.rb +18 -21
  46. data/app/models/effective/product.rb +7 -7
  47. data/app/models/effective/providers/ccbill_postback.rb +1 -1
  48. data/app/models/effective/providers/stripe_charge.rb +8 -19
  49. data/app/models/effective/subscripter.rb +230 -0
  50. data/app/models/effective/subscription.rb +27 -76
  51. data/app/models/effective/tax_rate_calculator.rb +10 -7
  52. data/app/views/admin/customers/_actions.html.haml +1 -2
  53. data/app/views/admin/customers/index.html.haml +1 -1
  54. data/app/views/admin/customers/show.html.haml +6 -0
  55. data/app/views/admin/orders/_actions.html.haml +9 -7
  56. data/app/views/admin/orders/_form.html.haml +11 -7
  57. data/app/views/admin/orders/_order_actions.html.haml +2 -1
  58. data/app/views/admin/orders/_order_item_fields.html.haml +1 -1
  59. data/app/views/admin/orders/edit.html.haml +4 -0
  60. data/app/views/admin/orders/index.html.haml +1 -4
  61. data/app/views/admin/orders/new.html.haml +1 -1
  62. data/app/views/admin/orders/show.html.haml +5 -6
  63. data/app/views/effective/carts/_cart.html.haml +2 -2
  64. data/app/views/effective/carts/show.html.haml +2 -2
  65. data/app/views/effective/customers/_customer.html.haml +152 -0
  66. data/app/views/effective/customers/_fields.html.haml +12 -0
  67. data/app/views/effective/customers/_form.html.haml +13 -0
  68. data/app/views/effective/customers/edit.html.haml +3 -0
  69. data/app/views/effective/orders/_checkout_step1.html.haml +8 -15
  70. data/app/views/effective/orders/_checkout_step2.html.haml +34 -21
  71. data/app/views/effective/orders/_order.html.haml +8 -9
  72. data/app/views/effective/orders/_order_actions.html.haml +7 -8
  73. data/app/views/effective/orders/_order_header.html.haml +1 -1
  74. data/app/views/effective/orders/_order_items.html.haml +11 -5
  75. data/app/views/effective/orders/_order_note.html.haml +4 -7
  76. data/app/views/effective/orders/_orders_table.html.haml +26 -26
  77. data/app/views/effective/orders/app_checkout/_form.html.haml +2 -2
  78. data/app/views/effective/orders/ccbill/_form.html.haml +1 -1
  79. data/app/views/effective/orders/cheque/_form.html.haml +3 -1
  80. data/app/views/effective/orders/declined.html.haml +1 -1
  81. data/app/views/effective/orders/{checkout_step1.html.haml → edit.html.haml} +0 -0
  82. data/app/views/effective/orders/free/_form.html.haml +4 -0
  83. data/app/views/effective/orders/index.html.haml +2 -4
  84. data/app/views/effective/orders/mark_as_paid/_form.html.haml +32 -0
  85. data/app/views/effective/orders/moneris/_form.html.haml +6 -6
  86. data/app/views/effective/orders/{checkout_step2.html.haml → new.html.haml} +1 -1
  87. data/app/views/effective/orders/paypal/_form.html.haml +2 -2
  88. data/app/views/effective/orders/pretend/_form.html.haml +2 -2
  89. data/app/views/effective/orders/purchased.html.haml +3 -0
  90. data/app/views/effective/orders/refund/_form.html.haml +32 -0
  91. data/app/views/effective/orders/show.html.haml +4 -1
  92. data/app/views/effective/orders/stripe/_form.html.haml +5 -5
  93. data/app/views/effective/orders_mailer/subscription_canceled.html.haml +9 -0
  94. data/app/views/effective/orders_mailer/subscription_payment_failed.html.haml +9 -0
  95. data/app/views/effective/orders_mailer/subscription_payment_succeeded.html.haml +9 -0
  96. data/app/views/effective/orders_mailer/subscription_trial_expired.html.haml +5 -0
  97. data/app/views/effective/orders_mailer/subscription_trial_expiring.html.haml +7 -0
  98. data/app/views/effective/subscriptions/_fields.html.haml +16 -0
  99. data/app/views/effective/subscriptions/_plan.html.haml +21 -0
  100. data/app/views/layouts/effective_orders_mailer_layout.html.haml +6 -8
  101. data/config/effective_orders.rb +41 -20
  102. data/config/routes.rb +48 -48
  103. data/db/migrate/01_create_effective_orders.rb.erb +19 -5
  104. data/lib/effective_orders.rb +78 -42
  105. data/lib/effective_orders/engine.rb +36 -82
  106. data/lib/effective_orders/version.rb +1 -1
  107. data/lib/generators/effective_orders/install_generator.rb +2 -2
  108. data/lib/generators/templates/effective_orders_mailer_preview.rb +39 -4
  109. data/lib/tasks/effective_orders_tasks.rake +42 -0
  110. data/spec/controllers/carts_controller_spec.rb +1 -1
  111. data/spec/controllers/moneris_orders_controller_spec.rb +4 -4
  112. data/spec/controllers/orders_controller_spec.rb +4 -4
  113. data/spec/controllers/stripe_orders_controller_spec.rb +2 -2
  114. data/spec/controllers/webhooks_controller_spec.rb +1 -1
  115. data/spec/dummy/config/initializers/effective_orders.rb +1 -7
  116. data/spec/dummy/db/schema.rb +1 -0
  117. data/spec/dummy/db/test.sqlite3 +0 -0
  118. data/spec/dummy/log/test.log +3 -0
  119. data/spec/models/acts_as_purchasable_spec.rb +0 -56
  120. data/spec/models/customer_spec.rb +3 -3
  121. data/spec/models/order_spec.rb +2 -2
  122. data/spec/spec_helper.rb +1 -1
  123. data/spec/support/factories.rb +2 -1
  124. metadata +37 -49
  125. data/active_admin/effective_carts.rb +0 -14
  126. data/active_admin/effective_orders.rb +0 -112
  127. data/app/assets/javascripts/effective_orders/providers/stripe_subscriptions.js.coffee +0 -28
  128. data/app/controllers/concerns/acts_as_active_admin_controller.rb +0 -69
  129. data/app/controllers/effective/subscriptions_controller.rb +0 -126
  130. data/app/models/effective/datatables/customers.rb +0 -40
  131. data/app/models/effective/datatables/order_items.rb +0 -101
  132. data/app/models/effective/datatables/orders.rb +0 -91
  133. data/app/models/inputs/price_field.rb +0 -63
  134. data/app/models/inputs/price_form_input.rb +0 -7
  135. data/app/models/inputs/price_formtastic_input.rb +0 -9
  136. data/app/models/inputs/price_input.rb +0 -19
  137. data/app/models/inputs/price_simple_form_input.rb +0 -8
  138. data/app/views/admin/orders/_form_mark_as_paid.html.haml +0 -33
  139. data/app/views/admin/orders/_order_payment_details.html.haml +0 -5
  140. data/app/views/admin/orders/mark_as_paid.html.haml +0 -7
  141. data/app/views/effective/orders/stripe/_subscription_fields.html.haml +0 -7
  142. data/app/views/effective/subscriptions/index.html.haml +0 -22
  143. data/app/views/effective/subscriptions/new.html.haml +0 -9
  144. data/app/views/effective/subscriptions/show.html.haml +0 -49
  145. data/db/upgrade/02_upgrade_effective_orders_from03x.rb.erb +0 -29
  146. data/db/upgrade/03_upgrade_effective_orders_from1x.rb.erb +0 -98
  147. data/db/upgrade/upgrade_price_column_on_table.rb.erb +0 -17
  148. data/lib/generators/effective_orders/upgrade_from03x_generator.rb +0 -31
  149. data/lib/generators/effective_orders/upgrade_from1x_generator.rb +0 -27
  150. data/lib/generators/effective_orders/upgrade_price_column_generator.rb +0 -30
@@ -9,13 +9,13 @@ module Effective
9
9
  @pending_orders = Effective::Order.pending.where(user: current_user) if current_user.present?
10
10
 
11
11
  @page_title ||= 'My Cart'
12
- EffectiveOrders.authorized?(self, :show, @cart)
12
+ EffectiveOrders.authorize!(self, :show, @cart)
13
13
  end
14
14
 
15
15
  def destroy
16
16
  @cart = current_cart
17
17
 
18
- EffectiveOrders.authorized?(self, :destroy, @cart)
18
+ EffectiveOrders.authorize!(self, :destroy, @cart)
19
19
 
20
20
  if @cart.destroy
21
21
  flash[:success] = 'Successfully emptied cart.'
@@ -23,18 +23,18 @@ module Effective
23
23
  flash[:danger] = 'Unable to destroy cart.'
24
24
  end
25
25
 
26
- request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
26
+ redirect_back_or_to_cart
27
27
  end
28
28
 
29
29
  def add_to_cart
30
30
  @purchasable = (add_to_cart_params[:purchasable_type].constantize.find(add_to_cart_params[:purchasable_id].to_i) rescue nil)
31
31
 
32
- EffectiveOrders.authorized?(self, :update, current_cart)
32
+ EffectiveOrders.authorize!(self, :update, current_cart)
33
33
 
34
34
  begin
35
35
  raise "Please select a valid #{add_to_cart_params[:purchasable_type] || 'item' }." unless @purchasable
36
36
 
37
- current_cart.add_to_cart(@purchasable, quantity: [add_to_cart_params[:quantity].to_i, 1].max)
37
+ current_cart.add(@purchasable, quantity: [add_to_cart_params[:quantity].to_i, 1].max)
38
38
  flash[:success] = 'Successfully added item to cart.'
39
39
  rescue EffectiveOrders::SoldOutException
40
40
  flash[:warning] = 'This item is sold out.'
@@ -42,13 +42,13 @@ module Effective
42
42
  flash[:danger] = 'Unable to add item to cart: ' + e.message
43
43
  end
44
44
 
45
- request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
45
+ redirect_back_or_to_cart
46
46
  end
47
47
 
48
48
  def remove_from_cart
49
49
  @cart_item = current_cart.cart_items.find(remove_from_cart_params[:id])
50
50
 
51
- EffectiveOrders.authorized?(self, :update, current_cart)
51
+ EffectiveOrders.authorize!(self, :update, current_cart)
52
52
 
53
53
  if @cart_item.destroy
54
54
  flash[:success] = 'Successfully removed item from cart.'
@@ -56,7 +56,7 @@ module Effective
56
56
  flash[:danger] = 'Unable to remove item from cart.'
57
57
  end
58
58
 
59
- request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
59
+ redirect_back_or_to_cart
60
60
  end
61
61
 
62
62
  private
@@ -69,5 +69,15 @@ module Effective
69
69
  params.permit(:id)
70
70
  end
71
71
 
72
+ def redirect_back_or_to_cart
73
+ if respond_to?(:redirect_back)
74
+ redirect_back(fallback_location: effective_orders.cart_path)
75
+ elsif request.referrer.present?
76
+ redirect_to(:back)
77
+ else
78
+ redirect_to(effective_orders.cart_path)
79
+ end
80
+ end
81
+
72
82
  end
73
83
  end
@@ -0,0 +1,39 @@
1
+ module Effective
2
+ module Concerns
3
+ module Purchase
4
+ extend ActiveSupport::Concern
5
+
6
+ protected
7
+
8
+ def order_purchased(provider:, card: 'none', details: 'none', email: true, skip_buyer_validations: false, purchased_url: nil, declined_url: nil)
9
+ begin
10
+ @order.purchase!(provider: provider, card: card, details: details, email: email, skip_buyer_validations: skip_buyer_validations)
11
+
12
+ Effective::Cart.where(user_id: @order.user_id).destroy_all
13
+
14
+ if EffectiveOrders.mailer[:send_order_receipt_to_buyer] && email
15
+ flash[:success] = "Payment successful! A receipt has been sent to #{@order.user.email}"
16
+ elsif @order.free?
17
+ # Nothing
18
+ else
19
+ flash[:success] = 'Payment successful! An email receipt has not been sent.'
20
+ end
21
+
22
+ redirect_to (purchased_url.presence || effective_orders.purchased_order_path(':id')).gsub(':id', @order.to_param.to_s)
23
+ rescue => e
24
+ flash[:danger] = "An error occurred while processing your payment: #{e.message}. Please try again."
25
+ redirect_to(declined_url.presence || effective_orders.cart_path).gsub(':id', @order.to_param.to_s)
26
+ end
27
+ end
28
+
29
+ def order_declined(provider:, card: 'none', details: 'none', message: nil, declined_url: nil)
30
+ @order.decline!(provider: provider, card: card, details: details) rescue nil
31
+
32
+ flash[:danger] = message.presence || 'Payment was unsuccessful. Your credit card was declined by the payment processor. Please try again.'
33
+
34
+ redirect_to(declined_url.presence || effective_orders.declined_order_path(@order)).gsub(':id', @order.to_param.to_s)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ module Effective
2
+ class CustomersController < ApplicationController
3
+ layout (EffectiveOrders.layout.kind_of?(Hash) ? EffectiveOrders.layout[:customers] : EffectiveOrders.layout)
4
+
5
+ before_action :authenticate_user!
6
+
7
+ # Get here by visiting /customer/settings
8
+ def edit
9
+ @customer = Effective::Customer.where(user: current_user).first!
10
+ EffectiveOrders.authorize!(self, :edit, @customer)
11
+
12
+ @subscripter = Effective::Subscripter.new(customer: @customer, user: @customer.user)
13
+
14
+ @page_title ||= "Customer #{current_user.to_s}"
15
+ end
16
+
17
+ def update
18
+ @customer = Effective::Customer.where(user: current_user).first!
19
+ EffectiveOrders.authorize!(self, :update, @customer)
20
+
21
+ @subscripter = Effective::Subscripter.new(customer: @customer, user: @customer.user)
22
+ @subscripter.assign_attributes(subscripter_params)
23
+
24
+ @page_title ||= "Customer #{current_user.to_s}"
25
+
26
+ if (@subscripter.save! rescue false)
27
+ flash[:success] = "Successfully updated customer settings"
28
+ redirect_to(effective_orders.customer_settings_path)
29
+ else
30
+ flash.now[:danger] = "Unable to update customer settings: #{@subscripter.errors.full_messages.to_sentence}"
31
+ render :edit
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ # StrongParameters
38
+ def subscripter_params
39
+ params.require(:effective_subscripter).permit(:stripe_token)
40
+ end
41
+
42
+ end
43
+ end
@@ -2,38 +2,31 @@ module Effective
2
2
  class OrdersController < ApplicationController
3
3
  include EffectiveCartsHelper
4
4
 
5
+ include Concerns::Purchase
6
+
7
+ include Providers::AppCheckout if EffectiveOrders.app_checkout_enabled
8
+ include Providers::Ccbill if EffectiveOrders.ccbill_enabled
5
9
  include Providers::Cheque if EffectiveOrders.cheque_enabled
10
+ include Providers::Free if EffectiveOrders.allow_free_orders
11
+ include Providers::MarkAsPaid if EffectiveOrders.mark_as_paid_enabled
6
12
  include Providers::Moneris if EffectiveOrders.moneris_enabled
7
13
  include Providers::Paypal if EffectiveOrders.paypal_enabled
8
- include Providers::Stripe if EffectiveOrders.stripe_enabled
9
- include Providers::StripeConnect if EffectiveOrders.stripe_connect_enabled
10
- include Providers::Ccbill if EffectiveOrders.ccbill_enabled
11
- include Providers::AppCheckout if EffectiveOrders.app_checkout_enabled
12
-
13
14
  include Providers::Pretend if EffectiveOrders.allow_pretend_purchase_in_development && !Rails.env.production?
14
15
  include Providers::Pretend if EffectiveOrders.allow_pretend_purchase_in_production && Rails.env.production?
16
+ include Providers::Refund if EffectiveOrders.allow_refunds
17
+ include Providers::Stripe if EffectiveOrders.stripe_enabled
18
+ include Providers::StripeConnect if EffectiveOrders.stripe_connect_enabled
15
19
 
16
20
  layout (EffectiveOrders.layout.kind_of?(Hash) ? EffectiveOrders.layout[:orders] : EffectiveOrders.layout)
17
21
 
18
- if defined?(Devise)
19
- if respond_to?(:before_action)
20
- before_action :authenticate_user!, except: [:paypal_postback, :ccbill_postback]
21
- else
22
- before_filter :authenticate_user!, except: [:paypal_postback, :ccbill_postback]
23
- end
24
- end
25
-
26
- if respond_to?(:before_action)
27
- before_action :set_page_title, except: [:show]
28
- else
29
- before_filter :set_page_title, except: [:show]
30
- end
22
+ before_action :authenticate_user!, except: [:ccbill_postback, :free, :paypal_postback, :pretend]
23
+ before_action :set_page_title, except: [:show]
31
24
 
32
- # This is the entry point for the "Checkout" buttons
25
+ # This is the entry point for any Checkout button
33
26
  def new
34
27
  @order ||= Effective::Order.new(current_cart, user: current_user)
35
28
 
36
- EffectiveOrders.authorized?(self, :new, @order)
29
+ EffectiveOrders.authorize!(self, :new, @order)
37
30
 
38
31
  # We're only going to check for a subset of errors on this step,
39
32
  # with the idea that we don't want to create an Order object if the Order is totally invalid
@@ -44,116 +37,87 @@ module Effective
44
37
  redirect_to(effective_orders.cart_path)
45
38
  return
46
39
  elsif @order.errors[:total].present?
47
- flash[:danger] = @order.errors[:total].first.gsub(EffectiveOrders.minimum_charge.to_i.to_s, view_context.price_to_currency(EffectiveOrders.minimum_charge.to_i))
40
+ flash[:danger] = @order.errors[:total].first
48
41
  redirect_to(effective_orders.cart_path)
49
42
  return
50
43
  end
51
44
 
52
45
  @order.errors.clear
53
- @order.billing_address.try(:errors).try(:clear)
54
- @order.shipping_address.try(:errors).try(:clear)
55
-
56
- render :checkout_step1
46
+ @order.billing_address.errors.clear if @order.billing_address
47
+ @order.shipping_address.errors.clear if @order.shipping_address
57
48
  end
58
49
 
59
- def edit
60
- @order ||= Effective::Order.find(params[:id])
50
+ def create
51
+ @order ||= Effective::Order.new(current_cart, user: current_user)
52
+ EffectiveOrders.authorize!(self, :create, @order)
61
53
 
62
- EffectiveOrders.authorized?(self, :edit, @order)
54
+ @order.assign_attributes(checkout_params) if params[:effective_order]
63
55
 
64
- render :checkout_step1
65
- end
56
+ Effective::Order.transaction do
57
+ begin
58
+ @order.save!
59
+ redirect_to(effective_orders.order_path(@order)) and return
60
+ rescue => e
61
+ raise ActiveRecord::Rollback
62
+ end
63
+ end
66
64
 
67
- def create
68
- @order ||= Effective::Order.new(current_cart, user: current_user)
69
- save_order_and_redirect_to_step2
65
+ flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
66
+ render :new
70
67
  end
71
68
 
72
- # If there is an existing order, it will be posted to the /update action, instead of /create
73
- def update
69
+ def edit
74
70
  @order ||= Effective::Order.find(params[:id])
75
- save_order_and_redirect_to_step2
71
+ EffectiveOrders.authorize!(self, :edit, @order)
76
72
  end
77
73
 
78
- def save_order_and_redirect_to_step2
79
- (redirect_to effective_orders.cart_path and return) if (@order.blank? || current_user.blank?)
80
-
81
- @order.attributes = order_params
82
- @order.user_id = current_user.id
83
-
84
- EffectiveOrders.authorized?(self, (@order.persisted? ? :update : :create), @order)
74
+ def update
75
+ @order ||= Effective::Order.find(params[:id])
76
+ EffectiveOrders.authorize!(self, :update, @order)
85
77
 
86
- @order.valid? # This makes sure the correct shipping_address is copied from billing_address if shipping_address_same_as_billing
78
+ @order.assign_attributes(checkout_params)
87
79
 
88
80
  Effective::Order.transaction do
89
81
  begin
90
- if @order.save_billing_address? && @order.user.respond_to?(:billing_address=) && @order.billing_address.present?
91
- @order.user.billing_address = @order.billing_address
92
- end
93
-
94
- if @order.save_shipping_address? && @order.user.respond_to?(:shipping_address=) && @order.shipping_address.present?
95
- @order.user.shipping_address = @order.shipping_address
96
- end
97
-
98
82
  @order.save!
99
-
100
- if @order.total == 0 && EffectiveOrders.allow_free_orders
101
- order_purchased(details: 'automatic purchase of free order', provider: 'free', card: 'none')
102
- else
103
- redirect_to(effective_orders.order_path(@order)) # This goes to checkout_step2
104
- end
105
-
106
- return true
83
+ redirect_to(effective_orders.order_path(@order)) and return
107
84
  rescue => e
108
- Rails.logger.info e.message
109
- flash.now[:danger] = "Unable to save order: #{@order.errors.full_messages.to_sentence}. Please try again."
110
85
  raise ActiveRecord::Rollback
111
86
  end
112
87
  end
113
88
 
114
- # Fall back to checkout step 1
115
- render :checkout_step1
89
+ flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
90
+ render :edit
116
91
  end
117
92
 
118
93
  def show
119
94
  @order = Effective::Order.find(params[:id])
120
- EffectiveOrders.authorized?(self, :show, @order)
121
-
122
- @page_title ||= (
123
- if @order.purchased?
124
- 'Receipt'
125
- elsif @order.user != current_user
126
- @order.pending? ? "Pending Order ##{@order.to_param}" : "Order ##{@order.to_param}"
127
- else
128
- 'Checkout'
129
- end
130
- )
95
+ EffectiveOrders.authorize!(self, :show, @order)
131
96
 
132
- render(:checkout_step2) if @order.purchased? == false && @order.user == current_user
97
+ @page_title ||= ((@order.user == current_user && !@order.purchased?) ? 'Checkout' : @order.to_s)
133
98
  end
134
99
 
135
100
  def index
136
- @orders = Effective::Order.purchased_by(current_user)
137
- @pending_orders = Effective::Order.pending.where(user: current_user)
101
+ @orders = Effective::Order.deep.purchased_by(current_user)
102
+ @pending_orders = Effective::Order.deep.pending.where(user: current_user)
138
103
 
139
- EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
104
+ EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
140
105
  end
141
106
 
142
107
  # Basically an index page.
143
108
  # Purchases is an Order History page. List of purchased orders
144
109
  def my_purchases
145
- @orders = Effective::Order.purchased_by(current_user)
146
-
147
- EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
110
+ @orders = Effective::Order.deep.purchased_by(current_user)
111
+ EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
148
112
  end
149
113
 
150
114
  # Sales is a list of what products beign sold by me have been purchased
151
115
  def my_sales
152
- @order_items = Effective::OrderItem.sold_by(current_user)
153
- EffectiveOrders.authorized?(self, :index, Effective::Order.new(user: current_user))
116
+ @order_items = Effective::OrderItem.deep.sold_by(current_user)
117
+ EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
154
118
  end
155
119
 
156
- # Thank you for Purchasing this Order. This is where a successfully purchased order ends up
120
+ # Thank you for Purchasing this Order. This is where a successfully purchased order ends up
157
121
  def purchased # Thank You!
158
122
  @order = if params[:id].present?
159
123
  Effective::Order.find(params[:id])
@@ -162,73 +126,63 @@ module Effective
162
126
  end
163
127
 
164
128
  if @order.blank?
165
- redirect_to(effective_orders.my_purchases_path) and return
129
+ redirect_to(effective_orders.my_purchases_orders_path) and return
166
130
  end
167
131
 
168
- EffectiveOrders.authorized?(self, :show, @order)
132
+ EffectiveOrders.authorize!(self, :show, @order)
169
133
 
170
134
  redirect_to(effective_orders.order_path(@order)) unless @order.purchased?
171
135
  end
172
136
 
173
- # An error has occurred, please try again
174
- def declined # An error occurred!
137
+ def declined
175
138
  @order = Effective::Order.find(params[:id])
176
- EffectiveOrders.authorized?(self, :show, @order)
139
+ EffectiveOrders.authorize!(self, :show, @order)
177
140
 
178
141
  redirect_to(effective_orders.order_path(@order)) unless @order.declined?
179
142
  end
180
143
 
181
- def resend_buyer_receipt
144
+ def send_buyer_receipt
182
145
  @order = Effective::Order.find(params[:id])
183
- EffectiveOrders.authorized?(self, :show, @order)
146
+ EffectiveOrders.authorize!(self, :show, @order)
184
147
 
185
148
  if @order.send_order_receipt_to_buyer!
186
- flash[:success] = "Successfully sent receipt to #{@order.user.email}"
149
+ flash[:success] = "A receipt has been sent to #{@order.user.email}"
187
150
  else
188
151
  flash[:danger] = "Unable to send receipt."
189
152
  end
190
153
 
191
- redirect_to(request.referer.present? ? :back : effective_orders.order_path(@order))
154
+ if respond_to?(:redirect_back)
155
+ redirect_back(fallback_location: effective_orders.order_path(@order))
156
+ elsif request.referrer.present?
157
+ redirect_to :back
158
+ else
159
+ redirect_to effective_orders.order_path(@order)
160
+ end
192
161
  end
193
162
 
194
- protected
163
+ def bulk_send_buyer_receipt
164
+ @orders = Effective::Order.purchased.where(id: params[:ids])
195
165
 
196
- def order_purchased(details: 'none', provider:, card: 'none', redirect_url: nil, declined_redirect_url: nil)
197
166
  begin
198
- @order.purchase!(details: details, provider: provider, card: card)
167
+ EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
199
168
 
200
- Effective::Cart.where(user_id: @order.user_id).try(:destroy_all) # current_cart won't work for provider post backs here
169
+ @orders.each do |order|
170
+ next unless (EffectiveOrders.authorize!(self, :show, order) rescue false)
201
171
 
202
- if EffectiveOrders.mailer[:send_order_receipt_to_buyer]
203
- flash[:success] = "Payment successful! Please check your email for a receipt."
204
- else
205
- flash[:success] = "Payment successful!"
172
+ order.send_order_receipt_to_buyer!
206
173
  end
207
174
 
208
- redirect_to (redirect_url.presence || effective_orders.order_purchased_path(':id')).gsub(':id', @order.to_param.to_s)
175
+ render json: { status: 200, message: "Successfully sent #{@orders.length} receipt emails"}
209
176
  rescue => e
210
- flash[:danger] = "An error occurred while processing your payment: #{e.message}. Please try again."
211
- redirect_to(declined_redirect_url.presence || effective_orders.cart_path).gsub(':id', @order.to_param.to_s)
177
+ render json: { status: 500, message: "Bulk send buyer receipt error: #{e.message}" }
212
178
  end
213
179
  end
214
180
 
215
- def order_declined(details: 'none', provider:, card: 'none', redirect_url: nil, message: nil)
216
- @order.decline!(details: details, provider: provider, card: card) rescue nil
217
-
218
- flash[:danger] = message.presence || 'Payment was unsuccessful. Your credit card was declined by the payment processor. Please try again.'
219
-
220
- redirect_to(redirect_url.presence || effective_orders.order_declined_path(@order)).gsub(':id', @order.id.to_s)
221
- end
222
-
223
181
  private
224
182
 
225
183
  # StrongParameters
226
- def order_params
227
- begin
228
- params.require(:effective_order).permit(EffectiveOrders.permitted_params)
229
- rescue => e
230
- params[:effective_order] || {}
231
- end
184
+ def checkout_params
185
+ params.require(:effective_order).permit(EffectiveOrders.permitted_params)
232
186
  end
233
187
 
234
188
  def set_page_title