increase 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +220 -12
- data/lib/increase/client.rb +2 -1
- data/lib/increase/configuration.rb +3 -6
- data/lib/increase/errors.rb +14 -0
- data/lib/increase/resource.rb +158 -49
- data/lib/increase/resources/account_numbers.rb +4 -4
- data/lib/increase/resources/account_transfers.rb +5 -5
- data/lib/increase/resources/accounts.rb +5 -5
- data/lib/increase/resources/ach_transfers.rb +5 -5
- data/lib/increase/resources/cards.rb +5 -5
- data/lib/increase/resources/check_transfers.rb +14 -0
- data/lib/increase/resources/events.rb +2 -2
- data/lib/increase/resources/limits.rb +12 -0
- data/lib/increase/resources/pending_transactions.rb +2 -2
- data/lib/increase/resources/routing_numbers.rb +9 -0
- data/lib/increase/resources/transactions.rb +2 -2
- data/lib/increase/util.rb +15 -0
- data/lib/increase/version.rb +1 -1
- data/lib/increase/webhook/signature.rb +56 -0
- data/lib/increase.rb +1 -1
- metadata +8 -18
- data/lib/increase/webhooks.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dab8c774d39cce112627bc75d36a10947419d640db56e45dfc4f2cd75330656
|
4
|
+
data.tar.gz: a93d12bf1e5a64938dcf41b339a1de33c36c82ad8b5430169156cb868e0cf87a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f74f1fd78799702a1dfed5180e93155ca7595a4f8d758545e483f3aee5db6cac853ff044fe043bcc33918eb85dd17c4544ea359654adfa1b52448240e1a38d42
|
7
|
+
data.tar.gz: ce8aacf00abb9d1660cff928a80ecc5f74900f6ea998cb47a512dedb080cfd252740e4c7f15d982580dae0f026c172948d3948fcbd9438a0348e6b8dd2ee28f6
|
data/README.md
CHANGED
@@ -1,35 +1,243 @@
|
|
1
1
|
# Increase
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/increase`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
A Ruby API client for [Increase](https://increase.com/), a platform for Bare-Metal Banking APIs!
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
-
|
11
7
|
Install the gem and add to the application's Gemfile by executing:
|
12
8
|
|
13
|
-
|
9
|
+
```sh
|
10
|
+
$ bundle add increase
|
11
|
+
```
|
14
12
|
|
15
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
14
|
|
17
|
-
|
15
|
+
```sh
|
16
|
+
$ gem install increase
|
17
|
+
```
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
```ruby
|
22
|
+
require 'increase'
|
23
|
+
|
24
|
+
# Grab your API key from https://dashboard.increase.com/developers/api_keys
|
25
|
+
Increase.api_key = 'my_api_key'
|
26
|
+
Increase.base_url = 'https://api.increase.com'
|
27
|
+
|
28
|
+
# List transactions
|
29
|
+
Increase::Transactions.list
|
30
|
+
|
31
|
+
# Retrieve a transaction
|
32
|
+
Increase::Transactions.retrieve('transaction_1234abcd')
|
33
|
+
|
34
|
+
# Create an ACH Transfer
|
35
|
+
Increase::AchTransfers.create(
|
36
|
+
account_id: 'account_1234abcd',
|
37
|
+
amount: 100_00, # 10,000 cents ($100 dollars)
|
38
|
+
routing_number: '123456789',
|
39
|
+
account_number: '9876543210',
|
40
|
+
statement_descriptor: 'broke the bank for some retail therapy'
|
41
|
+
)
|
42
|
+
```
|
43
|
+
|
44
|
+
### Per-request Configuration
|
45
|
+
|
46
|
+
By default, the client will use the global API key and configurations. However, you can define a custom client to be
|
47
|
+
used for per-request configuration.
|
48
|
+
|
49
|
+
For example, you may want to have access to production and sandbox data at the same.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
sandbox = Increase::Client.new(
|
53
|
+
api_key: 'time_is_money',
|
54
|
+
base_url: 'https://sandbox.increase.com'
|
55
|
+
)
|
56
|
+
|
57
|
+
# This request will use the `sandbox` client and its configurations
|
58
|
+
Increase::Transactions.with_config(sandbox).list
|
59
|
+
# => [{some sandbox transactions here}, {transaction}, {transaction}]
|
60
|
+
|
61
|
+
# This request will still use the global configurations (where the API key is a production key)
|
62
|
+
Increase::Transactions.list
|
63
|
+
# => [{some production transactions here}, {transaction}, {transaction}]
|
64
|
+
```
|
65
|
+
|
66
|
+
See the [Configuration](#configuration) section for more information on the available configurations.
|
67
|
+
|
68
|
+
### Pagination
|
69
|
+
|
70
|
+
When listing resources (e.g. transactions), **Increase** limits the number of results per page to 100. Luckily, the
|
71
|
+
client will automatically paginate through all the results for you!
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Increase::Transactions.list(limit: :all) do |transactions|
|
75
|
+
# This block will be called once for each page of results
|
76
|
+
puts "I got #{transactions.count} transactions!"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Or, if you'd like a gargantuan array of all the transactions
|
80
|
+
Increase::Transactions.list(limit: :all)
|
81
|
+
Increase::Transactions.list(limit: 2_000)
|
82
|
+
```
|
83
|
+
|
84
|
+
Watch out for the rate limit!
|
85
|
+
|
86
|
+
### Error Handling
|
87
|
+
|
88
|
+
Whenever you make an oopsies, the client will raise an error! Errors originating from the API will be a subclass
|
89
|
+
of `Increase::ApiError`.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
|
93
|
+
begin
|
94
|
+
Increase::Transactions.retrieve('transaction_1234abcd')
|
95
|
+
rescue Increase::ApiError => e
|
96
|
+
puts e.message # "[404: object_not_found_error] Could not find the specified object. No resource of type ..."
|
97
|
+
puts e.title # "Could not find the specified object."
|
98
|
+
puts e.detail # "No resource of type transaction was found with ID transaction_1234abcd."
|
99
|
+
puts e.status # 404
|
100
|
+
|
101
|
+
puts e.response # This contains the full response from the API, including headers! (its a Faraday::Response object)
|
102
|
+
|
103
|
+
puts e.class # Increase::ObjectNotFoundError (it's a subclass of Increase::ApiError!)
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
To disable this behavior, set `Increase.raise_api_errors = false`. Errors will then be returned as a normal response.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
Increase.raise_api_errors = false # Default: true
|
111
|
+
|
112
|
+
Increase::Transactions.retrieve('transaction_1234abcd')
|
113
|
+
# => {"status"=>404, "type"=>"object_not_found_error", ... }
|
114
|
+
```
|
115
|
+
|
116
|
+
### Configuration
|
117
|
+
|
118
|
+
| Name | Description | Default |
|
119
|
+
|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|
|
120
|
+
| **api_key** | Your Increase API Key. Grab it from https://dashboard.increase.com/developers/api_keys | `nil` |
|
121
|
+
| **base_url** | The base URL for Increase's API. You can use `:production` (https://api.increase.com), `:sandbox` (https://sandbox.increase.com), or set an actual URL | `"https://api.increase.com"` |
|
122
|
+
| **raise_api_errors** | Whether to raise an error when the API returns a non-2XX status. Learn more about Increase's errors [here](https://increase.com/documentation/api#errors). See error classes [here](https://github.com/garyhtou/increase-ruby/blob/main/lib/increase/errors.rb) | `true` |
|
123
|
+
|
124
|
+
There are multiple syntaxes for configuring the client. Choose your favorite!
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
# Set the configurations directly
|
128
|
+
Increase.api_key = 'terabytes_of_cash' # Default: nil (you'll need one tho!)
|
129
|
+
Increase.base_url = :production # Default: :production
|
130
|
+
Increase.raise_api_errors = true # Default: true
|
131
|
+
|
132
|
+
# Or, you can pass in a hash
|
133
|
+
Increase.configure(api_key: 'just_my_two_cents')
|
134
|
+
|
135
|
+
# Or, you can use a block!
|
136
|
+
Increase.configure do |config|
|
137
|
+
config.api_key = 'digital_dough'
|
138
|
+
config.base_url = :sandbox # Default: :production
|
139
|
+
config.raise_api_errors = false # Default: true
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
If you are using Rails, the recommended way is to set your configurations as a block in an initializer.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
# config/initializers/increase.rb
|
147
|
+
|
148
|
+
Increase.configure do |config|
|
149
|
+
config.api_key = 'money_cant_buy_happiness'
|
150
|
+
config.base_url = :production
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
### Webhooks
|
155
|
+
|
156
|
+
**Increase**'s webhooks include a `Increase-Webhook-Signature` header for securing your webhook endpoint. Although not
|
157
|
+
required, it's strongly recommended that you verify the signature to ensure the request is coming from **Increase**.
|
158
|
+
|
159
|
+
Here is an example for Rails.
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
|
163
|
+
class IncreaseController < ApplicationController
|
164
|
+
protect_from_forgery except: :webhook # Ignore CSRF checks
|
165
|
+
|
166
|
+
def webhook
|
167
|
+
payload = request.body.read
|
168
|
+
sig_header = request.headers['Increase-Webhook-Signature']
|
169
|
+
secret = Rails.application.credentials.dig(:increase, :webhook_secret)
|
170
|
+
|
171
|
+
Increase::Webhook::Signature.verify?(
|
172
|
+
payload: payload,
|
173
|
+
signature_header: sig_header,
|
174
|
+
secret: secret
|
175
|
+
)
|
176
|
+
|
177
|
+
# It's a valid webhook! Do something with it...
|
178
|
+
|
179
|
+
render json: {success: true}
|
180
|
+
rescue Increase::WebhookSignatureVerificationError => e
|
181
|
+
render json: {error: 'Webhook signature verification failed'}, status: :bad_request
|
182
|
+
end
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
186
|
+
### Idempotency
|
187
|
+
|
188
|
+
**Increase** supports [idempotent requests](https://increase.com/documentation/api#idempotency) to allow for safely
|
189
|
+
retrying requests without accidentally performing the same operation twice.
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
card = Increase::Cards.create(
|
193
|
+
{
|
194
|
+
# Card parameters
|
195
|
+
account_id: 'account_1234abcd',
|
196
|
+
description: 'My Chipotle card'
|
197
|
+
},
|
198
|
+
{
|
199
|
+
# Request headers
|
200
|
+
'Idempotency-Key': 'use a V4 UUID here'
|
201
|
+
}
|
202
|
+
)
|
203
|
+
# => {"id"=>"card_1234abcd", "type"=>"card", ... }
|
204
|
+
|
205
|
+
idempotent_replayed = card.response.headers['Idempotent-Replayed']
|
206
|
+
# => "false"
|
207
|
+
```
|
208
|
+
|
209
|
+
Reusing the key in subsequent requests will return the same response code and body as the original request along with an
|
210
|
+
additional HTTP header (Idempotent-Replayed: true). This applies to both success and error responses. In situations
|
211
|
+
where your request results in a validation error, you'll need to update your request and retry with a new idempotency
|
212
|
+
key.
|
22
213
|
|
23
214
|
## Development
|
24
215
|
|
25
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
216
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
217
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
218
|
+
|
219
|
+
You can also run `INCREASE_API_KEY=my_key_here INCREASE_BASE_URL=https://sandbox.increase.com bin/console` to run the
|
220
|
+
console with your Increase sandbox API key pre-filled.
|
221
|
+
|
222
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
26
223
|
|
27
|
-
To
|
224
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will
|
225
|
+
create a git tag for the version, push git commits and the created tag, and push the `.gem` file
|
226
|
+
to [rubygems.org](https://rubygems.org).
|
227
|
+
|
228
|
+
Alternatively, use [`gem-release`](https://github.com/svenfuchs/gem-release) and
|
229
|
+
run `gem bump --version patch|minor|major`. Then release the gem by running `bundle exec rake release`.
|
28
230
|
|
29
231
|
## Contributing
|
30
232
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
233
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/garyhtou/increase.
|
32
234
|
|
33
235
|
## License
|
34
236
|
|
35
|
-
The gem is available as open source under the terms of
|
237
|
+
The gem is available as open source under the terms of
|
238
|
+
the [MIT License](https://github.com/garyhtou/increase-ruby/blob/main/LICENSE.txt).
|
239
|
+
|
240
|
+
---
|
241
|
+
|
242
|
+
Please note that this is not an official library written by **Increase**. Its written and maintained
|
243
|
+
by [Gary Tou](https://garytou.com/) who just uses Increase at work!
|
data/lib/increase/client.rb
CHANGED
@@ -27,7 +27,8 @@ module Increase
|
|
27
27
|
Faraday.new(
|
28
28
|
url: @configuration.base_url,
|
29
29
|
headers: {
|
30
|
-
Authorization: "Bearer #{@configuration.api_key}"
|
30
|
+
Authorization: "Bearer #{@configuration.api_key}",
|
31
|
+
"User-Agent": "Increase Ruby Gem v#{Increase::VERSION} (https://github.com/garyhtou/increase-ruby)"
|
31
32
|
}
|
32
33
|
) do |f|
|
33
34
|
f.request :json
|
@@ -8,16 +8,13 @@ module Increase
|
|
8
8
|
# TODO: support Faraday config
|
9
9
|
|
10
10
|
def initialize(config = nil)
|
11
|
-
|
12
|
-
|
13
|
-
else
|
14
|
-
configure(config)
|
15
|
-
end
|
11
|
+
reset
|
12
|
+
configure(config) if config
|
16
13
|
end
|
17
14
|
|
18
15
|
def reset
|
19
16
|
@base_url = ENV["INCREASE_BASE_URL"] || Increase::PRODUCTION_URL
|
20
|
-
@api_key =
|
17
|
+
@api_key = ENV["INCREASE_API_KEY"]
|
21
18
|
@raise_api_errors = true
|
22
19
|
end
|
23
20
|
|
data/lib/increase/errors.rb
CHANGED
@@ -82,4 +82,18 @@ module Increase
|
|
82
82
|
"private_feature_error" => PrivateFeatureError,
|
83
83
|
"rate_limited_error" => RateLimitedError
|
84
84
|
}
|
85
|
+
|
86
|
+
# WebhookSignatureVerificationError is raised when a received webhook's
|
87
|
+
# signature is invalid.
|
88
|
+
class WebhookSignatureVerificationError < Error
|
89
|
+
attr_reader :signature_header
|
90
|
+
attr_reader :payload
|
91
|
+
|
92
|
+
def initialize(message = "Increase webhook signature verification failed", signature_header: nil, payload: nil)
|
93
|
+
@signature_header = signature_header
|
94
|
+
@payload = payload
|
95
|
+
|
96
|
+
super(message)
|
97
|
+
end
|
98
|
+
end
|
85
99
|
end
|
data/lib/increase/resource.rb
CHANGED
@@ -29,41 +29,170 @@ module Increase
|
|
29
29
|
name.split("::").last.gsub(/[A-Z]/, ' \0').strip
|
30
30
|
end
|
31
31
|
|
32
|
-
def self.endpoint(
|
33
|
-
if
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
def self.endpoint(name, http_method, to: :same_as_name, with: nil)
|
33
|
+
to = nil if to == :root
|
34
|
+
to = name.to_s if to == :same_as_name
|
35
|
+
to = [to].flatten.compact
|
36
|
+
with = [with].flatten.compact
|
37
|
+
|
38
|
+
raise Error, "Invalid `to`. Max of 2 elements allowed" if to.size > 2
|
39
|
+
raise Error, "Only one `to` allowed when not `with` an `id`" if to.size > 1 && !with.include?(:id)
|
40
|
+
|
41
|
+
request_method = :request
|
42
|
+
request_method = :paginated_request if with.include?(:pagination)
|
43
|
+
|
44
|
+
method =
|
45
|
+
if with.include?(:id)
|
46
|
+
# Method signature with a required `id` param
|
47
|
+
->(id, params = nil, headers = nil, &block) do
|
48
|
+
url = self.class.resource_url
|
49
|
+
url +=
|
50
|
+
if to.size == 2
|
51
|
+
"/#{to[0]}/#{id}/#{to[1]}"
|
52
|
+
elsif to.size == 1
|
53
|
+
# Default to id first
|
54
|
+
"/#{id}/#{to[0]}"
|
55
|
+
else
|
56
|
+
"/#{id}"
|
57
|
+
end
|
58
|
+
|
59
|
+
send(request_method, http_method, url, params, headers, &block)
|
60
|
+
end
|
61
|
+
else
|
62
|
+
# Method signature without a required `id` param
|
63
|
+
->(params = nil, headers = nil, &block) do
|
64
|
+
url = self.class.resource_url
|
65
|
+
url += "/#{to[0]}" if to.size == 1
|
38
66
|
|
39
|
-
|
40
|
-
|
41
|
-
|
67
|
+
send(request_method, http_method, url, params, headers, &block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Define instance method
|
72
|
+
define_method(name, &method)
|
42
73
|
|
43
|
-
|
74
|
+
# Define class method (uses default config by calling `new`)
|
75
|
+
define_singleton_method(name) do |*args, &block|
|
76
|
+
new.send(name, *args, &block)
|
77
|
+
end
|
44
78
|
end
|
45
79
|
|
46
80
|
private_class_method :endpoint
|
47
81
|
|
48
|
-
|
49
|
-
|
50
|
-
|
82
|
+
class << self
|
83
|
+
private
|
84
|
+
|
85
|
+
# These methods here are shortcuts for the `endpoint` method. They define
|
86
|
+
# commonly used endpoints. For example, nearly all resources have a `list`
|
87
|
+
# endpoint which is a `GET` request to the resource's root URL.
|
88
|
+
|
89
|
+
def create
|
90
|
+
endpoint :create, :post, to: :root
|
51
91
|
end
|
52
92
|
|
53
|
-
|
54
|
-
|
93
|
+
def list
|
94
|
+
endpoint :list, :get, to: :root, with: :pagination
|
95
|
+
end
|
96
|
+
|
97
|
+
def update
|
98
|
+
endpoint :update, :patch, to: :root, with: :id
|
99
|
+
end
|
100
|
+
|
101
|
+
def retrieve
|
102
|
+
endpoint :retrieve, :get, to: :root, with: :id
|
55
103
|
end
|
56
104
|
end
|
57
105
|
|
58
|
-
|
106
|
+
# def self.endpoint_action(method, http_method)
|
107
|
+
# define_singleton_method(method) do |*args, &block|
|
108
|
+
# new.send(:action, method, http_method, *args, &block)
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# define_method(method) do |*args, &block|
|
112
|
+
# new.send(:action, method, http_method, *args, &block)
|
113
|
+
# end
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# private_class_method :endpoint_action
|
117
|
+
#
|
118
|
+
# private
|
119
|
+
#
|
120
|
+
# def create(params = nil, headers = nil)
|
121
|
+
# request(:post, self.class.resource_url, params, headers)
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# def list(params = nil, headers = nil, &block)
|
125
|
+
# results = []
|
126
|
+
# count = 0
|
127
|
+
# limit = params&.[](:limit) || params&.[]("limit")
|
128
|
+
# if limit == :all || limit&.>(100)
|
129
|
+
# params&.delete(:limit)
|
130
|
+
# params&.delete("limit")
|
131
|
+
# end
|
132
|
+
#
|
133
|
+
# loop do
|
134
|
+
# res = request(:get, self.class.resource_url, params, headers)
|
135
|
+
# data = res["data"]
|
136
|
+
# count += data.size
|
137
|
+
# if ![nil, :all].include?(limit) && count >= limit
|
138
|
+
# data = data[0..(limit - (count - data.size) - 1)]
|
139
|
+
# end
|
140
|
+
#
|
141
|
+
# if block
|
142
|
+
# block.call(data)
|
143
|
+
# else
|
144
|
+
# results += data
|
145
|
+
# end
|
146
|
+
#
|
147
|
+
# if limit.nil? || (limit != :all && count >= limit) || res["next_cursor"].nil?
|
148
|
+
# if block
|
149
|
+
# break
|
150
|
+
# else
|
151
|
+
# return results
|
152
|
+
# end
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# params = (params || {}).merge({ cursor: res["next_cursor"] })
|
156
|
+
# end
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# def update(id, params = nil, headers = nil)
|
160
|
+
# raise Error, "id must be a string" unless id.is_a?(String)
|
161
|
+
# path = "#{self.class.resource_url}/#{id}"
|
162
|
+
# request(:patch, path, params, headers)
|
163
|
+
# end
|
164
|
+
#
|
165
|
+
# def retrieve(id, params = nil, headers = nil)
|
166
|
+
# raise Error, "id must be a string" unless id.is_a?(String)
|
167
|
+
# path = "#{self.class.resource_url}/#{id}"
|
168
|
+
# request(:get, path, params, headers)
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# # Such as for "/accounts/{account_id}/close"
|
172
|
+
# # "close" is the action.
|
173
|
+
# def action(action, http_method, id, params = nil, headers = nil)
|
174
|
+
# raise Error, "id must be a string" unless id.is_a?(String)
|
175
|
+
# path = "#{self.class.resource_url}/#{id}/#{action}"
|
176
|
+
# request(http_method, path, params, headers)
|
177
|
+
# end
|
59
178
|
|
60
179
|
private
|
61
180
|
|
62
|
-
def
|
63
|
-
|
181
|
+
def request(method, path, params = nil, headers = nil, &block)
|
182
|
+
if block
|
183
|
+
# Assume the caller wants to automatically paginate
|
184
|
+
return paginated_request(method, path, params, headers, &block)
|
185
|
+
end
|
186
|
+
|
187
|
+
if method == :post
|
188
|
+
headers = {"Content-Type" => "application/json"}.merge!(headers || {})
|
189
|
+
end
|
190
|
+
|
191
|
+
response = @client.connection.send(method, path, params, headers)
|
192
|
+
ResponseHash.new(response.body, response: response)
|
64
193
|
end
|
65
194
|
|
66
|
-
def
|
195
|
+
def paginated_request(method, path, params = nil, headers = nil, &block)
|
67
196
|
results = []
|
68
197
|
count = 0
|
69
198
|
limit = params&.[](:limit) || params&.[]("limit")
|
@@ -73,8 +202,17 @@ module Increase
|
|
73
202
|
end
|
74
203
|
|
75
204
|
loop do
|
76
|
-
res = request(
|
205
|
+
res = request(method, path, params, headers)
|
77
206
|
data = res["data"]
|
207
|
+
|
208
|
+
# Handle case where endpoint doesn't actually support pagination.
|
209
|
+
# For example, someone passes a block to `Account.create`
|
210
|
+
if data.nil?
|
211
|
+
# In this case, we'll both yield and return the response
|
212
|
+
yield res if block
|
213
|
+
return res
|
214
|
+
end
|
215
|
+
|
78
216
|
count += data.size
|
79
217
|
if ![nil, :all].include?(limit) && count >= limit
|
80
218
|
data = data[0..(limit - (count - data.size) - 1)]
|
@@ -97,34 +235,5 @@ module Increase
|
|
97
235
|
params = (params || {}).merge({cursor: res["next_cursor"]})
|
98
236
|
end
|
99
237
|
end
|
100
|
-
|
101
|
-
def update(id, params = nil, headers = nil)
|
102
|
-
raise Error, "id must be a string" unless id.is_a?(String)
|
103
|
-
path = "#{self.class.resource_url}/#{id}"
|
104
|
-
request(:patch, path, params, headers)
|
105
|
-
end
|
106
|
-
|
107
|
-
def retrieve(id, params = nil, headers = nil)
|
108
|
-
raise Error, "id must be a string" unless id.is_a?(String)
|
109
|
-
path = "#{self.class.resource_url}/#{id}"
|
110
|
-
request(:get, path, params, headers)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Such as for "/accounts/{account_id}/close"
|
114
|
-
# "close" is the action.
|
115
|
-
def action(action, http_method, id, params = nil, headers = nil)
|
116
|
-
raise Error, "id must be a string" unless id.is_a?(String)
|
117
|
-
path = "#{self.class.resource_url}/#{id}/#{action}"
|
118
|
-
request(http_method, path, params, headers)
|
119
|
-
end
|
120
|
-
|
121
|
-
def request(method, path, params = nil, headers = nil)
|
122
|
-
if method == :post
|
123
|
-
headers = {"Content-Type" => "application/json"}.merge!(headers || {})
|
124
|
-
end
|
125
|
-
|
126
|
-
response = @client.connection.send(method, path, params, headers)
|
127
|
-
ResponseHash.new(response.body, response: response)
|
128
|
-
end
|
129
238
|
end
|
130
239
|
end
|
@@ -4,10 +4,10 @@ require "increase/resource"
|
|
4
4
|
|
5
5
|
module Increase
|
6
6
|
class AccountTransfers < Resource
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
endpoint :approve,
|
11
|
-
endpoint :cancel,
|
7
|
+
create
|
8
|
+
list
|
9
|
+
retrieve
|
10
|
+
endpoint :approve, :post, with: :id
|
11
|
+
endpoint :cancel, :post, with: :id
|
12
12
|
end
|
13
13
|
end
|
@@ -4,10 +4,10 @@ require "increase/resource"
|
|
4
4
|
|
5
5
|
module Increase
|
6
6
|
class Accounts < Resource
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
endpoint :close,
|
7
|
+
create
|
8
|
+
list
|
9
|
+
update
|
10
|
+
retrieve
|
11
|
+
endpoint :close, :post, with: :id
|
12
12
|
end
|
13
13
|
end
|
@@ -4,10 +4,10 @@ require "increase/resource"
|
|
4
4
|
|
5
5
|
module Increase
|
6
6
|
class AchTransfers < Resource
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
endpoint :approve,
|
11
|
-
endpoint :cancel,
|
7
|
+
create
|
8
|
+
list
|
9
|
+
retrieve
|
10
|
+
endpoint :approve, :post, with: :id
|
11
|
+
endpoint :cancel, :post, with: :id
|
12
12
|
end
|
13
13
|
end
|
@@ -4,10 +4,10 @@ require "increase/resource"
|
|
4
4
|
|
5
5
|
module Increase
|
6
6
|
class Cards < Resource
|
7
|
-
|
8
|
-
|
9
|
-
endpoint :details,
|
10
|
-
|
11
|
-
|
7
|
+
create
|
8
|
+
list
|
9
|
+
endpoint :details, :get, with: :id
|
10
|
+
update
|
11
|
+
retrieve
|
12
12
|
end
|
13
13
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "increase/resource"
|
4
|
+
|
5
|
+
module Increase
|
6
|
+
class CheckTransfers < Resource
|
7
|
+
create
|
8
|
+
list
|
9
|
+
retrieve
|
10
|
+
endpoint :approve, :post, with: :id
|
11
|
+
endpoint :cancel, :post, with: :id
|
12
|
+
endpoint :stop_payment, :post, with: :id
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Increase
|
2
|
+
module Util
|
3
|
+
# Constant time string comparison to prevent timing attacks
|
4
|
+
# Code borrowed from `stripe-ruby`, which was borrowed from ActiveSupport
|
5
|
+
def self.secure_compare(a, b)
|
6
|
+
return false unless a.bytesize == b.bytesize
|
7
|
+
|
8
|
+
l = a.unpack "C#{a.bytesize}"
|
9
|
+
|
10
|
+
res = 0
|
11
|
+
b.each_byte { |byte| res |= byte ^ l.shift }
|
12
|
+
res.zero?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/increase/version.rb
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "increase/util"
|
2
|
+
require "increase/errors"
|
3
|
+
|
4
|
+
module Increase
|
5
|
+
# Keeping this module singular in case Increase adds a `webhooks` resource
|
6
|
+
module Webhook
|
7
|
+
module Signature
|
8
|
+
DEFAULT_TIME_TOLERANCE = 300 # 300 seconds (5 minutes)
|
9
|
+
DEFAULT_SCHEME = "v1"
|
10
|
+
|
11
|
+
def self.verify?(payload:, signature_header:, secret:, scheme: DEFAULT_SCHEME, time_tolerance: DEFAULT_TIME_TOLERANCE)
|
12
|
+
# Helper for raising errors with additional metadata
|
13
|
+
sig_error = ->(msg) do
|
14
|
+
WebhookSignatureVerificationError.new(msg, signature_header: signature_header, payload: payload)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Parse header
|
18
|
+
sig_values = signature_header.split(",").map { |pair| pair.split("=") }.to_h
|
19
|
+
|
20
|
+
# Extract values
|
21
|
+
t = sig_values["t"] # Should be a string (ISO-8601 timestamp)
|
22
|
+
sig = sig_values[scheme]
|
23
|
+
raise sig_error.call("No timestamp found in signature header") if t.nil?
|
24
|
+
raise sig_error.call("No signature found with scheme #{scheme} in signature header") if sig.nil?
|
25
|
+
|
26
|
+
# Check signature
|
27
|
+
expected_sig = compute_signature(timestamp: t, payload: payload, secret: secret)
|
28
|
+
matches = Util.secure_compare(expected_sig, sig)
|
29
|
+
raise sig_error.call("Signature mismatch") unless matches
|
30
|
+
|
31
|
+
# Check timestamp tolerance to prevent timing attacks
|
32
|
+
if time_tolerance > 0
|
33
|
+
begin
|
34
|
+
timestamp = DateTime.parse(t)
|
35
|
+
now = DateTime.now
|
36
|
+
diff = (now - timestamp) * 24 * 60 * 60 # in seconds
|
37
|
+
|
38
|
+
# Don't allow timestamps in the future
|
39
|
+
if diff > time_tolerance || diff < 0
|
40
|
+
raise sig_error.call("Timestamp outside of the tolerance zone")
|
41
|
+
end
|
42
|
+
rescue Date::Error
|
43
|
+
raise sig_error.call("Invalid timestamp in signature header: #{t}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
true
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.compute_signature(timestamp:, payload:, secret:)
|
51
|
+
signed_payload = timestamp.to_s + "." + payload.to_s
|
52
|
+
OpenSSL::HMAC.hexdigest("SHA256", secret, signed_payload)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/increase.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: increase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary Tou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.3.0
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: securecompare
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -147,12 +133,16 @@ files:
|
|
147
133
|
- lib/increase/resources/accounts.rb
|
148
134
|
- lib/increase/resources/ach_transfers.rb
|
149
135
|
- lib/increase/resources/cards.rb
|
136
|
+
- lib/increase/resources/check_transfers.rb
|
150
137
|
- lib/increase/resources/events.rb
|
138
|
+
- lib/increase/resources/limits.rb
|
151
139
|
- lib/increase/resources/pending_transactions.rb
|
140
|
+
- lib/increase/resources/routing_numbers.rb
|
152
141
|
- lib/increase/resources/transactions.rb
|
153
142
|
- lib/increase/response_hash.rb
|
143
|
+
- lib/increase/util.rb
|
154
144
|
- lib/increase/version.rb
|
155
|
-
- lib/increase/
|
145
|
+
- lib/increase/webhook/signature.rb
|
156
146
|
- sig/increase.rbs
|
157
147
|
homepage: https://github.com/garyhtou/increase-ruby
|
158
148
|
licenses:
|
@@ -169,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
169
159
|
requirements:
|
170
160
|
- - ">="
|
171
161
|
- !ruby/object:Gem::Version
|
172
|
-
version: 2.
|
162
|
+
version: 2.7.4
|
173
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
164
|
requirements:
|
175
165
|
- - ">="
|
data/lib/increase/webhooks.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require "openssl"
|
2
|
-
require "securecompare"
|
3
|
-
|
4
|
-
module Increase
|
5
|
-
class Webhooks
|
6
|
-
def self.verify?(payload:, signature_header:, secret:, scheme: "v1")
|
7
|
-
sig_values = signature_header.split(",").map { |pair| pair.split("=") }
|
8
|
-
sig_values = sig_values.to_h
|
9
|
-
|
10
|
-
signed_payload = sig_values["t"] + "." + payload.to_s
|
11
|
-
|
12
|
-
expected_sig = OpenSSL::HMAC.hexdigest("SHA256", secret, signed_payload)
|
13
|
-
SecureCompare.compare(expected_sig, sig_values["v1"])
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|