flowcommerce_spree 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -6
  3. data/app/controllers/concerns/current_zone_loader_decorator.rb +11 -17
  4. data/app/controllers/flowcommerce_spree/inventory_controller.rb +23 -0
  5. data/app/controllers/flowcommerce_spree/orders_controller.rb +20 -0
  6. data/app/controllers/flowcommerce_spree/webhooks_controller.rb +16 -18
  7. data/app/controllers/users/sessions_controller_decorator.rb +19 -2
  8. data/app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb +0 -16
  9. data/app/models/flowcommerce_spree/settings.rb +1 -0
  10. data/app/models/spree/address_decorator.rb +1 -1
  11. data/app/models/spree/calculator/flow_io.rb +24 -12
  12. data/app/models/spree/calculator/shipping/flow_io.rb +5 -2
  13. data/app/models/spree/flow_io_credit_card_decorator.rb +21 -0
  14. data/app/models/spree/flow_io_order_decorator.rb +181 -0
  15. data/app/models/spree/flow_io_product_decorator.rb +5 -0
  16. data/app/models/spree/flow_io_variant_decorator.rb +16 -6
  17. data/app/models/spree/gateway/flow_io.rb +61 -24
  18. data/app/models/spree/{credit_card_decorator.rb → payment_capture_event_decorator.rb} +1 -1
  19. data/app/models/spree/zones/flow_io_product_zone_decorator.rb +4 -0
  20. data/app/overrides/spree/admin/order_sidebar_summary_flow_link.rb +13 -0
  21. data/app/overrides/spree/admin/products/order_price_flow_message.rb +9 -0
  22. data/app/serializers/api/v2/order_serializer_decorator.rb +20 -0
  23. data/app/services/flowcommerce_spree/import_experience_items.rb +1 -21
  24. data/app/services/flowcommerce_spree/import_item.rb +45 -0
  25. data/app/services/flowcommerce_spree/order_sync.rb +50 -222
  26. data/app/services/flowcommerce_spree/order_updater.rb +78 -0
  27. data/app/services/flowcommerce_spree/webhooks/capture_upserted_v2.rb +76 -0
  28. data/app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb +66 -0
  29. data/app/services/flowcommerce_spree/webhooks/experience_upserted_v2.rb +25 -0
  30. data/app/services/flowcommerce_spree/webhooks/fraud_status_changed.rb +35 -0
  31. data/app/services/flowcommerce_spree/webhooks/local_item_upserted.rb +40 -0
  32. data/app/views/spree/admin/payments/source_views/_flow_io_gateway.html.erb +21 -0
  33. data/app/workers/flowcommerce_spree/import_item_worker.rb +24 -0
  34. data/config/routes.rb +3 -1
  35. data/db/migrate/20201021755957_add_meta_to_spree_tables.rb +6 -4
  36. data/lib/flow/simple_gateway.rb +0 -36
  37. data/lib/flowcommerce_spree.rb +6 -2
  38. data/lib/flowcommerce_spree/engine.rb +6 -1
  39. data/lib/flowcommerce_spree/experience_service.rb +1 -27
  40. data/lib/flowcommerce_spree/logging_http_client.rb +29 -13
  41. data/lib/flowcommerce_spree/session.rb +5 -25
  42. data/lib/flowcommerce_spree/version.rb +1 -1
  43. data/lib/tasks/flowcommerce_spree.rake +4 -1
  44. metadata +77 -28
  45. data/app/mailers/spree/spree_order_mailer_decorator.rb +0 -24
  46. data/app/models/spree/line_item_decorator.rb +0 -15
  47. data/app/models/spree/order_decorator.rb +0 -244
  48. data/app/views/spree/order_mailer/confirm_email.html.erb +0 -86
  49. data/app/views/spree/order_mailer/confirm_email.text.erb +0 -38
  50. data/lib/flow/error.rb +0 -73
  51. data/lib/flow/pay_pal.rb +0 -25
  52. data/lib/flowcommerce_spree/webhook_service.rb +0 -184
  53. data/lib/simple_csv_writer.rb +0 -44
@@ -1,17 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'activerecord-postgres-json'
4
+ require 'active_model_serializers'
3
5
  require 'flowcommerce'
4
6
  require 'flowcommerce_spree/api'
5
7
  require 'flowcommerce_spree/refresher'
6
8
  require 'flowcommerce_spree/engine'
7
9
  require 'flowcommerce_spree/logging_http_client'
8
10
  require 'flowcommerce_spree/logging_http_handler'
9
- require 'flowcommerce_spree/webhook_service'
10
11
  require 'flowcommerce_spree/session'
11
12
  require 'flow/simple_gateway'
12
- require 'request_store'
13
+ require 'oj'
13
14
 
14
15
  module FlowcommerceSpree
16
+ API_KEY = ENV.fetch('FLOW_TOKEN', 'test_key')
17
+ ENV['FLOW_TOKEN'] = API_KEY
18
+
15
19
  def self.client(logger: FlowcommerceSpree.logger, **opts)
16
20
  FlowCommerce.instance(http_handler: LoggingHttpHandler.new(logger: logger), **opts)
17
21
  end
@@ -9,6 +9,8 @@ module FlowcommerceSpree
9
9
  FlowcommerceSpree::ORGANIZATION = ENV.fetch('FLOW_ORGANIZATION', 'flow.io')
10
10
  FlowcommerceSpree::BASE_COUNTRY = ENV.fetch('FLOW_BASE_COUNTRY', 'USA')
11
11
  FlowcommerceSpree::API_KEY = ENV.fetch('FLOW_TOKEN', 'test_key')
12
+ FlowcommerceSpree::FLOW_IO_WEBHOOK_USER = ENV.fetch('FLOW_IO_WEBHOOK_USER', 'test_user')
13
+ FlowcommerceSpree::FLOW_IO_WEBHOOK_PASSWORD = ENV.fetch('FLOW_IO_WEBHOOK_PASSWORD', 'test_password')
12
14
 
13
15
  FlowcommerceSpree::Config = FlowcommerceSpree::Settings.new
14
16
  end
@@ -24,7 +26,7 @@ module FlowcommerceSpree
24
26
 
25
27
  app.config.flowcommerce_spree[:mounted_path] = ENV.fetch('FLOW_MOUNT_PATH', '/flow')
26
28
 
27
- app.routes.append do
29
+ app.routes.prepend do
28
30
  mount FlowcommerceSpree::Engine => app.config.flowcommerce_spree[:mounted_path]
29
31
  end
30
32
  end
@@ -40,6 +42,9 @@ module FlowcommerceSpree
40
42
  Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')).sort.each do |c|
41
43
  Rails.configuration.cache_classes ? require(c) : load(c)
42
44
  end
45
+ Dir.glob(File.join(File.dirname(__FILE__), '../app/overrides/*.rb')).sort.each do |c|
46
+ Rails.configuration.cache_classes ? require(c) : load(c)
47
+ end
43
48
  end
44
49
 
45
50
  config.to_prepare(&method(:activate).to_proc)
@@ -22,18 +22,6 @@ module FlowcommerceSpree
22
22
  nil
23
23
  end
24
24
 
25
- def get_by_subcatalog_id(subcatalog_id)
26
- fetch_from_flow.each do |experince|
27
- return experince if experince.subcatalog.id == subcatalog_id
28
- end
29
-
30
- nil
31
- end
32
-
33
- def compact
34
- all.map { |exp| [exp.country, exp.key, exp.name] }
35
- end
36
-
37
25
  def default
38
26
  FlowcommerceSpree::ExperienceService
39
27
  .all.select { |exp| exp.key.downcase == ENV.fetch('FLOW_BASE_COUNTRY').downcase }.first
@@ -42,24 +30,10 @@ module FlowcommerceSpree
42
30
  private
43
31
 
44
32
  def fetch_from_flow
45
- # return cached_experinces if cache_valid?
46
-
47
- experiences = FlowcommerceSpree.client.experiences.get ORGANIZATION
33
+ FlowcommerceSpree.client.experiences.get ORGANIZATION
48
34
 
49
35
  # work with active axperiences only
50
36
  # experiences = experiences.select { |it| it.status.value == 'active' }
51
-
52
- # @cache = [experiences, Time.now]
53
- experiences
54
- end
55
-
56
- def cache_valid?
57
- # cache experinces in worker memory for 1 minute
58
- @cache && @cache[1] > Time.now.ago(1.minute)
59
- end
60
-
61
- def cached_experinces
62
- @cache[0]
63
37
  end
64
38
  end
65
39
  end
@@ -15,31 +15,47 @@ module FlowcommerceSpree
15
15
 
16
16
  start_time = Time.now.utc.round(10)
17
17
 
18
+ # Contrived example to show how client settings can be adjusted
18
19
  # if request.path.start_with?('/organizations')
19
- # Contrived example to show how client settings can be adjusted
20
- # client.open_timeout = 60
21
- # client.read_timeout = 60
20
+ # client.open_timeout = 60
21
+ # client.read_timeout = 60
22
22
  # end
23
23
 
24
24
  begin
25
25
  response = super
26
26
  rescue Io::Flow::V0::HttpClient::ServerError => e
27
- @error = { error: e }.to_json
27
+ @error = { error: Oj.load(e.body), code: e.code, status: e.details }
28
+ raise exception_to_raise(e), @error.dig(:error, 'messages')
28
29
  ensure
29
30
  # client.open_timeout = original_open
30
31
  # client.read_timeout = original_read
31
32
 
32
- duration = ((Time.now.utc.round(10) - start_time) * 1000).round(0)
33
+ log_request(request, response, start_time)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def exception_to_raise(flow_io_exception)
40
+ resource = flow_io_exception.uri.split('/').last
41
+ exception = case resource
42
+ when 'reversals', 'refunds'
43
+ 'Spree::Core::GatewayError'
44
+ else
45
+ 'StandardError'
46
+ end
47
+ exception.constantize
48
+ end
33
49
 
34
- @logger.info(
35
- "Started #{request.method} #{request.path}\n"\
36
- "headers: #{request.instance_variable_get(:@header)}\nbody: #{request.body}\n"\
37
- "response: #{response&.force_encoding('utf-8')}\n"\
38
- "Completed #{request.method} #{request.path} #{duration} ms\n"
39
- )
50
+ def log_request(request, response, start_time)
51
+ duration = ((Time.now.utc.round(10) - start_time) * 1000).round(0)
40
52
 
41
- @logger.info "Error: #{e.inspect}" if e
42
- end
53
+ @logger.info("Started #{request.method} #{request.path}\n"\
54
+ "headers: #{request.instance_variable_get(:@header)}\nbody: #{request.body}\n"\
55
+ "response: #{response&.force_encoding('utf-8')}\n"\
56
+ "Completed #{request.method} #{request.path} #{duration} ms\n")
57
+
58
+ @logger.info "Error: #{@error.inspect}" if @error
43
59
  end
44
60
  end
45
61
  end
@@ -5,16 +5,14 @@ module FlowcommerceSpree
5
5
  class Session
6
6
  attr_accessor :session, :localized, :visitor
7
7
 
8
- def self.create(ip:, visitor:, experience: nil)
9
- instance = new(ip: ip, visitor: visitor, experience: experience)
8
+ def self.create(country:, visitor:, experience: nil)
9
+ instance = new(country: country, visitor: visitor, experience: experience)
10
10
  instance.create
11
11
  instance
12
12
  end
13
13
 
14
- def initialize(ip:, visitor:, experience: nil)
15
- ip = '127.0.0.1' if ip == '::1'
16
-
17
- @ip = ip
14
+ def initialize(country:, visitor:, experience: nil)
15
+ @country = country
18
16
  @visitor = visitor
19
17
  @experience = experience
20
18
  end
@@ -22,7 +20,7 @@ module FlowcommerceSpree
22
20
  # create session without or with experience (the latter is useful for creating a new session with the order's
23
21
  # experience on refreshing the checkout_token)
24
22
  def create
25
- data = { ip: @ip,
23
+ data = { country: @country,
26
24
  visit: { id: @visitor,
27
25
  expires_at: (Time.now + 30.minutes).iso8601 } }
28
26
  data[:experience] = @experience if @experience
@@ -54,23 +52,5 @@ module FlowcommerceSpree
54
52
  def id
55
53
  @session.id
56
54
  end
57
-
58
- # because we do not get full experience from session, we have to get from exp list
59
- def delivered_duty_options
60
- return nil unless experience
61
-
62
- return unless (flow_experience = Flow::Experience.get(experience.key))
63
-
64
- Hashie::Mash.new(flow_experience.settings.delivered_duty.to_hash)
65
- end
66
-
67
- # if we have more than one choice, we show choice popup
68
- def offers_delivered_duty_choice?
69
- if (options = delivered_duty_options)
70
- options.available.length > 1
71
- else
72
- false
73
- end
74
- end
75
55
  end
76
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FlowcommerceSpree
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.8'
5
5
  end
@@ -47,8 +47,11 @@ namespace :flowcommerce_spree do
47
47
 
48
48
  while current_page == 0 || !variants.empty?
49
49
  current_page += 1
50
- variants = Spree::Variant.order('updated_at desc').page(current_page).per(100).all
51
50
 
51
+ products = Spree::Product.where.not(country_of_origin: nil)
52
+ .order('updated_at desc')
53
+ .page(current_page).per(50).includes(:variants_including_master)
54
+ variants = products.map(&:variants_including_master).flatten
52
55
  variants.each do |variant|
53
56
  total_sum += 1
54
57
  result = variant.sync_product_to_flow
metadata CHANGED
@@ -1,16 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flowcommerce_spree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurel Branzeanu
8
8
  - Sebastian De Luca
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-02-22 00:00:00.000000000 Z
12
+ date: 2021-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_model_serializers
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 0.10.2
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 0.10.2
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord-postgres-json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.3
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.2.3
14
42
  - !ruby/object:Gem::Dependency
15
43
  name: colorize
16
44
  requirement: !ruby/object:Gem::Requirement
@@ -77,16 +105,16 @@ dependencies:
77
105
  name: oj
78
106
  requirement: !ruby/object:Gem::Requirement
79
107
  requirements:
80
- - - ">="
108
+ - - '='
81
109
  - !ruby/object:Gem::Version
82
- version: '0'
110
+ version: 3.7.12
83
111
  type: :runtime
84
112
  prerelease: false
85
113
  version_requirements: !ruby/object:Gem::Requirement
86
114
  requirements:
87
- - - ">="
115
+ - - '='
88
116
  - !ruby/object:Gem::Version
89
- version: '0'
117
+ version: 3.7.12
90
118
  - !ruby/object:Gem::Dependency
91
119
  name: pg
92
120
  requirement: !ruby/object:Gem::Requirement
@@ -102,33 +130,33 @@ dependencies:
102
130
  - !ruby/object:Gem::Version
103
131
  version: '0.21'
104
132
  - !ruby/object:Gem::Dependency
105
- name: request_store
133
+ name: spree_backend
106
134
  requirement: !ruby/object:Gem::Requirement
107
135
  requirements:
108
- - - ">="
136
+ - - "~>"
109
137
  - !ruby/object:Gem::Version
110
- version: '0'
138
+ version: 2.3.0
111
139
  type: :runtime
112
140
  prerelease: false
113
141
  version_requirements: !ruby/object:Gem::Requirement
114
142
  requirements:
115
- - - ">="
143
+ - - "~>"
116
144
  - !ruby/object:Gem::Version
117
- version: '0'
145
+ version: 2.3.0
118
146
  - !ruby/object:Gem::Dependency
119
- name: spree_backend
147
+ name: factory_bot_rails
120
148
  requirement: !ruby/object:Gem::Requirement
121
149
  requirements:
122
150
  - - "~>"
123
151
  - !ruby/object:Gem::Version
124
- version: 2.3.0
125
- type: :runtime
152
+ version: '4.0'
153
+ type: :development
126
154
  prerelease: false
127
155
  version_requirements: !ruby/object:Gem::Requirement
128
156
  requirements:
129
157
  - - "~>"
130
158
  - !ruby/object:Gem::Version
131
- version: 2.3.0
159
+ version: '4.0'
132
160
  - !ruby/object:Gem::Dependency
133
161
  name: github_changelog_generator
134
162
  requirement: !ruby/object:Gem::Requirement
@@ -143,6 +171,20 @@ dependencies:
143
171
  - - ">="
144
172
  - !ruby/object:Gem::Version
145
173
  version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: rspec-rails
176
+ requirement: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '3.5'
181
+ type: :development
182
+ prerelease: false
183
+ version_requirements: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '3.5'
146
188
  description: Integration of popular Rails/Spree store framework with e-commerce Flow
147
189
  API
148
190
  email:
@@ -159,22 +201,23 @@ files:
159
201
  - app/assets/javascripts/flowcommerce_spree/application.js
160
202
  - app/assets/stylesheets/flowcommerce_spree/application.css
161
203
  - app/controllers/concerns/current_zone_loader_decorator.rb
204
+ - app/controllers/flowcommerce_spree/inventory_controller.rb
205
+ - app/controllers/flowcommerce_spree/orders_controller.rb
162
206
  - app/controllers/flowcommerce_spree/webhooks_controller.rb
163
207
  - app/controllers/users/sessions_controller_decorator.rb
164
208
  - app/helpers/flowcommerce_spree/application_helper.rb
165
209
  - app/helpers/spree/admin/orders_helper_decorator.rb
166
210
  - app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
167
- - app/mailers/spree/spree_order_mailer_decorator.rb
168
211
  - app/models/flowcommerce_spree/settings.rb
169
212
  - app/models/spree/address_decorator.rb
170
213
  - app/models/spree/calculator/flow_io.rb
171
214
  - app/models/spree/calculator/shipping/flow_io.rb
172
- - app/models/spree/credit_card_decorator.rb
215
+ - app/models/spree/flow_io_credit_card_decorator.rb
216
+ - app/models/spree/flow_io_order_decorator.rb
173
217
  - app/models/spree/flow_io_product_decorator.rb
174
218
  - app/models/spree/flow_io_variant_decorator.rb
175
219
  - app/models/spree/gateway/flow_io.rb
176
- - app/models/spree/line_item_decorator.rb
177
- - app/models/spree/order_decorator.rb
220
+ - app/models/spree/payment_capture_event_decorator.rb
178
221
  - app/models/spree/promotion_decorator.rb
179
222
  - app/models/spree/promotion_handler/coupon_decorator.rb
180
223
  - app/models/spree/spree_user_decorator.rb
@@ -182,24 +225,32 @@ files:
182
225
  - app/models/spree/zone_decorator.rb
183
226
  - app/models/spree/zones/flow_io_product_zone_decorator.rb
184
227
  - app/models/tracking/setup_decorator.rb
228
+ - app/overrides/spree/admin/order_sidebar_summary_flow_link.rb
229
+ - app/overrides/spree/admin/products/order_price_flow_message.rb
230
+ - app/serializers/api/v2/order_serializer_decorator.rb
185
231
  - app/services/flowcommerce_spree/import_experience_items.rb
186
232
  - app/services/flowcommerce_spree/import_experiences.rb
233
+ - app/services/flowcommerce_spree/import_item.rb
187
234
  - app/services/flowcommerce_spree/order_sync.rb
235
+ - app/services/flowcommerce_spree/order_updater.rb
236
+ - app/services/flowcommerce_spree/webhooks/capture_upserted_v2.rb
237
+ - app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb
238
+ - app/services/flowcommerce_spree/webhooks/experience_upserted_v2.rb
239
+ - app/services/flowcommerce_spree/webhooks/fraud_status_changed.rb
240
+ - app/services/flowcommerce_spree/webhooks/local_item_upserted.rb
188
241
  - app/views/layouts/flowcommerce_spree/application.html.erb
189
242
  - app/views/spree/admin/payments/index.html.erb
243
+ - app/views/spree/admin/payments/source_views/_flow_io_gateway.html.erb
190
244
  - app/views/spree/admin/promotions/edit.html.erb
191
245
  - app/views/spree/admin/shared/_order_summary.html.erb
192
246
  - app/views/spree/admin/shared/_order_summary_flow.html.erb
193
- - app/views/spree/order_mailer/confirm_email.html.erb
194
- - app/views/spree/order_mailer/confirm_email.text.erb
247
+ - app/workers/flowcommerce_spree/import_item_worker.rb
195
248
  - config/rails_best_practices.yml
196
249
  - config/routes.rb
197
250
  - db/migrate/20201021160159_add_type_and_meta_to_spree_zone.rb
198
251
  - db/migrate/20201021755957_add_meta_to_spree_tables.rb
199
252
  - db/migrate/20201022173210_add_zone_type_to_spree_zone_members.rb
200
253
  - db/migrate/20201022174252_add_kind_to_zone.rb
201
- - lib/flow/error.rb
202
- - lib/flow/pay_pal.rb
203
254
  - lib/flow/simple_gateway.rb
204
255
  - lib/flowcommerce_spree.rb
205
256
  - lib/flowcommerce_spree/api.rb
@@ -211,14 +262,12 @@ files:
211
262
  - lib/flowcommerce_spree/session.rb
212
263
  - lib/flowcommerce_spree/test_support.rb
213
264
  - lib/flowcommerce_spree/version.rb
214
- - lib/flowcommerce_spree/webhook_service.rb
215
- - lib/simple_csv_writer.rb
216
265
  - lib/tasks/flowcommerce_spree.rake
217
266
  homepage: https://github.com/mejuri-inc/flowcommerce_spree
218
267
  licenses:
219
268
  - MIT
220
269
  metadata: {}
221
- post_install_message:
270
+ post_install_message:
222
271
  rdoc_options: []
223
272
  require_paths:
224
273
  - lib
@@ -234,7 +283,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
234
283
  version: '0'
235
284
  requirements: []
236
285
  rubygems_version: 3.0.8
237
- signing_key:
286
+ signing_key:
238
287
  specification_version: 4
239
288
  summary: Integration of Spree with Flow API
240
289
  test_files: []