Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Vesta URL config explicit and add optional logger config #4

Merged
merged 6 commits into from
Jan 19, 2017
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ client = VSafe::Client.new do |config|
end
```

### Vesta URLs

All four Vesta URLs can be specified in config:

```ruby
VSafe.configure do |config|
config.sandbox_url = 'https://my.sandbox.url/GatewayV4Proxy/Service'
config.sandbox_jsonp_url = 'https://my.sandboxtoken.url/GatewayV4ProxyJSON/Service'
config.production_url = 'https://my.production.url/GatewayV4Proxy/Service'
config.production_jsonp_url = 'https://my.production.url/GatewayV4ProxyJSON/Service'
end
```

### Logging

The logger passed to HTTParty can be specified in config:

```ruby
VSafe.configure do |config|
config.logger = Rails.logger
end
```

## Request & Response

First, Setup a client for making request:
Expand Down
15 changes: 6 additions & 9 deletions lib/vsafe/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,13 @@ def validate_charge_account(params)
end

def service_url(endpoint = nil, jsonp = false)
parts = [
config.url,
jsonp ? config.jsonp_service_path : config.service_path
]
parts << endpoint if endpoint

File.join(parts)
base_uri = jsonp ? config.jsonp_url : config.url
endpoint.nil? ? base_uri : File.join(base_uri, endpoint)
end

def fingerprint_url
@_fingerprint_url ||= URI.join(config.url, config.sandbox ? SANDBOX_FINGERPRINT_PATH : FINGERPRINT_PATH).to_s
base_uri = URI.join(config.jsonp_url, '/')
@_fingerprint_url ||= URI.join(base_uri, config.sandbox ? SANDBOX_FINGERPRINT_PATH : FINGERPRINT_PATH).to_s
end

private
Expand All @@ -83,7 +79,8 @@ def request(url, params = {})
).to_json,
headers: {
"Content-Type" => REQUEST_CONTENT_TYPE
}
},
logger: @config.logger
}

# The HTTPS endpoint for VSafe Sandbox has an outdated SSL version.
Expand Down
24 changes: 15 additions & 9 deletions lib/vsafe/config.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
module VSafe
class Config
DEFAULT_REQUEST_TIMEOUT = 20 # seconds
DEFAULT_SANDBOX_URL = "https://paysafesandbox.ecustomersupport.com".freeze
DEFAULT_PRODUCTION_URL = "https://paysafe.ecustomerpayments.com".freeze
DEFAULT_JSONP_SERVICE_PATH = "GatewayProxyJSON/Service".freeze
DEFAULT_SERVICE_PATH = "GatewayProxy/Service".freeze
DEFAULT_SANDBOX_URL = "https://paysafesandbox.ecustomersupport.com/GatewayProxy/Service".freeze
DEFAULT_SANDBOX_JSONP_URL = "https://paysafesandbox.ecustomersupport.com/GatewayProxyJSON/Service".freeze

DEFAULT_PRODUCTION_URL = "https://paysafe.ecustomerpayments.com/GatewayProxy/Service".freeze
DEFAULT_PRODUCTION_JSONP_URL = "https://paysafe.ecustomerpayments.com/GatewayProxyJSON/Service".freeze

attr_accessor :sandbox_url,
:production_url,
:sandbox_jsonp_url,
:production_jsonp_url,
:sandbox,
:account_name,
:password,
:service_path,
:jsonp_service_path,
:request_timeout
:request_timeout,
:logger

def initialize
# Set sandbox to true by default
@sandbox = true
@service_path = DEFAULT_SERVICE_PATH
@jsonp_service_path = DEFAULT_JSONP_SERVICE_PATH
@request_timeout = DEFAULT_REQUEST_TIMEOUT
@sandbox_url = DEFAULT_SANDBOX_URL
@production_url = DEFAULT_PRODUCTION_URL
@sandbox_jsonp_url = DEFAULT_SANDBOX_JSONP_URL
@production_jsonp_url = DEFAULT_PRODUCTION_JSONP_URL
end

def url
sandbox ? @sandbox_url : @production_url
end

def jsonp_url
sandbox ? @sandbox_jsonp_url : @production_jsonp_url
end
end
end
51 changes: 20 additions & 31 deletions spec/vsafe/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,50 +136,32 @@ def stub_vsafe_request(endpoint)
let(:endpoint) { "foo" }

it "returns endpoint url" do
expect(client.service_url(endpoint, jsonp)).to eq("#{client.config.url}/#{service_path}/#{endpoint}")
expect(client.service_url(endpoint, jsonp)).to eq("#{base_uri}/#{endpoint}")
end
end

describe "when endpoint not defined" do
let(:endpoint) { nil }

it "returns base url" do
expect(client.service_url(endpoint, jsonp)).to eq("#{client.config.url}/#{service_path}")
expect(client.service_url(endpoint, jsonp)).to eq(base_uri)
end
end
end

context "when jsonp is false" do
let(:service_path) { VSafe::Config::DEFAULT_SERVICE_PATH }

let(:base_uri) { 'https://base.url/GatewayProxy/Service'}
before do
expect(client.config).to receive(:url).and_return(base_uri)
end
it_behaves_like "returns url"
end

context "when jsonp is true" do
let(:service_path) { VSafe::Config::DEFAULT_JSONP_SERVICE_PATH }

it_behaves_like "returns url", true
end

context "when custom service_path is set" do
let(:service_path) { "__custom_service" }
let(:client) {
VSafe::Client.new do |config|
config.service_path = "__custom_service"
end
}

it_behaves_like "returns url"
end

context "when custom jsonp_service_path is set" do
let(:service_path) { "__custom_jsonp" }
let(:client) {
VSafe::Client.new do |config|
config.jsonp_service_path = "__custom_jsonp"
end
}

let(:base_uri) { 'https://base.url/GatewayProxyJSON/Service'}
before do
expect(client.config).to receive(:jsonp_url).and_return(base_uri)
end
it_behaves_like "returns url", true
end
end
Expand All @@ -190,8 +172,14 @@ def stub_vsafe_request(endpoint)
allow(client.config).to receive(:sandbox).and_return(true)
end

it "returns url" do
expect(client.fingerprint_url).to eq("#{client.config.url}/#{VSafe::Client::SANDBOX_FINGERPRINT_PATH}")
it "use url when jsonp url is not set" do
client.config.sandbox_jsonp_url = 'https://sandbox.url/GatewayProxy/Service'
expect(client.fingerprint_url).to eq("https://sandbox.url/#{VSafe::Client::SANDBOX_FINGERPRINT_PATH}")
end

it "uses the jsonp_url when set" do
client.config.sandbox_jsonp_url = 'https://google.com/'
expect(client.fingerprint_url).to eq("https://google.com/#{VSafe::Client::SANDBOX_FINGERPRINT_PATH}")
end
end

Expand All @@ -201,7 +189,8 @@ def stub_vsafe_request(endpoint)
end

it "returns url" do
expect(client.fingerprint_url).to eq("#{client.config.url}/#{VSafe::Client::FINGERPRINT_PATH}")
allow(client.config).to receive(:jsonp_url).and_return('https://google.com/')
expect(client.fingerprint_url).to eq("https://google.com/#{VSafe::Client::FINGERPRINT_PATH}")
end
end
end
Expand Down
49 changes: 43 additions & 6 deletions spec/vsafe/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,77 @@

RSpec.describe VSafe::Config do
let (:config) { VSafe::Config.new }

describe ".new" do
it "sets default configs" do
expect(config.sandbox).to eq(true)
expect(config.request_timeout).to be_a(Fixnum)
expect(config.url).not_to be_empty
end
end

describe "#url" do
context "sandbox is true" do
it "uses default sandbox_url" do
expect(config.url).to eq(VSafe::Config::DEFAULT_SANDBOX_URL)
end

it "uses custom sandbox_url when sandbox" do
config.sandbox_url = "https://www.google.com"
expect(config.url).to eq("https://www.google.com")
end
end

context "sandbox is not true" do
before do
config.sandbox = false
end

it "uses default production_url when not sandbox" do
expect(config.url).to eq(VSafe::Config::DEFAULT_PRODUCTION_URL)
end

it "uses custom production_url when not sandbox" do
config.production_url = "https://www.google.com"
expect(config.url).to eq("https://www.google.com")
end
end
end

describe "#jsonp_url" do
context "sandbox is true" do
it "uses default sandbox_url" do
expect(config.jsonp_url).to eq(VSafe::Config::DEFAULT_SANDBOX_JSONP_URL)
end

it "uses custom sandbox_jsonp_url when set" do
config.sandbox_jsonp_url = "https://www.googlesandbox.com"
expect(config.jsonp_url).to eq("https://www.googlesandbox.com")
end
end

context "sandbox is not true" do
before do
config.sandbox = false
end

it "uses custom production_url when not sandbox" do
config.production_jsonp_url = "https://www.googlesandbox.com"
expect(config.jsonp_url).to eq("https://www.googlesandbox.com")
end
end
end

describe "#jsonp_url" do
context "sandbox is true" do
it "uses default sandbox_url" do
expect(config.jsonp_url).to eq(VSafe::Config::DEFAULT_SANDBOX_JSONP_URL)
end

it "uses custom sandbox_jsonp_url when set" do
config.sandbox_jsonp_url = "https://www.googlesandbox.com"
expect(config.jsonp_url).to eq("https://www.googlesandbox.com")
end
end
end
end