flowcommerce_spree 0.0.8 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/users/sessions_controller_decorator.rb +3 -1
- data/app/models/spree/flow_io_product_decorator.rb +14 -10
- data/app/models/spree/gateway/flow_io.rb +22 -11
- data/app/services/flowcommerce_spree/order_sync.rb +1 -1
- data/app/services/flowcommerce_spree/webhooks/capture_upserted_v2.rb +13 -4
- data/app/workers/flowcommerce_spree/flow_io_worker.rb +16 -0
- data/app/workers/flowcommerce_spree/import_item_worker.rb +1 -11
- data/app/workers/flowcommerce_spree/update_payment_capture_worker.rb +15 -0
- data/lib/flowcommerce_spree/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cab2d22c877f0d20eff325cb6dc587b1632837052c49a72d08e2cbcca1c5a2b
|
4
|
+
data.tar.gz: 872d34b049b6fc265e53d4471a18338b3cde86a89ef887b9cdf971cde142d760
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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
|
44
|
-
max = price
|
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
|
51
|
-
rmax = max
|
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
|
74
|
-
max = price
|
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
|
84
|
-
rmax = max
|
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
|
-
|
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
|
-
|
86
|
-
|
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
|
115
|
+
original_payment = Spree::Payment.find_by(response_code: response.authorization)
|
104
116
|
payment = order.payments.create!(state: 'completed',
|
105
|
-
response_code: response.authorization
|
117
|
+
response_code: response.authorization,
|
106
118
|
payment_method_id: original_payment&.payment_method_id,
|
107
|
-
amount: - response.amount,
|
108
|
-
|
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.
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
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.
|
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-
|
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
|