Class: Spree::Gateway::FlowIo

Inherits:
Spree::Gateway show all
Defined in:
app/models/spree/gateway/flow_io.rb

Constant Summary collapse

REFUND_VALID_STATES =
%w[succeeded pending].freeze
REFUND_SUCCESS =
'succeeded'
REFUND_PENDING =
'pending'

Instance Method Summary collapse

Instance Method Details

#actionsObject



16
17
18
# File 'app/models/spree/gateway/flow_io.rb', line 16

def actions
  %w[capture authorize purchase refund void]
end

#authorize(_amount, _payment_method, options = {}) ⇒ Object



42
43
44
45
# File 'app/models/spree/gateway/flow_io.rb', line 42

def authorize(_amount, _payment_method, options = {})
  order = load_order options
  order.cc_authorization
end

#auto_capture?Boolean

if user wants to force auto capture

Returns:

  • (Boolean)


21
22
23
# File 'app/models/spree/gateway/flow_io.rb', line 21

def auto_capture?
  false
end

#cancel(authorization) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/models/spree/gateway/flow_io.rb', line 55

def cancel(authorization)
  original_payment = Spree::Payment.find_by(response_code: authorization)
  response = request_refund_store_result(original_payment.order, original_payment.amount)
  map_refund_to_payment(response, original_payment.order) if response.success?
  response
rescue StandardError => e
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
end

#create_profile(payment) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/spree/gateway/flow_io.rb', line 77

def create_profile(payment)
  # payment.order.state
  @credit_card = payment.source

  profile_ensure_payment_method_is_present!
  create_flow_cc_profile!
end

#credit(payment, credit_amount) ⇒ Object



85
86
87
88
89
# File 'app/models/spree/gateway/flow_io.rb', line 85

def credit(payment, credit_amount)
  request_refund_store_result(payment.order, credit_amount)
rescue StandardError => e
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
end

#method_typeObject



29
30
31
# File 'app/models/spree/gateway/flow_io.rb', line 29

def method_type
  'flow_io_gateway'
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/spree/gateway/flow_io.rb', line 25

def payment_profiles_supported?
  false
end

#preferencesObject



33
34
35
# File 'app/models/spree/gateway/flow_io.rb', line 33

def preferences
  {}
end

#provider_classObject



12
13
14
# File 'app/models/spree/gateway/flow_io.rb', line 12

def provider_class
  self.class
end

#refund(payment, amount, _options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'app/models/spree/gateway/flow_io.rb', line 47

def refund(payment, amount, _options = {})
  response = request_refund_store_result(payment.order, amount)
  map_refund_to_payment(response, payment.order) if response.success?
  response
rescue StandardError => e
  ActiveMerchant::Billing::Response.new(false, e.to_s, {}, {})
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'app/models/spree/gateway/flow_io.rb', line 37

def supports?(source)
  # flow supports credit cards
  source.class == Spree::CreditCard
end

#void(authorization_id, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/gateway/flow_io.rb', line 64

def void(authorization_id, options = {})
  amount = (options[:subtotal] + options[:shipping]) * 0.01
  order = Spree::Order.find_by(number: options[:order_id].split('-').first)
  refund_form = Io::Flow::V0::Models::RefundForm.new(order_number: order.number,
                                                     amount: amount,
                                                     currency: options[:currency],
                                                     authorization_id: authorization_id)
  response = FlowcommerceSpree.client.refunds.post(FlowcommerceSpree::ORGANIZATION, refund_form)
  validate_flow_refund_response(response, order)

  ActiveMerchant::Billing::Response.new(true, 'flow-payment-void', {}, {})
end