amex_enhanced_authorization 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/amex_enhanced_authorization.gemspec +28 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/amex_enhanced_authorization.rb +15 -0
- data/lib/amex_enhanced_authorization/connection.rb +43 -0
- data/lib/amex_enhanced_authorization/logged_request.rb +56 -0
- data/lib/amex_enhanced_authorization/online_purchase_payload.rb +49 -0
- data/lib/amex_enhanced_authorization/request.rb +49 -0
- data/lib/amex_enhanced_authorization/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a9eae2794641cb519d74cb5cd54fb67886e4137511e0cd8e27a4f9e132441c76
|
4
|
+
data.tar.gz: 520b3cfc160be5885d5da1e632e04fb16d4b9d7eff5a0e465eed88b96c40afd4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b4488bbe0aa4e996e3c7fac4f374fa457726ef34416a737dad4956f229d3d6f4bb061eb1eb7ca5cf03751bb1467c8e2196ffc6435a8647207d8706ccb56a60b8
|
7
|
+
data.tar.gz: d71769a9438f9b596f685e2d8a0afd140633861ff43a46376c7e1e8a1ec8e0bf6beef590240d755f85c44e140eef43dcba555544edb657f4a70b2bc84318a7df
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Piers Chambers
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# AmexEnhancedAuthorization
|
2
|
+
|
3
|
+
[![Travis CI Status](https://secure.travis-ci.org/varyonic/amex_enhanced_authorization.svg)](https://travis-ci.org/varyonic/amex_enhanced_authorization)
|
4
|
+
|
5
|
+
Unofficial Ruby wrapper for American Express Enhanced Authorization
|
6
|
+
published via the [amex developer portal](https://developer.americanexpress.com/products/enhanced-authorization-v2/),
|
7
|
+
see also [amex-api-java-client-core](https://github.com/americanexpress/amex-api-java-client-core).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'amex_enhanced_authorization'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amex_enhanced_authorization.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "amex_enhanced_authorization/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "amex_enhanced_authorization"
|
8
|
+
spec.version = AmexEnhancedAuthorization::VERSION
|
9
|
+
spec.authors = ["Piers Chambers"]
|
10
|
+
spec.email = ["piers@varyonic.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Unofficial Ruby wrapper for American Express Enhanced Authorization.}
|
13
|
+
spec.homepage = "https://github.com/varyonic/amex_enhanced_authorization"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "amex_enhanced_authorization"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'json'
|
3
|
+
require 'securerandom'
|
4
|
+
require 'logger'
|
5
|
+
require 'openssl'
|
6
|
+
require 'amex_enhanced_authorization/online_purchase_payload'
|
7
|
+
require "amex_enhanced_authorization/logged_request"
|
8
|
+
require "amex_enhanced_authorization/request"
|
9
|
+
require "amex_enhanced_authorization/connection"
|
10
|
+
require "amex_enhanced_authorization/version"
|
11
|
+
|
12
|
+
module AmexEnhancedAuthorization
|
13
|
+
class Error < StandardError; end
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module AmexEnhancedAuthorization
|
2
|
+
class Connection
|
3
|
+
attr_reader :host
|
4
|
+
attr_reader :base_path
|
5
|
+
attr_reader :client_id, :client_secret
|
6
|
+
attr_accessor :logger
|
7
|
+
|
8
|
+
def initialize(host:,
|
9
|
+
client_id:, client_secret:,
|
10
|
+
logger: Logger.new('/dev/null'))
|
11
|
+
@host = host
|
12
|
+
@base_path = "/risk/fraud/v2/apiplatform/enhanced_authorizations".freeze
|
13
|
+
@client_id, @client_secret = client_id, client_secret
|
14
|
+
@logger = logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def online_purchase(params)
|
18
|
+
payload = OnlinePurchasePayload.new(params).to_json
|
19
|
+
JSON.parse send_authorized_request('POST', 'online_purchases', payload)
|
20
|
+
end
|
21
|
+
|
22
|
+
def send_authorized_request(method, route, payload = nil)
|
23
|
+
resource_path = "#{base_path}/#{route}"
|
24
|
+
authorization = hmac_authorization(method, resource_path, payload)
|
25
|
+
request = Request.new(method, "https://#{host}#{resource_path}", client_id, logger)
|
26
|
+
request.send(payload, authorization)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [String] method, e.g. 'POST'
|
30
|
+
# @param [String] resource_path, e.g. '/payments/digital/v2/tokens/provisioning'
|
31
|
+
# @param [String] JSON payload
|
32
|
+
# @return [String] Authorization: MAC id="gfFb4K8esqZgMpzwF9SXzKLCCbPYV8bR",ts="1463772177193",nonce="61129a8d-ca24-464b-8891-9251501d86f0", bodyhash="YJpz6NdGP0aV6aYaa+6qKCjQt46of+Cj4liBz90G6X8=", mac="uzybzLPj3fD8eBZaBzb4E7pZs+l+IWS0w/w2wwsExdo="
|
33
|
+
def hmac_authorization(method, resource_path, payload, nonce = SecureRandom.uuid, ts = (Time.now.to_r * 1000).to_i)
|
34
|
+
bodyhash = hmac_digest(payload)
|
35
|
+
mac = hmac_digest([ts, nonce, method, resource_path, host, 443, bodyhash, ''].join("\n"))
|
36
|
+
%(MAC id="#{client_id}",ts="#{ts}",nonce="#{nonce}",bodyhash="#{bodyhash}",mac="#{mac}")
|
37
|
+
end
|
38
|
+
|
39
|
+
def hmac_digest(s)
|
40
|
+
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, client_secret, s.to_s))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'benchmark'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module AmexEnhancedAuthorization
|
5
|
+
|
6
|
+
# An HTTPS request whose content, response and latency are logged.
|
7
|
+
class LoggedRequest
|
8
|
+
attr_reader :uri, :request
|
9
|
+
attr_reader :logger
|
10
|
+
|
11
|
+
def initialize(method, path, logger)
|
12
|
+
@uri = URI(path)
|
13
|
+
@request = Net::HTTP.const_get(method.capitalize).new(@uri)
|
14
|
+
@logger = logger
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add provided headers and invoke request over HTTPS.
|
18
|
+
# @return response
|
19
|
+
def send(data)
|
20
|
+
headers.each_pair { |k, v| request[k] = v }
|
21
|
+
log_request_response(headers, data) do
|
22
|
+
https(uri).request(request)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Hash]
|
27
|
+
def headers
|
28
|
+
@headers ||= {}
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def https(uri)
|
34
|
+
Net::HTTP.new(uri.host, uri.port).tap { |http| http.use_ssl = true }
|
35
|
+
end
|
36
|
+
|
37
|
+
# Log URI, method, data
|
38
|
+
# Start timer.
|
39
|
+
# Yield to request block.
|
40
|
+
# Log response and time taken.
|
41
|
+
def log_request_response(headers, data = nil)
|
42
|
+
logger.info "[#{self.class.name}] request = #{request.method} #{uri}#{data ? '?' + data : ''}"
|
43
|
+
logger.info "[#{self.class.name}] headers = #{headers}"
|
44
|
+
response = nil
|
45
|
+
tms = Benchmark.measure do
|
46
|
+
response = yield
|
47
|
+
end
|
48
|
+
logger.info("[#{self.class.name}] response (#{ms(tms)}ms): #{response.inspect} #{response.body}")
|
49
|
+
response
|
50
|
+
end
|
51
|
+
|
52
|
+
def ms(tms)
|
53
|
+
(tms.real*1000).round(3)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module AmexEnhancedAuthorization
|
4
|
+
class OnlinePurchasePayload
|
5
|
+
attr_accessor :card_acceptor_id, :card_number, :currency_code, :amount, :timestamp
|
6
|
+
attr_accessor :customer_email
|
7
|
+
attr_accessor :billing_address, :billing_postal_code, :billing_first_name, :billing_last_name, :billing_phone_number
|
8
|
+
attr_accessor :shipto_address, :shipto_postal_code, :shipto_first_name, :shipto_last_name, :shipto_phone_number, :shipto_country_code
|
9
|
+
|
10
|
+
def initialize(params)
|
11
|
+
params.each_pair { |k, v| send "#{k}=", v }
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_json
|
15
|
+
payload = {
|
16
|
+
timestamp: Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
|
17
|
+
transaction_data: {
|
18
|
+
card_acceptor_id: card_acceptor_id, # aka. SE10 or Merchant code
|
19
|
+
card_number: card_number,
|
20
|
+
amount: amount,
|
21
|
+
currency_code: currency_code,
|
22
|
+
transaction_timestamp: timestamp.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
|
23
|
+
additional_information: { risk_score: true },
|
24
|
+
}
|
25
|
+
}
|
26
|
+
payload[:purchaser_information] = purchaser_information if purchaser_information.any?
|
27
|
+
payload.to_json
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def purchaser_information
|
33
|
+
@purchaser_information ||= {
|
34
|
+
customer_email: customer_email,
|
35
|
+
billing_address: billing_address,
|
36
|
+
billing_postal_code: billing_postal_code,
|
37
|
+
billing_first_name: billing_first_name,
|
38
|
+
billing_last_name: billing_last_name,
|
39
|
+
billing_phone_number: billing_phone_number,
|
40
|
+
shipto_address: shipto_address,
|
41
|
+
shipto_postal_code: shipto_postal_code,
|
42
|
+
shipto_first_name: shipto_first_name,
|
43
|
+
shipto_last_name: shipto_last_name,
|
44
|
+
shipto_phone_number: shipto_phone_number,
|
45
|
+
shipto_country_code: shipto_country_code,
|
46
|
+
}.compact
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module AmexEnhancedAuthorization
|
2
|
+
|
3
|
+
# Amex requests use extended headers and a JSON body.
|
4
|
+
class Request < LoggedRequest
|
5
|
+
attr_reader :client_id
|
6
|
+
attr_reader :authorization
|
7
|
+
|
8
|
+
def initialize(method, path, client_id, logger)
|
9
|
+
super(method, path, logger)
|
10
|
+
@client_id = client_id
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return response on success or log failure and throw error.
|
14
|
+
def send(data, authorization)
|
15
|
+
@authorization = authorization
|
16
|
+
request.body = data if data
|
17
|
+
response = super(data)
|
18
|
+
fail_unless_expected_response response, Net::HTTPSuccess
|
19
|
+
response.body
|
20
|
+
end
|
21
|
+
|
22
|
+
def headers
|
23
|
+
Hash[
|
24
|
+
'Content-Type' => 'application/json',
|
25
|
+
'Content-Language' => 'en-US',
|
26
|
+
'x-amex-api-key' => client_id,
|
27
|
+
'x-amex-request-id' => SecureRandom.uuid,
|
28
|
+
'authorization' => authorization,
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
class UnexpectedHttpResponse < StandardError
|
33
|
+
attr_reader :response
|
34
|
+
|
35
|
+
def initialize(response)
|
36
|
+
@response = response
|
37
|
+
super "#{response.message} (#{response.code}): #{response.body}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def fail_unless_expected_response(response, *allowed_responses)
|
42
|
+
unless allowed_responses.any? { |allowed| response.is_a?(allowed) }
|
43
|
+
logger.error "#{response.inspect}: #{response.body}"
|
44
|
+
raise UnexpectedHttpResponse, response
|
45
|
+
end
|
46
|
+
response
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: amex_enhanced_authorization
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piers Chambers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- piers@varyonic.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- amex_enhanced_authorization.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/amex_enhanced_authorization.rb
|
73
|
+
- lib/amex_enhanced_authorization/connection.rb
|
74
|
+
- lib/amex_enhanced_authorization/logged_request.rb
|
75
|
+
- lib/amex_enhanced_authorization/online_purchase_payload.rb
|
76
|
+
- lib/amex_enhanced_authorization/request.rb
|
77
|
+
- lib/amex_enhanced_authorization/version.rb
|
78
|
+
homepage: https://github.com/varyonic/amex_enhanced_authorization
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubygems_version: 3.0.3
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Unofficial Ruby wrapper for American Express Enhanced Authorization.
|
101
|
+
test_files: []
|