effective_orders 3.2.3 → 4.0.0beta1

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 +5 -5
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +11 -99
  4. data/app/assets/stylesheets/effective_orders.scss +1 -0
  5. data/app/assets/stylesheets/effective_orders/_cart.scss +4 -0
  6. data/app/assets/stylesheets/effective_orders/_order.scss +6 -2
  7. data/app/controllers/admin/orders_controller.rb +17 -17
  8. data/app/controllers/effective/carts_controller.rb +4 -2
  9. data/app/controllers/effective/orders_controller.rb +50 -105
  10. data/app/controllers/effective/providers/cheque.rb +1 -1
  11. data/app/controllers/effective/providers/moneris.rb +17 -23
  12. data/app/controllers/effective/providers/stripe.rb +2 -37
  13. data/app/datatables/effective_customers_datatable.rb +1 -5
  14. data/app/datatables/effective_order_items_datatable.rb +10 -10
  15. data/app/datatables/effective_orders_datatable.rb +23 -46
  16. data/app/helpers/effective_carts_helper.rb +5 -25
  17. data/app/helpers/effective_orders_helper.rb +29 -61
  18. data/app/helpers/effective_paypal_helper.rb +10 -8
  19. data/app/helpers/effective_stripe_helper.rb +2 -33
  20. data/app/models/concerns/acts_as_purchasable.rb +26 -37
  21. data/app/models/concerns/acts_as_subscribable.rb +1 -1
  22. data/app/models/effective/cart.rb +4 -5
  23. data/app/models/effective/customer.rb +1 -4
  24. data/app/models/effective/order.rb +132 -163
  25. data/app/models/effective/order_item.rb +2 -21
  26. data/app/models/{effective → validators/effective}/sold_out_validator.rb +0 -0
  27. data/app/views/admin/customers/index.html.haml +1 -4
  28. data/app/views/admin/order_items/index.html.haml +1 -4
  29. data/app/views/admin/orders/_actions.html.haml +18 -9
  30. data/app/views/admin/orders/_form.html.haml +12 -14
  31. data/app/views/admin/orders/_form_note_internal.html.haml +2 -2
  32. data/app/views/admin/orders/_order_actions.html.haml +8 -5
  33. data/app/views/admin/orders/_order_item_fields.html.haml +9 -8
  34. data/app/views/admin/orders/checkout.html.haml +3 -0
  35. data/app/views/admin/orders/edit.html.haml +3 -1
  36. data/app/views/admin/orders/new.html.haml +2 -1
  37. data/app/views/admin/orders/show.html.haml +2 -5
  38. data/app/views/effective/carts/_cart.html.haml +1 -1
  39. data/app/views/effective/carts/_cart_actions.html.haml +3 -4
  40. data/app/views/effective/carts/show.html.haml +4 -4
  41. data/app/views/effective/orders/_checkout_actions.html.haml +3 -0
  42. data/app/views/effective/orders/_checkout_step1.html.haml +20 -27
  43. data/app/views/effective/orders/_checkout_step2.html.haml +26 -30
  44. data/app/views/effective/orders/_order.html.haml +1 -2
  45. data/app/views/effective/orders/_order_actions.html.haml +14 -6
  46. data/app/views/effective/orders/_order_header.html.haml +2 -2
  47. data/app/views/effective/orders/_order_note_fields.html.haml +6 -4
  48. data/app/views/effective/orders/_order_notes.html.haml +15 -0
  49. data/app/views/effective/orders/_order_payment_details.html.haml +1 -1
  50. data/app/views/effective/orders/_order_shipping.html.haml +6 -6
  51. data/app/views/effective/orders/_order_terms_and_conditions_fields.html.haml +5 -6
  52. data/app/views/effective/orders/_order_user_fields.html.haml +5 -11
  53. data/app/views/effective/orders/_orders_table.html.haml +2 -6
  54. data/app/views/effective/orders/index.html.haml +2 -1
  55. data/app/views/effective/orders/mark_as_paid/_form.html.haml +8 -14
  56. data/app/views/effective/orders/moneris/_form.html.haml +43 -4
  57. data/app/views/effective/orders/pretend/_form.html.haml +1 -3
  58. data/app/views/effective/orders/refund/_form.html.haml +8 -12
  59. data/app/views/effective/orders/show.html.haml +2 -2
  60. data/app/views/effective/orders/stripe/_form.html.haml +4 -4
  61. data/app/views/effective/orders_mailer/payment_request_to_buyer.html.haml +1 -1
  62. data/app/views/effective/orders_mailer/pending_order_invoice_to_buyer.html.haml +1 -1
  63. data/config/effective_orders.rb +116 -182
  64. data/config/routes.rb +16 -27
  65. data/db/migrate/01_create_effective_orders.rb.erb +6 -5
  66. data/lib/effective_orders.rb +72 -76
  67. data/lib/effective_orders/engine.rb +8 -80
  68. data/lib/effective_orders/version.rb +1 -1
  69. data/lib/generators/templates/effective_orders_mailer_preview.rb +4 -4
  70. metadata +26 -38
  71. data/Rakefile +0 -21
  72. data/app/assets/config/effective_orders_manifest.js +0 -3
  73. data/app/assets/images/effective_orders/stripe_connect.png +0 -0
  74. data/app/controllers/effective/providers/app_checkout.rb +0 -38
  75. data/app/controllers/effective/providers/ccbill.rb +0 -34
  76. data/app/controllers/effective/providers/stripe_connect.rb +0 -47
  77. data/app/helpers/effective_ccbill_helper.rb +0 -25
  78. data/app/models/effective/providers/ccbill_postback.rb +0 -85
  79. data/app/models/effective/providers/moneris_charge.rb +0 -115
  80. data/app/views/effective/orders/_order_note.html.haml +0 -5
  81. data/app/views/effective/orders/_order_note_to_buyer.html.haml +0 -9
  82. data/app/views/effective/orders/app_checkout/_form.html.haml +0 -2
  83. data/app/views/effective/orders/ccbill/_form.html.haml +0 -24
  84. data/app/views/effective/orders/my_purchases.html.haml +0 -3
  85. data/app/views/effective/orders/my_sales.html.haml +0 -25
  86. data/app/views/effective/orders_mailer/order_receipt_to_seller.html.haml +0 -25
  87. data/lib/effective_orders/app_checkout_service.rb +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 031a1810c8fc9ae6aa2909a0ee16cf73ad9b90b6a0aee17a353aafa033a39d7a
4
- data.tar.gz: 652fad3647b97f20d0f950fa9da4d989111eee33f98d5110ce9b70aa421208e0
2
+ SHA1:
3
+ metadata.gz: e0cf749b4cef2660e59b16b802211c75e79e7400
4
+ data.tar.gz: c4c452a35a548596146b2fdf099916065361dd55
5
5
  SHA512:
6
- metadata.gz: b1c8c8e6b0554a22e6a091ab4ba206a33e66ef775172961df346b10ac5898b0d16a04d7d1469cef3dc3bc77ec5b8137e6cb2b349c690eb46c67eb0d4c9bf78cc
7
- data.tar.gz: c4187ce976881522ac9c45448c0c2f58cb8cb18211f8254f33e8e8dc1e9bfcee075e61e58e0dab36a375506f450665bb80888ae1a7b95a3e561754b35ea9336b
6
+ metadata.gz: af34590308483e7db60eb579b952167622809d2c0cfb9c1101221800e0a3ce51e5ffe6d8d7895755f7fe18282ac379e6de0406f47802c445e16a2492fd838a7a
7
+ data.tar.gz: c872866386ab0490fd154cb8caf59119c75ea61cb2f601049d92a75ec0d9a97a7f4e939563a13807b53f3440fe8a0d636c40326da2aa6b89f2858a4fa321f73e
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2019 Code and Effect Inc.
1
+ Copyright 2018 Code and Effect Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -4,20 +4,19 @@ Carts, Orders, and collecting payment via Stripe, PayPal and Moneris.
4
4
 
5
5
  A Rails Engine to handle the purchase workflow in a Rails 3.2.x / Rails 4 application.
6
6
 
7
- Also works with Stripe Connect and Stripe Subscriptions with coupons.
7
+ Also works with Stripe Subscriptions.
8
8
 
9
9
  Sends order receipt emails automatically.
10
10
 
11
11
  Has Order History, My Purchases, My Sales and Admin screens.
12
12
 
13
- ## Bootstrap3
13
+ ## effective_orders 4.0
14
14
 
15
- This is the `bootstrap3` branch of effective_orders which supports Twitter Bootstrap 3.
15
+ This is the 4.0 series of effective_orders.
16
16
 
17
- All published effective_orders 3.x gems will support Twitter Bootstrap 3 and SimpleForm.
18
-
19
- For Bootstrap 4 please see the master branch and/or effective_orders 4.x gems.
17
+ This requires Twitter Bootstrap 4 and Rails 5.1+
20
18
 
19
+ Please check out [Effective Orders 3.x](https://github.com/code-and-effect/effective_orders/tree/bootstrap3) for more information using this gem with Bootstrap 3.
21
20
 
22
21
  ## Getting Started
23
22
 
@@ -26,7 +25,7 @@ Please first install the [effective_addresses](https://github.com/code-and-effec
26
25
  Add to your Gemfile:
27
26
 
28
27
  ```ruby
29
- gem 'effective_orders', '~> 3.0'
28
+ gem 'effective_orders'
30
29
  ```
31
30
 
32
31
  Run the bundle command to install it:
@@ -264,10 +263,6 @@ acts_as_purchsable provides the following scopes:
264
263
 
265
264
  `Product.purchased_by(user)` all the Products purchased by a given user.
266
265
 
267
- `Product.sold` all the Products that have been solid (same as purchased)
268
-
269
- `Product.sold_by(user)` all the Products that this user has sold via Stripe Connect
270
-
271
266
  `Product.not_purchased` all unpurchased Products
272
267
 
273
268
  ### Digital Downloads
@@ -464,7 +459,7 @@ And call `render_cart(current_cart)` to display the Cart anywhere.
464
459
 
465
460
  On the Checkout page (`effective_orders.new_order_path`) a new `Effective::Order` object is created and one or more `Effective::OrderItem`s are initialized based on the `current_cart`.
466
461
 
467
- If the configuration options `config.require_billing_address` and/or `config.require_shipping_address` options are `true` then the user will be prompted for the appropriate addresses, based on [effective_addresses](https://github.com/code-and-effect/effective_addresses/).
462
+ If the configuration options `config.billing_address` and/or `config.shipping_address` options are `true` then the user will be prompted for the appropriate addresses, based on [effective_addresses](https://github.com/code-and-effect/effective_addresses/).
468
463
 
469
464
  If `config.use_address_full_name` is set to `true` then appropriate form field will be shown and the user will be prompted for the appropriate address full name during the checkout process, based on [effective_addresses](https://github.com/code-and-effect/effective_addresses/).
470
465
 
@@ -512,7 +507,7 @@ The `acts_as_purchasable` `.purchased?` and `.purchased_by?(user)` methods only
512
507
  To programatically purchase one or more `acts_as_purchasable` objects:
513
508
 
514
509
  ```ruby
515
- Effective::Order.new(items: [@product1, @product2], user: current_user).purchase!(details: 'from my rake task')
510
+ Effective::Order.new(@product1, @product2, user: current_user).purchase!(details: 'from my rake task')
516
511
  ```
517
512
 
518
513
  Here the `billing_address` and `shipping_address` are copied from the `current_user` object if the `current_user` responds_to `billing_address` / `shipping_address` as per [effective_addresses](https://github.com/code-and-effect/effective_addresses/).
@@ -541,7 +536,7 @@ The one gotcha with the above two scenarios, is that when `purchase!` is called,
541
536
  You can skip validations with the following command, but be careful as this skips all validations:
542
537
 
543
538
  ```ruby
544
- Effective::Order.new(item: @product, user: @user).purchase!(validate: false)
539
+ Effective::Order.new(@product, user: @user).purchase!(validate: false)
545
540
  ```
546
541
 
547
542
  The `@product` is now considered purchased.
@@ -554,32 +549,10 @@ There also exist the scopes: `Effective::Order.purchased` and `Effective::Order.
554
549
 
555
550
  ### My Purchases / Order History
556
551
 
557
- This screen displays all past purchases made by the current user. You can add it to your site's main menu or User profile area:
558
-
559
- ```ruby
560
- = link_to_my_purchases() # To display My Purchases
561
- ```
562
-
563
- or
564
-
565
- ```ruby
566
- = link_to_my_purchases(label: 'Order History', class: 'btn btn-primary')
567
- ```
568
-
569
- or
570
-
571
552
  ```ruby
572
- = link_to 'My Order History', effective_orders.my_purchases_orders_path
553
+ = link_to 'Order History', effective_orders.orders_path
573
554
  ```
574
555
 
575
- or render it inline on an existing page with
576
-
577
- ```ruby
578
- render_order_history(user_or_orders)
579
- ```
580
-
581
- If a user is passed, a call to `Effective::Order.purchased_by(user)` will be made to assign all purchased orders.
582
-
583
556
  Totally optional, but another way of displaying the Order History is to use the included datatable, based on [effective_datatables](https://github.com/code-and-effect/effective_datatables/)
584
557
 
585
558
  In your controller:
@@ -829,58 +802,6 @@ You an find these keys from the Stripe Dashbaord -> Your Account (dropdown) -> A
829
802
 
830
803
  You're ready to accept payments.
831
804
 
832
- ### Stripe Connect
833
-
834
- Stripe Connect allows effective_orders to collect payments on behalf of users.
835
-
836
- First register your application with Stripe
837
-
838
- Stripe Dashboard -> Your Account (dropdown) -> Account Settings -> Apps
839
-
840
- Register your application
841
-
842
- Name: Your Application Name
843
- Website URL: root_url
844
-
845
- Development:
846
-
847
- client_id: given by stripe, we need to record this.
848
- redirect_uri: stripe_connect_redirect_uri_url # http://www.example.com/effective/orders/stripe_connect_redirect_uri
849
- webhook_uri: none
850
-
851
- And add these values to the app/config/effective_orders.rb initializer:
852
-
853
- ```ruby
854
- config.stripe = {
855
- secret_key: 'sk_live_IKd6HDaYUfoRjflWQTXfFNfc',
856
- publishable_key: 'pk_live_liEGn9f0mcxKmoSjoeNbbuE1',
857
- currency: 'usd',
858
- connect_client_id: 'ca_35jLok5G9kosyYF7quTOwcauJjTnUnud'
859
- }
860
- ```
861
-
862
- There are a few additional steps you need to take on the rails application side of things:
863
-
864
-
865
- Before allowing one of your Users to create a Product for sale, you must enforce that they have a Stripe Connect account setup and configured.
866
-
867
- You can check if they have an account set up using the built in helper `is_stripe_connect_seller?(current_user)`
868
-
869
- If the above check returns false, you must send them to Stripe to set up their StripeConnect account, using the built in helper `link_to_new_stripe_connect_customer`
870
-
871
- Once they've registered their account on the Stripe side, Stripe sends a webhook request, which is processed by the `webhooks_controller.rb`
872
-
873
- In the webhook controller, an `Effective::Customer` object is created, and your user is now ready to sell stuff via StripeConnect.
874
-
875
-
876
- Your product model must also define a `seller` method so that effective_orders knows who is selling the Product. Add the following to your `acts_as_purchasable` model:
877
-
878
- ```ruby
879
- def seller
880
- User.find(user_id)
881
- end
882
- ```
883
-
884
805
  ### Stripe Subscriptions
885
806
 
886
807
  To set up stripe subscriptions:
@@ -935,7 +856,7 @@ To set up stripe:
935
856
 
936
857
  4.) Click API -> Webhooks and add an endpoint `root_url/webhooks/stripe`. You will need something like ngrok to test this.
937
858
 
938
- 5.) Record the webhook Signing secret in `config.subscription[:webhook_secret]`
859
+ 5.) Record the webhook Signing secret in `config.subscriptions[:webhook_secret]`
939
860
 
940
861
 
941
862
  ## Paying Via PayPal
@@ -1154,15 +1075,6 @@ end
1154
1075
 
1155
1076
  MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
1156
1077
 
1157
- ## Testing
1158
-
1159
- Run tests by:
1160
-
1161
- ```ruby
1162
- rspec
1163
- ```
1164
-
1165
-
1166
1078
  ## Contributing
1167
1079
 
1168
1080
  1. Fork it
@@ -1,2 +1,3 @@
1
+ @import 'effective_orders/cart';
1
2
  @import 'effective_orders/order';
2
3
  @import 'effective_orders/subscriptions';
@@ -0,0 +1,4 @@
1
+ .effective-cart {
2
+ .subtotal { text-align: right; }
3
+ }
4
+
@@ -28,7 +28,11 @@
28
28
  // Print - Resend Receipt. Ontop of order.
29
29
  .effective-order-actions {
30
30
  text-align: right;
31
- margin-bottom: 10px;
31
+ margin-bottom: 0.5rem;
32
+ }
33
+
34
+ .effective-order-change-items {
35
+ margin-bottom: 1rem;
32
36
  }
33
37
 
34
38
  // Checkout buttons. Bottom of order.
@@ -41,7 +45,7 @@
41
45
  }
42
46
 
43
47
  .effective-order-admin-purchase-actions {
44
- margin-top: 10px;
48
+ margin-top: 0.5rem;
45
49
  }
46
50
 
47
51
  form.new_effective_order {
@@ -4,6 +4,10 @@ module Admin
4
4
 
5
5
  layout (EffectiveOrders.layout.kind_of?(Hash) ? EffectiveOrders.layout[:admin_orders] : EffectiveOrders.layout)
6
6
 
7
+ # def effective_resource
8
+ # @_effective_resource ||= Effective::Resource.new('effective/order', namespace: :admin)
9
+ # end
10
+
7
11
  def new
8
12
  @order = Effective::Order.new
9
13
 
@@ -40,8 +44,7 @@ module Admin
40
44
  end
41
45
 
42
46
  @order.attributes = order_params.except(:order_items_attributes, :user_id)
43
-
44
- @order.create_as_pending!
47
+ @order.pending!
45
48
 
46
49
  message = 'Successfully created order'
47
50
  message << ". A request for payment has been sent to #{@order.user.email}" if @order.send_payment_request_to_buyer?
@@ -55,7 +58,7 @@ module Admin
55
58
  end
56
59
 
57
60
  @page_title = 'New Order'
58
- flash.now[:danger] = "Unable to create order: #{error || @order.errors.full_messages.to_sentence}"
61
+ flash.now[:danger] = flash_danger(@order)
59
62
  render :new
60
63
  end
61
64
 
@@ -68,7 +71,6 @@ module Admin
68
71
 
69
72
  def update
70
73
  @order = Effective::Order.find(params[:id])
71
- @order.skip_minimum_charge_validation = true if @order.refund?
72
74
 
73
75
  @page_title ||= @order.to_s
74
76
 
@@ -90,7 +92,6 @@ module Admin
90
92
 
91
93
  def show
92
94
  @order = Effective::Order.find(params[:id])
93
- @order.skip_minimum_charge_validation = true if @order.refund?
94
95
 
95
96
  @page_title ||= @order.to_s
96
97
 
@@ -101,24 +102,26 @@ module Admin
101
102
  # See Effective::OrdersController checkout
102
103
  def checkout
103
104
  @order = Effective::Order.find(params[:id])
104
- @order.skip_minimum_charge_validation = true if @order.refund?
105
-
106
- @page_title ||= 'Checkout'
107
105
 
108
106
  authorize_effective_order!
109
107
 
108
+ if request.get?
109
+ @order.assign_confirmed_if_valid!
110
+ render :checkout and return
111
+ end
112
+
110
113
  Effective::Order.transaction do
111
114
  begin
112
115
  @order.assign_attributes(checkout_params)
113
- @order.save!
114
- redirect_to(effective_orders.admin_order_path(@order)) and return
116
+ @order.confirm!
117
+ redirect_to(effective_orders.checkout_admin_order_path(@order)) and return
115
118
  rescue => e
116
119
  raise ActiveRecord::Rollback
117
120
  end
118
121
  end
119
122
 
120
- flash.now[:danger] = "Unable to save order: #{@order.errors.full_messages.to_sentence}. Please try again."
121
- render :show
123
+ flash.now[:danger] = "Unable to proceed: #{flash_errors(@order)}. Please try again."
124
+ render :checkout
122
125
  end
123
126
 
124
127
  def index
@@ -130,7 +133,7 @@ module Admin
130
133
  end
131
134
 
132
135
  def destroy
133
- @order = Effective::Order.find(params[:id])
136
+ @order = Effective::Order.all.not_purchased.find(params[:id])
134
137
 
135
138
  authorize_effective_order!
136
139
 
@@ -209,13 +212,10 @@ module Admin
209
212
  case params[:commit].to_s
210
213
  when 'Save' ; effective_orders.admin_order_path(@order)
211
214
 
212
- when 'Save and Continue' ; effective_orders.admin_orders_path
213
- when 'Save and Add New' ; effective_orders.new_admin_order_path(user_id: @order.user.try(:to_param))
214
- when 'Save and Duplicate' ; effective_orders.new_admin_order_path(duplicate_id: @order.to_param)
215
-
216
215
  when 'Continue' ; effective_orders.admin_orders_path
217
216
  when 'Add New' ; effective_orders.new_admin_order_path(user_id: @order.user.try(:to_param))
218
217
  when 'Duplicate' ; effective_orders.new_admin_order_path(duplicate_id: @order.to_param)
218
+ when 'Checkout' ; effective_orders.checkout_admin_order_path(@order.to_param)
219
219
 
220
220
  else effective_orders.admin_order_path(@order)
221
221
  end
@@ -1,7 +1,5 @@
1
1
  module Effective
2
2
  class CartsController < ApplicationController
3
- include EffectiveCartsHelper
4
-
5
3
  layout (EffectiveOrders.layout.kind_of?(Hash) ? EffectiveOrders.layout[:carts] : EffectiveOrders.layout)
6
4
 
7
5
  def show
@@ -61,6 +59,10 @@ module Effective
61
59
 
62
60
  private
63
61
 
62
+ def current_cart
63
+ view_context.current_cart
64
+ end
65
+
64
66
  def add_to_cart_params
65
67
  params.permit(:purchasable_type, :purchasable_id, :quantity)
66
68
  end
@@ -1,137 +1,105 @@
1
1
  module Effective
2
2
  class OrdersController < ApplicationController
3
- include EffectiveCartsHelper
4
-
5
3
  include Concerns::Purchase
6
4
 
7
- include Providers::AppCheckout if EffectiveOrders.app_checkout_enabled
8
- include Providers::Ccbill if EffectiveOrders.ccbill_enabled
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
12
- include Providers::Moneris if EffectiveOrders.moneris_enabled
13
- include Providers::Paypal if EffectiveOrders.paypal_enabled
14
- include Providers::Pretend if EffectiveOrders.allow_pretend_purchase_in_development && !Rails.env.production?
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
5
+ include Providers::Cheque if EffectiveOrders.cheque?
6
+ include Providers::Free if EffectiveOrders.free?
7
+ include Providers::MarkAsPaid if EffectiveOrders.mark_as_paid?
8
+ include Providers::Moneris if EffectiveOrders.moneris?
9
+ include Providers::Paypal if EffectiveOrders.paypal?
10
+ include Providers::Pretend if EffectiveOrders.pretend?
11
+ include Providers::Refund if EffectiveOrders.refunds?
12
+ include Providers::Stripe if EffectiveOrders.stripe?
19
13
 
20
14
  layout (EffectiveOrders.layout.kind_of?(Hash) ? EffectiveOrders.layout[:orders] : EffectiveOrders.layout)
21
15
 
22
- before_action :authenticate_user!, except: [:ccbill_postback, :free, :moneris_postback, :paypal_postback, :pretend]
16
+ before_action :authenticate_user!, except: [:ccbill_postback, :free, :paypal_postback, :pretend]
23
17
  before_action :set_page_title, except: [:show]
24
18
 
25
- # This is the entry point for any Checkout button
19
+ # If you want to use the Add to Cart -> Checkout flow
20
+ # Add one or more items however you do.
21
+ # redirect_to effective_orders.new_order_path, which is here.
22
+ # This is the entry point for any Checkout button.
23
+ # It displayes an order based on the cart
24
+ # Always step1
26
25
  def new
27
- @order ||= Effective::Order.new(item: current_cart, user: current_user)
26
+ @order ||= Effective::Order.new(view_context.current_cart)
28
27
 
29
28
  EffectiveOrders.authorize!(self, :new, @order)
30
29
 
31
- # We're only going to check for a subset of errors on this step,
32
- # with the idea that we don't want to create an Order object if the Order is totally invalid
33
- @order.valid?
34
-
35
- if @order.errors[:order_items].present?
36
- flash[:danger] = @order.errors[:order_items].first
37
- redirect_to(effective_orders.cart_path)
38
- return
39
- elsif @order.errors[:total].present?
40
- flash[:danger] = @order.errors[:total].first
30
+ unless @order.valid?
31
+ flash[:danger] = "Unable to proceed: #{flash_errors(@order)}. Please try again."
41
32
  redirect_to(effective_orders.cart_path)
42
33
  return
43
34
  end
44
-
45
- @order.errors.clear
46
- @order.billing_address.errors.clear if @order.billing_address
47
- @order.shipping_address.errors.clear if @order.shipping_address
48
35
  end
49
36
 
37
+ # Confirms an order from the cart.
50
38
  def create
51
- @order ||= Effective::Order.new(item: current_cart, user: current_user)
39
+ @order ||= Effective::Order.new(view_context.current_cart)
52
40
  EffectiveOrders.authorize!(self, :create, @order)
53
41
 
54
- @order.assign_attributes(checkout_params) if params[:effective_order]
42
+ @order.assign_attributes(checkout_params)
55
43
 
56
- Effective::Order.transaction do
57
- begin
58
- @order.save!
59
- set_redirect_cookie!
60
- redirect_to(effective_orders.order_path(@order)) and return
61
- rescue => e
62
- raise ActiveRecord::Rollback
63
- end
44
+ if (@order.confirm! rescue false)
45
+ redirect_to(effective_orders.order_path(@order))
46
+ else
47
+ flash.now[:danger] = "Unable to proceed: #{flash_errors(@order)}. Please try again."
48
+ render :new
64
49
  end
50
+ end
65
51
 
66
- flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
67
- render :new
52
+ # If you want to use the order = Effective::Order.new(@thing); order.save! flow
53
+ # Add one or more items to the order.
54
+ # redirect_to effective_orders.order_path(order), which is here
55
+ # This is the entry point for an existing order.
56
+ # Might render step1 or step2
57
+ def show
58
+ @order = Effective::Order.find(params[:id])
59
+ EffectiveOrders.authorize!(self, :show, @order)
60
+
61
+ @page_title ||= ((@order.user == current_user && !@order.purchased?) ? 'Checkout' : @order.to_s)
68
62
  end
69
63
 
64
+ # Always step1
70
65
  def edit
71
66
  @order ||= Effective::Order.find(params[:id])
72
67
  EffectiveOrders.authorize!(self, :edit, @order)
73
68
  end
74
69
 
70
+ # Confirms the order from existing order
75
71
  def update
76
72
  @order ||= Effective::Order.find(params[:id])
77
73
  EffectiveOrders.authorize!(self, :update, @order)
78
74
 
79
75
  @order.assign_attributes(checkout_params)
80
76
 
81
- Effective::Order.transaction do
82
- begin
83
- @order.save!
84
- set_redirect_cookie!
85
-
86
- redirect_to(effective_orders.order_path(@order)) and return
87
- rescue => e
88
- raise ActiveRecord::Rollback
89
- end
77
+ if (@order.confirm! rescue false)
78
+ redirect_to(effective_orders.order_path(@order))
79
+ else
80
+ flash.now[:danger] = "Unable to proceed: #{flash_errors(@order)}. Please try again."
81
+ render :edit
90
82
  end
91
-
92
- flash.now[:danger] = "Unable to proceed: #{@order.errors.full_messages.to_sentence}. Please try again."
93
- render :edit
94
- end
95
-
96
- def show
97
- @order = Effective::Order.find(params[:id])
98
- EffectiveOrders.authorize!(self, :show, @order)
99
-
100
- @page_title ||= ((@order.user == current_user && !@order.purchased?) ? 'Checkout' : @order.to_s)
101
83
  end
102
84
 
85
+ # My Orders History
103
86
  def index
104
- @orders = Effective::Order.deep.purchased_by(current_user)
87
+ @orders = Effective::Order.deep.purchased.where(user: current_user)
105
88
  @pending_orders = Effective::Order.deep.pending.where(user: current_user)
106
89
 
107
90
  EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
108
91
  end
109
92
 
110
- # Basically an index page.
111
- # Purchases is an Order History page. List of purchased orders
112
- def my_purchases
113
- @orders = Effective::Order.deep.purchased_by(current_user)
114
- EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
115
- end
116
-
117
- # Sales is a list of what products beign sold by me have been purchased
118
- def my_sales
119
- @order_items = Effective::OrderItem.deep.sold_by(current_user)
120
- EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
121
- end
122
-
123
93
  # Thank you for Purchasing this Order. This is where a successfully purchased order ends up
124
94
  def purchased # Thank You!
125
95
  @order = if params[:id].present?
126
96
  Effective::Order.find(params[:id])
127
97
  elsif current_user.present?
128
- Effective::Order.purchased_by(current_user).first
98
+ Effective::Order.sorted.purchased_by(current_user).last
129
99
  end
130
100
 
131
- clear_redirect_cookie!
132
-
133
101
  if @order.blank?
134
- redirect_to(effective_orders.my_purchases_orders_path) and return
102
+ redirect_to(effective_orders.orders_path) and return
135
103
  end
136
104
 
137
105
  EffectiveOrders.authorize!(self, :show, @order)
@@ -143,8 +111,6 @@ module Effective
143
111
  @order = Effective::Order.find(params[:id])
144
112
  EffectiveOrders.authorize!(self, :show, @order)
145
113
 
146
- clear_redirect_cookie!
147
-
148
114
  redirect_to(effective_orders.order_path(@order)) unless @order.declined?
149
115
  end
150
116
 
@@ -174,7 +140,7 @@ module Effective
174
140
  EffectiveOrders.authorize!(self, :index, Effective::Order.new(user: current_user))
175
141
 
176
142
  @orders.each do |order|
177
- next unless (EffectiveOrders.authorize!(self, :show, order) rescue false)
143
+ next unless EffectiveOrders.authorized?(self, :show, order)
178
144
 
179
145
  order.send_order_receipt_to_buyer!
180
146
  end
@@ -187,25 +153,6 @@ module Effective
187
153
 
188
154
  private
189
155
 
190
- def set_redirect_cookie!
191
- return unless @order.present?
192
-
193
- if params[:purchased_url]
194
- session["effective_orders_#{@order.id}_purchased_url"] = params[:purchased_url]
195
- end
196
-
197
- if params[:declined_url]
198
- session["effective_orders_#{@order.id}_declined_url"] = params[:declined_url]
199
- end
200
- end
201
-
202
- def clear_redirect_cookie!
203
- return unless @order.present?
204
-
205
- session["effective_orders_#{@order.id}_purchased_url"] = nil
206
- session["effective_orders_#{@order.id}_declined_url"] = nil
207
- end
208
-
209
156
  # StrongParameters
210
157
  def checkout_params
211
158
  params.require(:effective_order).permit(EffectiveOrders.permitted_params)
@@ -213,9 +160,7 @@ module Effective
213
160
 
214
161
  def set_page_title
215
162
  @page_title ||= case params[:action]
216
- when 'index' ; 'Orders'
217
- when 'my_purchases' ; 'Order History'
218
- when 'my_sales' ; 'Sales History'
163
+ when 'index' ; 'Order History'
219
164
  when 'purchased' ; 'Thank You'
220
165
  when 'declined' ; 'Payment Declined'
221
166
  else 'Checkout'