-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merchant-specific DNS names Support (#145)
- Loading branch information
1 parent
f790bf7
commit a7bfe71
Showing
10 changed files
with
250 additions
and
34 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
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
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module CheckoutSdk | ||
# @!attribute environment | ||
# @return [Environment] | ||
# @!attribute subdomain | ||
# @return [String] | ||
class EnvironmentSubdomain | ||
attr_reader :base_uri, :environment, :subdomain | ||
|
||
# Initializes the EnvironmentSubdomain with the given environment and subdomain. | ||
# | ||
# @param environment [Environment] The environment object which should have a base_uri method. | ||
# @param subdomain [String] The subdomain to add to the environment's API URL. | ||
def initialize(environment, subdomain) | ||
@environment = environment | ||
@subdomain = subdomain | ||
@base_uri = add_subdomain_to_api_url_environment(environment, subdomain) | ||
end | ||
|
||
private | ||
|
||
# Adds the subdomain to the API URL of the given environment. | ||
# | ||
# @param environment [Environment] The environment object which should have a base_uri method. | ||
# @param subdomain [String] The subdomain to add to the environment's API URL. | ||
# @return [String] The new environment URL with the subdomain added. | ||
def add_subdomain_to_api_url_environment(environment, subdomain) | ||
api_url = environment.base_uri | ||
new_environment = api_url | ||
|
||
# Regex to match a subdomain consisting of 8 to 11 lowercase alphanumeric characters | ||
if subdomain =~ /^[0-9a-z]{8,11}$/ | ||
url_parts = URI.parse(api_url) | ||
new_host = "#{subdomain}.#{url_parts.host}" | ||
|
||
port = url_parts.scheme == 'https' && url_parts.port == 443 ? nil : url_parts.port | ||
|
||
new_url_parts = URI::Generic.build( | ||
scheme: url_parts.scheme, | ||
userinfo: url_parts.userinfo, | ||
host: new_host, | ||
port: port, | ||
path: url_parts.path, | ||
query: url_parts.query, | ||
fragment: url_parts.fragment | ||
) | ||
|
||
new_environment = new_url_parts.to_s | ||
end | ||
|
||
new_environment | ||
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,82 @@ | ||
class FakeHttpClient | ||
end | ||
|
||
class FakeLogger | ||
end | ||
|
||
RSpec.describe CheckoutSdk::StaticKeysSdkCredentials do | ||
before do | ||
@secret_key = ENV['CHECKOUT_DEFAULT_SECRET_KEY'] | ||
@public_key = ENV['CHECKOUT_DEFAULT_PUBLIC_KEY'] | ||
@credentials = CheckoutSdk::StaticKeysSdkCredentials.new(@secret_key, @public_key) | ||
@http_client = FakeHttpClient.new | ||
@multipart_http_client = FakeHttpClient.new | ||
@logger = FakeLogger.new | ||
end | ||
|
||
it 'should create configuration' do | ||
configuration = CheckoutSdk::CheckoutConfiguration.new( | ||
@credentials, | ||
CheckoutSdk::Environment.sandbox, | ||
@http_client, | ||
@multipart_http_client, | ||
@logger | ||
) | ||
|
||
expect(configuration.credentials).to eq(@credentials) | ||
expect(configuration.environment.base_uri).to eq(CheckoutSdk::Environment.sandbox.base_uri) | ||
expect(configuration.environment.base_uri).to eq("https://api.sandbox.checkout.com/") | ||
expect(configuration.http_client).to eq(@http_client) | ||
expect(configuration.environment_subdomain).to be_nil | ||
end | ||
|
||
[ | ||
%w[123dmain https://123dmain.api.sandbox.checkout.com/], | ||
%w[123domain https://123domain.api.sandbox.checkout.com/], | ||
%w[1234domain https://1234domain.api.sandbox.checkout.com/], | ||
%w[12345domain https://12345domain.api.sandbox.checkout.com/] | ||
].each do |subdomain, expected_url| | ||
it "should create configuration with subdomain #{subdomain}" do | ||
environment_subdomain = CheckoutSdk::EnvironmentSubdomain.new(CheckoutSdk::Environment.sandbox, subdomain) | ||
|
||
configuration = CheckoutSdk::CheckoutConfiguration.new( | ||
@credentials, | ||
CheckoutSdk::Environment.sandbox, | ||
@http_client, | ||
@multipart_http_client, | ||
@logger, | ||
environment_subdomain | ||
) | ||
|
||
expect(configuration.credentials).to eq(@credentials) | ||
expect(configuration.environment.base_uri).to eq(CheckoutSdk::Environment.sandbox.base_uri) | ||
expect(configuration.http_client).to eq(@http_client) | ||
expect(configuration.environment_subdomain.base_uri).to eq(expected_url) | ||
end | ||
end | ||
|
||
[ | ||
['', 'https://api.sandbox.checkout.com/'], | ||
%w[123 https://api.sandbox.checkout.com/], | ||
%w[123bad https://api.sandbox.checkout.com/], | ||
%w[12345domainBad https://api.sandbox.checkout.com/] | ||
].each do |subdomain, expected_url| | ||
it 'should create configuration with bad subdomain #{subdomain}' do | ||
environment_subdomain = CheckoutSdk::EnvironmentSubdomain.new(CheckoutSdk::Environment.sandbox, subdomain) | ||
|
||
configuration = CheckoutSdk::CheckoutConfiguration.new( | ||
@credentials, | ||
CheckoutSdk::Environment.sandbox, | ||
@http_client, | ||
@multipart_http_client, | ||
@logger, | ||
environment_subdomain | ||
) | ||
|
||
expect(configuration.credentials).to eq(@credentials) | ||
expect(configuration.environment.base_uri).to eq(CheckoutSdk::Environment.sandbox.base_uri) | ||
expect(configuration.http_client).to eq(@http_client) | ||
expect(configuration.environment_subdomain.base_uri).to eq(expected_url) | ||
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