Skip to content

Support Payrix Canada API through region configuration#13

Merged
harrylewis merged 6 commits intomainfrom
region-support
Sep 22, 2025
Merged

Support Payrix Canada API through region configuration#13
harrylewis merged 6 commits intomainfrom
region-support

Conversation

@harrylewis
Copy link
Copy Markdown
Contributor

@harrylewis harrylewis commented Sep 22, 2025

Description

This PR introduces support for the Payrix Canada API. This API is served through a different set of endpoints.

These differ from the USA API endpoints. Therefore, a new required region configuration has been introduced in order to specify which API to use. This configuration can be set globally.

Payrix.region = :ca

It can also be configured on a per-request basis.

Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { region: :ca })

Comment thread lib/payrix/client.rb
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.

Comment on lines +16 to +20
def region=(region)
validate_region!(region)

@region = 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 is used for setting the region configuration globally.

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.

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.

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.

Comment thread lib/payrix.rb
Comment on lines +37 to +40
REGIONS.fetch(:ca) => {
ENVIRONMENTS.fetch(:sandbox) => 'https://test-api.payrixcanada.com',
ENVIRONMENTS.fetch(:production) => 'https://api.payrixcanada.com',
},
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.

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.

I didn't skimp on the tests. We have many permutations and combinations below.

Comment thread spec/spec_helper.rb
require 'webmock/rspec'
require 'pry'

Payrix.region = :us
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.

Since the region is now required, this means our tests required this configuration as well.

@harrylewis harrylewis marked this pull request as ready for review September 22, 2025 20:26
@harrylewis harrylewis merged commit 880dddd into main Sep 22, 2025
5 checks passed
@harrylewis harrylewis deleted the region-support branch September 22, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant