affirm-ruby-api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gem-release.yml +5 -0
- data/.github/workflows/test.yml +34 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +81 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +6 -0
- data/affirm-ruby-api.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/affirm-ruby-api.rb +1 -0
- data/lib/affirm.rb +11 -0
- data/lib/affirm/client.rb +201 -0
- data/lib/affirm/configuration.rb +38 -0
- data/lib/affirm/error.rb +29 -0
- data/lib/affirm/struct.rb +17 -0
- data/lib/affirm/structs/checkout.rb +27 -0
- data/lib/affirm/structs/checkout/response.rb +11 -0
- data/lib/affirm/structs/transaction.rb +17 -0
- data/lib/affirm/structs/transaction/event.rb +13 -0
- data/lib/affirm/version.rb +3 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 500b0f21a5256ed36d30d22a734e737f2938933df023df809f8a291cb2e4f549
|
4
|
+
data.tar.gz: 580273948b296ae930df947f70a3805c12e4dbcb6fbd66ce173d0d15c0a8fef6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89e902264083837edde774445406af7f86285e9286651b16671129640c28b4b16b6629abcd9bda6f03b0436d0ea5bdca1ecb0bca98db34b3707d1681668a2c40
|
7
|
+
data.tar.gz: d62d3b718b7acf23f9963485c75926b7d3f65dd479004f5f8f6dda7b96a63fe93ecf2450f923561ab8f0968cd884d4b6678b0bc62f1b69d9138bab9278ec936b
|
data/.gem-release.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Run Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: 2.6.x
|
20
|
+
- name: Build and test with Rake
|
21
|
+
run: |
|
22
|
+
gem install bundler
|
23
|
+
bundle install --jobs 4 --retry 3
|
24
|
+
bundle exec rake
|
25
|
+
- uses: devmasx/coverage-check-action@v1.1.0
|
26
|
+
with:
|
27
|
+
type: simplecov
|
28
|
+
result_path: coverage/.last_run.json
|
29
|
+
min_coverage: 100
|
30
|
+
token: ${{secrets.GITHUB_TOKEN}}
|
31
|
+
- name: StandardRB Linter
|
32
|
+
uses: andrewmcodes/standardrb-action@v0.0.2
|
33
|
+
env:
|
34
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at info@peterberkenbosch.nl. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in affirm-ruby-api.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem "rake", "~> 12.0"
|
7
|
+
gem "rspec", "~> 3.0"
|
8
|
+
gem "webmock", "~> 3.8"
|
9
|
+
gem "pry"
|
10
|
+
gem "standard"
|
11
|
+
gem "simplecov", require: false, group: :test
|
12
|
+
|
13
|
+
gem "gem-release"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
affirm-ruby-api (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.7.0)
|
10
|
+
public_suffix (>= 2.0.2, < 5.0)
|
11
|
+
ast (2.4.0)
|
12
|
+
coderay (1.1.2)
|
13
|
+
crack (0.4.3)
|
14
|
+
safe_yaml (~> 1.0.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
docile (1.3.2)
|
17
|
+
gem-release (2.1.1)
|
18
|
+
hashdiff (1.0.1)
|
19
|
+
jaro_winkler (1.5.4)
|
20
|
+
method_source (0.9.2)
|
21
|
+
parallel (1.19.1)
|
22
|
+
parser (2.7.0.5)
|
23
|
+
ast (~> 2.4.0)
|
24
|
+
pry (0.12.2)
|
25
|
+
coderay (~> 1.1.0)
|
26
|
+
method_source (~> 0.9.0)
|
27
|
+
public_suffix (4.0.3)
|
28
|
+
rainbow (3.0.0)
|
29
|
+
rake (12.3.3)
|
30
|
+
rspec (3.9.0)
|
31
|
+
rspec-core (~> 3.9.0)
|
32
|
+
rspec-expectations (~> 3.9.0)
|
33
|
+
rspec-mocks (~> 3.9.0)
|
34
|
+
rspec-core (3.9.1)
|
35
|
+
rspec-support (~> 3.9.1)
|
36
|
+
rspec-expectations (3.9.1)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.9.0)
|
39
|
+
rspec-mocks (3.9.1)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.9.0)
|
42
|
+
rspec-support (3.9.2)
|
43
|
+
rubocop (0.79.0)
|
44
|
+
jaro_winkler (~> 1.5.1)
|
45
|
+
parallel (~> 1.10)
|
46
|
+
parser (>= 2.7.0.1)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
ruby-progressbar (~> 1.7)
|
49
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
50
|
+
rubocop-performance (1.5.2)
|
51
|
+
rubocop (>= 0.71.0)
|
52
|
+
ruby-progressbar (1.10.1)
|
53
|
+
safe_yaml (1.0.5)
|
54
|
+
simplecov (0.18.5)
|
55
|
+
docile (~> 1.1)
|
56
|
+
simplecov-html (~> 0.11)
|
57
|
+
simplecov-html (0.12.2)
|
58
|
+
standard (0.2.2)
|
59
|
+
rubocop (~> 0.79.0)
|
60
|
+
rubocop-performance (~> 1.5.1)
|
61
|
+
unicode-display_width (1.6.1)
|
62
|
+
webmock (3.8.3)
|
63
|
+
addressable (>= 2.3.6)
|
64
|
+
crack (>= 0.3.2)
|
65
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
affirm-ruby-api!
|
72
|
+
gem-release
|
73
|
+
pry
|
74
|
+
rake (~> 12.0)
|
75
|
+
rspec (~> 3.0)
|
76
|
+
simplecov
|
77
|
+
standard
|
78
|
+
webmock (~> 3.8)
|
79
|
+
|
80
|
+
BUNDLED WITH
|
81
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Peter Berkenbosch
|
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,45 @@
|
|
1
|
+
# Affirm Ruby
|
2
|
+
|
3
|
+
![Run Tests](https://github.com/peterberkenbosch/affirm-ruby-api/workflows/Run%20Tests/badge.svg)
|
4
|
+
|
5
|
+
Implements the Affirm api using just Ruby. [Affirm documentation](https://docs.affirm.com/affirm-developers/reference)
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'affirm-ruby-api'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install affirm-ruby-api
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
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.
|
31
|
+
|
32
|
+
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).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/peterberkenbosch/affirm-ruby-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/peterberkenbosch/affirm-ruby-api/blob/master/CODE_OF_CONDUCT.md).
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
42
|
+
|
43
|
+
## Code of Conduct
|
44
|
+
|
45
|
+
Everyone interacting in the Affirm::Ruby::Api project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/peterberkenbosch/affirm-ruby-api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "lib/affirm/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "affirm-ruby-api"
|
5
|
+
spec.version = Affirm::VERSION
|
6
|
+
spec.authors = ["Peter Berkenbosch"]
|
7
|
+
spec.email = ["info@peterberkenbosch.nl"]
|
8
|
+
|
9
|
+
spec.summary = "Affirm Ruby client implementing the new Transaction api"
|
10
|
+
spec.description = spec.summary
|
11
|
+
spec.homepage = "https://github.com/peterberkenbosch/affirm-ruby-api"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/peterberkenbosch/affirm-ruby-api"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/peterberkenbosch/affirm-ruby-api/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "affirm"
|
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 @@
|
|
1
|
+
require_relative "./affirm"
|
data/lib/affirm.rb
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
require "pry"
|
2
|
+
module Affirm
|
3
|
+
class Client
|
4
|
+
def initialize
|
5
|
+
@version_strings = []
|
6
|
+
add_version_string "Affirm/" << VERSION
|
7
|
+
add_version_string "Ruby/" << RUBY_VERSION
|
8
|
+
add_version_string OpenSSL::OPENSSL_VERSION.split(" ").slice(0, 2).join "/"
|
9
|
+
end
|
10
|
+
|
11
|
+
def user_agent_string
|
12
|
+
@version_strings.join(" ")
|
13
|
+
end
|
14
|
+
|
15
|
+
# Start the 'store' checkout with Affirm.
|
16
|
+
#
|
17
|
+
# @see https://docs.affirm.com/affirm-developers/reference#checkout-store
|
18
|
+
#
|
19
|
+
# @param [Hash] payload
|
20
|
+
# @return <Struct::Checkout::Response>
|
21
|
+
#
|
22
|
+
# @raise Affirm::RequestError
|
23
|
+
def start_checkout_store(payload)
|
24
|
+
response = http_call(:post, "checkout/store", nil, payload)
|
25
|
+
Struct::Checkout::Response.new(response)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Start the 'direct' checkout with Affirm.
|
29
|
+
#
|
30
|
+
# @see https://docs.affirm.com/affirm-developers/reference#checkout-direct
|
31
|
+
#
|
32
|
+
# @param [Hash] payload
|
33
|
+
# @return <Struct::Checkout::Response>
|
34
|
+
#
|
35
|
+
# @raise Affirm::RequestError
|
36
|
+
def start_checkout_direct(payload)
|
37
|
+
response = http_call(:post, "checkout/direct", nil, payload)
|
38
|
+
Struct::Checkout::Response.new(response)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Fetch the checkout by checkout_id
|
42
|
+
#
|
43
|
+
# @see https://docs.affirm.com/affirm-developers/reference#checkout-read
|
44
|
+
#
|
45
|
+
# @param [String] checkout_id
|
46
|
+
# @return <Struct::Checkout>
|
47
|
+
#
|
48
|
+
# @raise Affirm::RequestError
|
49
|
+
def get_checkout(checkout_id)
|
50
|
+
response = http_call(:get, :checkout, checkout_id)
|
51
|
+
Struct::Checkout.new(response)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Authorize the transaction
|
55
|
+
#
|
56
|
+
# @see https://docs.affirm.com/affirm-developers/reference#authorize
|
57
|
+
#
|
58
|
+
# @param [String] checkout_id
|
59
|
+
# @return <Struct::Transaction>
|
60
|
+
#
|
61
|
+
# @raise Affirm::RequestError
|
62
|
+
def authorize(checkout_id)
|
63
|
+
response = http_call(:post, :transactions, nil, {transaction_id: checkout_id})
|
64
|
+
Struct::Transaction.new(response)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Captures the transaction
|
68
|
+
#
|
69
|
+
# @see https://docs.affirm.com/affirm-developers/reference#capture
|
70
|
+
#
|
71
|
+
# @param [String] transaction_id
|
72
|
+
# @return <Struct::Transaction::Event>
|
73
|
+
#
|
74
|
+
# @raise Affirm::RequestError
|
75
|
+
def capture(transaction_id)
|
76
|
+
response = http_call(:post, "transactions/#{transaction_id}/capture")
|
77
|
+
Struct::Transaction::Event.new(response)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Get the transaction by transaction_id
|
81
|
+
#
|
82
|
+
# @see https://docs.affirm.com/affirm-developers/reference#read-transaction
|
83
|
+
#
|
84
|
+
# @param [String] transaction_id
|
85
|
+
# @param [String] expand
|
86
|
+
# @return <Struct::Transaction::Event>
|
87
|
+
#
|
88
|
+
# @raise Affirm::RequestError
|
89
|
+
def read_transaction(transaction_id, expand = nil)
|
90
|
+
transaction_path = "transactions/#{transaction_id}?expand=#{expand}" if expand
|
91
|
+
transaction_path = "transactions/#{transaction_id}" unless expand
|
92
|
+
response = http_call(:get, transaction_path)
|
93
|
+
Struct::Transaction.new(response)
|
94
|
+
end
|
95
|
+
|
96
|
+
# Void the transaction
|
97
|
+
#
|
98
|
+
# @see https://docs.affirm.com/affirm-developers/reference#void-1
|
99
|
+
#
|
100
|
+
# @param [String] transaction_id
|
101
|
+
# @return <Struct::Transaction::Event>
|
102
|
+
#
|
103
|
+
# @raise Affirm::RequestError
|
104
|
+
def void(transaction_id)
|
105
|
+
response = http_call(:post, "transactions/#{transaction_id}/void")
|
106
|
+
Struct::Transaction::Event.new(response)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Refunds the transaction
|
110
|
+
#
|
111
|
+
# @see https://docs.affirm.com/affirm-developers/reference#refund-1
|
112
|
+
#
|
113
|
+
# @param [String] transaction_id
|
114
|
+
# @param [Integer] amount in cents to refund, when nil the total amount is refunded
|
115
|
+
# @return <Struct::Transaction::Event>
|
116
|
+
#
|
117
|
+
# @raise Affirm::RequestError
|
118
|
+
def refund(transaction_id, amount = nil)
|
119
|
+
response = if amount
|
120
|
+
http_call(:post, "transactions/#{transaction_id}/refund", nil, {amount: amount})
|
121
|
+
else
|
122
|
+
http_call(:post, "transactions/#{transaction_id}/refund")
|
123
|
+
end
|
124
|
+
Struct::Transaction::Event.new(response)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Executes a request, validates and returns the response.
|
128
|
+
#
|
129
|
+
# @param [String] http_method The HTTP method (:get, :post)
|
130
|
+
# @param [String] api_method The api method to call
|
131
|
+
# @param [String] id the optional id to be pasted in
|
132
|
+
# @param [Hash] an optional body to post to the endpoint
|
133
|
+
# @return [Hash]
|
134
|
+
# @raise [RequestError]
|
135
|
+
# @raise [NotFoundError]
|
136
|
+
# @raise [AuthenticationFailed]
|
137
|
+
def http_call(http_method, api_method, id = nil, body = {})
|
138
|
+
version = "v2"
|
139
|
+
|
140
|
+
# The Transaction api is actually the new API and replaces
|
141
|
+
# the Charge api. However the api endpoint for Transactions
|
142
|
+
# is set under a v1 version path.
|
143
|
+
version = "v1" if (api_method == :transactions) || ((api_method.class == String) && api_method.start_with?("transactions"))
|
144
|
+
|
145
|
+
path = "/api/#{version}/#{api_method}/#{id}".chomp("/")
|
146
|
+
api_endpoint = Affirm.config.endpoint
|
147
|
+
uri = URI.parse(api_endpoint)
|
148
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
149
|
+
http.use_ssl = true
|
150
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
151
|
+
http.set_debug_output($stdout) if ENV["DEBUG"]
|
152
|
+
|
153
|
+
case http_method
|
154
|
+
when :get
|
155
|
+
request = Net::HTTP::Get.new(path)
|
156
|
+
when :post
|
157
|
+
body.delete_if { |_k, v| v.nil? }
|
158
|
+
request = Net::HTTP::Post.new(path)
|
159
|
+
request.body = body.to_json
|
160
|
+
else
|
161
|
+
raise RequestError, "Invalid HTTP Method: #{http_method.to_s.upcase}"
|
162
|
+
end
|
163
|
+
request.basic_auth Affirm.config.public_api_key, Affirm.config.private_api_key
|
164
|
+
|
165
|
+
request["Accept"] = "application/json"
|
166
|
+
request["Content-Type"] = "application/json"
|
167
|
+
request["User-Agent"] = user_agent_string
|
168
|
+
|
169
|
+
begin
|
170
|
+
response = http.request(request)
|
171
|
+
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
|
172
|
+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
173
|
+
raise RequestError, e.message
|
174
|
+
end
|
175
|
+
|
176
|
+
http_code = response.code.to_i
|
177
|
+
case http_code
|
178
|
+
when 200, 201
|
179
|
+
JSON.parse(response.body)
|
180
|
+
when 400, 403
|
181
|
+
# 400 - Bad request
|
182
|
+
raise RequestError, response
|
183
|
+
when 401
|
184
|
+
# 401 - Unauthorized
|
185
|
+
raise AuthenticationError, response
|
186
|
+
when 404
|
187
|
+
# 404 - Not found
|
188
|
+
raise NotFoundError, response
|
189
|
+
when 500, 502, 503, 504
|
190
|
+
# 500, 502, 503, 504 - Server Errors
|
191
|
+
raise Error
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def add_version_string(version_string)
|
198
|
+
@version_strings << version_string.gsub(/\s+/, "-")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Affirm
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :public_api_key
|
4
|
+
attr_accessor :private_api_key
|
5
|
+
attr_reader :environment
|
6
|
+
|
7
|
+
ENDPOINTS = {
|
8
|
+
production: "api.affirm.com",
|
9
|
+
production_ca: "api.affirm.ca",
|
10
|
+
sandbox: "sandbox.affirm.com",
|
11
|
+
sandbox_ca: "sandbox.affirm.ca"
|
12
|
+
}
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@environment = :production
|
16
|
+
end
|
17
|
+
|
18
|
+
def environment=(env)
|
19
|
+
@environment = env.to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
def endpoint
|
23
|
+
"https://#{ENDPOINTS[environment]}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
def config
|
29
|
+
@configuration ||= Configuration.new
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_writer :configuration
|
33
|
+
|
34
|
+
def configure
|
35
|
+
yield config
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/affirm/error.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Affirm
|
2
|
+
class Error < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class RequestError < Error
|
6
|
+
attr_reader :http_response
|
7
|
+
|
8
|
+
def initialize(http_response)
|
9
|
+
@http_response = http_response
|
10
|
+
super(message_from(http_response))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def message_from(http_response)
|
16
|
+
return http_response if http_response.class == String
|
17
|
+
content_type = http_response.header["Content-Type"]
|
18
|
+
if content_type&.start_with?("application/json")
|
19
|
+
JSON.parse(http_response.body)["message"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class NotFoundError < RequestError
|
25
|
+
end
|
26
|
+
|
27
|
+
class AuthenticationError < RequestError
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Affirm
|
2
|
+
module Struct
|
3
|
+
class Base
|
4
|
+
def initialize(attributes = {})
|
5
|
+
attributes.each do |key, value|
|
6
|
+
m = "#{key}=".to_sym
|
7
|
+
send(m, value) if respond_to?(m)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require_relative "structs/checkout"
|
15
|
+
require_relative "structs/checkout/response"
|
16
|
+
require_relative "structs/transaction"
|
17
|
+
require_relative "structs/transaction/event"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Affirm
|
2
|
+
module Struct
|
3
|
+
class Checkout < Base
|
4
|
+
attr_accessor :api_version
|
5
|
+
attr_accessor :billing
|
6
|
+
attr_accessor :checkout_flow_type
|
7
|
+
attr_accessor :checkout_status
|
8
|
+
attr_accessor :checkout_type
|
9
|
+
attr_accessor :config
|
10
|
+
attr_accessor :currency
|
11
|
+
attr_accessor :discounts
|
12
|
+
attr_accessor :financing_program_external_name
|
13
|
+
attr_accessor :financing_program_name
|
14
|
+
attr_accessor :items
|
15
|
+
attr_accessor :loan_type
|
16
|
+
attr_accessor :merchant
|
17
|
+
attr_accessor :merchant_external_reference
|
18
|
+
attr_accessor :mfp_rule_input_data
|
19
|
+
attr_accessor :order_id
|
20
|
+
attr_accessor :product
|
21
|
+
attr_accessor :shipping
|
22
|
+
attr_accessor :shipping_amount
|
23
|
+
attr_accessor :tax_amount
|
24
|
+
attr_accessor :total
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Affirm
|
2
|
+
module Struct
|
3
|
+
class Checkout::Response < Base
|
4
|
+
# @return [String] The checkout ID for Affirm payment.
|
5
|
+
attr_accessor :checkout_id
|
6
|
+
|
7
|
+
# @return [String] The Affirm hosted URL checkout generated.
|
8
|
+
attr_accessor :redirect_url
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Affirm
|
2
|
+
module Struct
|
3
|
+
class Transaction < Base
|
4
|
+
attr_accessor :id
|
5
|
+
attr_accessor :amount
|
6
|
+
attr_accessor :amount_refunded
|
7
|
+
attr_accessor :authorization_expiration
|
8
|
+
attr_accessor :checkout_id
|
9
|
+
attr_accessor :created
|
10
|
+
attr_accessor :currency
|
11
|
+
attr_accessor :events
|
12
|
+
attr_accessor :order_id
|
13
|
+
attr_accessor :provider_id
|
14
|
+
attr_accessor :status
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: affirm-ruby-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Berkenbosch
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Affirm Ruby client implementing the new Transaction api
|
14
|
+
email:
|
15
|
+
- info@peterberkenbosch.nl
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gem-release.yml"
|
21
|
+
- ".github/workflows/test.yml"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".rspec"
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- affirm-ruby-api.gemspec
|
31
|
+
- bin/console
|
32
|
+
- bin/setup
|
33
|
+
- lib/affirm-ruby-api.rb
|
34
|
+
- lib/affirm.rb
|
35
|
+
- lib/affirm/client.rb
|
36
|
+
- lib/affirm/configuration.rb
|
37
|
+
- lib/affirm/error.rb
|
38
|
+
- lib/affirm/struct.rb
|
39
|
+
- lib/affirm/structs/checkout.rb
|
40
|
+
- lib/affirm/structs/checkout/response.rb
|
41
|
+
- lib/affirm/structs/transaction.rb
|
42
|
+
- lib/affirm/structs/transaction/event.rb
|
43
|
+
- lib/affirm/version.rb
|
44
|
+
homepage: https://github.com/peterberkenbosch/affirm-ruby-api
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/peterberkenbosch/affirm-ruby-api
|
49
|
+
source_code_uri: https://github.com/peterberkenbosch/affirm-ruby-api
|
50
|
+
changelog_uri: https://github.com/peterberkenbosch/affirm-ruby-api/CHANGELOG.md
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.3.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.0.3
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Affirm Ruby client implementing the new Transaction api
|
70
|
+
test_files: []
|