Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Replace `Payrix.test_mode = false` with `Payrix.environment = :production`.
- Change default request environment to `:sandbox`.
- Set `Payrix.environment = :production` to keep existing behaviour.
- Require specifying the destination API through `Payrix.region=` configuration or a per-request `:region` option.

### Removals

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require 'bundler/setup'
require 'payrix'

Payrix.api_key = '928b...'
Payrix.region = :us

# Retrieve a single transaction
Payrix::Txn.retrieve('t1_txn_64026b07cc6a79dd5cfd0da')
Expand Down Expand Up @@ -243,10 +244,11 @@ merchant.members # => [...}
Set the following configuration parameters per request.

- `:api_key` - Set the API key used.
- `:region` - Set the API used (see valid values in Configuration below).
- `:environment` - Set the environment used (see valid values in Configuration below).

```ruby
Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...', environment: :production })
Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...', region: :ca, environment: :production })
```

This is useful if you need to configure the request in a thread-safe way. This is not possible using the static configuration mentioned in Configuration below. The per-request configuration always takes precedent.
Expand Down Expand Up @@ -276,6 +278,7 @@ end
There are a few configuration parameters.

- `Payrix.api_key=` - Use this to set the API key.
- `Payrix.region=` - Use this to set the appropriate API. Set to `:us` or `:ca`.
- `Payrix.environment=` - Use this to set the request environment. Set to `:sandbox` or `:production`.

## Development
Expand Down
20 changes: 20 additions & 0 deletions lib/payrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ module Payrix
production: :production,
}.freeze

REGIONS = {
us: :us,
ca: :ca,
}.freeze

ENDPOINTS = {
REGIONS.fetch(:us) => {
ENVIRONMENTS.fetch(:sandbox) => 'https://test-api.payrix.com',
ENVIRONMENTS.fetch(:production) => 'https://api.payrix.com',
},
REGIONS.fetch(:ca) => {
ENVIRONMENTS.fetch(:sandbox) => 'https://test-api.payrixcanada.com',
ENVIRONMENTS.fetch(:production) => 'https://api.payrixcanada.com',
},
Comment on lines +37 to +40
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support the Payrix Canada API by routing to these endpoints, when configured with :ca above.

}.freeze

class << self
attr_writer :configuration
end
Expand All @@ -36,6 +52,10 @@ def self.api_key=(api_key)
configuration.api_key = api_key
end

def self.region=(region)
configuration.region = region
end

def self.environment=(environment)
configuration.environment = environment
end
Expand Down
2 changes: 1 addition & 1 deletion lib/payrix/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Payrix
# The Payrix::Client is used both internally and externally to initiate API requests to the Payrix API.
class Client
def request(method:, resource:, data: {}, filters: {}, options: {})
url = Payrix.configuration.url(options[:environment])
url = Payrix.configuration.url(options[:region], options[:environment])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are supporting a new :region option for all requests.


page_number = Payrix::RequestOptions::Page::Number.construct(options[:page])
page_limit = Payrix::RequestOptions::Page::Limit.construct(options[:limit])
Expand Down
33 changes: 19 additions & 14 deletions lib/payrix/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ module Payrix
# Use this class to configure API parameters such as API URL, API key, etc.
class Configuration
attr_accessor :api_key, :session_key
attr_reader :environment
attr_reader :region, :environment

def initialize
@api_key = ''
@session_key = ''
@region = nil
@environment = Payrix::ENVIRONMENTS.fetch(:sandbox)
end

def region=(region)
validate_region!(region)

@region = region.to_sym
end
Comment on lines +16 to +20
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for setting the region configuration globally.


def environment=(environment)
validate_environment!(environment)

@environment = environment.to_sym
end

def url(environment_override = nil)
environment = @environment

unless environment_override.nil?
validate_environment!(environment_override)
def url(region_override = nil, environment_override = nil)
region = region_override || @region
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This supports a region override, which always take precedent over the global configuration.

environment = environment_override || @environment

environment = environment_override.to_sym
end
validate_region!(region)
validate_environment!(environment)

case environment
when Payrix::ENVIRONMENTS.fetch(:sandbox)
'https://test-api.payrix.com'
when Payrix::ENVIRONMENTS.fetch(:production)
'https://api.payrix.com'
end
Payrix::ENDPOINTS[region.to_sym][environment.to_sym]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set of API endpoints has been extracted to a "look up table", where additional regions can be added easily in the future.

end

private
Expand All @@ -41,5 +41,10 @@ def validate_environment!(environment)
raise InvalidEnvironmentError unless environment.respond_to?(:to_sym)
raise InvalidEnvironmentError unless Payrix::ENVIRONMENTS.values.include?(environment.to_sym)
end

def validate_region!(region)
raise InvalidRegionError unless region.respond_to?(:to_sym)
raise InvalidRegionError unless Payrix::REGIONS.values.include?(region.to_sym)
end
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library aims to be as developer friendly as possible. This means surfacing developer errors as soon as possible. We ensure the region setting is within the supported list, or raise an error before we even send the request to the Payrix API.

This way, a developer doesn't need to wait long to get this feedback.

end
end
4 changes: 4 additions & 0 deletions lib/payrix/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class NotFoundError < Error
class InvalidEnvironmentError < Error
end

# A pre-flight error indicating that the region set is not valid.
class InvalidRegionError < Error
end

# An error returned when the API returns an invalid authentication error.
class InvalidAuthenticationError < Error
end
Expand Down
12 changes: 8 additions & 4 deletions spec/lib/payrix/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
WebMock
.stub_request(
:get,
'https://api.payrix.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=',
'https://api.payrixcanada.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=',
)
.with(
headers: {
Expand Down Expand Up @@ -68,6 +68,7 @@
limit: 10,
expand: ['merchant'],
api_key: 'custom-key',
region: :ca,
environment: :production,
},
)
Expand Down Expand Up @@ -111,7 +112,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:post, 'https://api.payrix.com/txns?expand[merchant][]=')
.stub_request(:post, 'https://api.payrixcanada.com/txns?expand[merchant][]=')
.with(
headers: {
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -143,6 +144,7 @@
options: {
expand: ['merchant'],
api_key: 'custom-key',
region: :ca,
environment: :production,
},
)
Expand Down Expand Up @@ -194,7 +196,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:put, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.stub_request(:put, 'https://api.payrixcanada.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.with(
headers: {
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -225,6 +227,7 @@
},
options: {
api_key: 'custom-key',
region: :ca,
environment: :production,
},
)
Expand Down Expand Up @@ -264,7 +267,7 @@
it 'supports advanced requests' do
stub =
WebMock
.stub_request(:delete, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.stub_request(:delete, 'https://api.payrixcanada.com/txns/t1_txn_64026b07cc6a79dd5cfd0da')
.with(
headers: {
'Content-Type' => 'application/json',
Expand All @@ -289,6 +292,7 @@
resource: 'txns/t1_txn_64026b07cc6a79dd5cfd0da',
options: {
api_key: 'custom-key',
region: :ca,
environment: :production,
},
)
Expand Down
Loading