Module: LoyverseApi::Endpoints::Webhooks

Included in:
Client
Defined in:
lib/loyverse_api/endpoints/webhooks.rb

Instance Method Summary collapse

Instance Method Details

#create_webhook(url:, event_types:, description: nil) ⇒ Hash

Create a new webhook Available event types:

  • ORDER_CREATED
  • ITEM_UPDATED
  • INVENTORY_UPDATED

Parameters:

  • url (String) —

    Webhook endpoint URL

  • event_types (Array<String>) —

    Array of event types to subscribe to

  • description (String) (defaults to: nil) —

    Webhook description (optional)

Returns:

  • (Hash) —

    Created webhook details



34
35
36
37
38
39
40
41
42
# File 'lib/loyverse_api/endpoints/webhooks.rb', line 34

def create_webhook(url:, event_types:, description: nil)
  body = {
    url: url,
    event_types: Array(event_types)
  }
  body[:description] = description if description

  post("webhooks", body: body)
end

#delete_webhook(webhook_id) ⇒ Hash

Delete a webhook

Parameters:

  • webhook_id (String) —

    UUID of the webhook

Returns:

  • (Hash) —

    Response



47
48
49
# File 'lib/loyverse_api/endpoints/webhooks.rb', line 47

def delete_webhook(webhook_id)
  delete("webhooks/#{webhook_id}")
end

#get_webhook(webhook_id) ⇒ Hash

Get a specific webhook by ID

Parameters:

  • webhook_id (String) —

    UUID of the webhook

Returns:

  • (Hash) —

    Webhook details



7
8
9
# File 'lib/loyverse_api/endpoints/webhooks.rb', line 7

def get_webhook(webhook_id)
  get("webhooks/#{webhook_id}")
end

#list_webhooks(limit: 250, cursor: nil) ⇒ Hash

List all webhooks

Parameters:

  • limit (Integer) (defaults to: 250) —

    Maximum number of results per page (default: 250)

  • cursor (String) (defaults to: nil) —

    Pagination cursor for next page

Returns:

  • (Hash) —

    Response with webhooks array



15
16
17
18
19
20
21
22
# File 'lib/loyverse_api/endpoints/webhooks.rb', line 15

def list_webhooks(limit: 250, cursor: nil)
  params = {
    limit: limit,
    cursor: cursor
  }.compact

  get("webhooks", params: params)
end

#verify_webhook_signature(payload, signature, secret) ⇒ Boolean

Verify webhook signature (for OAuth 2.0 created webhooks)

Parameters:

  • payload (String) —

    Raw request body

  • signature (String) —

    X-Loyverse-Signature header value

  • secret (String) —

    Your webhook secret

Returns:

  • (Boolean) —

    True if signature is valid



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/loyverse_api/endpoints/webhooks.rb', line 56

def verify_webhook_signature(payload, signature, secret)
  return false if payload.nil? || signature.nil? || secret.nil?

  require "openssl"

  computed_signature = OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest.new("sha256"),
    secret,
    payload
  )

  secure_compare(computed_signature, signature)
end