flowcommerce_spree 0.0.12 → 0.0.16

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: 6cab2d22c877f0d20eff325cb6dc587b1632837052c49a72d08e2cbcca1c5a2b
4
- data.tar.gz: 872d34b049b6fc265e53d4471a18338b3cde86a89ef887b9cdf971cde142d760
3
+ metadata.gz: 5c9cb2032b50a1392231cb88c7ef6549e623ac604bcdcfecc8d8224e9f8558c4
4
+ data.tar.gz: 1564b9412b0be89b38ff6a7feb86397311ebe400614618c13c495c58c90059f4
5
5
  SHA512:
6
- metadata.gz: 0a838ec7ff7bfe0ffd5ea257726590a0a2f79c6c187db1318057886212510b11124e3d6f9b9b1c8a21f7964b2ad1a07d4e165bf43fd2540e881549ae1ce2a196
7
- data.tar.gz: a23bce0c4414a93833812b4a4495021d0555979c04bf4efd351134c6cb47fb8fab1bc4b2c7f05ac2c938098c04c6816e4d0e1a2133133e5d5f47d907c8e9e644
6
+ metadata.gz: 88f39ac40b274fbcbf29de37a461b14b7aca12d0496d20c89ba27eeee63d3013c776631737cc1f5b154ffe2f67c812aed6aac850d49584853b4063ef81c85acb
7
+ data.tar.gz: f085428b7616287640093de5cb1635dd6a7e8d63b77b64991f88062fa8c2ef271dc0763a7bc21b5331b2348a822485f496716b4ce7f5db7e6147584f56c2735c
@@ -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
 
@@ -5,6 +5,8 @@ module FlowcommerceSpree
5
5
  wrap_parameters false
6
6
 
7
7
  skip_before_action :setup_tracking, only: :order_completed
8
+ skip_before_action :prepare_order
9
+ skip_before_action :before_render
8
10
 
9
11
  # proxy enpoint between flow and thankyou page.
10
12
  # /flow/order_completed endpoint
@@ -14,7 +16,11 @@ module FlowcommerceSpree
14
16
  flow_updater = FlowcommerceSpree::OrderUpdater.new(order: order)
15
17
  flow_updater.complete_checkout
16
18
 
17
- redirect_to "/thankyou?order=#{params[:order]}&t=#{params[:t]}"
19
+ redirection_path = "/thankyou?order=#{params[:order]}&t=#{params[:t]}"
20
+ locale = order.locale_path
21
+ redirection_path = "/#{locale}#{redirection_path}" if locale
22
+
23
+ redirect_to redirection_path
18
24
  end
19
25
  end
20
26
  end
@@ -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
@@ -59,13 +61,17 @@ module Spree
59
61
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
60
62
  end
61
63
 
62
- def void(authorization_id, _source, options = {})
64
+ def void(authorization_id, options = {})
63
65
  amount = (options[:subtotal] + options[:shipping]) * 0.01
64
- reversal_form = Io::Flow::V0::Models::ReversalForm.new(key: options[:order_id],
65
- authorization_id: authorization_id,
66
- amount: amount,
67
- currency: options[:currency])
68
- FlowcommerceSpree.client.reversals.post(FlowcommerceSpree::ORGANIZATION, reversal_form)
66
+ order = Spree::Order.find_by(number: options[:order_id].split('-').first)
67
+ refund_form = Io::Flow::V0::Models::RefundForm.new(order_number: order.number,
68
+ amount: amount,
69
+ currency: options[:currency],
70
+ authorization_id: authorization_id)
71
+ response = FlowcommerceSpree.client.refunds.post(FlowcommerceSpree::ORGANIZATION, refund_form)
72
+ validate_flow_refund_response(response, order)
73
+
74
+ ActiveMerchant::Billing::Response.new(true, 'flow-payment-void', {}, {})
69
75
  end
70
76
 
71
77
  def create_profile(payment)
@@ -89,19 +95,31 @@ module Spree
89
95
  amount: amount,
90
96
  currency: order.currency)
91
97
  response = FlowcommerceSpree.client.refunds.post(FlowcommerceSpree::ORGANIZATION, refund_form)
98
+ result = validate_flow_refund_response(response, order)
99
+ result
100
+ end
101
+
102
+ def validate_flow_refund_response(response, order)
92
103
  response_status = response.status.value
93
- if response_status == REFUND_SUCCESS
104
+ if REFUND_VALID_STATES.include? response_status
94
105
  add_refund_to_order(response, order)
106
+ schedule_status_check(response, order)
95
107
  ActiveMerchant::Billing::Response.new(true,
96
108
  REFUND_SUCCESS,
97
109
  response.to_hash,
98
110
  authorization: response.authorization.id)
99
111
  else
100
- msg = "Partial refund fail. Details: #{response_status}"
101
- ActiveMerchant::Billing::Response.new(false, msg, {}, {})
112
+ ActiveMerchant::Billing::Response.new(false, "Partial refund fail. Details: #{response_status}", {}, {})
102
113
  end
103
114
  end
104
115
 
116
+ def schedule_status_check(response, order)
117
+ return if response.status.value != REFUND_PENDING
118
+
119
+ Rails.logger.warn("[!] #{self.class} for #{order.number} - refund request without succeeded status.")
120
+ FlowcommerceSpree::RefundStatusWorker.perform_async(order.number, response.key)
121
+ end
122
+
105
123
  def add_refund_to_order(response, order)
106
124
  order.flow_data ||= {}
107
125
  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.12'
4
+ VERSION = '0.0.16'
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.12
4
+ version: 0.0.16
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-13 00:00:00.000000000 Z
12
+ date: 2021-09-22 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
@@ -284,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
284
285
  - !ruby/object:Gem::Version
285
286
  version: '0'
286
287
  requirements: []
287
- rubygems_version: 3.0.8
288
+ rubygems_version: 3.0.3
288
289
  signing_key:
289
290
  specification_version: 4
290
291
  summary: Integration of Spree with Flow API