Skip to content

Commit

Permalink
Adds support for Financial Actions API
Browse files Browse the repository at this point in the history
  • Loading branch information
martinseco committed Feb 17, 2023
1 parent 47227a0 commit 35d2a67
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/checkout_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
require 'checkout_sdk/balances/balances'
require 'checkout_sdk/transfers/transfers'
require 'checkout_sdk/metadata/metadata'
require 'checkout_sdk/financial/financial'

# Checkout modules (previous)
require 'checkout_sdk/sources/sources'
Expand Down
6 changes: 5 additions & 1 deletion lib/checkout_sdk/checkout_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module CheckoutSdk
# @return [CheckoutSdk::Transfers::TransfersClient]
# @!attribute metadata
# @return [CheckoutSdk::Metadata::MetadataClient]
# @!attribute financial
# @return [CheckoutSdk::Financial::FinancialClient]
class CheckoutApi
attr_reader :customers,
:disputes,
Expand All @@ -52,7 +54,8 @@ class CheckoutApi
:risk,
:balances,
:transfers,
:metadata
:metadata,
:financial

# @param [CheckoutConfiguration] configuration
def initialize(configuration)
Expand All @@ -74,6 +77,7 @@ def initialize(configuration)
@balances = CheckoutSdk::Balances::BalancesClient.new(balances_client(configuration), configuration)
@transfers = CheckoutSdk::Transfers::TransfersClient.new(transfers_client(configuration), configuration)
@metadata = CheckoutSdk::Metadata::MetadataClient.new api_client, configuration
@financial = CheckoutSdk::Financial::FinancialClient.new api_client, configuration
end

private
Expand Down
4 changes: 4 additions & 0 deletions lib/checkout_sdk/financial/financial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require 'checkout_sdk/financial/financial_client'
require 'checkout_sdk/financial/financial_actions_query'
20 changes: 20 additions & 0 deletions lib/checkout_sdk/financial/financial_actions_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module CheckoutSdk
module Financial
# @!attribute payment_id
# @return [String]
# @!attribute action_id
# @return [String]
# @!attribute limit
# @return [Integer]
# @!attribute pagination_token
# @return [String]
class FinancialActionsQuery
attr_accessor :payment_id,
:action_id,
:limit,
:pagination_token
end
end
end
21 changes: 21 additions & 0 deletions lib/checkout_sdk/financial/financial_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module CheckoutSdk
module Financial
class FinancialClient < Client
FINANCIAL_ACTIONS = 'financial-actions'
private_constant :FINANCIAL_ACTIONS

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

# @param [FinancialActionsQuery] query_filter
def query(query_filter)
api_client.invoke_get(FINANCIAL_ACTIONS, sdk_authorization, query_filter)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/checkout_sdk/oauth_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ module OAuthScopes
REPORTS = 'reports'
REPORTS_VIEW = 'reports:view'
CARD_METADATA = 'vault:card-metadata'
FINANCIAL_ACTIONS = 'financial-actions'
FINANCIAL_ACTIONS_VIEW = 'financial-actions:view'
end
end
1 change: 0 additions & 1 deletion spec/checkout_sdk/disputes/disputes_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,5 @@
end

private def there_are_disputes(response)
sleep 10
response.total_count > 0
end
53 changes: 53 additions & 0 deletions spec/checkout_sdk/financial/financial_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

RSpec.describe CheckoutSdk::Financial do
include PaymentsHelper

describe '.query' do
context 'when querying with valid filters' do
it 'returns valid financial actions' do
payment = make_card_payment(amount: 100, capture: true)

query = CheckoutSdk::Financial::FinancialActionsQuery.new
query.limit = 5
query.payment_id = payment.id

proc = -> { oauth_sdk.financial.query(query) }
predicate = ->(response) { there_are_financial_actions response }

response = retriable proc, predicate, 5

assert_response response,
%w[metadata
count
data
_links]

response.data&.each do |action|
assert_response action,
%w[payment_id
action_id
action_type
entity_id
currency_account_id
processed_on
requested_on]
end
end

context 'when querying with invalid filters' do
it 'raises an error' do
query = CheckoutSdk::Financial::FinancialActionsQuery.new
query.limit = 300

expect { oauth_sdk.financial.query(query) }.to raise_error(CheckoutSdk::CheckoutApiException)
end
end
end
end
end

private def there_are_financial_actions(response)
response.count > 0
end

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
proc = -> { default_sdk.payments.get_payments_list(query) }
predicate = ->(response) { there_are_payments response }

response = retriable proc, predicate
response = retriable proc, predicate, 15

assert_response response, %w[limit
skip
Expand All @@ -29,6 +29,5 @@
end

private def there_are_payments(response)
sleep 15
response.total_count > 0
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
proc = -> { default_sdk.payments.get_payments_list(query) }
predicate = ->(response) { there_are_payments response }

response = retriable proc, predicate
response = retriable proc, predicate, 15

assert_response response, %w[limit
skip
Expand All @@ -31,6 +31,5 @@
end

private def there_are_payments(response)
sleep 15
response.total_count > 0
end
8 changes: 5 additions & 3 deletions spec/support/sandbox_test_fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ def oauth_sdk
@oauth_sdk
end

def retriable(callback, predicate = nil)
def retriable(callback, predicate = nil, timeout = 2)
current_attempt = 1
max_attempts = 10
while current_attempt <= max_attempts
begin
response = callback.call
return response if predicate.nil?
return response if predicate.call(response)

sleep timeout
rescue CheckoutSdk::CheckoutApiException => e
sleep 2
sleep timeout
end
current_attempt += 1
end
Expand Down Expand Up @@ -88,7 +90,7 @@ def get_oauth_scopes
CheckoutSdk::OAuthScopes::FLOW, CheckoutSdk::OAuthScopes::FILES,
CheckoutSdk::OAuthScopes::FX, CheckoutSdk::OAuthScopes::BALANCES_VIEW,
CheckoutSdk::OAuthScopes::MARKETPLACE, CheckoutSdk::OAuthScopes::TRANSFERS,
CheckoutSdk::OAuthScopes::CARD_METADATA]
CheckoutSdk::OAuthScopes::CARD_METADATA, CheckoutSdk::OAuthScopes::FINANCIAL_ACTIONS]
end
end
end

0 comments on commit 35d2a67

Please sign in to comment.