Class: Payflow::Providers::Asaas::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/payflow/providers/asaas/webhook.rb

Constant Summary collapse

ASAAS_TOKEN_HEADER =
"asaas-access-token"

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Webhook

Returns a new instance of Webhook.



9
10
11
# File 'lib/payflow/providers/asaas/webhook.rb', line 9

def initialize(client)
  @client = client
end

Instance Method Details

#parse(payload:, headers: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/payflow/providers/asaas/webhook.rb', line 20

def parse(payload:, headers: {})
  body = payload.is_a?(String) ? JSON.parse(payload) : payload
  {
    idempotency_key: body["id"] || body.dig("payment", "id"),
    event_type: map_event_type(body["event"]),
    provider: :asaas,
    data: body
  }
end

#verify_signature(payload:, headers:) ⇒ Object



13
14
15
16
17
18
# File 'lib/payflow/providers/asaas/webhook.rb', line 13

def verify_signature(payload:, headers:)
  token = headers[ASAAS_TOKEN_HEADER] || headers[ASAAS_TOKEN_HEADER.upcase]
  expected = @client.send(:credentials)[:webhook_token]
  return false if expected.blank? || token.blank?
  ActiveSupport::SecurityUtils.secure_compare(expected.to_s, token.to_s)
end