Class: SpreeSquare::WebhooksController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/spree_square/webhooks_controller.rb

Overview

Receives Square webhook notifications. Square-signature-verified instead of Spree/Devise-authenticated, and deliberately does the least possible work synchronously: verify, record, ack, hand off to a job. Square treats a slow or non-2xx response as a delivery failure and retries.

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/spree_square/webhooks_controller.rb', line 9

def create
  raw_body = request.raw_post
  signature = request.headers['x-square-hmacsha256-signature']

  unless SpreeSquare::WebhookVerifier.valid?(
    url: request.original_url,
    body: raw_body,
    signature: signature,
    signing_key: SpreeSquare::Client.instance.webhook_signature_key
  )
    Rails.logger.warn('[SpreeSquare] webhook signature verification failed')
    return head :unauthorized
  end

  payload = JSON.parse(raw_body)
  event = find_or_log_event(payload)
  enqueue_job(event) if event.previously_new_record?

  head :ok
rescue JSON::ParserError
  head :bad_request
end