flowcommerce_spree 0.0.8 → 0.0.12

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: c89d8869da7867a7ae0400dab1005c763c5c98f001336bf915f14743bd279c7a
4
- data.tar.gz: f664b4f9f7380860b5c5eec7cf5aeda85d59c8b6d2753830f553ccc8734718c4
3
+ metadata.gz: 6cab2d22c877f0d20eff325cb6dc587b1632837052c49a72d08e2cbcca1c5a2b
4
+ data.tar.gz: 872d34b049b6fc265e53d4471a18338b3cde86a89ef887b9cdf971cde142d760
5
5
  SHA512:
6
- metadata.gz: f32478dce9ed19e62ddb9955a0e0d8f7b7388eb9c5079be107282306c85ab524b49f83b35cff0d5835fc79c485d0dd3fc15cb924969ba250dd80182dfb6242c4
7
- data.tar.gz: b1e68c45f17888c1d717001a2a352957e04a4fb32c47d2864349299d868e01e0cbfd138f7ed599cd5c1a06e3847b4e4745d8630b0472d104c7eceabe6177f518
6
+ metadata.gz: 0a838ec7ff7bfe0ffd5ea257726590a0a2f79c6c187db1318057886212510b11124e3d6f9b9b1c8a21f7964b2ad1a07d4e165bf43fd2540e881549ae1ce2a196
7
+ data.tar.gz: a23bce0c4414a93833812b4a4495021d0555979c04bf4efd351134c6cb47fb8fab1bc4b2c7f05ac2c938098c04c6816e4d0e1a2133133e5d5f47d907c8e9e644
@@ -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
@@ -21,7 +21,7 @@ module Spree
21
21
  end
22
22
 
23
23
  def payment_profiles_supported?
24
- true
24
+ false
25
25
  end
26
26
 
27
27
  def method_type
@@ -43,14 +43,18 @@ module Spree
43
43
  end
44
44
 
45
45
  def refund(payment, amount, _options = {})
46
- request_refund_store_result(payment.order, amount)
46
+ response = request_refund_store_result(payment.order, amount)
47
+ map_refund_to_payment(response, payment.order) if response.success?
48
+ response
47
49
  rescue StandardError => e
48
50
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
49
51
  end
50
52
 
51
53
  def cancel(authorization)
52
54
  original_payment = Spree::Payment.find_by(response_code: authorization)
53
- request_refund_store_result(original_payment.order, original_payment.amount)
55
+ response = request_refund_store_result(original_payment.order, original_payment.amount)
56
+ map_refund_to_payment(response, original_payment.order) if response.success?
57
+ response
54
58
  rescue StandardError => e
55
59
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
56
60
  end
@@ -72,6 +76,12 @@ module Spree
72
76
  create_flow_cc_profile!
73
77
  end
74
78
 
79
+ def credit(payment, credit_amount)
80
+ request_refund_store_result(payment.order, credit_amount)
81
+ rescue StandardError => e
82
+ ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
83
+ end
84
+
75
85
  private
76
86
 
77
87
  def request_refund_store_result(order, amount)
@@ -82,8 +92,10 @@ module Spree
82
92
  response_status = response.status.value
83
93
  if response_status == REFUND_SUCCESS
84
94
  add_refund_to_order(response, order)
85
- map_refund_to_payment(response, order)
86
- ActiveMerchant::Billing::Response.new(true, REFUND_SUCCESS, {}, {})
95
+ ActiveMerchant::Billing::Response.new(true,
96
+ REFUND_SUCCESS,
97
+ response.to_hash,
98
+ authorization: response.authorization.id)
87
99
  else
88
100
  msg = "Partial refund fail. Details: #{response_status}"
89
101
  ActiveMerchant::Billing::Response.new(false, msg, {}, {})
@@ -100,18 +112,17 @@ module Spree
100
112
  end
101
113
 
102
114
  def map_refund_to_payment(response, order)
103
- original_payment = Spree::Payment.find_by(response_code: response.authorization.id)
115
+ original_payment = Spree::Payment.find_by(response_code: response.authorization)
104
116
  payment = order.payments.create!(state: 'completed',
105
- response_code: response.authorization.id,
117
+ response_code: response.authorization,
106
118
  payment_method_id: original_payment&.payment_method_id,
107
- amount: - response.amount,
108
- source_id: original_payment&.source_id,
109
- source_type: original_payment&.source_type)
119
+ amount: - response.params['amount'].to_f,
120
+ source: original_payment)
110
121
 
111
122
  # For now this additional update is overwriting the generated identifier with flow.io payment identifier.
112
123
  # TODO: Check and possibly refactor in Spree 3.0, where the `before_create :set_unique_identifier`
113
124
  # has been removed.
114
- payment.update_column(:identifier, response.id)
125
+ payment.update_column(:identifier, response.params['id'])
115
126
  end
116
127
 
117
128
  # hard inject Flow as payment method unless defined
@@ -124,7 +124,7 @@ module FlowcommerceSpree
124
124
  { center: FLOW_CENTER,
125
125
  number: variant.sku,
126
126
  quantity: line_item.quantity,
127
- price: { amount: price_root['amount'] || variant.cost_price,
127
+ price: { amount: price_root['amount'] || variant.price,
128
128
  currency: price_root['currency'] || variant.cost_currency } }
129
129
  end
130
130
 
@@ -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.8'
4
+ VERSION = '0.0.12'
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.8
4
+ version: 0.0.12
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-16 00:00:00.000000000 Z
12
+ date: 2021-08-13 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