Skip to content

Commit

Permalink
Added support for Webhooks and Events (CS1)
Browse files Browse the repository at this point in the history
a-ibarra committed Nov 29, 2022

Verified

This commit was signed with the committer’s verified signature.
samuelattwood Samuel Attwood
1 parent dd55b36 commit 9a25a89
Showing 13 changed files with 324 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/checkout_sdk.rb
Original file line number Diff line number Diff line change
@@ -66,6 +66,8 @@
# Checkout modules (previous)
require 'checkout_sdk/sources/sources'
require 'checkout_sdk/reconciliation/reconciliation'
require 'checkout_sdk/webhooks/webhooks'
require 'checkout_sdk/events/events'

module CheckoutSdk
include PlatformType
8 changes: 1 addition & 7 deletions lib/checkout_sdk/disputes/disputes_query_filter.rb
Original file line number Diff line number Diff line change
@@ -6,10 +6,6 @@ module Disputes
# @return [Integer]
# @!attribute skip
# @return [Integer]
# @!attribute from
# @return [DateTime]
# @!attribute to
# @return [DateTime]
# @!attribute id
# @return [String]
# @!attribute statuses
@@ -28,11 +24,9 @@ module Disputes
# @return [String] - Not available on Previous.
# @!attribute payment_mcc
# @return [String] - Not available on Previous.
class DisputesQueryFilter
class DisputesQueryFilter < CheckoutSdk::Common::DateRangeQueryFilter
attr_accessor :limit,
:skip,
:from,
:to,
:id,
:statuses,
:payment_id,
4 changes: 4 additions & 0 deletions lib/checkout_sdk/events/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'checkout_sdk/events/events_query_filter'
require 'checkout_sdk/events/events_client'
56 changes: 56 additions & 0 deletions lib/checkout_sdk/events/events_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module CheckoutSdk
module Previous
module Events
class EventsClient < Client
EVENT_TYPES = 'event-types'
EVENTS = 'events'
NOTIFICATIONS = 'notifications'
WEBHOOKS = 'webhooks'
EVENT_ID = 'eventId'
RETRY = 'retry'
private_constant :EVENT_TYPES, :EVENTS, :NOTIFICATIONS, :WEBHOOKS, :EVENT_ID, :RETRY

# @param [ApiClient] api_client
# @param [CheckoutConfiguration] configuration
def initialize(api_client, configuration)
super(api_client, configuration, CheckoutSdk::AuthorizationType::SECRET_KEY)
end

# @param [String,nil] version
def retrieve_all_event_types(version = nil)
query = "version=#{version}" unless version.nil?
api_client.invoke_get(EVENT_TYPES, sdk_authorization, query)
end

# @param [EventsQueryFilter] query_filter
def retrieve_events(query_filter)
api_client.invoke_get(EVENTS, sdk_authorization, query_filter)
end

# @param [String] event_id
def retrieve_event(event_id)
api_client.invoke_get(build_path(EVENTS, event_id), sdk_authorization)
end

# @param [String] event_id
# @param [String] notification_id
def retrieve_event_notification(event_id, notification_id)
api_client.invoke_get(build_path(EVENTS, event_id, NOTIFICATIONS, notification_id), sdk_authorization)
end

# @param [String] event_id
# @param [String] webhook_id
def retry_webhook(event_id, webhook_id)
api_client.invoke_post(build_path(EVENTS, event_id, WEBHOOKS, webhook_id), sdk_authorization)
end

# @param [String] event_id
def retry_all_webhooks(event_id)
api_client.invoke_post(build_path(EVENTS, event_id, WEBHOOKS, RETRY), sdk_authorization)
end
end
end
end
end
28 changes: 28 additions & 0 deletions lib/checkout_sdk/events/events_query_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module CheckoutSdk
module Previous
module Events
# @!attribute payment_id
# @return [String]
# @!attribute charge_id
# @return [String]
# @!attribute track_id
# @return [String]
# @!attribute reference
# @return [String]
# @!attribute skip
# @return [Integer]
# @!attribute limit
# @return [Integer]
class EventsQueryFilter < CheckoutSdk::Common::DateRangeQueryFilter
attr_accessor :payment_id,
:charge_id,
:track_id,
:reference,
:skip,
:limit
end
end
end
end
12 changes: 10 additions & 2 deletions lib/checkout_sdk/previous/checkout_api.rb
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ module Previous
# @return [CheckoutSdk::Customers::CustomerClient]
# @!attribute disputes
# @return [CheckoutSdk::Disputes::DisputesClient]
# @!attribute payments
# @!attribute hosted
# @return [CheckoutSdk::Payments::HostedPaymentsClient]
# @!attribute instruments
# @return [CheckoutSdk::Previous::Instruments::InstrumentsClient]
@@ -26,6 +26,10 @@ module Previous
# @return [CheckoutSdk::Risk::RiskClient]
# @!attribute reconciliation
# @return [CheckoutSdk::Previous::Reconciliation::ReconciliationClient]
# @!attribute webhooks
# @return [CheckoutSdk::Previous::Webhooks::WebhooksClient]
# @!attribute events
# @return [CheckoutSdk::Previous::Events::EventsClient]
class CheckoutApi
attr_reader :customers,
:disputes,
@@ -38,7 +42,9 @@ class CheckoutApi
:klarna,
:sepa,
:risk,
:reconciliation
:reconciliation,
:webhooks,
:events

# @param [CheckoutConfiguration] configuration
def initialize(configuration)
@@ -55,6 +61,8 @@ def initialize(configuration)
@sepa = CheckoutSdk::Previous::Apm::SepaClient.new api_client, configuration
@risk = CheckoutSdk::Risk::RiskClient.new api_client, configuration
@reconciliation = CheckoutSdk::Previous::Reconciliation::ReconciliationClient.new api_client, configuration
@webhooks = CheckoutSdk::Previous::Webhooks::WebhooksClient.new api_client, configuration
@events = CheckoutSdk::Previous::Events::EventsClient.new api_client, configuration
end

private
25 changes: 25 additions & 0 deletions lib/checkout_sdk/webhooks/webhook_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module CheckoutSdk
module Previous
module Webhooks
# @!attribute url
# @return [String]
# @!attribute active
# @return [TrueClass, FalseClass]
# @!attribute headers
# @return [Hash(String=>String)]
# @!attribute content_type
# @return [String]
# @!attribute event_types
# @return [Array(String)]
class WebhookRequest
attr_accessor :url,
:active,
:headers,
:content_type,
:event_types
end
end
end
end
4 changes: 4 additions & 0 deletions lib/checkout_sdk/webhooks/webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'checkout_sdk/webhooks/webhook_request'
require 'checkout_sdk/webhooks/webhooks_client'
50 changes: 50 additions & 0 deletions lib/checkout_sdk/webhooks/webhooks_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module CheckoutSdk
module Previous
module Webhooks
class WebhooksClient < Client
WEBHOOKS = 'webhooks'
private_constant :WEBHOOKS

# @param [ApiClient] api_client
# @param [CheckoutConfiguration] configuration
def initialize(api_client, configuration)
super(api_client, configuration, CheckoutSdk::AuthorizationType::SECRET_KEY)
end

def retrieve_webhooks
api_client.invoke_get(WEBHOOKS, sdk_authorization)
end

# @param [WebhookRequest] webhook_request
# @param [String,nil] idempotency_key
def register_webhook(webhook_request, idempotency_key = nil)
api_client.invoke_post(WEBHOOKS, sdk_authorization, webhook_request, idempotency_key)
end

# @param [String] webhook_id
def retrieve_webhook(webhook_id)
api_client.invoke_get(build_path(WEBHOOKS, webhook_id), sdk_authorization)
end

# @param [String] webhook_id
# @param [WebhookRequest] webhook_request
def update_webhook(webhook_id, webhook_request)
api_client.invoke_put(build_path(WEBHOOKS, webhook_id), sdk_authorization, webhook_request)
end

# @param [String] webhook_id
# @param [WebhookRequest] webhook_request
def patch_webhook(webhook_id, webhook_request)
api_client.invoke_patch(build_path(WEBHOOKS, webhook_id), sdk_authorization, webhook_request)
end

# @param [String] webhook_id
def remove_webhook(webhook_id)
api_client.invoke_delete(build_path(WEBHOOKS, webhook_id), sdk_authorization)
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/checkout_sdk/events/events_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
RSpec.describe CheckoutSdk::Previous::Webhooks do

describe '.retrieve_all_event_types' do
context 'when fetching event types' do
it 'should return even types for both versions' do
response = previous_sdk.events.retrieve_all_event_types
expect(response).not_to be_nil
expect(response.items).not_to be_nil
expect(response.items[0].version).to eq '1.0'
expect(response.items[1].version).to eq '2.0'
end

it 'should return even types for version one' do
response = previous_sdk.events.retrieve_all_event_types '1.0'
expect(response).not_to be_nil
expect(response.items).not_to be_nil
expect(response.items.length).to be 1
expect(response.items[0].version).to eq '1.0'
end
end
end

end
36 changes: 36 additions & 0 deletions spec/checkout_sdk/webhooks/webhooks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module WebhooksHelper

GATEWAY_EVENT_TYPES = %w[payment_approved
payment_canceled
payment_capture_declined
payment_capture_pending
payment_captured
payment_chargeback
payment_declined
payment_expired
payment_paid
payment_pending
payment_refund_declined
payment_refund_pending
payment_refunded
payment_retrieval
payment_void_declined
payment_voided].freeze

def register_webhook(url)
request = CheckoutSdk::Previous::Webhooks::WebhookRequest.new
request.url = url
request.event_types = GATEWAY_EVENT_TYPES

previous_sdk.webhooks.register_webhook request
end

def delete_webhook(webhook_id)

previous_sdk.webhooks.remove_webhook webhook_id
rescue CheckoutSdk::CheckoutApiException => e
# Ignore might be already deleted
end
end
84 changes: 84 additions & 0 deletions spec/checkout_sdk/webhooks/webhooks_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
RSpec.describe CheckoutSdk::Previous::Webhooks do
include WebhooksHelper

before(:all) do
@webhook = register_webhook 'https://checkout.ruby.com/webhooks'
end

after(:all) do
delete_webhook @webhook.id
end

describe '.retrieve_webhooks' do
context 'when fetching all webhooks' do
it 'should successfully return all registered webhooks' do
response = previous_sdk.webhooks.retrieve_webhooks
expect(response).not_to be_nil
end
end
end

describe '.register_webhook' do
context 'when register a webhook' do
it 'should successfully registered' do
assert_response @webhook, %w[id
url
active
headers
content_type
event_types]
end
end
end

describe '.retrieve_webhook' do
context 'when register a webhook' do
it 'should retrieve registered webhook' do
response = previous_sdk.webhooks.retrieve_webhook @webhook.id
assert_response response, %w[id
url
active
headers
content_type
event_types]
expect(response.url).to eq @webhook.url
end
end
end

describe '.update_webhook' do
context 'when request update a webhook' do
it 'should successfully update a webhook' do

request = CheckoutSdk::Previous::Webhooks::WebhookRequest.new
request.url = 'https://checkout.ruby.com/failed'
request.headers = @webhook.headers.to_h
request.event_types = ['source_updated']
request.active = true
request.content_type = 'json'

proc = -> { previous_sdk.webhooks.update_webhook @webhook.id, request }

response = retriable(proc)
assert_response response, %w[id
url
active
headers
content_type
event_types]
expect(response.url).to eq request.url
end
end
end

describe '.remove_webhook' do
context 'when deleting a webhook' do
it 'should raise error when retrieve deleted webhook' do
delete_webhook @webhook.id
expect { previous_sdk.webhooks.retrieve_webhook @webhook.id }
.to raise_error(CheckoutSdk::CheckoutApiException) { |e| expect(e.http_metadata.status_code).to eq 404 }
end
end
end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
require './spec/checkout_sdk/sessions/sessions_helper'
require './spec/checkout_sdk/payments/previous/payments_helper'
require './spec/checkout_sdk/workflows/workflows_helper'
require './spec/checkout_sdk/webhooks/webhooks_helper'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure

0 comments on commit 9a25a89

Please sign in to comment.