flowcommerce_spree 0.0.18 → 0.0.21

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,4 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: a58357f2f4891679ea1b21bf15603d6d61964e1a07d3d4c5259b834207be4605
4
- data.tar.gz: 16f2b8a0ba784bfeb3685178f0efe12fbd3ddd963d4e7058c84397f7b4d3ac5f
5
2
  SHA512:
6
- metadata.gz: 4fe235560e3132fcd9ed90d4b3e6577af09817f59a79564ad41b2a60477d08158e7fe52ed67eaea28749af87e59c192a5c98c16f360ea48d810dc97396a4fdde
7
- data.tar.gz: 0beb3883f44c1b08f520e3a94490b03e960c87fa8be7c41f38399ddbc4ff3db9801e3f47adca93056d6e4e9b2b86c773dfcc670bad6b3954a41279ab6ac472a5
3
+ metadata.gz: 95290cdfeae1006b8ebedf57d37755bf553c11111d09bb6fb81e856e15672763ecefb751f2a3d928e6e81c27f8a62fdc1c96de562b8b86ecb12c790735b02234
4
+ data.tar.gz: 6fc0c835d7c0e6fc9ee7e0dd95e6faa4924c81c09f8014cba04c1fb053825157f76415a9f15dc7c4f6b4098344bce937e151da84c359619f34f55dbed5b17cb2
@@ -26,8 +26,8 @@ module Spree
26
26
 
27
27
  attrs_to_update = {}
28
28
 
29
- # Update last_ip_address only for a new request_id
30
- attrs_to_update = { last_ip_address: ip_address } if @request_id != request_id
29
+ # Update last_ip_address only when last_ip_address is different.
30
+ attrs_to_update = { last_ip_address: ip_address } if @current_order.last_ip_address != ip_address
31
31
 
32
32
  # :meta is a jsonb column costly to update every time, especially with all the flow.io data, that's why
33
33
  # here it is updated only if no zone_id there was inside :meta
@@ -63,8 +63,16 @@ module FlowcommerceSpree
63
63
  payment.update_column(:identifier, p['id'])
64
64
  end
65
65
 
66
+ if @order.payments.blank?
67
+ payment_method = Spree::PaymentMethod.find_by type: 'Spree::Gateway::FlowIo'
68
+ placeholder_payment = Spree::Payment.new(amount: @order.flow_io_total_amount, order: @order,
69
+ source: nil, payment_method_id: payment_method.id, state: 'pending')
70
+ @order.payments << placeholder_payment
71
+ @order.save
72
+ end
73
+
66
74
  return if @order.completed?
67
- return if @order.payments.sum(:amount) < @order.flow_io_total_amount || @order.flow_io_balance_amount > 0
75
+ return if @order.payments.sum(:amount) < @order.flow_io_total_amount
68
76
 
69
77
  @order.state = 'confirm'
70
78
  @order.save!
@@ -36,8 +36,7 @@ module FlowcommerceSpree
36
36
 
37
37
  def store_payment_capture(order, capture)
38
38
  upsert_order_captures(order, capture)
39
- payments = order.flow_io_payments
40
- map_payment_captures_to_spree(order, payments) if payments.present?
39
+ map_payment_captures_to_spree(order)
41
40
  order
42
41
  end
43
42
 
@@ -52,26 +51,32 @@ module FlowcommerceSpree
52
51
  order.update_column(:meta, order.meta.to_json)
53
52
  end
54
53
 
55
- def map_payment_captures_to_spree(order, payments)
54
+ def map_payment_captures_to_spree(order)
55
+ payments = order.flow_io_payments
56
56
  order.flow_data['captures']&.each do |c|
57
- next unless (payment = captured_payment(payments, c))
57
+ next unless c['status'] == 'succeeded'
58
+
59
+ payment = map_payment(payments, c, order)
60
+ next unless payment
58
61
 
59
62
  payment.capture_events.create!(amount: c['amount'], meta: { 'flow_data' => { 'id' => c['id'] } })
60
63
  return if payment.completed? || payment.capture_events.sum(:amount) < payment.amount
61
64
 
62
65
  payment.complete
63
66
  end
64
-
65
67
  return if order.completed?
66
68
  return unless order.flow_io_captures_sum >= order.flow_io_total_amount && order.flow_io_balance_amount <= 0
67
69
 
68
70
  FlowcommerceSpree::OrderUpdater.new(order: order).finalize_order
69
71
  end
70
72
 
71
- def captured_payment(flow_order_payments, capture)
72
- return unless capture['status'] == 'succeeded'
73
+ def map_payment(payments, capture, order)
74
+ payments.present? ? captured_payment(payments, capture) : get_payments_from_flow(order, capture)
75
+ end
73
76
 
77
+ def captured_payment(flow_order_payments, capture)
74
78
  auth = capture.dig('authorization', 'id')
79
+
75
80
  return unless flow_order_payments&.find { |p| p['reference'] == auth }
76
81
 
77
82
  return unless (payment = Spree::Payment.find_by(response_code: auth))
@@ -80,6 +85,23 @@ module FlowcommerceSpree
80
85
 
81
86
  payment
82
87
  end
88
+
89
+ def get_payments_from_flow(order, capture)
90
+ flow_io_order ||= FlowcommerceSpree.client.orders
91
+ .get_by_number(FlowcommerceSpree::ORGANIZATION, order.number).to_hash
92
+ return unless flow_io_order[:payments]
93
+
94
+ order.flow_data['order']['payments'] = flow_io_order[:payments]
95
+ attrs_to_update = { meta: order.meta.to_json }
96
+ attrs_to_update.merge!(order.prepare_flow_addresses)
97
+ order.update_columns(attrs_to_update)
98
+ payment = order.payments.first
99
+ payment.response_code = capture.dig('authorization', 'id')
100
+ payment.identifier = capture.dig('authorization', 'id')
101
+ payment.save
102
+
103
+ payment
104
+ end
83
105
  end
84
106
  end
85
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FlowcommerceSpree
4
- VERSION = '0.0.18'
4
+ VERSION = '0.0.21'
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flowcommerce_spree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurel Branzeanu
8
8
  - Sebastian De Luca
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-11-01 00:00:00.000000000 Z
12
+ date: 2022-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_model_serializers
@@ -270,7 +270,7 @@ homepage: https://github.com/mejuri-inc/flowcommerce_spree
270
270
  licenses:
271
271
  - MIT
272
272
  metadata: {}
273
- post_install_message:
273
+ post_install_message:
274
274
  rdoc_options: []
275
275
  require_paths:
276
276
  - lib
@@ -285,8 +285,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
285
285
  - !ruby/object:Gem::Version
286
286
  version: '0'
287
287
  requirements: []
288
- rubygems_version: 3.0.3
289
- signing_key:
288
+ rubygems_version: 3.0.9
289
+ signing_key:
290
290
  specification_version: 4
291
291
  summary: Integration of Spree with Flow API
292
292
  test_files: []