From 5da0df594364f96eb489a061f4ae9804d66bd64c Mon Sep 17 00:00:00 2001 From: Mathijs Date: Mon, 26 Jan 2026 12:11:35 +0100 Subject: [PATCH 1/2] Add support for webhooks --- lib/mollie.rb | 1 + lib/mollie/webhook.rb | 32 +++++++++++ test/fixtures/webhooks/create.json | 9 ++++ test/fixtures/webhooks/get.json | 22 ++++++++ test/fixtures/webhooks/list.json | 31 +++++++++++ test/fixtures/webhooks/update.json | 8 +++ test/mollie/webhook_test.rb | 87 ++++++++++++++++++++++++++++++ 7 files changed, 190 insertions(+) create mode 100644 lib/mollie/webhook.rb create mode 100644 test/fixtures/webhooks/create.json create mode 100644 test/fixtures/webhooks/get.json create mode 100644 test/fixtures/webhooks/list.json create mode 100644 test/fixtures/webhooks/update.json create mode 100644 test/mollie/webhook_test.rb diff --git a/lib/mollie.rb b/lib/mollie.rb index 3949cd9..5942378 100644 --- a/lib/mollie.rb +++ b/lib/mollie.rb @@ -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' diff --git a/lib/mollie/webhook.rb b/lib/mollie/webhook.rb new file mode 100644 index 0000000..844c9b6 --- /dev/null +++ b/lib/mollie/webhook.rb @@ -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 diff --git a/test/fixtures/webhooks/create.json b/test/fixtures/webhooks/create.json new file mode 100644 index 0000000..e3deb9d --- /dev/null +++ b/test/fixtures/webhooks/create.json @@ -0,0 +1,9 @@ +{ + "name": "Payment webhook", + "url": "https://webshop.example.org/webhooks/payments", + "eventTypes": [ + "payment.paid", + "payment.failed", + "payment.canceled" + ] +} diff --git a/test/fixtures/webhooks/get.json b/test/fixtures/webhooks/get.json new file mode 100644 index 0000000..18bf2ff --- /dev/null +++ b/test/fixtures/webhooks/get.json @@ -0,0 +1,22 @@ +{ + "resource": "webhook", + "id": "hook_7UhSN1zuXS", + "name": "Payment webhook", + "url": "https://webshop.example.org/webhooks/payments", + "eventTypes": [ + "payment.paid", + "payment.failed", + "payment.canceled" + ], + "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" + } + } +} diff --git a/test/fixtures/webhooks/list.json b/test/fixtures/webhooks/list.json new file mode 100644 index 0000000..8f40022 --- /dev/null +++ b/test/fixtures/webhooks/list.json @@ -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" + } + } +} diff --git a/test/fixtures/webhooks/update.json b/test/fixtures/webhooks/update.json new file mode 100644 index 0000000..4faf51b --- /dev/null +++ b/test/fixtures/webhooks/update.json @@ -0,0 +1,8 @@ +{ + "name": "Updated payment webhook", + "url": "https://webshop.example.org/webhooks/payments-updated", + "eventTypes": [ + "payment.paid", + "payment.authorized" + ] +} diff --git a/test/mollie/webhook_test.rb b/test/mollie/webhook_test.rb new file mode 100644 index 0000000..0132eb9 --- /dev/null +++ b/test/mollie/webhook_test.rb @@ -0,0 +1,87 @@ +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.paid", + "payment.failed", + "payment.canceled" + ] + ) + + 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.paid", "payment.failed", "payment.canceled"], 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 From 65f3ef7b66081ec9a3b18b31c73964b754373e30 Mon Sep 17 00:00:00 2001 From: Mathijs Date: Wed, 28 Jan 2026 10:42:39 +0100 Subject: [PATCH 2/2] Fix tests --- test/fixtures/webhooks/create.json | 10 +++------ test/fixtures/webhooks/get.json | 34 +++++++++++++----------------- test/fixtures/webhooks/update.json | 9 +++----- test/mollie/webhook_test.rb | 6 ++---- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/test/fixtures/webhooks/create.json b/test/fixtures/webhooks/create.json index e3deb9d..a614259 100644 --- a/test/fixtures/webhooks/create.json +++ b/test/fixtures/webhooks/create.json @@ -1,9 +1,5 @@ { - "name": "Payment webhook", - "url": "https://webshop.example.org/webhooks/payments", - "eventTypes": [ - "payment.paid", - "payment.failed", - "payment.canceled" - ] + "name": "Payment webhook", + "url": "https://webshop.example.org/webhooks/payments", + "eventTypes": ["payment-link.paid"] } diff --git a/test/fixtures/webhooks/get.json b/test/fixtures/webhooks/get.json index 18bf2ff..d6631bd 100644 --- a/test/fixtures/webhooks/get.json +++ b/test/fixtures/webhooks/get.json @@ -1,22 +1,18 @@ { - "resource": "webhook", - "id": "hook_7UhSN1zuXS", - "name": "Payment webhook", - "url": "https://webshop.example.org/webhooks/payments", - "eventTypes": [ - "payment.paid", - "payment.failed", - "payment.canceled" - ], - "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" - } + "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" } + } } diff --git a/test/fixtures/webhooks/update.json b/test/fixtures/webhooks/update.json index 4faf51b..94f6f1e 100644 --- a/test/fixtures/webhooks/update.json +++ b/test/fixtures/webhooks/update.json @@ -1,8 +1,5 @@ { - "name": "Updated payment webhook", - "url": "https://webshop.example.org/webhooks/payments-updated", - "eventTypes": [ - "payment.paid", - "payment.authorized" - ] + "name": "Updated payment webhook", + "url": "https://webshop.example.org/webhooks/payments-updated", + "eventTypes": ["payment-link.paid"] } diff --git a/test/mollie/webhook_test.rb b/test/mollie/webhook_test.rb index 0132eb9..c7d3ba7 100644 --- a/test/mollie/webhook_test.rb +++ b/test/mollie/webhook_test.rb @@ -17,9 +17,7 @@ def test_create_webhook name: "Payment webhook", url: "https://webshop.example.org/webhooks/payments", event_types: [ - "payment.paid", - "payment.failed", - "payment.canceled" + "payment-link.paid" ] ) @@ -36,7 +34,7 @@ def test_get_webhook 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.paid", "payment.failed", "payment.canceled"], webhook.event_types + assert_equal ["payment-link.paid"], webhook.event_types assert_equal Time.parse("2024-01-15T10:30:00+00:00"), webhook.created_at end