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..a614259 --- /dev/null +++ b/test/fixtures/webhooks/create.json @@ -0,0 +1,5 @@ +{ + "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 new file mode 100644 index 0000000..d6631bd --- /dev/null +++ b/test/fixtures/webhooks/get.json @@ -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" + } + } +} 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..94f6f1e --- /dev/null +++ b/test/fixtures/webhooks/update.json @@ -0,0 +1,5 @@ +{ + "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 new file mode 100644 index 0000000..c7d3ba7 --- /dev/null +++ b/test/mollie/webhook_test.rb @@ -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