Skip to content

Commit

Permalink
Merge pull request #92 from gocardless/template-changes
Browse files Browse the repository at this point in the history
Template changes
  • Loading branch information
vdandugc authored Aug 12, 2021
2 parents ef60e5a + 06ade33 commit 7c6988c
Show file tree
Hide file tree
Showing 54 changed files with 2,113 additions and 379 deletions.
21 changes: 20 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ jobs:
steps:
- checkout
- run: bundle install && bundle exec rspec

publish: &publish
docker:
- image: ruby:2.6
steps:
- checkout
- run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems_api_key: $RUBYGEM_PUBLISH_API_KEY\n" > $HOME/.gem/credentials
gem build *.gemspec
gem push *.gem
workflows:
version: 2
tests:
Expand All @@ -30,3 +42,10 @@ workflows:
ruby-version: "2.2"
- faraday-version: "0.9.2"
ruby-version: "3.0"
- publish:
filters:
branches:
only:
- master
requires:
- test
6 changes: 6 additions & 0 deletions lib/gocardless_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module GoCardlessPro
require_relative 'gocardless_pro/error/invalid_api_usage_error'
require_relative 'gocardless_pro/error/invalid_state_error'
require_relative 'gocardless_pro/error/api_error'
require_relative 'gocardless_pro/error/permission_error'
require_relative 'gocardless_pro/error/authentication_error'
require_relative 'gocardless_pro/error/rate_limit_error'
require_relative 'gocardless_pro/paginator'
require_relative 'gocardless_pro/request'
require_relative 'gocardless_pro/response'
Expand All @@ -50,6 +53,9 @@ module GoCardlessPro
require_relative 'gocardless_pro/resources/billing_request_flow'
require_relative 'gocardless_pro/services/billing_request_flows_service'

require_relative 'gocardless_pro/resources/billing_request_template'
require_relative 'gocardless_pro/services/billing_request_templates_service'

require_relative 'gocardless_pro/resources/creditor'
require_relative 'gocardless_pro/services/creditors_service'

Expand Down
4 changes: 4 additions & 0 deletions lib/gocardless_pro/api_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def initialize(url, token, options = {})
@headers = options[:default_headers] || {}
@headers['Authorization'] = "Bearer #{token}"
@on_idempotency_conflict = options[:on_idempotency_conflict] || :fetch

unless %i[fetch raise].include?(@on_idempotency_conflict)
raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
end
end

# Make a request to the API
Expand Down
7 changes: 6 additions & 1 deletion lib/gocardless_pro/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def billing_request_flows
@billing_request_flows ||= Services::BillingRequestFlowsService.new(@api_service)
end

# Access to the service for billing_request_template to make API calls
def billing_request_templates
@billing_request_templates ||= Services::BillingRequestTemplatesService.new(@api_service)
end

# Access to the service for creditor to make API calls
def creditors
@creditors ||= Services::CreditorsService.new(@api_service)
Expand Down Expand Up @@ -188,7 +193,7 @@ def default_options
'User-Agent' => user_agent.to_s,
'Content-Type' => 'application/json',
'GoCardless-Client-Library' => 'gocardless-pro-ruby',
'GoCardless-Client-Version' => '2.27.0',
'GoCardless-Client-Version' => '2.28.0',
},
}
end
Expand Down
4 changes: 4 additions & 0 deletions lib/gocardless_pro/error/authentication_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GoCardlessPro
class AuthenticationError < InvalidApiUsageError
end
end
4 changes: 4 additions & 0 deletions lib/gocardless_pro/error/permission_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GoCardlessPro
class PermissionError < InvalidApiUsageError
end
end
4 changes: 4 additions & 0 deletions lib/gocardless_pro/error/rate_limit_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GoCardlessPro
class RateLimitError < InvalidApiUsageError
end
end
13 changes: 12 additions & 1 deletion lib/gocardless_pro/middlewares/raise_gocardless_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ def on_complete(env)
if CLIENT_ERROR_STATUSES.include?(env.status)
json_body ||= JSON.parse(env.body) unless env.body.empty?
error_type = json_body['error']['type']
raise(error_class_for_type(error_type), json_body['error'])

error_class = error_class_for_status(env.status) || error_class_for_type(error_type)

raise(error_class, json_body['error'])
end
end

private

def error_class_for_status(code)
{
401 => GoCardlessPro::AuthenticationError,
403 => GoCardlessPro::PermissionError,
429 => GoCardlessPro::RateLimitError,
}.fetch(code, nil)
end

def error_class_for_type(type)
{
validation_failed: GoCardlessPro::ValidationError,
Expand Down
8 changes: 3 additions & 5 deletions lib/gocardless_pro/resources/bank_authorisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ module Resources
# Creation of Bank Authorisations is only permitted from GoCardless hosted
# UIs
# (see Billing Request Flows) to ensure we meet regulatory requirements for
# checkout flows. The exceptions are integrators with the custom payment
# pages
# upgrade, who have been audited to check their flows meet requirements.
# checkout flows.
class BankAuthorisation
attr_reader :authorisation_type
attr_reader :authorised_at
attr_reader :created_at
attr_reader :expires_at
attr_reader :id
attr_reader :last_visited_at
attr_reader :redirect_uri
attr_reader :short_url
attr_reader :url

# Initialize a bank_authorisation resource instance
Expand All @@ -40,13 +38,13 @@ def initialize(object, response = nil)
@object = object

@authorisation_type = object['authorisation_type']
@authorised_at = object['authorised_at']
@created_at = object['created_at']
@expires_at = object['expires_at']
@id = object['id']
@last_visited_at = object['last_visited_at']
@links = object['links']
@redirect_uri = object['redirect_uri']
@short_url = object['short_url']
@url = object['url']
@response = response
end
Expand Down
36 changes: 29 additions & 7 deletions lib/gocardless_pro/resources/billing_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ module GoCardlessPro
module Resources
# Represents an instance of a billing_request resource returned from the API

# Billing Requests
# Billing Requests help create resources that require input or action from a
# customer. An example of required input might be additional customer
# billing
# details, while an action would be asking a customer to authorise a payment
# using their mobile banking app.
#
# See [Billing Requests:
# Overview](https://developer.gocardless.com/getting-started/billing-requests/overview/)
# for how-to's, explanations and tutorials.
class BillingRequest
attr_reader :actions
attr_reader :auto_fulfil
attr_reader :created_at
attr_reader :id
attr_reader :mandate_request
Expand All @@ -30,7 +37,6 @@ def initialize(object, response = nil)
@object = object

@actions = object['actions']
@auto_fulfil = object['auto_fulfil']
@created_at = object['created_at']
@id = object['id']
@links = object['links']
Expand Down Expand Up @@ -61,6 +67,14 @@ def initialize(links)
@links = links || {}
end

def bank_authorisation
@links['bank_authorisation']
end

def creditor
@links['creditor']
end

def customer
@links['customer']
end
Expand All @@ -73,12 +87,20 @@ def customer_billing_detail
@links['customer_billing_detail']
end

def mandate_bank_authorisation
@links['mandate_bank_authorisation']
def mandate_request
@links['mandate_request']
end

def mandate_request_mandate
@links['mandate_request_mandate']
end

def payment_request
@links['payment_request']
end

def payment_bank_authorisation
@links['payment_bank_authorisation']
def payment_request_payment
@links['payment_request_payment']
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/gocardless_pro/resources/billing_request_flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@ module Resources
# authorisation (such as open banking single payments).
class BillingRequestFlow
attr_reader :authorisation_url
attr_reader :auto_fulfil
attr_reader :created_at
attr_reader :expires_at
attr_reader :id
attr_reader :lock_bank_account
attr_reader :lock_customer_details
attr_reader :redirect_uri
attr_reader :session_token

# Initialize a billing_request_flow resource instance
# @param object [Hash] an object returned from the API
def initialize(object, response = nil)
@object = object

@authorisation_url = object['authorisation_url']
@auto_fulfil = object['auto_fulfil']
@created_at = object['created_at']
@expires_at = object['expires_at']
@id = object['id']
@links = object['links']
@lock_bank_account = object['lock_bank_account']
@lock_customer_details = object['lock_customer_details']
@redirect_uri = object['redirect_uri']
@session_token = object['session_token']
@response = response
end

Expand Down
68 changes: 68 additions & 0 deletions lib/gocardless_pro/resources/billing_request_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# encoding: utf-8

#
# This client is automatically generated from a template and JSON schema definition.
# See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing.
#

require 'uri'

module GoCardlessPro
# A module containing classes for each of the resources in the GC Api
module Resources
# Represents an instance of a billing_request_template resource returned from the API

# Billing Request Templates
class BillingRequestTemplate
attr_reader :authorisation_url
attr_reader :created_at
attr_reader :id
attr_reader :mandate_request_currency
attr_reader :mandate_request_metadata
attr_reader :mandate_request_scheme
attr_reader :mandate_request_verify
attr_reader :metadata
attr_reader :name
attr_reader :payment_request_amount
attr_reader :payment_request_currency
attr_reader :payment_request_description
attr_reader :payment_request_metadata
attr_reader :payment_request_scheme
attr_reader :redirect_uri
attr_reader :updated_at

# Initialize a billing_request_template resource instance
# @param object [Hash] an object returned from the API
def initialize(object, response = nil)
@object = object

@authorisation_url = object['authorisation_url']
@created_at = object['created_at']
@id = object['id']
@mandate_request_currency = object['mandate_request_currency']
@mandate_request_metadata = object['mandate_request_metadata']
@mandate_request_scheme = object['mandate_request_scheme']
@mandate_request_verify = object['mandate_request_verify']
@metadata = object['metadata']
@name = object['name']
@payment_request_amount = object['payment_request_amount']
@payment_request_currency = object['payment_request_currency']
@payment_request_description = object['payment_request_description']
@payment_request_metadata = object['payment_request_metadata']
@payment_request_scheme = object['payment_request_scheme']
@redirect_uri = object['redirect_uri']
@updated_at = object['updated_at']
@response = response
end

def api_response
ApiResponse.new(@response)
end

# Provides the billing_request_template resource as a hash of all its readable attributes
def to_h
@object
end
end
end
end
9 changes: 9 additions & 0 deletions lib/gocardless_pro/resources/payer_authorisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ module GoCardlessPro
module Resources
# Represents an instance of a payer_authorisation resource returned from the API

# <p class="restricted-notice">
# Payer Authorisations is deprecated in favour of
# <a
# href="https://developer.gocardless.com/getting-started/billing-requests/overview/">
# Billing Requests</a>. Please consider using Billing Requests to build
# any
# future integrations.
# </p>
#
# Payer Authorisation resource acts as a wrapper for creating customer, bank
# account and mandate details in a single request.
# PayerAuthorisation API enables the integrators to build their own custom
Expand Down
2 changes: 0 additions & 2 deletions lib/gocardless_pro/services/bank_authorisations_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ def create(options = {})
raise IdempotencyConflict, e.error
when :fetch
return get(e.conflicting_resource_id)
else
raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
end
end

Expand Down
23 changes: 23 additions & 0 deletions lib/gocardless_pro/services/billing_request_flows_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ def create(options = {})
Resources::BillingRequestFlow.new(unenvelope_body(response.body), response)
end

# Returns the flow having generated a fresh session token which can be used to
# power
# integrations that manipulate the flow.
# Example URL: /billing_request_flows/:identity/actions/initialise
#
# @param identity # Unique identifier, beginning with "BRQ".
# @param options [Hash] parameters as a hash, under a params key.
def initialise(identity, options = {})
path = sub_url('/billing_request_flows/:identity/actions/initialise', 'identity' => identity)

params = options.delete(:params) || {}
options[:params] = {}
options[:params]['data'] = params

options[:retry_failures] = false

response = make_request(:post, path, options)

return if response.body.nil?

Resources::BillingRequestFlow.new(unenvelope_body(response.body), response)
end

private

# Unenvelope the response of the body using the service's `envelope_key`
Expand Down
Loading

0 comments on commit 7c6988c

Please sign in to comment.