flowcommerce_spree 0.0.9 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f47f9a9debf77ea1ca7d10d8a2661a4abac350f3602b98a50686919476038187
4
- data.tar.gz: '04964f886f5dab774f4e1631a5ab9c0bda612e35599ad4c639dc7dcee8d7396b'
3
+ metadata.gz: 4805d4a464c5441b33c467972ef7750f5c350b6748dad10fb5a8d079e0c72b2c
4
+ data.tar.gz: dd5a4de7ff06de36712c55b4602d5eb6e490939978ec1bc71a15765393478229
5
5
  SHA512:
6
- metadata.gz: fc5929eff62c6c11b58eed26b580d0cc34c3c51e5a4a81869773a115e5210ac9808e740fb00112de84f5fcc6afc63db404bd97960398bf09d021d588f2624520
7
- data.tar.gz: 9311543f7e2b18e55326f1826d7240b6315efed2ac7f81c94c60d4c8236f5d4e5381cbb5e7e956880723d76384cf89b23e8306ac8026c14af36ad16344b8bd56
6
+ metadata.gz: 465a5624b6a9e4a80732dca72a5d618f1cd14064fb3005212f25f7e9d687c2f2c48ab90992bcae145cb87a8de16bc33c37e5d924c545372fb31f0ff7f90df2d2
7
+ data.tar.gz: dcfc5356d0954104ab64c0d4da2282fd21cba1600613c84565734d50ed58bd1c1289bd936e3d8624f2703ed80a3e48c7af4ad20442041fb9dc590c3852839b37
@@ -6,14 +6,18 @@ CurrentZoneLoader.module_eval do
6
6
  def current_zone
7
7
  return @current_zone if defined?(@current_zone)
8
8
 
9
- @current_zone = if (session_region_name = session['region']&.[]('name'))
9
+ @current_zone = if (session_region_name = session['region']&.[](:name))
10
10
  Spree::Zones::Product.find_by(name: session_region_name, status: 'active')
11
11
  end
12
12
 
13
- @current_zone ||= if request_iso_code.present?
14
- @current_zone = flow_zone
15
- @current_zone ||= Spree::Country.find_by(iso: request_iso_code)&.product_zones&.active&.first
16
- end
13
+ @current_zone ||= fetch_current_zone
14
+ @current_zone
15
+ end
16
+
17
+ def fetch_current_zone
18
+ if request_iso_code.present?
19
+ @current_zone = flow_zone || Spree::Country.find_by(iso: request_iso_code)&.product_zones&.active&.first
20
+ end
17
21
 
18
22
  @current_zone ||= Spree::Zones::Product.find_by(name: 'International') ||
19
23
  Spree::Zones::Product.new(name: 'International', taxon_ids: [], currencies: %w[USD CAD])
@@ -21,8 +25,8 @@ CurrentZoneLoader.module_eval do
21
25
  current_zone_name = @current_zone.name
22
26
  session['region'] = { name: current_zone_name, available_currencies: @current_zone.available_currencies,
23
27
  request_iso_code: request_iso_code }
24
-
25
28
  Rails.logger.debug("Using product zone: #{current_zone_name}")
29
+
26
30
  @current_zone
27
31
  end
28
32
 
@@ -11,7 +11,9 @@ module Users
11
11
  FlowcommerceSpree::OrderSync.new(order: current_order, flow_session_id: flow_session_id).synchronize!
12
12
  return render json: { error: :checkout_token_missing }, status: 422 if checkout_token.blank?
13
13
 
14
- render json: { checkout_url: "https://checkout.flow.io/tokens/#{checkout_token}" }, status: 200
14
+ checkout_url = ENV['FLOW_CHECKOUT_URL'] || 'https://checkout.flow.io'
15
+
16
+ render json: { checkout_url: "#{checkout_url}/tokens/#{checkout_token}" }, status: 200
15
17
  end
16
18
 
17
19
  private
@@ -28,9 +28,9 @@ module Spree
28
28
  flow_data["#{flow_exp.key}.excluded"].to_i != 1
29
29
  end
30
30
 
31
- def price_range(product_zone)
31
+ def price_range(product_zone, currencies = [])
32
32
  prices = {}
33
- master_prices.each do |p|
33
+ master_prices_with_currencies(currencies).each do |p|
34
34
  currency = p.currency
35
35
  min = nil
36
36
  max = nil
@@ -40,15 +40,15 @@ module Spree
40
40
  price = v.price_in(currency)
41
41
  next if price.nil? || price.amount.nil?
42
42
 
43
- min = price if min.nil? || min.amount > price.amount
44
- max = price if max.nil? || max.amount < price.amount
43
+ min = [price, min].compact.min { |a, b| a.amount <=> b.amount }
44
+ max = [price, max].compact.max { |a, b| a.amount <=> b.amount }
45
45
  end
46
46
  else
47
47
  min = max = master.price_in(currency)
48
48
  end
49
49
 
50
- rmin = min&.amount&.to_s(:rounded, precision: 0) || 0
51
- rmax = max&.amount&.to_s(:rounded, precision: 0) || 0
50
+ rmin = round_with_precision(min, 0)
51
+ rmax = round_with_precision(max, 0)
52
52
 
53
53
  prices[currency] = { min: rmin, max: rmax }
54
54
  end
@@ -56,6 +56,10 @@ module Spree
56
56
  add_flow_price_range(prices, product_zone)
57
57
  end
58
58
 
59
+ def round_with_precision(number, precision)
60
+ number&.amount&.to_s(:rounded, precision: precision) || 0
61
+ end
62
+
59
63
  def add_flow_price_range(prices, product_zone)
60
64
  flow_experience_key = product_zone&.flow_data&.[]('key')
61
65
  return prices if flow_experience_key.blank?
@@ -70,8 +74,8 @@ module Spree
70
74
  price = v.flow_local_price(flow_experience_key)
71
75
  next if price.amount.nil? || price.currency != currency
72
76
 
73
- min = price if min.nil? || min.amount > price.amount
74
- max = price if max.nil? || max.amount < price.amount
77
+ min = [price, min].compact.min { |a, b| a.amount <=> b.amount }
78
+ max = [price, max].compact.max { |a, b| a.amount <=> b.amount }
75
79
  end
76
80
  end
77
81
 
@@ -80,8 +84,8 @@ module Spree
80
84
  max ||= master_price
81
85
  end
82
86
 
83
- rmin = min&.amount&.to_s(:rounded, precision: 0) || 0
84
- rmax = max&.amount&.to_s(:rounded, precision: 0) || 0
87
+ rmin = round_with_precision(min, 0)
88
+ rmax = round_with_precision(max, 0)
85
89
 
86
90
  prices[currency] = { min: rmin, max: rmax }
87
91
  prices
@@ -22,16 +22,25 @@ module FlowcommerceSpree
22
22
  errors << { message: 'Order number param missing' } && (return self) unless order_number
23
23
 
24
24
  if (order = Spree::Order.find_by(number: order_number))
25
- upsert_order_captures(order, capture)
26
- payments = order.flow_io_payments
27
- map_payment_captures_to_spree(order, payments) if payments.present?
28
- order
25
+ if order.payments.any?
26
+ store_payment_capture(order, capture)
27
+ else
28
+ FlowcommerceSpree::UpdatePaymentCaptureWorker.perform_in(1.minute, order.number, capture)
29
+ order
30
+ end
29
31
  else
30
32
  errors << { message: "Order #{order_number} not found" }
31
33
  self
32
34
  end
33
35
  end
34
36
 
37
+ def store_payment_capture(order, capture)
38
+ upsert_order_captures(order, capture)
39
+ payments = order.flow_io_payments
40
+ map_payment_captures_to_spree(order, payments) if payments.present?
41
+ order
42
+ end
43
+
35
44
  private
36
45
 
37
46
  def upsert_order_captures(order, capture)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FlowcommerceSpree
4
+ class FlowIoWorker
5
+ include Sidekiq::Worker
6
+
7
+ sidekiq_retries_exhausted do |message, exception|
8
+ Rails.logger.warn("[!] #{self.class} max attempts reached: #{message} - #{exception}")
9
+ notification_setting = FlowcommerceSpree::Config.notification_setting
10
+ return unless notification_setting[:slack].present?
11
+
12
+ slack_message = "[#{Rails.env}] #{message}"
13
+ Slack_client.chat_postMessage(channel: notification_setting[:slack][:channel], text: slack_message)
14
+ end
15
+ end
16
+ end
@@ -1,19 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FlowcommerceSpree
4
- class ImportItemWorker
5
- include Sidekiq::Worker
4
+ class ImportItemWorker < FlowIoWorker
6
5
  sidekiq_options retry: 3, queue: :flow_io
7
6
 
8
- sidekiq_retries_exhausted do |message, exception|
9
- Rails.logger.warn("[!] FlowcommerceSpree::ImportItemWorker max attempts reached: #{message} - #{exception}")
10
- notification_setting = FlowcommerceSpree::Config.notification_setting
11
- return unless notification_setting[:slack].present?
12
-
13
- slack_message = "[#{Rails.env}] #{message}"
14
- Slack_client.chat_postMessage(channel: notification_setting[:slack][:channel], text: slack_message)
15
- end
16
-
17
7
  def perform(variant_sku)
18
8
  variant = Spree::Variant.find_by sku: variant_sku
19
9
  return unless variant
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FlowcommerceSpree
4
+ class UpdatePaymentCaptureWorker < FlowIoWorker
5
+ sidekiq_options retry: 3, queue: :flow_io
6
+
7
+ def perform(order_number, capture = {})
8
+ order = Spree::Order.find_by number: order_number
9
+ raise 'Order has no payments' if order.payments.empty?
10
+
11
+ FlowcommerceSpree::Webhooks::CaptureUpsertedV2.new({ capture: capture }.as_json)
12
+ .store_payment_capture(order, capture)
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FlowcommerceSpree
4
- VERSION = '0.0.9'
4
+ VERSION = '0.0.13'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flowcommerce_spree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurel Branzeanu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-06-30 00:00:00.000000000 Z
12
+ date: 2021-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_model_serializers
@@ -244,7 +244,9 @@ files:
244
244
  - app/views/spree/admin/promotions/edit.html.erb
245
245
  - app/views/spree/admin/shared/_order_summary.html.erb
246
246
  - app/views/spree/admin/shared/_order_summary_flow.html.erb
247
+ - app/workers/flowcommerce_spree/flow_io_worker.rb
247
248
  - app/workers/flowcommerce_spree/import_item_worker.rb
249
+ - app/workers/flowcommerce_spree/update_payment_capture_worker.rb
248
250
  - config/rails_best_practices.yml
249
251
  - config/routes.rb
250
252
  - db/migrate/20201021160159_add_type_and_meta_to_spree_zone.rb