Skip to content

Commit

Permalink
Merge pull request #78 from mejuri-inc/develop
Browse files Browse the repository at this point in the history
Preparing the new v0.0.20 release
  • Loading branch information
abalpablo authored Mar 17, 2022
2 parents 1b3ed18 + 1e5d672 commit 68be9b2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/services/flowcommerce_spree/order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ def map_payments_to_spree
payment.update_column(:identifier, p['id'])
end

if @order.payments.blank?
payment_method = Spree::PaymentMethod.find_by type: 'Spree::Gateway::FlowIo'
placeholder_payment = Spree::Payment.new(amount: @order.flow_io_total_amount, order: @order,
source: nil, payment_method_id: payment_method.id, state: 'pending')
@order.payments << placeholder_payment
@order.save
end

return if @order.completed?
return if @order.payments.sum(:amount) < @order.flow_io_total_amount || @order.flow_io_balance_amount > 0
return if @order.payments.sum(:amount) < @order.flow_io_total_amount

@order.state = 'confirm'
@order.save!
Expand Down
14 changes: 11 additions & 3 deletions app/services/flowcommerce_spree/webhooks/capture_upserted_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def upsert_order_captures(order, capture)

def map_payment_captures_to_spree(order, payments)
order.flow_data['captures']&.each do |c|
next unless (payment = captured_payment(payments, c))
next unless (payment = captured_payment(payments, c, order))

payment.capture_events.create!(amount: c['amount'], meta: { 'flow_data' => { 'id' => c['id'] } })
return if payment.completed? || payment.capture_events.sum(:amount) < payment.amount
Expand All @@ -68,10 +68,18 @@ def map_payment_captures_to_spree(order, payments)
FlowcommerceSpree::OrderUpdater.new(order: order).finalize_order
end

def captured_payment(flow_order_payments, capture)
def captured_payment(flow_order_payments, capture, order)
return unless capture['status'] == 'succeeded'

auth = capture.dig('authorization', 'id')
payment = order.payments.first

if flow_order_payments.blank? && payment.response_code.blank?
payment.response_code = capture.dig('authorization', 'key')
payment.identifier = capture.dig('authorization', 'key')
payment.save
return payment
end

return unless flow_order_payments&.find { |p| p['reference'] == auth }

return unless (payment = Spree::Payment.find_by(response_code: auth))
Expand Down
2 changes: 1 addition & 1 deletion lib/flowcommerce_spree/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module FlowcommerceSpree
VERSION = '0.0.15'
VERSION = '0.0.19'
end
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@
end
end

context 'and the order contains placeholder payment' do
let!(:payment) { create(:payment, order: order, payment_method_id: gateway.id) }

it 'returns the Spree::Order with upserted captures' do
payment.response_code = nil
result = instance.process

expect_order_with_capture(result, 'succeeded')
end
end

context 'and the order contains flow_io payments' do
let(:flow_payment) { build(:flow_order_payment, reference: order_auth.id) }
let(:zone) { create(:germany_zone, :with_flow_data) }
Expand Down

0 comments on commit 68be9b2

Please sign in to comment.