Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mollie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Mollie
require 'mollie/payment'
require 'mollie/payment_link'
require 'mollie/permission'
require 'mollie/webhook'
require 'mollie/profile'
require 'mollie/refund'
require 'mollie/settlement'
Expand Down
32 changes: 32 additions & 0 deletions lib/mollie/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Mollie
class Webhook < Base
attr_accessor :id,
:name,
:url,
:event_types,
:created_at,
:_links

alias links _links

def self.embedded_resource_name(_parent_id = nil)
'webhooks'
end

def self.resource_name(_parent_id = nil)
'webhooks'
end

def created_at=(created_at)
@created_at = begin
Time.parse(created_at.to_s)
rescue StandardError
nil
end
end

def test!(options = {})
Client.instance.perform_http_call('POST', "webhooks/#{id}", 'test', {}, options)
end
end
end
5 changes: 5 additions & 0 deletions test/fixtures/webhooks/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Payment webhook",
"url": "https://webshop.example.org/webhooks/payments",
"eventTypes": ["payment-link.paid"]
}
18 changes: 18 additions & 0 deletions test/fixtures/webhooks/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"resource": "webhook",
"id": "hook_7UhSN1zuXS",
"name": "Payment webhook",
"url": "https://webshop.example.org/webhooks/payments",
"eventTypes": ["payment-link.paid"],
"createdAt": "2024-01-15T10:30:00+00:00",
"_links": {
"self": {
"href": "https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS",
"type": "application/hal+json"
},
"documentation": {
"href": "https://docs.mollie.com/reference/v2/webhooks-api/get-webhook",
"type": "text/html"
}
}
}
31 changes: 31 additions & 0 deletions test/fixtures/webhooks/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"count": 3,
"_embedded": {
"webhooks": [
{
"resource": "webhook",
"id": "hook_one"
},
{
"resource": "webhook",
"id": "hook_two"
},
{
"resource": "webhook",
"id": "hook_three"
}
]
},
"_links": {
"self": {
"href": "https://api.mollie.com/v2/webhooks?limit=50",
"type": "application/hal+json"
},
"previous": null,
"next": null,
"documentation": {
"href": "https://docs.mollie.com/reference/v2/webhooks-api/list-webhooks",
"type": "text/html"
}
}
}
5 changes: 5 additions & 0 deletions test/fixtures/webhooks/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Updated payment webhook",
"url": "https://webshop.example.org/webhooks/payments-updated",
"eventTypes": ["payment-link.paid"]
}
85 changes: 85 additions & 0 deletions test/mollie/webhook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require "helper"

module Mollie
class WebhookTest < Test::Unit::TestCase
CREATE_WEBHOOK = read_fixture("webhooks/create.json")
GET_WEBHOOK = read_fixture("webhooks/get.json")
UPDATE_WEBHOOK = read_fixture("webhooks/update.json")
LIST_WEBHOOKS = read_fixture("webhooks/list.json")

def test_create_webhook
minified_body = JSON.parse(CREATE_WEBHOOK).to_json
stub_request(:post, "https://api.mollie.com/v2/webhooks")
.with(body: minified_body)
.to_return(status: 201, body: GET_WEBHOOK, headers: {})

webhook = Webhook.create(
name: "Payment webhook",
url: "https://webshop.example.org/webhooks/payments",
event_types: [
"payment-link.paid"
]
)

assert_kind_of Webhook, webhook
assert_equal "hook_7UhSN1zuXS", webhook.id
end

def test_get_webhook
stub_request(:get, "https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS")
.to_return(status: 200, body: GET_WEBHOOK, headers: {})

webhook = Webhook.get("hook_7UhSN1zuXS")

assert_equal "hook_7UhSN1zuXS", webhook.id
assert_equal "Payment webhook", webhook.name
assert_equal "https://webshop.example.org/webhooks/payments", webhook.url
assert_equal ["payment-link.paid"], webhook.event_types
assert_equal Time.parse("2024-01-15T10:30:00+00:00"), webhook.created_at
end

def test_update_webhook
minified_body = JSON.parse(UPDATE_WEBHOOK).to_json
stub_request(:patch, 'https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS')
.with(body: minified_body)
.to_return(status: 200, body: GET_WEBHOOK, headers: {})

webhook = Webhook.update("hook_7UhSN1zuXS", JSON.parse(UPDATE_WEBHOOK))

assert_kind_of Webhook, webhook
assert_equal "hook_7UhSN1zuXS", webhook.id
end

def test_delete_webhook
stub_request(:delete, 'https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS')
.to_return(status: 204, body: GET_WEBHOOK, headers: {})

webhook = Webhook.delete("hook_7UhSN1zuXS")
assert_nil webhook
end

def test_list_webhooks
stub_request(:get, "https://api.mollie.com/v2/webhooks")
.to_return(status: 200, body: LIST_WEBHOOKS, headers: {})

webhooks = Webhook.all
assert_equal 3, webhooks.size
assert_equal "hook_one", webhooks[0].id
assert_equal "hook_two", webhooks[1].id
assert_equal "hook_three", webhooks[2].id
end

def test_test_webhook
stub_request(:get, "https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS")
.to_return(status: 200, body: GET_WEBHOOK, headers: {})

stub_request(:post, "https://api.mollie.com/v2/webhooks/hook_7UhSN1zuXS/test")
.to_return(status: 200, body: %({}), headers: {})

webhook = Webhook.get("hook_7UhSN1zuXS")
result = webhook.test!

assert_equal({}, result)
end
end
end