wajub 1.1.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +83 -0
- data/README.md +91 -116
- data/lib/wajub/errors.rb +30 -13
- data/lib/wajub/http/base_client.rb +8 -0
- data/lib/wajub/http/http_utils.rb +17 -0
- data/lib/wajub/http/retry_policy.rb +14 -0
- data/lib/wajub/resources.rb +110 -32
- data/lib/wajub/version.rb +1 -1
- data/lib/wajub/webhooks.rb +22 -4
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 39630ba4b905b7b327497eed4892d544935f43cbec061955453fa13663519da9
|
|
4
|
+
data.tar.gz: b51e523f15ba315aeaef028067886c121a87232d8d5d7010e3f26f185cf76ca6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a95b30792066aaf19bbded87a2ce674dd2548ecb8b785a491d348f9661d701238be99effd522bbc6ed923877ada5df8e58303f03fc946e72f650913cdb26596b
|
|
7
|
+
data.tar.gz: 56fa9971254484f578696c290837f1b9ca73025f48dc38e872aee5ca397e2b1242fd1788a110d5cae77f49b3a50e3582c2b7683ab623bd10827858bf1e65ef64
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the `wajub` gem are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
|
+
`wajub` uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## [1.4.0] — 2026-10-15
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- **`field_errors` on API errors, every validation message of each field.** A
|
|
14
|
+
`422` can give a field several messages; `errors` keeps only the first one.
|
|
15
|
+
`e.field_errors['amount']` lists them all, in the order the API sent them.
|
|
16
|
+
`errors` is unchanged.
|
|
17
|
+
- **`invoices.send_invoice`** sends an invoice to its customer. `invoices.send`
|
|
18
|
+
still does, but it is also the name of Ruby's `send`: prefer `send_invoice`.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
- **`invoices.send` with a Symbol calls that method**, as Ruby's `send` does,
|
|
22
|
+
for code and libraries that call methods by name.
|
|
23
|
+
`invoices.send(:retrieve, id)` used to try to send an invoice whose id is
|
|
24
|
+
`retrieve`. A String is still an invoice id, whatever it reads.
|
|
25
|
+
- **A `nil` id raises `ArgumentError`** before anything is sent. It used to
|
|
26
|
+
send the request without the id: `payments.retrieve(nil)` listed payments.
|
|
27
|
+
|
|
28
|
+
### Deprecated
|
|
29
|
+
- **`listen.config` and `listen.auth`.** The API no longer offers them. To
|
|
30
|
+
receive webhooks on your machine, forward them with the Wajub CLI:
|
|
31
|
+
`wajub listen`. Both warn when Ruby shows deprecation warnings
|
|
32
|
+
(`-W:deprecated`).
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
- **A space in an id goes out as `%20`.** It went out as `+`, which the API
|
|
36
|
+
reads as a plus sign.
|
|
37
|
+
- **`webhooks.construct_event` keeps the replay window.** A tolerance of `0` or
|
|
38
|
+
less switched the timestamp check off, so an old delivery sent again was
|
|
39
|
+
accepted. A tolerance that is not a positive number now keeps the default of
|
|
40
|
+
300 seconds; `nil` used to raise `NoMethodError`.
|
|
41
|
+
|
|
42
|
+
### Security
|
|
43
|
+
- **An id that is not a Wajub id is refused before anything is sent.** If an
|
|
44
|
+
id you pass can come from a URL or a form, a call could act on another
|
|
45
|
+
resource of your account: `customers.delete('cus_1/tax_ids/txi_1')` deleted a
|
|
46
|
+
tax ID. An id that is empty, `.` or `..`, or contains `/`, now raises
|
|
47
|
+
`ArgumentError`. Wajub ids never take these forms, so valid calls are
|
|
48
|
+
unchanged.
|
|
49
|
+
|
|
50
|
+
## [1.3.0] — 2026-10-12
|
|
51
|
+
|
|
52
|
+
1.2.1 was not published to RubyGems: its changes ship in this release.
|
|
53
|
+
|
|
54
|
+
### Added
|
|
55
|
+
- Tax and Shield methods:
|
|
56
|
+
- `tax.reports`, `tax.list_codes`, `tax.retrieve_code`, `tax.jurisdictions`,
|
|
57
|
+
`tax.thresholds` and `tax.threshold_alerts`;
|
|
58
|
+
- `tax.list_registrations` (a page), `tax.create_registration`,
|
|
59
|
+
`tax.retrieve_registration`, `tax.update_registration` and
|
|
60
|
+
`tax.delete_registration`;
|
|
61
|
+
- `shield.list_blocklist`, `shield.add_to_blocklist` and
|
|
62
|
+
`shield.remove_from_blocklist`.
|
|
63
|
+
|
|
64
|
+
The README listed them, and calling one raised `NoMethodError`.
|
|
65
|
+
|
|
66
|
+
### Fixed
|
|
67
|
+
- `customers.delete_tax_id` deletes the customer's tax id. It answered `404`.
|
|
68
|
+
- `webhooks.construct_event` with a missing `X-Wajub-Signature` or
|
|
69
|
+
`X-Wajub-Timestamp` header (`nil`) raised `NoMethodError`, which a handler
|
|
70
|
+
rescuing `WebhookSignatureVerificationError` let through as a 500. It now
|
|
71
|
+
raises `WebhookSignatureVerificationError`, as does a timestamp that is not a
|
|
72
|
+
number.
|
|
73
|
+
|
|
74
|
+
### Changed
|
|
75
|
+
- **A create sent again with the same idempotency key while the first is still
|
|
76
|
+
running** now returns the first request's result instead of a `409` error.
|
|
77
|
+
|
|
78
|
+
## [1.2.0] — 2026-10-01
|
|
79
|
+
|
|
80
|
+
Not published to RubyGems. Its one change ships with 1.3.0.
|
|
81
|
+
|
|
82
|
+
### Removed
|
|
83
|
+
- `payments.initialize_payment`, an alias of `payments.create`. Call `create`.
|
data/README.md
CHANGED
|
@@ -4,33 +4,24 @@
|
|
|
4
4
|
[](https://www.ruby-lang.org/)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
|
|
7
|
-
Official
|
|
7
|
+
Official server-side SDK for the [Wajub API](https://docs.wajub.com): create payments, confirm them, pay out and verify webhooks from your Ruby backend.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- Resource-oriented API (`client.payments`, `client.customers`, …)
|
|
14
|
-
- Automatic `Idempotency-Key` on mutating requests (override per call)
|
|
15
|
-
- Typed errors per HTTP status (`AuthenticationError`, `RateLimitError`, …)
|
|
16
|
-
- Automatic retries on 429 and 5xx (max 2, exponential backoff)
|
|
17
|
-
- Page-based pagination with `auto_paging_each` and `next_page`
|
|
18
|
-
- Webhook signature verification (HMAC-SHA256, timestamp tolerance)
|
|
19
|
-
- Zero third-party runtime dependencies (stdlib only)
|
|
9
|
+
It runs on your server with a secret (`sk.…`, `sk_test.…`) or restricted (`rk.…`, `rk_test.…`) API key. Never expose these keys in client-side code: in the browser, use [Wajub.js](https://docs.wajub.com/libraries/components/js) with your publishable key (`pk.…`, `pk_test.…`).
|
|
20
10
|
|
|
21
11
|
## Requirements
|
|
22
12
|
|
|
23
13
|
| Requirement | Version |
|
|
24
14
|
|-------------|---------|
|
|
25
15
|
| Ruby | 3.1 or later |
|
|
26
|
-
|
|
16
|
+
|
|
17
|
+
No other gem is needed.
|
|
27
18
|
|
|
28
19
|
## Installation
|
|
29
20
|
|
|
30
21
|
Add to your Gemfile:
|
|
31
22
|
|
|
32
23
|
```ruby
|
|
33
|
-
gem 'wajub', '~> 1.
|
|
24
|
+
gem 'wajub', '~> 1.4'
|
|
34
25
|
```
|
|
35
26
|
|
|
36
27
|
Or install directly:
|
|
@@ -41,67 +32,94 @@ gem install wajub
|
|
|
41
32
|
|
|
42
33
|
## Quick start
|
|
43
34
|
|
|
44
|
-
Amounts are
|
|
45
|
-
|
|
46
|
-
### Redirect checkout
|
|
35
|
+
Amounts are in the currency's **main unit**: `12.50` for GHS 12.50, `5000` for XAF 5,000.
|
|
47
36
|
|
|
48
37
|
```ruby
|
|
49
38
|
require 'wajub'
|
|
50
39
|
|
|
51
|
-
client = Wajub::Client.new(
|
|
40
|
+
client = Wajub::Client.new(
|
|
41
|
+
api_key: ENV.fetch('WAJUB_API_KEY'),
|
|
42
|
+
webhook_secret: ENV.fetch('WAJUB_WEBHOOK_SECRET')
|
|
43
|
+
)
|
|
52
44
|
|
|
53
45
|
payment = client.payments.create(
|
|
54
46
|
'amount' => 15_000,
|
|
55
47
|
'currency' => 'XAF',
|
|
56
48
|
'email' => 'buyer@example.com',
|
|
49
|
+
'reference' => 'order-1042',
|
|
57
50
|
'callback' => 'https://shop.example.com/order/complete'
|
|
58
51
|
)
|
|
59
52
|
|
|
60
|
-
|
|
53
|
+
# Redirect the customer to payment.authorization_url,
|
|
54
|
+
# or mount payment.authorization_token with Wajub.js in your page.
|
|
61
55
|
```
|
|
62
56
|
|
|
63
|
-
|
|
57
|
+
Then confirm the result on your server: handle the `payment.succeeded` webhook, or call `client.payments.retrieve(payment.id)`.
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
'currency' => 'XAF',
|
|
69
|
-
'metadata' => { 'mode' => 'embed' }
|
|
70
|
-
)
|
|
59
|
+
Parameters go in as a hash with the API's field names. `create` and `retrieve` return an object whose fields read as methods (`payment.authorization_url`) or with brackets (`payment['authorization_url']`). Use brackets for an optional field: a method raises `NoMethodError` when the field is absent, brackets return `nil`.
|
|
60
|
+
|
|
61
|
+
## Configuration
|
|
71
62
|
|
|
72
|
-
|
|
63
|
+
| Keyword | Description |
|
|
64
|
+
|---------|-------------|
|
|
65
|
+
| `api_key` | Secret or restricted API key (`sk.…`, `sk_test.…`, `rk.…`, `rk_test.…`). Falls back to `WAJUB_API_KEY`. |
|
|
66
|
+
| `webhook_secret` | Webhook signing secret (`whsec_…`), needed only for `webhooks.construct_event`. Falls back to `WAJUB_WEBHOOK_SECRET`. |
|
|
67
|
+
|
|
68
|
+
An empty key raises `ArgumentError` when the client is built.
|
|
69
|
+
|
|
70
|
+
## Resources
|
|
71
|
+
|
|
72
|
+
| Purpose | Property | Methods |
|
|
73
|
+
|---------|----------|---------|
|
|
74
|
+
| Payments | `client.payments` | `create`, `retrieve`, `list`, `cancel`, `process`, `process_split`, `list_refunds` |
|
|
75
|
+
| Refunds | `client.refunds` | `create`, `retrieve`, `list` |
|
|
76
|
+
| Customers | `client.customers` | `create`, `retrieve`, `update`, `delete`, `list`, `block`, `unblock`, `activate`, `deactivate`, `list_tax_ids`, `create_tax_id`, `delete_tax_id` |
|
|
77
|
+
| Payment links | `client.links` | `create`, `retrieve`, `update`, `delete`, `list` |
|
|
78
|
+
| Invoices | `client.invoices` | `create`, `retrieve`, `update`, `delete`, `list`, `send_invoice`, `mark_paid`, `cancel` |
|
|
79
|
+
| Payouts | `client.transfers` | `create`, `retrieve`, `list` |
|
|
80
|
+
| Payout recipients | `client.beneficiaries` | `create`, `retrieve`, `update`, `delete`, `list` |
|
|
81
|
+
| Destination checks | `client.identity` | `resolve`, `validate` |
|
|
82
|
+
| Balance | `client.balance` | `retrieve` |
|
|
83
|
+
| Disputes | `client.disputes` | `list`, `retrieve`, `submit_evidence`, `accept`, `close`, `send_message` |
|
|
84
|
+
| Events | `client.events` | `list`, `retrieve`, `resend` |
|
|
85
|
+
| Webhook endpoints | `client.webhook_endpoints` | `create`, `retrieve`, `update`, `delete`, `list`, `rotate_secret` |
|
|
86
|
+
| Webhook verification | `client.webhooks` | `construct_event` |
|
|
87
|
+
| Connected accounts (Sync) | `client.accounts` | `create`, `retrieve`, `update`, `delete`, `list`, `regenerate_token` |
|
|
88
|
+
| Tax | `client.tax` | `get_settings`, `update_settings`, `rates`, `calculate`, `reports`, `list_codes`, `retrieve_code`, `list_registrations`, `create_registration`, `retrieve_registration`, `update_registration`, `delete_registration`, `jurisdictions`, `thresholds`, `threshold_alerts` |
|
|
89
|
+
| Fraud (Shield) | `client.shield` | `get_settings`, `update_settings`, `stats`, `list_blocklist`, `add_to_blocklist`, `remove_from_blocklist` |
|
|
90
|
+
| Reference data | `client.global` | `ping`, `channels`, `countries`, `currencies` |
|
|
91
|
+
|
|
92
|
+
## Acting for a connected account
|
|
93
|
+
|
|
94
|
+
Pass the connected account with `sync` in the call's options:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
client.payments.create(params, Wajub::RequestOptions.new(sync: 'acct_sync_ref'))
|
|
73
98
|
```
|
|
74
99
|
|
|
75
|
-
|
|
100
|
+
## Webhooks
|
|
76
101
|
|
|
77
|
-
|
|
102
|
+
Pass the **raw request body**, not `params`, with the `X-Wajub-Signature` and `X-Wajub-Timestamp` headers. In Rails, build the client once in an initializer and share it:
|
|
78
103
|
|
|
79
104
|
```ruby
|
|
80
105
|
# config/initializers/wajub.rb
|
|
81
|
-
|
|
82
|
-
api_key: Rails.application.credentials.dig(:wajub, :api_key) || ENV['WAJUB_API_KEY'],
|
|
83
|
-
webhook_secret: ENV['WAJUB_WEBHOOK_SECRET']
|
|
84
|
-
)
|
|
106
|
+
WAJUB = Wajub::Client.new # reads WAJUB_API_KEY and WAJUB_WEBHOOK_SECRET
|
|
85
107
|
```
|
|
86
108
|
|
|
87
|
-
### Webhook controller
|
|
88
|
-
|
|
89
|
-
Use the **raw request body**:
|
|
90
|
-
|
|
91
109
|
```ruby
|
|
92
|
-
class
|
|
110
|
+
class WajubWebhooksController < ApplicationController
|
|
93
111
|
skip_before_action :verify_authenticity_token
|
|
94
112
|
|
|
95
|
-
def
|
|
96
|
-
event =
|
|
97
|
-
request.
|
|
113
|
+
def create
|
|
114
|
+
event = WAJUB.webhooks.construct_event(
|
|
115
|
+
request.raw_post,
|
|
98
116
|
request.headers['X-Wajub-Signature'],
|
|
99
117
|
request.headers['X-Wajub-Timestamp']
|
|
100
118
|
)
|
|
101
119
|
|
|
102
|
-
case event['
|
|
120
|
+
case event['event']
|
|
103
121
|
when 'payment.succeeded'
|
|
104
|
-
#
|
|
122
|
+
# fulfil the order
|
|
105
123
|
end
|
|
106
124
|
|
|
107
125
|
head :ok
|
|
@@ -111,82 +129,26 @@ class WebhooksController < ApplicationController
|
|
|
111
129
|
end
|
|
112
130
|
```
|
|
113
131
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
| Variable | Description |
|
|
117
|
-
|----------|-------------|
|
|
118
|
-
| `WAJUB_API_KEY` | Secret or restricted API key (`sk_`, `sk_test.`, `rk_`, …) |
|
|
119
|
-
| `WAJUB_WEBHOOK_SECRET` | Webhook signing secret (`whsec_`) for `construct_event()` |
|
|
120
|
-
|
|
121
|
-
Test mode is selected by your API key prefix (`sk_test.…`), not by the API URL. Production calls always go to `https://api.wajub.com`.
|
|
122
|
-
|
|
123
|
-
## Resources (merchant API)
|
|
124
|
-
|
|
125
|
-
| Property | Methods |
|
|
126
|
-
|----------|---------|
|
|
127
|
-
| `client.global` | `ping`, `channels`, `countries`, `currencies` |
|
|
128
|
-
| `client.payments` | `create`, `initialize_payment`, `retrieve`, `list`, `cancel`, `process`, `process_split`, `list_refunds` |
|
|
129
|
-
| `client.customers` | `create`, `retrieve`, `update`, `delete`, `list`, `block`, `unblock`, `activate`, `deactivate`, `list_tax_ids`, `create_tax_id`, `delete_tax_id` |
|
|
130
|
-
| `client.refunds` | `create`, `retrieve`, `list` |
|
|
131
|
-
| `client.transfers` | `create`, `retrieve`, `list` |
|
|
132
|
-
| `client.beneficiaries` | `create`, `retrieve`, `update`, `delete`, `list` |
|
|
133
|
-
| `client.links` | `create`, `retrieve`, `update`, `delete`, `list` |
|
|
134
|
-
| `client.invoices` | `create`, `retrieve`, `update`, `delete`, `list`, `send`, `mark_paid`, `cancel` |
|
|
135
|
-
| `client.accounts` | `create`, `retrieve`, `update`, `delete`, `list`, `regenerate_token` |
|
|
136
|
-
| `client.webhook_endpoints` | `create`, `retrieve`, `update`, `delete`, `list`, `rotate_secret` |
|
|
137
|
-
| `client.balance` | `retrieve` |
|
|
138
|
-
| `client.events` | `list`, `retrieve`, `resend` |
|
|
139
|
-
| `client.disputes` | `list`, `retrieve`, `submit_evidence`, `accept`, `close`, `send_message` |
|
|
140
|
-
| `client.identity` | `resolve`, `validate` |
|
|
141
|
-
| `client.tax` | `get_settings`, `update_settings`, `rates`, `calculate`, `reports`, `list_codes`, `retrieve_code`, `list_registrations`, `create_registration`, `retrieve_registration`, `update_registration`, `delete_registration`, `jurisdictions`, `thresholds`, `threshold_alerts` |
|
|
142
|
-
| `client.shield` | `get_settings`, `update_settings`, `stats`, `list_blocklist`, `add_to_blocklist`, `remove_from_blocklist` |
|
|
143
|
-
| `client.listen` | `config`, `auth` |
|
|
144
|
-
| `client.webhooks` | `construct_event` (local — no HTTP) |
|
|
145
|
-
|
|
146
|
-
## Sync (Connect)
|
|
147
|
-
|
|
148
|
-
```ruby
|
|
149
|
-
client.payments.create(params, Wajub::RequestOptions.new(sync: 'acct_sync_ref'))
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
## Webhooks
|
|
153
|
-
|
|
154
|
-
```ruby
|
|
155
|
-
begin
|
|
156
|
-
event = client.webhooks.construct_event(
|
|
157
|
-
request.body.read, # String — raw body bytes
|
|
158
|
-
request.env['HTTP_X_WAJUB_SIGNATURE'],
|
|
159
|
-
request.env['HTTP_X_WAJUB_TIMESTAMP']
|
|
160
|
-
)
|
|
161
|
-
rescue Wajub::WebhookSignatureVerificationError
|
|
162
|
-
halt 400
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
case event['type']
|
|
166
|
-
when 'payment.succeeded'
|
|
167
|
-
# fulfill order
|
|
168
|
-
end
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
During local development, use the [Wajub CLI](https://github.com/wajubhq/wajub-cli) to forward webhooks to your machine.
|
|
132
|
+
A delivery that does not verify, or that lacks one of the headers, raises `Wajub::WebhookSignatureVerificationError`. If your server's clock drifts, pass a tolerance in seconds as a fourth argument.
|
|
172
133
|
|
|
173
134
|
## Pagination
|
|
174
135
|
|
|
136
|
+
`list` returns a page you can walk one page at a time, or iterate across all pages. Its rows are plain hashes:
|
|
137
|
+
|
|
175
138
|
```ruby
|
|
176
139
|
page = client.payments.list('per_page' => 50)
|
|
140
|
+
puts page.data, page.meta, page.has_more
|
|
141
|
+
|
|
142
|
+
next_page = page.next_page if page.has_more
|
|
177
143
|
|
|
178
144
|
page.auto_paging_each do |payment|
|
|
179
145
|
puts "#{payment['id']} #{payment['status']}"
|
|
180
146
|
end
|
|
181
|
-
|
|
182
|
-
# Manual page control
|
|
183
|
-
first = client.payments.list
|
|
184
|
-
second = first.next_page if first.has_more
|
|
185
147
|
```
|
|
186
148
|
|
|
187
149
|
## Idempotency
|
|
188
150
|
|
|
189
|
-
|
|
151
|
+
Temporary failures are retried for you. To make a create safe to repeat on your side too, after a restart or in a job retry, pass your own key:
|
|
190
152
|
|
|
191
153
|
```ruby
|
|
192
154
|
client.payments.create(
|
|
@@ -197,28 +159,41 @@ client.payments.create(
|
|
|
197
159
|
|
|
198
160
|
## Error handling
|
|
199
161
|
|
|
162
|
+
API errors raise a subclass of `Wajub::WajubError`:
|
|
163
|
+
|
|
200
164
|
```ruby
|
|
201
165
|
begin
|
|
202
166
|
client.payments.create(params)
|
|
203
167
|
rescue Wajub::InvalidRequestError => e
|
|
204
|
-
puts e.
|
|
168
|
+
puts e.field_errors # every validation message, by field
|
|
205
169
|
rescue Wajub::AuthenticationError
|
|
206
|
-
#
|
|
207
|
-
rescue Wajub::RateLimitError
|
|
208
|
-
|
|
170
|
+
# check your API key
|
|
171
|
+
rescue Wajub::RateLimitError => e
|
|
172
|
+
puts e.retry_after # seconds to wait
|
|
173
|
+
rescue Wajub::WajubError => e
|
|
174
|
+
puts e.code, e.http_status, e.message
|
|
209
175
|
end
|
|
210
176
|
```
|
|
211
177
|
|
|
212
|
-
|
|
178
|
+
| Class | Raised on |
|
|
179
|
+
|-------|-----------|
|
|
180
|
+
| `Wajub::AuthenticationError` | `401` |
|
|
181
|
+
| `Wajub::PermissionError` | `403` |
|
|
182
|
+
| `Wajub::NotFoundError` | `404` |
|
|
183
|
+
| `Wajub::InvalidRequestError` | `400`, `422` |
|
|
184
|
+
| `Wajub::RateLimitError` | `429`, with `retry_after` in seconds |
|
|
185
|
+
| `Wajub::ApiConnectionError` | No response: network error or timeout |
|
|
186
|
+
| `Wajub::WajubError` | Any other error status, and the parent of all of the above |
|
|
213
187
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
188
|
+
Every error carries `message`, `code`, `http_status`, `field_errors` (every validation message of each field), `errors` (the first message of each field) and `raw` (the response body).
|
|
189
|
+
|
|
190
|
+
## Testing
|
|
191
|
+
|
|
192
|
+
Use a sandbox key (`sk_test.…`): the key chooses the environment, and no real money moves. The test phone numbers and cards are on [Test scenarios](https://docs.wajub.com/developer-tools/testing/test-scenarios). To receive webhooks on your machine, forward them with the [Wajub CLI](https://docs.wajub.com/developer-tools/cli/listen).
|
|
218
193
|
|
|
219
194
|
## Documentation & support
|
|
220
195
|
|
|
221
|
-
-
|
|
196
|
+
- Documentation: [docs.wajub.com/libraries/sdks/ruby](https://docs.wajub.com/libraries/sdks/ruby)
|
|
222
197
|
- Report issues: [github.com/wajubhq/wajub-ruby/issues](https://github.com/wajubhq/wajub-ruby/issues)
|
|
223
198
|
|
|
224
199
|
## License
|
data/lib/wajub/errors.rb
CHANGED
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
module Wajub
|
|
4
4
|
class WajubError < StandardError
|
|
5
|
-
|
|
5
|
+
# errors holds the first message of each field; field_errors holds every
|
|
6
|
+
# message of each field, as an Array, in the order the API sent them.
|
|
7
|
+
attr_reader :code, :http_status, :errors, :field_errors, :raw
|
|
6
8
|
|
|
7
|
-
def initialize(message, code:, http_status:, errors: nil, raw: nil)
|
|
9
|
+
def initialize(message, code:, http_status:, errors: nil, raw: nil, field_errors: nil)
|
|
8
10
|
super(message)
|
|
9
11
|
@code = code
|
|
10
12
|
@http_status = http_status
|
|
11
13
|
@errors = errors
|
|
14
|
+
@field_errors = field_errors || (errors.transform_values { |value| value.is_a?(Array) ? value : [value] } if errors.is_a?(Hash))
|
|
12
15
|
@raw = raw
|
|
13
16
|
end
|
|
14
17
|
end
|
|
@@ -21,15 +24,15 @@ module Wajub
|
|
|
21
24
|
class RateLimitError < WajubError
|
|
22
25
|
attr_reader :retry_after
|
|
23
26
|
|
|
24
|
-
def initialize(message, code:, http_status:, errors: nil, raw: nil, retry_after: nil)
|
|
25
|
-
super(message, code: code, http_status: http_status, errors: errors, raw: raw)
|
|
27
|
+
def initialize(message, code:, http_status:, errors: nil, raw: nil, retry_after: nil, field_errors: nil)
|
|
28
|
+
super(message, code: code, http_status: http_status, errors: errors, raw: raw, field_errors: field_errors)
|
|
26
29
|
@retry_after = retry_after
|
|
27
30
|
end
|
|
28
31
|
end
|
|
29
32
|
|
|
30
33
|
class ApiConnectionError < WajubError
|
|
31
|
-
def initialize(message, code: 'network_error', http_status: 0, errors: nil, raw: nil)
|
|
32
|
-
super(message, code: code, http_status: http_status, errors: errors, raw: raw)
|
|
34
|
+
def initialize(message, code: 'network_error', http_status: 0, errors: nil, raw: nil, field_errors: nil)
|
|
35
|
+
super(message, code: code, http_status: http_status, errors: errors, raw: raw, field_errors: field_errors)
|
|
33
36
|
end
|
|
34
37
|
end
|
|
35
38
|
|
|
@@ -41,15 +44,21 @@ module Wajub
|
|
|
41
44
|
def from_response(status, body, retry_after: nil)
|
|
42
45
|
message = body['message'] || "Request failed (#{status})"
|
|
43
46
|
code = body['error_code'] || body['code'] || "http_#{status}"
|
|
44
|
-
|
|
47
|
+
details = {
|
|
48
|
+
code: code,
|
|
49
|
+
http_status: status,
|
|
50
|
+
errors: parse_field_errors(body['errors']),
|
|
51
|
+
field_errors: parse_all_field_errors(body['errors']),
|
|
52
|
+
raw: body
|
|
53
|
+
}
|
|
45
54
|
|
|
46
55
|
case status
|
|
47
|
-
when 401 then AuthenticationError.new(message,
|
|
48
|
-
when 403 then PermissionError.new(message,
|
|
49
|
-
when 404 then NotFoundError.new(message,
|
|
50
|
-
when 429 then RateLimitError.new(message,
|
|
51
|
-
when 400, 422 then InvalidRequestError.new(message,
|
|
52
|
-
else WajubError.new(message,
|
|
56
|
+
when 401 then AuthenticationError.new(message, **details)
|
|
57
|
+
when 403 then PermissionError.new(message, **details)
|
|
58
|
+
when 404 then NotFoundError.new(message, **details)
|
|
59
|
+
when 429 then RateLimitError.new(message, **details, retry_after: retry_after)
|
|
60
|
+
when 400, 422 then InvalidRequestError.new(message, **details)
|
|
61
|
+
else WajubError.new(message, **details)
|
|
53
62
|
end
|
|
54
63
|
end
|
|
55
64
|
|
|
@@ -60,5 +69,13 @@ module Wajub
|
|
|
60
69
|
value.is_a?(Array) ? value.first.to_s : value.to_s
|
|
61
70
|
end
|
|
62
71
|
end
|
|
72
|
+
|
|
73
|
+
def parse_all_field_errors(raw)
|
|
74
|
+
return nil unless raw.is_a?(Hash)
|
|
75
|
+
|
|
76
|
+
raw.transform_values do |value|
|
|
77
|
+
value.is_a?(Array) ? value.map(&:to_s) : [value.to_s]
|
|
78
|
+
end
|
|
79
|
+
end
|
|
63
80
|
end
|
|
64
81
|
end
|
|
@@ -89,6 +89,14 @@ module Wajub
|
|
|
89
89
|
next
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
+
# The same key still being processed: the first request's answer is
|
|
93
|
+
# coming. Asked again with the same key, the API returns it.
|
|
94
|
+
if idempotency_key && RetryPolicy.idempotency_key_in_progress?(status, data) && attempt < RetryPolicy.max_retries
|
|
95
|
+
attempt += 1
|
|
96
|
+
sleep RetryPolicy.idempotency_conflict_delay_seconds(attempt)
|
|
97
|
+
next
|
|
98
|
+
end
|
|
99
|
+
|
|
92
100
|
retry_after = response.dig(:headers, 'retry-after')&.to_i
|
|
93
101
|
raise Errors.from_response(status, data, retry_after: retry_after)
|
|
94
102
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'securerandom'
|
|
4
4
|
require 'json'
|
|
5
|
+
require 'uri'
|
|
5
6
|
|
|
6
7
|
module Wajub
|
|
7
8
|
module HttpUtils
|
|
@@ -17,6 +18,22 @@ module Wajub
|
|
|
17
18
|
API_URL
|
|
18
19
|
end
|
|
19
20
|
|
|
21
|
+
# An id as one segment of a request path. The API reads an encoded "/" as
|
|
22
|
+
# a separator, an empty id leaves its segment out, and "." or ".." are dot
|
|
23
|
+
# segments that servers resolve: such an id would reach another route. No
|
|
24
|
+
# Wajub id takes these forms, so it is refused before anything is sent.
|
|
25
|
+
def path_segment(id)
|
|
26
|
+
raise ArgumentError, 'Wajub: an id is required (got nil)' if id.nil?
|
|
27
|
+
|
|
28
|
+
value = id.to_s
|
|
29
|
+
raise ArgumentError, "Wajub: an id must not contain \"/\" (got #{value.inspect})" if value.include?('/')
|
|
30
|
+
raise ArgumentError, "Wajub: #{value.inspect} is not a Wajub id" if ['', '.', '..'].include?(value)
|
|
31
|
+
|
|
32
|
+
# Form encoding writes a space as "+", which a path reads as a plus sign;
|
|
33
|
+
# it writes a "+" as %2B, so every "+" left is a space.
|
|
34
|
+
URI.encode_www_form_component(value).gsub('+', '%20')
|
|
35
|
+
end
|
|
36
|
+
|
|
20
37
|
def create_idempotency_key(prefix = 'wajub')
|
|
21
38
|
"#{prefix}-#{SecureRandom.uuid}"
|
|
22
39
|
end
|
|
@@ -15,6 +15,20 @@ module Wajub
|
|
|
15
15
|
MAX_RETRIES
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
# The 409 the API answers while a request with the same Idempotency-Key is
|
|
19
|
+
# still running (errors.idempotency_key). A call sent again after a timeout
|
|
20
|
+
# can arrive before the first has finished: its answer is on the way, so the
|
|
21
|
+
# call is worth repeating with the same key. Every other 409 is a refusal.
|
|
22
|
+
def idempotency_key_in_progress?(status, body)
|
|
23
|
+
status == 409 && body.is_a?(Hash) && body['errors'].is_a?(Hash) && body['errors'].key?('idempotency_key')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The pause before asking again about a key still being processed:
|
|
27
|
+
# 2, 4, then 8 seconds.
|
|
28
|
+
def idempotency_conflict_delay_seconds(attempt)
|
|
29
|
+
2**[attempt, 1].max
|
|
30
|
+
end
|
|
31
|
+
|
|
18
32
|
def delay_seconds(attempt, response: nil)
|
|
19
33
|
header = response&.[]('retry-after')&.strip
|
|
20
34
|
if header && !header.empty?
|
data/lib/wajub/resources.rb
CHANGED
|
@@ -20,7 +20,7 @@ module Wajub
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def retrieve(id)
|
|
23
|
-
HttpUtils.pick_resource(get("/#{@path}/#{
|
|
23
|
+
HttpUtils.pick_resource(get("/#{@path}/#{HttpUtils.path_segment(id)}"), @singular)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def list(params = nil)
|
|
@@ -28,11 +28,11 @@ module Wajub
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def update(id, params, options = nil)
|
|
31
|
-
HttpUtils.pick_resource(put("/#{@path}/#{
|
|
31
|
+
HttpUtils.pick_resource(put("/#{@path}/#{HttpUtils.path_segment(id)}", params, options), @singular)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
def delete(id, options = nil)
|
|
35
|
-
request('DELETE', "/#{@path}/#{
|
|
35
|
+
request('DELETE', "/#{@path}/#{HttpUtils.path_segment(id)}", options: options)
|
|
36
36
|
end
|
|
37
37
|
end
|
|
38
38
|
|
|
@@ -41,12 +41,8 @@ module Wajub
|
|
|
41
41
|
to_payment_object(post('/payments', params, options))
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
def initialize_payment(params, options = nil)
|
|
45
|
-
create(params, options)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
44
|
def retrieve(id)
|
|
49
|
-
res = get("/payments/#{
|
|
45
|
+
res = get("/payments/#{HttpUtils.path_segment(id)}")
|
|
50
46
|
to_payment_object('transaction' => HttpUtils.pick_resource(res, 'transaction'))
|
|
51
47
|
end
|
|
52
48
|
|
|
@@ -55,22 +51,22 @@ module Wajub
|
|
|
55
51
|
end
|
|
56
52
|
|
|
57
53
|
def cancel(id, options = nil)
|
|
58
|
-
res = delete("/payments/#{
|
|
54
|
+
res = delete("/payments/#{HttpUtils.path_segment(id)}", options)
|
|
59
55
|
to_payment_object('transaction' => HttpUtils.pick_resource(res, 'transaction'))
|
|
60
56
|
end
|
|
61
57
|
|
|
62
58
|
def process(id, params, options = nil)
|
|
63
|
-
res = post("/payments/#{
|
|
59
|
+
res = post("/payments/#{HttpUtils.path_segment(id)}", params, options)
|
|
64
60
|
to_payment_object('transaction' => HttpUtils.pick_resource(res, 'transaction'))
|
|
65
61
|
end
|
|
66
62
|
|
|
67
63
|
def process_split(id, params, options = nil)
|
|
68
|
-
res = post("/payments/#{
|
|
64
|
+
res = post("/payments/#{HttpUtils.path_segment(id)}/splits", params, options)
|
|
69
65
|
to_payment_object('transaction' => HttpUtils.pick_resource(res, 'transaction'))
|
|
70
66
|
end
|
|
71
67
|
|
|
72
68
|
def list_refunds(id, params = nil)
|
|
73
|
-
list = HttpUtils.pick_list(get("/payments/#{
|
|
69
|
+
list = HttpUtils.pick_list(get("/payments/#{HttpUtils.path_segment(id)}/refunds", params), 'refunds')
|
|
74
70
|
[list[:data], list[:meta]]
|
|
75
71
|
end
|
|
76
72
|
|
|
@@ -120,11 +116,11 @@ module Wajub
|
|
|
120
116
|
end
|
|
121
117
|
|
|
122
118
|
def retrieve(id)
|
|
123
|
-
HttpUtils.pick_resource(get("/events/#{
|
|
119
|
+
HttpUtils.pick_resource(get("/events/#{HttpUtils.path_segment(id)}"), 'event')
|
|
124
120
|
end
|
|
125
121
|
|
|
126
122
|
def resend(id, options = nil)
|
|
127
|
-
HttpUtils.pick_resource(post("/events/#{
|
|
123
|
+
HttpUtils.pick_resource(post("/events/#{HttpUtils.path_segment(id)}/resend", nil, options), 'event')
|
|
128
124
|
end
|
|
129
125
|
end
|
|
130
126
|
|
|
@@ -134,32 +130,34 @@ module Wajub
|
|
|
134
130
|
end
|
|
135
131
|
|
|
136
132
|
def block(id, params = nil, options = nil)
|
|
137
|
-
HttpUtils.pick_resource(post("/customers/#{
|
|
133
|
+
HttpUtils.pick_resource(post("/customers/#{HttpUtils.path_segment(id)}/block", params, options), 'customer')
|
|
138
134
|
end
|
|
139
135
|
|
|
140
136
|
def unblock(id, options = nil)
|
|
141
|
-
HttpUtils.pick_resource(post("/customers/#{
|
|
137
|
+
HttpUtils.pick_resource(post("/customers/#{HttpUtils.path_segment(id)}/unblock", nil, options), 'customer')
|
|
142
138
|
end
|
|
143
139
|
|
|
144
140
|
def activate(id, options = nil)
|
|
145
|
-
HttpUtils.pick_resource(post("/customers/#{
|
|
141
|
+
HttpUtils.pick_resource(post("/customers/#{HttpUtils.path_segment(id)}/activate", nil, options), 'customer')
|
|
146
142
|
end
|
|
147
143
|
|
|
148
144
|
def deactivate(id, options = nil)
|
|
149
|
-
HttpUtils.pick_resource(post("/customers/#{
|
|
145
|
+
HttpUtils.pick_resource(post("/customers/#{HttpUtils.path_segment(id)}/deactivate", nil, options), 'customer')
|
|
150
146
|
end
|
|
151
147
|
|
|
152
148
|
def list_tax_ids(customer_id)
|
|
153
|
-
list = HttpUtils.pick_list(get("/customers/#{
|
|
149
|
+
list = HttpUtils.pick_list(get("/customers/#{HttpUtils.path_segment(customer_id)}/tax_ids"), 'tax_ids')
|
|
154
150
|
[list[:data], list[:meta]]
|
|
155
151
|
end
|
|
156
152
|
|
|
157
153
|
def create_tax_id(customer_id, params, options = nil)
|
|
158
|
-
HttpUtils.pick_resource(post("/customers/#{
|
|
154
|
+
HttpUtils.pick_resource(post("/customers/#{HttpUtils.path_segment(customer_id)}/tax_ids", params, options), 'tax_id')
|
|
159
155
|
end
|
|
160
156
|
|
|
161
157
|
def delete_tax_id(customer_id, tax_id, options = nil)
|
|
162
|
-
delete
|
|
158
|
+
# Not `delete`: on a CrudResource that one takes an id and encodes it as a path segment.
|
|
159
|
+
path = "/customers/#{HttpUtils.path_segment(customer_id)}/tax_ids/#{HttpUtils.path_segment(tax_id)}"
|
|
160
|
+
request('DELETE', path, options: options)
|
|
163
161
|
end
|
|
164
162
|
end
|
|
165
163
|
|
|
@@ -169,7 +167,7 @@ module Wajub
|
|
|
169
167
|
end
|
|
170
168
|
|
|
171
169
|
def regenerate_token(id, options = nil)
|
|
172
|
-
HttpUtils.pick_resource(post("/accounts/#{
|
|
170
|
+
HttpUtils.pick_resource(post("/accounts/#{HttpUtils.path_segment(id)}/token", nil, options), 'account')
|
|
173
171
|
end
|
|
174
172
|
end
|
|
175
173
|
|
|
@@ -179,7 +177,7 @@ module Wajub
|
|
|
179
177
|
end
|
|
180
178
|
|
|
181
179
|
def rotate_secret(id, options = nil)
|
|
182
|
-
HttpUtils.pick_resource(post("/webhooks/#{
|
|
180
|
+
HttpUtils.pick_resource(post("/webhooks/#{HttpUtils.path_segment(id)}/rotate-secret", nil, options), 'endpoint')
|
|
183
181
|
end
|
|
184
182
|
end
|
|
185
183
|
|
|
@@ -188,16 +186,27 @@ module Wajub
|
|
|
188
186
|
super(api_key, base_url, 'invoices', 'invoice', 'invoices', idempotency_key_prefix, transport: transport)
|
|
189
187
|
end
|
|
190
188
|
|
|
191
|
-
|
|
192
|
-
|
|
189
|
+
# Sends the invoice to its customer.
|
|
190
|
+
def send_invoice(id, options = nil)
|
|
191
|
+
HttpUtils.pick_resource(post("/invoices/#{HttpUtils.path_segment(id)}/send", nil, options), 'invoice')
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Kept for code that sends an invoice with send(id). Called with a Symbol,
|
|
195
|
+
# it is Object#send, so code that calls methods by name keeps working. A
|
|
196
|
+
# String is always an invoice id, even one that names a method, so an id
|
|
197
|
+
# taken from a request never calls a method.
|
|
198
|
+
def send(id_or_method, *args, **kwargs, &block)
|
|
199
|
+
return super if id_or_method.is_a?(Symbol)
|
|
200
|
+
|
|
201
|
+
send_invoice(id_or_method, *args, **kwargs)
|
|
193
202
|
end
|
|
194
203
|
|
|
195
204
|
def mark_paid(id, params = nil, options = nil)
|
|
196
|
-
HttpUtils.pick_resource(post("/invoices/#{
|
|
205
|
+
HttpUtils.pick_resource(post("/invoices/#{HttpUtils.path_segment(id)}/mark-paid", params, options), 'invoice')
|
|
197
206
|
end
|
|
198
207
|
|
|
199
208
|
def cancel(id, options = nil)
|
|
200
|
-
HttpUtils.pick_resource(post("/invoices/#{
|
|
209
|
+
HttpUtils.pick_resource(post("/invoices/#{HttpUtils.path_segment(id)}/cancel", nil, options), 'invoice')
|
|
201
210
|
end
|
|
202
211
|
end
|
|
203
212
|
|
|
@@ -207,23 +216,23 @@ module Wajub
|
|
|
207
216
|
end
|
|
208
217
|
|
|
209
218
|
def retrieve(id)
|
|
210
|
-
HttpUtils.pick_resource(get("/disputes/#{
|
|
219
|
+
HttpUtils.pick_resource(get("/disputes/#{HttpUtils.path_segment(id)}"), 'dispute')
|
|
211
220
|
end
|
|
212
221
|
|
|
213
222
|
def submit_evidence(id, params, options = nil)
|
|
214
|
-
HttpUtils.pick_resource(post("/disputes/#{
|
|
223
|
+
HttpUtils.pick_resource(post("/disputes/#{HttpUtils.path_segment(id)}/submit-evidence", params, options), 'dispute')
|
|
215
224
|
end
|
|
216
225
|
|
|
217
226
|
def accept(id, options = nil)
|
|
218
|
-
HttpUtils.pick_resource(post("/disputes/#{
|
|
227
|
+
HttpUtils.pick_resource(post("/disputes/#{HttpUtils.path_segment(id)}/accept", nil, options), 'dispute')
|
|
219
228
|
end
|
|
220
229
|
|
|
221
230
|
def close(id, options = nil)
|
|
222
|
-
HttpUtils.pick_resource(post("/disputes/#{
|
|
231
|
+
HttpUtils.pick_resource(post("/disputes/#{HttpUtils.path_segment(id)}/close", nil, options), 'dispute')
|
|
223
232
|
end
|
|
224
233
|
|
|
225
234
|
def send_message(id, params, options = nil)
|
|
226
|
-
HttpUtils.pick_resource(post("/disputes/#{
|
|
235
|
+
HttpUtils.pick_resource(post("/disputes/#{HttpUtils.path_segment(id)}/messages", params, options), 'dispute')
|
|
227
236
|
end
|
|
228
237
|
end
|
|
229
238
|
|
|
@@ -254,6 +263,55 @@ module Wajub
|
|
|
254
263
|
def calculate(params, options = nil)
|
|
255
264
|
HttpUtils.pick_resource(post('/tax/calculate', params, options), 'tax', 'calculation')
|
|
256
265
|
end
|
|
266
|
+
|
|
267
|
+
def reports(params = nil)
|
|
268
|
+
list = HttpUtils.pick_list(get('/tax/reports', params), 'reports', 'tax_reports')
|
|
269
|
+
[list[:data], list[:meta]]
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def list_codes(params = nil)
|
|
273
|
+
list = HttpUtils.pick_list(get('/tax/codes', params), 'tax_codes', 'codes')
|
|
274
|
+
[list[:data], list[:meta]]
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def retrieve_code(code)
|
|
278
|
+
HttpUtils.pick_resource(get("/tax/codes/#{HttpUtils.path_segment(code)}"), 'tax_code', 'code')
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def list_registrations(params = nil)
|
|
282
|
+
PagedResult.create(self, '/tax/registrations', 'registrations', params)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def create_registration(params, options = nil)
|
|
286
|
+
HttpUtils.pick_resource(post('/tax/registrations', params, options), 'registration', 'tax_registration')
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def retrieve_registration(id)
|
|
290
|
+
HttpUtils.pick_resource(get("/tax/registrations/#{HttpUtils.path_segment(id)}"), 'registration', 'tax_registration')
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def update_registration(id, params, options = nil)
|
|
294
|
+
HttpUtils.pick_resource(put("/tax/registrations/#{HttpUtils.path_segment(id)}", params, options), 'registration', 'tax_registration')
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def delete_registration(id, options = nil)
|
|
298
|
+
request('DELETE', "/tax/registrations/#{HttpUtils.path_segment(id)}", options: options)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def jurisdictions(params = nil)
|
|
302
|
+
list = HttpUtils.pick_list(get('/tax/jurisdictions', params), 'jurisdictions', 'tax_jurisdictions')
|
|
303
|
+
[list[:data], list[:meta]]
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def thresholds(params = nil)
|
|
307
|
+
list = HttpUtils.pick_list(get('/tax/thresholds', params), 'thresholds', 'tax_thresholds')
|
|
308
|
+
[list[:data], list[:meta]]
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def threshold_alerts(params = nil)
|
|
312
|
+
list = HttpUtils.pick_list(get('/tax/thresholds/alerts', params), 'alerts', 'threshold_alerts')
|
|
313
|
+
[list[:data], list[:meta]]
|
|
314
|
+
end
|
|
257
315
|
end
|
|
258
316
|
|
|
259
317
|
class ShieldResource < BaseClient
|
|
@@ -268,14 +326,34 @@ module Wajub
|
|
|
268
326
|
def stats(params = nil)
|
|
269
327
|
HttpUtils.pick_resource(get('/shield/stats', params), 'shield', 'stats')
|
|
270
328
|
end
|
|
329
|
+
|
|
330
|
+
def list_blocklist(params = nil)
|
|
331
|
+
list = HttpUtils.pick_list(get('/shield/blocklist', params), 'blocklist', 'entries')
|
|
332
|
+
[list[:data], list[:meta]]
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def add_to_blocklist(params, options = nil)
|
|
336
|
+
HttpUtils.pick_resource(post('/shield/blocklist', params, options), 'blocklist', 'entry')
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def remove_from_blocklist(id, options = nil)
|
|
340
|
+
request('DELETE', "/shield/blocklist/#{HttpUtils.path_segment(id)}", options: options)
|
|
341
|
+
end
|
|
271
342
|
end
|
|
272
343
|
|
|
344
|
+
# Deprecated: the API no longer offers these calls. To receive webhooks on
|
|
345
|
+
# your machine, forward them with the Wajub CLI's `wajub listen`. Each call
|
|
346
|
+
# warns when Ruby shows deprecation warnings (-W:deprecated).
|
|
273
347
|
class ListenResource < BaseClient
|
|
274
348
|
def config
|
|
349
|
+
warn('Wajub: listen.config is deprecated: the API no longer offers it. ' \
|
|
350
|
+
'Forward webhooks with the Wajub CLI: wajub listen', uplevel: 1, category: :deprecated)
|
|
275
351
|
HttpUtils.pick_resource(get('/listen/config'), 'config', 'listen')
|
|
276
352
|
end
|
|
277
353
|
|
|
278
354
|
def auth(params, options = nil)
|
|
355
|
+
warn('Wajub: listen.auth is deprecated: the API no longer offers it. ' \
|
|
356
|
+
'Forward webhooks with the Wajub CLI: wajub listen', uplevel: 1, category: :deprecated)
|
|
279
357
|
HttpUtils.pick_resource(post('/listen/auth', params, options), 'auth', 'listen')
|
|
280
358
|
end
|
|
281
359
|
end
|
data/lib/wajub/version.rb
CHANGED
data/lib/wajub/webhooks.rb
CHANGED
|
@@ -15,8 +15,16 @@ module Wajub
|
|
|
15
15
|
@secret = secret.to_s
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
# A missing signature or timestamp header (nil, as request.headers returns
|
|
19
|
+
# it) raises WebhookSignatureVerificationError, like a wrong one.
|
|
20
|
+
#
|
|
21
|
+
# +tolerance+ is the replay window in seconds. Anything but a positive
|
|
22
|
+
# number (0, a negative number, nil, a String) keeps the default of 300.
|
|
18
23
|
def construct_event(payload, signature, timestamp, tolerance = DEFAULT_TOLERANCE)
|
|
19
24
|
raise ArgumentError, 'Wajub: webhook_secret is required for construct_event' if @secret.empty?
|
|
25
|
+
if signature.to_s.empty? || timestamp.to_s.empty?
|
|
26
|
+
raise WebhookSignatureVerificationError, 'Missing X-Wajub-Signature or X-Wajub-Timestamp header.'
|
|
27
|
+
end
|
|
20
28
|
raise WebhookSignatureVerificationError, 'Invalid webhook signature format. Expected v1={hash}.' unless signature.start_with?('v1=')
|
|
21
29
|
raise ArgumentError, 'Wajub: payload must be the raw request body as a String, not a parsed object' unless payload.is_a?(String)
|
|
22
30
|
|
|
@@ -26,13 +34,14 @@ module Wajub
|
|
|
26
34
|
|
|
27
35
|
raise WebhookSignatureVerificationError, 'Webhook signature verification failed.' unless Crypto.timing_safe_equal(expected, received)
|
|
28
36
|
|
|
29
|
-
ts = Float(timestamp)
|
|
30
|
-
raise WebhookSignatureVerificationError, 'Invalid webhook timestamp.' unless ts
|
|
37
|
+
ts = Float(timestamp, exception: false)
|
|
38
|
+
raise WebhookSignatureVerificationError, 'Invalid webhook timestamp.' unless ts&.finite?
|
|
31
39
|
|
|
40
|
+
window = replay_window(tolerance)
|
|
32
41
|
drift = (Time.now.to_i - ts).abs
|
|
33
|
-
if
|
|
42
|
+
if drift > window
|
|
34
43
|
raise WebhookSignatureVerificationError,
|
|
35
|
-
"Timestamp outside tolerance zone (#{drift.to_i}s drift, allowed #{
|
|
44
|
+
"Timestamp outside tolerance zone (#{drift.to_i}s drift, allowed #{window}s)."
|
|
36
45
|
end
|
|
37
46
|
|
|
38
47
|
event = JSON.parse(body)
|
|
@@ -40,5 +49,14 @@ module Wajub
|
|
|
40
49
|
|
|
41
50
|
event
|
|
42
51
|
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# A tolerance of 0 or less used to switch the timestamp check off, which let
|
|
56
|
+
# a replayed delivery through.
|
|
57
|
+
def replay_window(tolerance)
|
|
58
|
+
valid = tolerance.is_a?(Numeric) && tolerance.real? && tolerance.finite? && tolerance.positive?
|
|
59
|
+
valid ? tolerance : DEFAULT_TOLERANCE
|
|
60
|
+
end
|
|
43
61
|
end
|
|
44
62
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wajub
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Wajub
|
|
@@ -16,6 +16,7 @@ executables: []
|
|
|
16
16
|
extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
|
+
- CHANGELOG.md
|
|
19
20
|
- LICENSE
|
|
20
21
|
- README.md
|
|
21
22
|
- lib/wajub.rb
|
|
@@ -37,6 +38,7 @@ licenses:
|
|
|
37
38
|
metadata:
|
|
38
39
|
homepage_uri: https://docs.wajub.com/libraries/sdks/ruby
|
|
39
40
|
source_code_uri: https://github.com/wajubhq/wajub-ruby
|
|
41
|
+
changelog_uri: https://github.com/wajubhq/wajub-ruby/blob/main/CHANGELOG.md
|
|
40
42
|
rdoc_options: []
|
|
41
43
|
require_paths:
|
|
42
44
|
- lib
|
|
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
51
53
|
- !ruby/object:Gem::Version
|
|
52
54
|
version: '0'
|
|
53
55
|
requirements: []
|
|
54
|
-
rubygems_version: 4.0.
|
|
56
|
+
rubygems_version: 4.0.20
|
|
55
57
|
specification_version: 4
|
|
56
58
|
summary: Official Wajub Ruby SDK
|
|
57
59
|
test_files: []
|