flowcommerce_spree 0.0.10 → 0.0.14

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad7616da6a9fb5e7fbffe0a8869730ffedacb91deec3a5d08f328d2b876338ad
4
- data.tar.gz: 76ffd0d23d05028ba0d24df80fa5a092efe62f0b5f14ddde339e73ede5ee53bc
3
+ metadata.gz: 78bcd2ec2168ade147fb11d14e8a8f94a30133a0f6fa2df225c3abd0cbb7a7ff
4
+ data.tar.gz: fc255f7fc844d6209cd852c84a8f86db9cb97b3ad8d1e80586e5f692ec51a055
5
5
  SHA512:
6
- metadata.gz: 1eb2b92c39dd528cc4990d9957ee5dba29fbdfa39d7f7a4b821d07a6f5cb366541821b67a6d23ccbc9854b3c3c0867e0cd3fff8b36f64cea627b2fa6d7da47df
7
- data.tar.gz: 75175908d8ff3a85ca061ea82890af39544e66bf5898409e0b38d467bc40880eb6ac8e1773263cc5e933ef54d28bf00a2fb1a3f5cddbd935793b92c7fa0e89f2
6
+ metadata.gz: 15778423ce300091577ff41aad2c639da5ddb59421db10cf37e7844a9c74997220a0864788675a98d81c330f2fa800fe7ee38bcf703cb813333f2cf99af01687
7
+ data.tar.gz: 8b0795bf474136c0c7abd383933b2d809e6e1efc26e00b4c40164f389d707d50c75e7c6f950fc3285d3fc898a7442c2555dfc6d959b067b5a8851c7c9a56fc63
@@ -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
 
@@ -14,7 +14,11 @@ module FlowcommerceSpree
14
14
  flow_updater = FlowcommerceSpree::OrderUpdater.new(order: order)
15
15
  flow_updater.complete_checkout
16
16
 
17
- redirect_to "/thankyou?order=#{params[:order]}&t=#{params[:t]}"
17
+ redirection_path = "/thankyou?order=#{params[:order]}&t=#{params[:t]}"
18
+ locale = order.locale_path
19
+ redirection_path = "/#{locale}#{redirection_path}" if locale
20
+
21
+ redirect_to redirection_path
18
22
  end
19
23
  end
20
24
  end
@@ -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
@@ -5,7 +5,9 @@
5
5
  module Spree
6
6
  class Gateway
7
7
  class FlowIo < Gateway
8
+ REFUND_VALID_STATES = %w[succeeded pending].freeze
8
9
  REFUND_SUCCESS = 'succeeded'
10
+ REFUND_PENDING = 'pending'
9
11
 
10
12
  def provider_class
11
13
  self.class
@@ -90,18 +92,25 @@ module Spree
90
92
  currency: order.currency)
91
93
  response = FlowcommerceSpree.client.refunds.post(FlowcommerceSpree::ORGANIZATION, refund_form)
92
94
  response_status = response.status.value
93
- if response_status == REFUND_SUCCESS
95
+ if REFUND_VALID_STATES.include? response_status
94
96
  add_refund_to_order(response, order)
97
+ schedule_status_check(response, order)
95
98
  ActiveMerchant::Billing::Response.new(true,
96
99
  REFUND_SUCCESS,
97
100
  response.to_hash,
98
101
  authorization: response.authorization.id)
99
102
  else
100
- msg = "Partial refund fail. Details: #{response_status}"
101
- ActiveMerchant::Billing::Response.new(false, msg, {}, {})
103
+ ActiveMerchant::Billing::Response.new(false, "Partial refund fail. Details: #{response_status}", {}, {})
102
104
  end
103
105
  end
104
106
 
107
+ def schedule_status_check(response, order)
108
+ return if response.status.value != REFUND_PENDING
109
+
110
+ Rails.logger.warn("[!] #{self.class} for #{order.number} - refund request without succeeded status.")
111
+ FlowcommerceSpree::RefundStatusWorker.perform_async(order.number, response.key)
112
+ end
113
+
105
114
  def add_refund_to_order(response, order)
106
115
  order.flow_data ||= {}
107
116
  order.flow_data['refunds'] ||= []
@@ -54,19 +54,20 @@ module FlowcommerceSpree
54
54
  end
55
55
 
56
56
  def refresh_checkout_token
57
- root_url = Rails.application.routes.url_helpers.root_url
58
57
  order_number = @order.number
58
+ root_url = Rails.application.routes.url_helpers.root_url
59
+ root_url_with_locale = "#{root_url}#{@order.try(:locale_path)}"
59
60
  confirmation_url = "#{root_url}flow/order-completed?order=#{order_number}&t=#{@order.guest_token}"
60
61
  @order.flow_io_attribute_add('flow_return_url', confirmation_url)
61
- @order.flow_io_attribute_add('checkout_continue_shopping_url', root_url)
62
+ @order.flow_io_attribute_add('checkout_continue_shopping_url', root_url_with_locale)
62
63
 
63
64
  FlowcommerceSpree.client.checkout_tokens.post_checkout_and_tokens_by_organization(
64
65
  FlowcommerceSpree::ORGANIZATION, discriminator: 'checkout_token_reference_form',
65
66
  order_number: order_number,
66
67
  session_id: @flow_session_id,
67
- urls: { continue_shopping: root_url,
68
+ urls: { continue_shopping: root_url_with_locale,
68
69
  confirmation: confirmation_url,
69
- invalid_checkout: root_url }
70
+ invalid_checkout: root_url_with_locale }
70
71
  )&.id
71
72
  end
72
73
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FlowcommerceSpree
4
+ class RefundStatusWorker < FlowIoWorker
5
+ sidekiq_options retry: 3, queue: :flow_io
6
+
7
+ def perform(order_number, refund_key)
8
+ response = FlowcommerceSpree.client.refunds.get_by_key(FlowcommerceSpree::ORGANIZATION, refund_key)
9
+ response_status = response.status.value
10
+ return if response_status == 'succeeded'
11
+
12
+ raise "Refund with capture pending for order: #{order_number}, refund status: #{response_status}"
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.10'
4
+ VERSION = '0.0.14'
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.10
4
+ version: 0.0.14
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-08-04 00:00:00.000000000 Z
12
+ date: 2021-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_model_serializers
@@ -246,6 +246,7 @@ files:
246
246
  - app/views/spree/admin/shared/_order_summary_flow.html.erb
247
247
  - app/workers/flowcommerce_spree/flow_io_worker.rb
248
248
  - app/workers/flowcommerce_spree/import_item_worker.rb
249
+ - app/workers/flowcommerce_spree/refund_status_worker.rb
249
250
  - app/workers/flowcommerce_spree/update_payment_capture_worker.rb
250
251
  - config/rails_best_practices.yml
251
252
  - config/routes.rb