affirm-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +60 -0
- data/LICENSE.txt +22 -0
- data/README.md +108 -0
- data/Rakefile +19 -0
- data/affirm.gemspec +25 -0
- data/lib/affirm.rb +27 -0
- data/lib/affirm/charge.rb +45 -0
- data/lib/affirm/client.rb +50 -0
- data/lib/affirm/configuration.rb +34 -0
- data/lib/affirm/failure_result.rb +16 -0
- data/lib/affirm/objects/address.rb +14 -0
- data/lib/affirm/objects/billing.rb +9 -0
- data/lib/affirm/objects/contact.rb +12 -0
- data/lib/affirm/objects/discount.rb +10 -0
- data/lib/affirm/objects/event.rb +14 -0
- data/lib/affirm/objects/item.rb +15 -0
- data/lib/affirm/objects/order.rb +16 -0
- data/lib/affirm/objects/shipping.rb +10 -0
- data/lib/affirm/responses/auth.rb +22 -0
- data/lib/affirm/responses/capture.rb +15 -0
- data/lib/affirm/responses/error.rb +13 -0
- data/lib/affirm/responses/refund.rb +14 -0
- data/lib/affirm/responses/update.rb +14 -0
- data/lib/affirm/responses/void.rb +12 -0
- data/lib/affirm/success_result.rb +26 -0
- data/lib/affirm/version.rb +3 -0
- data/spec/charge_object_interface.rb +131 -0
- data/spec/charge_spec.rb +337 -0
- data/spec/client_spec.rb +94 -0
- data/spec/configuration_spec.rb +78 -0
- data/spec/failure_result_spec.rb +43 -0
- data/spec/fixtures/auth.json +1 -0
- data/spec/helper.rb +7 -0
- data/spec/spec_helper.rb +87 -0
- data/spec/success_result_spec.rb +31 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c4208863e40741f394c027dd416dd1a170ba2b1a
|
4
|
+
data.tar.gz: cce5e6bc2320398a1ceb86e25236752a883d7ff6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e96e16fc8fcccae57a39622b4349bf7c1e327ebb01816dca846fd1274719709d39cbc0fbc7d8d6cb6670d9acfd85790ecf5799c00141e6d8ea89647db9efc2ff
|
7
|
+
data.tar.gz: 0ca26687b5b56f9340beb1d2bae268a66d955cca443c93742e70a45c7a7ea3dd9a7676fbdff1b3e592758e200ec1b80ea01548498983f1953a8b38e1707d12ef
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
affirm-ruby (0.0.1)
|
5
|
+
faraday (~> 0.9.1)
|
6
|
+
faraday_middleware (~> 0.9.1)
|
7
|
+
virtus (~> 1.0, >= 1.0.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
axiom-types (0.1.1)
|
13
|
+
descendants_tracker (~> 0.0.4)
|
14
|
+
ice_nine (~> 0.11.0)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
16
|
+
byebug (5.0.0)
|
17
|
+
columnize (= 0.9.0)
|
18
|
+
coercible (1.0.0)
|
19
|
+
descendants_tracker (~> 0.0.1)
|
20
|
+
columnize (0.9.0)
|
21
|
+
descendants_tracker (0.0.4)
|
22
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
23
|
+
diff-lcs (1.2.5)
|
24
|
+
equalizer (0.0.11)
|
25
|
+
faraday (0.9.1)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
faraday_middleware (0.9.1)
|
28
|
+
faraday (>= 0.7.4, < 0.10)
|
29
|
+
ice_nine (0.11.1)
|
30
|
+
multipart-post (2.0.0)
|
31
|
+
rake (10.4.2)
|
32
|
+
rspec (3.2.0)
|
33
|
+
rspec-core (~> 3.2.0)
|
34
|
+
rspec-expectations (~> 3.2.0)
|
35
|
+
rspec-mocks (~> 3.2.0)
|
36
|
+
rspec-core (3.2.3)
|
37
|
+
rspec-support (~> 3.2.0)
|
38
|
+
rspec-expectations (3.2.1)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.2.0)
|
41
|
+
rspec-mocks (3.2.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.2.0)
|
44
|
+
rspec-support (3.2.2)
|
45
|
+
thread_safe (0.3.5)
|
46
|
+
virtus (1.0.5)
|
47
|
+
axiom-types (~> 0.1)
|
48
|
+
coercible (~> 1.0)
|
49
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
50
|
+
equalizer (~> 0.0, >= 0.0.9)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
affirm-ruby!
|
57
|
+
bundler (~> 1.7)
|
58
|
+
byebug
|
59
|
+
rake (~> 10.0)
|
60
|
+
rspec (~> 3.2.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Yury Velikanau
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/spectator/affirm.svg?branch=master)](http://travis-ci.org/spectator/affirm)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/spectator/affirm/badges/gpa.svg)](https://codeclimate.com/github/spectator/affirm)
|
3
|
+
|
4
|
+
# Affirm
|
5
|
+
|
6
|
+
Affirm gem is a simple Ruby wrapper for Affirm.com API. Please check Affirm API
|
7
|
+
[documentation](http://docs.affirm.com/v2/api/#sandbox-api-keys) for more
|
8
|
+
details.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'affirm'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install affirm
|
25
|
+
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
Affirm.configure do |config|
|
30
|
+
config.public_api_key = "ABC"
|
31
|
+
config.private_api_key = "XYZ"
|
32
|
+
config.environment = :sandbox # or :production (default if not specified)
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
To authorize a charge `checkout_token` is required. This token gets `POST`ed to
|
39
|
+
`user_confirmation_url` that is setup in Checkout JavaScript object.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Affirm::Charge.authorize(checkout_token)
|
43
|
+
```
|
44
|
+
|
45
|
+
The rest of the API uses `charge_id` which is received as `id` after charge is
|
46
|
+
authorized.
|
47
|
+
|
48
|
+
#### Read charge
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Affirm::Charge.find("TEST-AL04-UVGR")
|
52
|
+
```
|
53
|
+
|
54
|
+
#### Capture charge
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Affirm::Charge.capture("TEST-ALO4-UVGR")
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Void charge
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Affirm::Charge.void("TEST-ALO4-UVGR")
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Refund charge
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Affirm::Charge.refund("TEST-ALO4-UVGR", amount: 500)
|
70
|
+
```
|
71
|
+
|
72
|
+
#### Update charge
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Affirm::Charge.update("TEST-ALO4-UVGR",
|
76
|
+
order_id: "CUSTOM_ORDER_ID",
|
77
|
+
shipping_carrier: "USPS",
|
78
|
+
shipping_confirmation: "1Z23223"
|
79
|
+
)
|
80
|
+
```
|
81
|
+
|
82
|
+
Failed response will return [error
|
83
|
+
object](http://docs.affirm.com/v2/api/errors/#error-object) as a plain ruby
|
84
|
+
object with values coerced in corresponding types.
|
85
|
+
|
86
|
+
The same will happen for all successful responses described (not in full) in
|
87
|
+
[server integration](http://docs.affirm.com/v2/api/charges/#authentication)
|
88
|
+
guide.
|
89
|
+
|
90
|
+
#### Example
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
response = Affirm::Charge.authorize(checkout_token)
|
94
|
+
if response.success?
|
95
|
+
charge_id = response.id
|
96
|
+
# save charge_id
|
97
|
+
else
|
98
|
+
puts response.error.message
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
1. Fork it ( https://github.com/spectator/affirm/fork )
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
desc "Run specs"
|
5
|
+
RSpec::Core::RakeTask.new("spec") do |task|
|
6
|
+
task.verbose = false
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Console"
|
10
|
+
task :console do
|
11
|
+
require "irb"
|
12
|
+
require "irb/completion"
|
13
|
+
require "affirm"
|
14
|
+
require "byebug"
|
15
|
+
ARGV.clear
|
16
|
+
IRB.start
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: :spec
|
data/affirm.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'affirm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "affirm-ruby"
|
8
|
+
spec.version = Affirm::VERSION
|
9
|
+
spec.authors = ["Yury Velikanau", "Igor Pstyga"]
|
10
|
+
spec.email = ["yury.velikanau@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby wrapper for Affirm.com API}
|
12
|
+
spec.description = %q{Ruby client library for Affirm.com API}
|
13
|
+
spec.homepage = "https://github.com/spectator/affirm"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = "~> 2.1"
|
22
|
+
spec.add_dependency "faraday", "~> 0.9.1"
|
23
|
+
spec.add_dependency "faraday_middleware", "~> 0.9.1"
|
24
|
+
spec.add_dependency "virtus", "~> 1.0", ">= 1.0.0"
|
25
|
+
end
|
data/lib/affirm.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "faraday_middleware"
|
5
|
+
require "virtus"
|
6
|
+
|
7
|
+
require "affirm/client"
|
8
|
+
require "affirm/charge"
|
9
|
+
require "affirm/configuration"
|
10
|
+
require "affirm/success_result"
|
11
|
+
require "affirm/failure_result"
|
12
|
+
require "affirm/objects/event"
|
13
|
+
require "affirm/objects/contact"
|
14
|
+
require "affirm/objects/address"
|
15
|
+
require "affirm/objects/billing"
|
16
|
+
require "affirm/objects/shipping"
|
17
|
+
require "affirm/objects/discount"
|
18
|
+
require "affirm/objects/item"
|
19
|
+
require "affirm/objects/order"
|
20
|
+
require "affirm/responses/auth"
|
21
|
+
require "affirm/responses/capture"
|
22
|
+
require "affirm/responses/void"
|
23
|
+
require "affirm/responses/refund"
|
24
|
+
require "affirm/responses/update"
|
25
|
+
require "affirm/responses/error"
|
26
|
+
|
27
|
+
require "affirm/version"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Affirm
|
2
|
+
module Charge
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def authorize(token)
|
6
|
+
respond Client.request(:post, "charges", checkout_token: token)
|
7
|
+
end
|
8
|
+
|
9
|
+
def find(charge_id)
|
10
|
+
respond Client.request(:get, "charges/#{charge_id}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def capture(charge_id)
|
14
|
+
respond Client.request(:post, "charges/#{charge_id}/capture")
|
15
|
+
end
|
16
|
+
|
17
|
+
def void(charge_id)
|
18
|
+
respond Client.request(:post, "charges/#{charge_id}/void")
|
19
|
+
end
|
20
|
+
|
21
|
+
def refund(charge_id, amount:)
|
22
|
+
respond Client.request(:post, "charges/#{charge_id}/refund", amount: amount)
|
23
|
+
end
|
24
|
+
|
25
|
+
def update(charge_id, **updates)
|
26
|
+
respond Client.request(:post, "charges/#{charge_id}/update", updates)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def respond(response)
|
32
|
+
return FailureResult.new(response) unless response.success?
|
33
|
+
|
34
|
+
type = response.body["type"]
|
35
|
+
klass = case type
|
36
|
+
when *%w(capture void refund update)
|
37
|
+
Object.const_get("Affirm::Responses::#{type.capitalize}")
|
38
|
+
else
|
39
|
+
Affirm::Responses::Auth
|
40
|
+
end
|
41
|
+
|
42
|
+
SuccessResult.new(klass.new(response.body))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Affirm
|
2
|
+
class Client
|
3
|
+
attr_reader :connection
|
4
|
+
|
5
|
+
attr_reader :url_prefix
|
6
|
+
private :url_prefix
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def request(method, path, **data)
|
10
|
+
new.public_send(method, path, data)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@url_prefix = "/api/v2"
|
16
|
+
@connection = Faraday.new(Affirm.configuration.endpoint) do |conn|
|
17
|
+
conn.basic_auth(basic_auth_user, basic_auth_password)
|
18
|
+
conn.request :json
|
19
|
+
conn.response :json, content_type: /\bjson$/
|
20
|
+
conn.adapter Faraday.default_adapter
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(path, **data)
|
25
|
+
connection.get(normalized_path(path), data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def post(path, **data)
|
29
|
+
connection.post(normalized_path(path), data)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def basic_auth_user
|
35
|
+
Affirm.configuration.public_api_key
|
36
|
+
end
|
37
|
+
|
38
|
+
def basic_auth_password
|
39
|
+
Affirm.configuration.private_api_key
|
40
|
+
end
|
41
|
+
|
42
|
+
def normalized_path(path)
|
43
|
+
url_prefix + normalize_path(path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def normalize_path(path)
|
47
|
+
Faraday::Utils.normalize_path(path)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Affirm
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :public_api_key
|
4
|
+
attr_accessor :private_api_key
|
5
|
+
attr_accessor :environment
|
6
|
+
|
7
|
+
ENDPOINTS = {
|
8
|
+
production: "api.affirm.com",
|
9
|
+
sandbox: "sandbox.affirm.com"
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@environment = :production
|
14
|
+
end
|
15
|
+
|
16
|
+
def endpoint
|
17
|
+
"https://#{ENDPOINTS[environment]}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def configuration
|
23
|
+
@configuration ||= Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def configuration=(config)
|
27
|
+
@configuration = config
|
28
|
+
end
|
29
|
+
|
30
|
+
def configure
|
31
|
+
yield configuration
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Affirm
|
2
|
+
class FailureResult
|
3
|
+
extend Forwardable
|
4
|
+
def_delegators :response, :status, :success?
|
5
|
+
|
6
|
+
attr_reader :error
|
7
|
+
|
8
|
+
attr_reader :response
|
9
|
+
private :response
|
10
|
+
|
11
|
+
def initialize(response)
|
12
|
+
@response = response
|
13
|
+
@error = Affirm::Responses::Error.new(response.body)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|