Class: FlowcommerceSpree::Webhooks::CardAuthorizationUpsertedV2

Inherits:
Object
  • Object
show all
Defined in:
app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CardAuthorizationUpsertedV2

Returns a new instance of CardAuthorizationUpsertedV2.



13
14
15
16
17
# File 'app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb', line 13

def initialize(data)
  @data = data['authorization']&.to_hash
  @data&.[]('method')&.delete('images')
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly) Also known as: full_messages

Returns the value of attribute errors.



6
7
8
# File 'app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb', line 6

def errors
  @errors
end

Class Method Details

.process(data) ⇒ Object



9
10
11
# File 'app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb', line 9

def self.process(data)
  new(data).process
end

Instance Method Details

#processObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/flowcommerce_spree/webhooks/card_authorization_upserted_v2.rb', line 19

def process
  errors << { message: 'Authorization param missing' } && (return self) unless @data

  errors << { message: 'Card param missing' } && (return self) unless (flow_io_card = @data.delete('card'))

  if (order_number = @data.dig('order', 'number'))
    if (order = Spree::Order.find_by(number: order_number))
      card = upsert_card(flow_io_card, order)

      order.payments.where(response_code: @data['id'])
           .update_all(source_id: card.id, source_type: 'Spree::CreditCard')

      return card
    else
      errors << { message: "Order #{order_number} not found" }
    end
  else
    errors << { message: 'Order number param missing' }
  end

  self
end