solidus_drip 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +26 -0
  3. data/README.md +55 -0
  4. data/Rakefile +32 -0
  5. data/app/assets/javascripts/spree/frontend/solidus_drip.js.erb +11 -0
  6. data/app/decorators/models/solidus_drip/spree/order_decorator.rb +47 -0
  7. data/app/decorators/models/solidus_drip/spree/variant_decorator.rb +25 -0
  8. data/config/initializers/default_host.rb +9 -0
  9. data/config/initializers/update_hooks.rb +3 -0
  10. data/config/locales/en.yml +5 -0
  11. data/config/routes.rb +5 -0
  12. data/lib/generators/solidus_drip/install/install_generator.rb +28 -0
  13. data/lib/solidus_drip.rb +9 -0
  14. data/lib/solidus_drip/base.rb +31 -0
  15. data/lib/solidus_drip/engine.rb +22 -0
  16. data/lib/solidus_drip/factories.rb +8 -0
  17. data/lib/solidus_drip/shopper_activity.rb +6 -0
  18. data/lib/solidus_drip/shopper_activity/order.rb +138 -0
  19. data/lib/solidus_drip/shopper_activity/product.rb +54 -0
  20. data/lib/solidus_drip/version.rb +5 -0
  21. data/spec/dummy/README.md +24 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/assets/config/manifest.js +2 -0
  24. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  25. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  26. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  28. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  29. data/spec/dummy/app/javascript/channels/consumer.js +6 -0
  30. data/spec/dummy/app/javascript/channels/index.js +5 -0
  31. data/spec/dummy/app/javascript/packs/application.js +17 -0
  32. data/spec/dummy/app/jobs/application_job.rb +7 -0
  33. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  34. data/spec/dummy/app/models/application_record.rb +3 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  36. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  37. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  38. data/spec/dummy/bin/rails +4 -0
  39. data/spec/dummy/bin/rake +4 -0
  40. data/spec/dummy/bin/setup +36 -0
  41. data/spec/dummy/bin/yarn +11 -0
  42. data/spec/dummy/config.ru +5 -0
  43. data/spec/dummy/config/application.rb +59 -0
  44. data/spec/dummy/config/boot.rb +6 -0
  45. data/spec/dummy/config/cable.yml +10 -0
  46. data/spec/dummy/config/credentials.yml.enc +1 -0
  47. data/spec/dummy/config/database.yml +12 -0
  48. data/spec/dummy/config/environment.rb +5 -0
  49. data/spec/dummy/config/environments/development.rb +62 -0
  50. data/spec/dummy/config/environments/production.rb +112 -0
  51. data/spec/dummy/config/environments/test.rb +33 -0
  52. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  53. data/spec/dummy/config/initializers/assets.rb +14 -0
  54. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  55. data/spec/dummy/config/initializers/content_security_policy.rb +30 -0
  56. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  57. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  58. data/spec/dummy/config/initializers/inflections.rb +16 -0
  59. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  60. data/spec/dummy/config/initializers/spree.rb +71 -0
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/spec/dummy/config/locales/en.yml +33 -0
  63. data/spec/dummy/config/master.key +1 -0
  64. data/spec/dummy/config/routes.rb +9 -0
  65. data/spec/dummy/config/storage.yml +34 -0
  66. data/spec/dummy/db/migrate/20191101230305_create_active_storage_tables.active_storage.rb +27 -0
  67. data/spec/dummy/db/migrate/20191101230306_create_action_mailbox_tables.action_mailbox.rb +14 -0
  68. data/spec/dummy/db/migrate/20191101230307_create_action_text_tables.action_text.rb +14 -0
  69. data/spec/dummy/db/migrate/20191101230308_solidus_one_four.spree.rb +1103 -0
  70. data/spec/dummy/db/migrate/20191101230309_create_spree_wallet_payment_sources.spree.rb +28 -0
  71. data/spec/dummy/db/migrate/20191101230310_migrate_credit_cards_to_wallet_payment_sources.spree.rb +30 -0
  72. data/spec/dummy/db/migrate/20191101230311_remove_is_default_from_prices.spree.rb +8 -0
  73. data/spec/dummy/db/migrate/20191101230312_remove_currency_from_line_items.spree.rb +8 -0
  74. data/spec/dummy/db/migrate/20191101230313_add_available_to_columns_and_remove_display_on_from_payment_methods.spree.rb +31 -0
  75. data/spec/dummy/db/migrate/20191101230314_create_spree_promotion_code_batch.spree.rb +39 -0
  76. data/spec/dummy/db/migrate/20191101230315_add_available_to_users_and_remove_display_on_from_shipping_methods.spree.rb +23 -0
  77. data/spec/dummy/db/migrate/20191101230316_add_index_to_spree_payments_number.spree.rb +8 -0
  78. data/spec/dummy/db/migrate/20191101230317_remove_spree_store_credits_column.spree.rb +8 -0
  79. data/spec/dummy/db/migrate/20191101230318_add_lft_and_rgt_indexes_to_taxons.spree.rb +9 -0
  80. data/spec/dummy/db/migrate/20191101230319_remove_order_id_from_inventory_units.spree.rb +31 -0
  81. data/spec/dummy/db/migrate/20191101230320_transform_tax_rate_category_relation.spree.rb +51 -0
  82. data/spec/dummy/db/migrate/20191101230321_add_roles_unique_constraints.spree.rb +9 -0
  83. data/spec/dummy/db/migrate/20191101230322_add_time_range_to_tax_rate.spree.rb +9 -0
  84. data/spec/dummy/db/migrate/20191101230323_rename_bogus_gateways.spree.rb +19 -0
  85. data/spec/dummy/db/migrate/20191101230324_remove_default_tax_from_spree_zones.spree.rb +8 -0
  86. data/spec/dummy/db/migrate/20191101230325_create_promotion_rule_stores.spree.rb +13 -0
  87. data/spec/dummy/db/migrate/20191101230326_create_store_shipping_methods.spree.rb +13 -0
  88. data/spec/dummy/db/migrate/20191101230327_add_available_locales_to_stores.spree.rb +10 -0
  89. data/spec/dummy/db/migrate/20191101230328_add_amount_remaining_to_store_credit_events.spree.rb +62 -0
  90. data/spec/dummy/db/migrate/20191101230329_add_join_characters_to_promotion_code_batch.spree.rb +12 -0
  91. data/spec/dummy/db/migrate/20191101230330_create_spree_store_credit_reasons_table.spree.rb +56 -0
  92. data/spec/dummy/db/migrate/20191101230331_remove_code_from_spree_promotions.spree.rb +43 -0
  93. data/spec/dummy/db/migrate/20191101230332_drop_spree_store_credit_update_reasons.spree.rb +20 -0
  94. data/spec/dummy/db/migrate/20191101230333_add_api_key_to_spree_users.spree_api.rb +10 -0
  95. data/spec/dummy/db/migrate/20191101230334_resize_api_key_field.spree_api.rb +10 -0
  96. data/spec/dummy/db/migrate/20191101230335_rename_api_key_to_spree_api_key.spree_api.rb +10 -0
  97. data/spec/dummy/db/migrate/20191101230336_add_index_to_user_spree_api_key.spree_api.rb +10 -0
  98. data/spec/dummy/db/migrate/20191101230337_create_users.solidus_auth.rb +32 -0
  99. data/spec/dummy/db/migrate/20191101230338_rename_columns_for_devise.solidus_auth.rb +40 -0
  100. data/spec/dummy/db/migrate/20191101230339_convert_user_remember_field.solidus_auth.rb +14 -0
  101. data/spec/dummy/db/migrate/20191101230340_add_reset_password_sent_at_to_spree_users.solidus_auth.rb +11 -0
  102. data/spec/dummy/db/migrate/20191101230341_make_users_email_index_unique.solidus_auth.rb +12 -0
  103. data/spec/dummy/db/migrate/20191101230342_add_deleted_at_to_users.solidus_auth.rb +9 -0
  104. data/spec/dummy/db/migrate/20191101230343_add_confirmable_to_users.solidus_auth.rb +10 -0
  105. data/spec/dummy/db/migrate/20191101230344_add_reset_password_token_index_to_spree_users.solidus_auth.rb +35 -0
  106. data/spec/dummy/db/schema.rb +1211 -0
  107. data/spec/dummy/db/seeds.rb +10 -0
  108. data/spec/dummy/db/solidus_test.sqlite3 +0 -0
  109. data/spec/dummy/log/test.log +12695 -0
  110. data/spec/dummy/package.json +11 -0
  111. data/spec/dummy/public/404.html +67 -0
  112. data/spec/dummy/public/422.html +67 -0
  113. data/spec/dummy/public/500.html +66 -0
  114. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  115. data/spec/dummy/public/apple-touch-icon.png +0 -0
  116. data/spec/dummy/public/favicon.ico +0 -0
  117. data/spec/dummy/tmp/development_secret.txt +1 -0
  118. data/spec/dummy/vendor/assets/javascripts/spree/backend/all.js +11 -0
  119. data/spec/dummy/vendor/assets/javascripts/spree/frontend/all.js +11 -0
  120. data/spec/dummy/vendor/assets/stylesheets/spree/backend/all.css +10 -0
  121. data/spec/dummy/vendor/assets/stylesheets/spree/frontend/all.css +10 -0
  122. data/spec/models/order_spec.rb +45 -0
  123. data/spec/models/variant_spec.rb +27 -0
  124. data/spec/spec_helper.rb +34 -0
  125. data/spec/support/stub_drip_responses.rb +13 -0
  126. metadata +495 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 290275654534a1c5517175eda1d17cc7be8fc7ace035f306f78d41ebabcc130b
4
+ data.tar.gz: c93f0603d7e88e4ddbcd076fc41c759c60abb78392531deedefab2f4ac847649
5
+ SHA512:
6
+ metadata.gz: 913a6ee519bed0452b89f2ee0e566593e2b97c3de0e22d58184bfadb10f722877e98b05cff71b921e10e9b6feeaefa2af9b4ad239c975c3d1a5222f5f6003245
7
+ data.tar.gz: e718d1fca112dbb26fe783df56230eb2cb855638cc3bb5b7133902f4506a6134565349c13bd589cbe88dfb073d2b4d19a37a177f0d952aba57c7281cc60803f6
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2019 [name of plugin creator]
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # SolidusDrip
2
+
3
+ Solidus integration for [Drip](https://www.drip.com/)
4
+
5
+ ## Installation
6
+
7
+ Add `solidus_drip` to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'solidus_drip'
11
+ ```
12
+
13
+ Bundle your dependencies and run the installation generator:
14
+
15
+ ```shell
16
+ bundle
17
+ bundle exec rails g solidus_drip:install
18
+ ```
19
+
20
+ Add your Drip credentials as environment variables. Get your Drip API key [here](https://www.getdrip.com/user/edit).
21
+
22
+ ```ruby
23
+ ENV['DRIP_API_KEY'] = 'YOUR_API_KEY'
24
+ ENV['DRIP_ACCOUNT_ID'] = 'YOUR_ACCOUNT_ID'
25
+ ```
26
+
27
+ ## Testing
28
+
29
+ First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run specs, and [Rubocop](https://github.com/bbatsov/rubocop) static code analysis. The dummy app can be regenerated by using `rake test_app`.
30
+
31
+ ```shell
32
+ bundle
33
+ bundle exec rake
34
+ ```
35
+
36
+ When testing your application's integration with this extension you may use its factories.
37
+ Simply add this require statement to your spec_helper:
38
+
39
+ ```ruby
40
+ require 'solidus_drip/factories'
41
+ ```
42
+
43
+ ## Releasing
44
+
45
+ Your new extension version can be released using `gem-release` like this:
46
+
47
+ ```shell
48
+ bundle exec gem bump -v VERSION --tag --push --remote upstream && gem release
49
+ ```
50
+
51
+ ## References
52
+
53
+ - [Drip API Documentation](https://developer.drip.com/)
54
+
55
+ Copyright (c) 2019 Eric Saupe, released under the New BSD License
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ begin
8
+ require 'spree/testing_support/extension_rake'
9
+ require 'rubocop/rake_task'
10
+ require 'rspec/core/rake_task'
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i(first_run rubocop spec)
17
+ rescue LoadError
18
+ # no rspec available
19
+ end
20
+
21
+ task :first_run do
22
+ if Dir['spec/dummy'].empty?
23
+ Rake::Task[:test_app].invoke
24
+ Dir.chdir('../../')
25
+ end
26
+ end
27
+
28
+ desc 'Generates a dummy app for testing'
29
+ task :test_app do
30
+ ENV['LIB_NAME'] = 'solidus_drip'
31
+ Rake::Task['extension:test_app'].invoke
32
+ end
@@ -0,0 +1,11 @@
1
+ var _dcq = _dcq || [];
2
+ var _dcs = _dcs || {};
3
+ _dcs.account = '<%= ENV['DRIP_API_KEY'] %>';
4
+
5
+ (function() {
6
+ var dc = document.createElement('script');
7
+ dc.type = 'text/javascript'; dc.async = true;
8
+ dc.src = '<%= "//tag.getdrip.com/#{ENV['DRIP_API_KEY']}.js" %>';
9
+ var s = document.getElementsByTagName('script')[0];
10
+ s.parentNode.insertBefore(dc, s);
11
+ })();
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ module Spree
5
+ module OrderDecorator
6
+ def self.prepended(base)
7
+ base.after_create(proc { |order|
8
+ order.drip.cart_activity('created')
9
+ })
10
+ base.state_machine.after_transition(to: :complete, do: proc { |order|
11
+ order.drip.order_activity('placed')
12
+ })
13
+ base.state_machine.after_transition(to: :canceled, do: proc { |order|
14
+ order.drip.order_activity('canceled')
15
+ })
16
+ end
17
+
18
+ ##
19
+ # Updates activity on Drip
20
+ #
21
+ # This method is called as part of the Spree::Order.update_hooks
22
+ #
23
+ # @see SolidusDrip::ShopperActivity::Order
24
+ #
25
+ def update_drip_activity
26
+ # If the order is complete it is no longer considered cart data
27
+ if completed?
28
+ if shipment_state_changed? && shipped?
29
+ drip.order_activity('fulfilled')
30
+ end
31
+ if payment_state_changed? && paid?
32
+ drip.order_activity('paid')
33
+ end
34
+ drip.order_activity('updated')
35
+ else
36
+ drip.cart_activity('updated')
37
+ end
38
+ end
39
+
40
+ def drip
41
+ @drip ||= SolidusDrip::ShopperActivity::Order.new(self)
42
+ end
43
+
44
+ ::Spree::Order.prepend self
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ module Spree
5
+ module VariantDecorator
6
+ def self.prepended(base)
7
+ base.after_create(proc { |variant|
8
+ variant.drip.product_activity('created')
9
+ })
10
+ base.after_update(proc { |variant|
11
+ variant.drip.product_activity('updated')
12
+ })
13
+ base.after_destroy(proc { |variant|
14
+ variant.drip.product_activity('deleted')
15
+ })
16
+ end
17
+
18
+ def drip
19
+ @drip ||= SolidusDrip::ShopperActivity::Product.new(self)
20
+ end
21
+
22
+ ::Spree::Variant.prepend self
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A default_host is required to generate full URL paths to be sent to Drip
4
+ # Here we initialize one if one is not present
5
+
6
+ if Spree::Core::Engine.routes.default_url_options.dig(:host).blank?
7
+ Rails.logger.warn('solidus_drip, default_url_options was blank and is being set to localhost')
8
+ Spree::Core::Engine.routes.default_url_options[:host] = 'localhost:3000'
9
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ Spree::Order.register_update_hook(:update_drip_activity)
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: Hello world
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Spree::Core::Engine.routes.draw do
4
+ # Add your extension routes here
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ class_option :auto_run_migrations, type: :boolean, default: false
7
+
8
+ def add_javascripts
9
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_drip\n"
10
+ end
11
+
12
+ def add_stylesheets; end
13
+
14
+ def add_migrations
15
+ run 'bundle exec rake railties:install:migrations FROM=solidus_drip'
16
+ end
17
+
18
+ def run_migrations
19
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
20
+ if run_migrations
21
+ run 'bundle exec rake db:migrate'
22
+ else
23
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'drip'
4
+ require 'solidus_core'
5
+ require 'solidus_drip/engine'
6
+ require 'solidus_drip/base'
7
+ require 'solidus_drip/shopper_activity'
8
+ require 'solidus_drip/shopper_activity/order'
9
+ require 'solidus_drip/shopper_activity/product'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ class Base
5
+ include Spree::Core::Engine.routes.url_helpers
6
+
7
+ attr_accessor :client
8
+
9
+ def initialize(*_args)
10
+ @client = ::Drip::Client.new do |c|
11
+ c.api_key = ENV['DRIP_API_KEY']
12
+ c.account_id = ENV['DRIP_ACCOUNT_ID']
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ ##
19
+ # When a response is not successful this method should be called
20
+ # to handle it appropriately. Currently it logs the bad request and does
21
+ # not stop the execution flow. A failed response from Drip shouldn't affect
22
+ # the user experience with the site.
23
+ #
24
+ def handle_error_response(response)
25
+ # Some errors come back under the `message` key while others are nested
26
+ # under `error`
27
+ error_message = response.body.dig('message') || response.body.dig('error', 'message')
28
+ Rails.logger.error("SOLIDUS DRIP | #{error_message}")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ class Engine < Rails::Engine
5
+ require 'spree/core'
6
+ isolate_namespace Spree
7
+ engine_name 'solidus_drip'
8
+
9
+ # use rspec for tests
10
+ config.generators do |g|
11
+ g.test_framework :rspec
12
+ end
13
+
14
+ def self.activate
15
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
16
+ Rails.configuration.cache_classes ? require(c) : load(c)
17
+ end
18
+ end
19
+
20
+ config.to_prepare(&method(:activate).to_proc)
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ # Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
5
+ #
6
+ # Example adding this to your spec_helper will load these Factories for use:
7
+ # require 'solidus_drip/factories'
8
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ module ShopperActivity
5
+ end
6
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusDrip
4
+ module ShopperActivity
5
+ class Order < SolidusDrip::Base
6
+ attr_accessor :order
7
+
8
+ ##
9
+ # ShopperActivity::Order relies on order data to be useful. We call super
10
+ # to initialize the client and then we set the order attribute to be used
11
+ # in the API calls.
12
+ #
13
+ # @param order [Spree::Order] the order to be recorded
14
+ #
15
+ def initialize(order)
16
+ super
17
+ @order = order
18
+ end
19
+
20
+ ##
21
+ # Cart Activity helps identify cart abandonment.
22
+ #
23
+ # @param action [String] the cart action, `created` or `updated`
24
+ # @see https://developer.drip.com/#cart-activity
25
+ #
26
+ def cart_activity(action)
27
+ response = client.create_cart_activity_event(cart_data(action))
28
+ handle_error_response(response) if !response.success?
29
+
30
+ response.success?
31
+ end
32
+
33
+ ##
34
+ # Order Activity helps identify a user's lifetime value by tracking the values
35
+ #
36
+ # @param action [String] the cart action, `placed`, `updated`, `paid`,
37
+ # `fulfilled`, `refunded`, or `canceled`
38
+ # @see https://developer.drip.com/#order-activity
39
+ #
40
+ def order_activity(action)
41
+ response = client.create_order_activity_event(order_data(action))
42
+ handle_error_response(response) if !response.success?
43
+
44
+ response.success?
45
+ end
46
+
47
+ private
48
+
49
+ ##
50
+ # Formats data to be used in Drip cart calls
51
+ #
52
+ def cart_data(action)
53
+ {
54
+ provider: 'solidus',
55
+ email: order.email,
56
+ action: action,
57
+ cart_id: order.id.to_s,
58
+ cart_public_id: order.number,
59
+ grand_total: order.total.to_f,
60
+ total_discounts: order.promo_total.to_f,
61
+ currency: order.currency,
62
+ cart_url: cart_url,
63
+ items: order.line_items.map do |line_item|
64
+ {
65
+ product_id: line_item.product.id.to_s,
66
+ product_variant_id: line_item.variant_id.to_s,
67
+ sku: line_item.sku,
68
+ name: line_item.name,
69
+ categories: line_item.product.taxons.pluck(:name),
70
+ price: line_item.price.to_f,
71
+ quantity: line_item.quantity,
72
+ discounts: line_item.promo_total.to_f,
73
+ total: line_item.total.to_f,
74
+ product_url: product_url(line_item.product)
75
+ }.compact
76
+ end
77
+ }.compact
78
+ end
79
+
80
+ ##
81
+ # Formats data to be used in Drip order calls
82
+ #
83
+ def order_data(action)
84
+ {
85
+ provider: 'solidus',
86
+ email: order.email,
87
+ action: action,
88
+ order_id: order.id.to_s,
89
+ order_public_id: order.number,
90
+ grand_total: order.total.to_f,
91
+ total_taxes: order.tax_total.to_f,
92
+ total_discounts: order.promo_total.to_f,
93
+ currency: order.currency,
94
+ order_url: order_url(order),
95
+ items: order.line_items.map do |line_item|
96
+ {
97
+ product_id: line_item.product.id.to_s,
98
+ product_variant_id: line_item.variant_id.to_s,
99
+ sku: line_item.sku,
100
+ name: line_item.name,
101
+ categories: line_item.product.taxons.pluck(:name),
102
+ price: line_item.price.to_f,
103
+ quantity: line_item.quantity,
104
+ discounts: line_item.promo_total.to_f,
105
+ taxes: line_item.additional_tax_total.to_f,
106
+ total: line_item.total.to_f,
107
+ product_url: product_url(line_item.product)
108
+ }.compact
109
+ end,
110
+ billing_address: {
111
+ first_name: order.billing_address.firstname,
112
+ last_name: order.billing_address.lastname,
113
+ company: order.billing_address.company,
114
+ address_1: order.billing_address.address1,
115
+ address_2: order.billing_address.address2,
116
+ city: order.billing_address.city,
117
+ state: order.billing_address.state&.abbr || order.billing_address.state_name,
118
+ postal_code: order.billing_address.zipcode,
119
+ country: order.billing_address.country&.name,
120
+ phone: order.billing_address.phone
121
+ }.compact,
122
+ shipping_address: {
123
+ first_name: order.shipping_address.firstname,
124
+ last_name: order.shipping_address.lastname,
125
+ company: order.shipping_address.company,
126
+ address_1: order.shipping_address.address1,
127
+ address_2: order.shipping_address.address2,
128
+ city: order.shipping_address.city,
129
+ state: order.shipping_address.state&.abbr || order.shipping_address.state_name,
130
+ postal_code: order.shipping_address.zipcode,
131
+ country: order.shipping_address.country&.name,
132
+ phone: order.shipping_address.phone
133
+ }.compact
134
+ }.compact
135
+ end
136
+ end
137
+ end
138
+ end