Class: AbacatePay::Clients::WebhookClient

Inherits:
Client
  • Object
show all
Defined in:
lib/abacate_pay/clients/webhook_client.rb

Overview

Client for managing webhook endpoint registrations in the AbacatePay API.

This manages where AbacatePay delivers events. To verify and parse an inbound delivery, use Webhooks.construct_event.

Constant Summary collapse

URI =
"webhooks"
MINIMUM_SECRET_LENGTH =

Shortest signing secret the API accepts.

32

Constants inherited from Client

Client::RETRIABLE_EXCEPTIONS, Client::RETRIABLE_METHODS, Client::RETRIABLE_STATUSES

Instance Method Summary collapse

Methods inherited from Client

#auto_paging_each, #each_page

Constructor Details

#initialize(client = nil) ⇒ WebhookClient

Returns a new instance of WebhookClient.

Parameters:

  • client (Faraday::Connection, nil) (defaults to: nil)

    Optional Faraday client



18
19
20
# File 'lib/abacate_pay/clients/webhook_client.rb', line 18

def initialize(client = nil)
  super(URI, client)
end

Instance Method Details

#create(name:, endpoint:, secret:, events:) ⇒ Resources::WebhookEndpoints

Registers a new webhook endpoint.

The secret is what AbacatePay signs deliveries with, pass the same value to Webhooks.construct_event when handling them.

Parameters:

  • name (String)

    Identifying name

  • endpoint (String)

    HTTPS URL that will receive the events

  • secret (String)

    Signing secret

  • events (Array<String>)

    Event types to subscribe to

Returns:

Raises:

  • (ArgumentError)

    if the endpoint is not HTTPS, the event list is empty, or the secret is shorter than the API accepts



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/abacate_pay/clients/webhook_client.rb', line 48

def create(name:, endpoint:, secret:, events:)
  raise ArgumentError, "endpoint must be an HTTPS URL, got #{endpoint.inspect}" unless https_url?(endpoint)
  raise ArgumentError, "events must not be empty" if Array(events).empty?

  # The API rejects anything shorter with
  # "Expected string length greater or equal to 32".
  if secret.to_s.length < MINIMUM_SECRET_LENGTH
    raise ArgumentError,
          "secret must be at least #{MINIMUM_SECRET_LENGTH} characters, got #{secret.to_s.length}"
  end

  response = request("POST", "create", json: {
                       name: name,
                       endpoint: endpoint,
                       secret: secret,
                       events: Array(events)
                     })
  Resources::WebhookEndpoints.new(response)
end

#delete(id) ⇒ Resources::WebhookEndpoints?

Returns The deleted webhook, or nil when the API confirms the deletion without echoing the object.

Parameters:

  • id (String)

    The webhook ID

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/abacate_pay/clients/webhook_client.rb', line 71

def delete(id)
  # The API reads the id from the query string here, like
  # transparents/simulate-payment. Sending it in the body fails with
  # "Expected property 'id' to be string but found: undefined".
  response = request("POST", "delete", params: { id: id }, json: {})
  return nil if response.nil?

  Resources::WebhookEndpoints.new(response)
end

#get(id) ⇒ Resources::WebhookEndpoints

Parameters:

  • id (String)

    The webhook ID

Returns:



31
32
33
34
# File 'lib/abacate_pay/clients/webhook_client.rb', line 31

def get(id)
  response = request("GET", "get", params: { id: id })
  Resources::WebhookEndpoints.new(response)
end

#list(**params) ⇒ Array<Resources::WebhookEndpoints>

Parameters:

  • params (Hash)

    Optional params (limit, after, before, startDate, endDate)

Returns:



24
25
26
27
# File 'lib/abacate_pay/clients/webhook_client.rb', line 24

def list(**params)
  response = request("GET", "list", params: params.empty? ? nil : params)
  build_list(response, Resources::WebhookEndpoints)
end