-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Webhooks and Events (CS1)
Showing
13 changed files
with
324 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters