Sha256: 45c4b8c18e3a76dc7ee6e98ce42ed581184c52221a1f08705152f6eb03e6912e

Contents?: true

Size: 1.66 KB

Versions: 13

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require_relative 'webhook_event'

# ref: https://developer.intuit.com/app/developer/qbo/docs/develop/webhooks/managing-webhooks-notifications#validating-the-notification
module LedgerSync
  module QuickBooksOnline
    class Webhook
      attr_reader :notifications,
                  :original_payload,
                  :payload

      def initialize(payload:)
        @original_payload = payload
        @payload = payload.is_a?(String) ? JSON.parse(payload) : payload

        event_notifications_payload = @payload['eventNotifications']
        raise 'Invalid payload: Could not find eventNotifications' unless event_notifications_payload.is_a?(Array)

        @notifications = []

        event_notifications_payload.each do |event_notification_payload|
          @notifications << WebhookNotification.new(
            payload: event_notification_payload,
            webhook: self
          )
        end
      end

      def events
        notifications.map(&:events)
      end

      def resources
        @resources ||= notifications.map(&:resources).flatten.compact
      end

      def valid?(signature:, verification_token:)
        self.class.valid?(
          payload: payload.to_json,
          signature: signature,
          verification_token: verification_token
        )
      end

      def self.valid?(payload:, signature:, verification_token:)
        raise 'Cannot verify non-String payload' unless payload.is_a?(String)

        digest = OpenSSL::Digest.new('sha256')
        hmac = OpenSSL::HMAC.digest(digest, verification_token, payload)
        base64 = Base64.encode64(hmac).strip
        base64 == signature
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ledger_sync-quickbooks_online-2.0.0 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-1.0.1 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-1.0.0 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.4.0 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.3.1 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.3.0 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.6 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.5 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.4 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.2 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.1 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.2.0 lib/ledger_sync/quickbooks_online/webhook.rb
ledger_sync-quickbooks_online-0.1.1 lib/ledger_sync/quickbooks_online/webhook.rb