spreedly-composer-sdk-server 1.0.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 +7 -0
- data/CHANGELOG.md +27 -0
- data/LICENSE +203 -0
- data/NOTICE +15 -0
- data/README.md +447 -0
- data/THIRD-PARTY-LICENSES +578 -0
- data/lib/composer/composer.rb +250 -0
- data/lib/composer/constants.rb +43 -0
- data/lib/composer/dodgeball.rb +9 -0
- data/lib/composer/logger.rb +150 -0
- data/lib/composer/types.rb +128 -0
- data/lib/composer/utilities.rb +172 -0
- data/lib/composer/version.rb +5 -0
- data/lib/composer.rb +13 -0
- metadata +149 -0
data/README.md
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
# Spreedly Composer Server SDK for Ruby
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
|
|
5
|
+
- [Purpose](#purpose)
|
|
6
|
+
- [Prerequisites](#prerequisites)
|
|
7
|
+
- [Related](#related)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
- [API](#api)
|
|
11
|
+
- [Development](#development)
|
|
12
|
+
- [License](#license)
|
|
13
|
+
- [Support](#support)
|
|
14
|
+
|
|
15
|
+
## Purpose
|
|
16
|
+
|
|
17
|
+
[Spreedly Composer](https://developer.spreedly.com) enables developers to decouple security logic from their application code. This has several benefits including:
|
|
18
|
+
|
|
19
|
+
- The ability to toggle and compare security services like fraud engines, MFA, KYC, and bot prevention.
|
|
20
|
+
- Faster responses to new attacks. When threats evolve and new vulnerabilities are identified, your application's security logic can be updated without changing a single line of code.
|
|
21
|
+
- The ability to put in placeholders for future security improvements while focusing on product development.
|
|
22
|
+
- A way to visualize all application security logic in one place.
|
|
23
|
+
|
|
24
|
+
The Composer Server SDK for Ruby makes integration with the Composer API easy and is maintained by the Spreedly team.
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
- Ruby 3.2 or higher
|
|
29
|
+
- You will need to obtain an API key for your application from the Composer developer center.
|
|
30
|
+
|
|
31
|
+
## Related
|
|
32
|
+
|
|
33
|
+
Check out the [Client SDK](https://www.npmjs.com/package/@spreedly/composer-sdk-client) for how to integrate Composer into your frontend applications.
|
|
34
|
+
|
|
35
|
+
[Composer examples](https://github.com/spreedly/composer-examples) has a runnable example server for this SDK in `server/ruby`, and a web client that calls it.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Add this line to your application's Gemfile:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
gem 'spreedly-composer-sdk-server'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
And then run:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
bundle install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or install it yourself:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
gem install spreedly-composer-sdk-server
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
This example protects an order endpoint end to end. Your API calls a checkpoint before placing the order. When the workflow needs more input from the user, such as MFA, your frontend gathers it with the Client SDK and calls your API again with the verification ID. See [Identifiers](#identifiers) for what each ID is and [useVerification](#useverification) for how the second call works.
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
# In your API
|
|
63
|
+
require 'composer'
|
|
64
|
+
require 'ipaddr'
|
|
65
|
+
require 'json'
|
|
66
|
+
require 'sinatra'
|
|
67
|
+
|
|
68
|
+
enable :sessions
|
|
69
|
+
|
|
70
|
+
# composer = Composer::DodgeballClient.new(ENV['DODGEBALL_SECRET_KEY']) # The Dodgeball name is still supported.
|
|
71
|
+
composer = Composer::ComposerClient.new(ENV['SPREEDLY_SECRET_KEY'])
|
|
72
|
+
|
|
73
|
+
# Client IP for the risk engine. Callers must not choose this value.
|
|
74
|
+
# Default (TRUSTED_PROXY_COUNT=0): socket address, ignore X-Forwarded-For.
|
|
75
|
+
# Positive count: that many hops from the right of X-Forwarded-For; the hop must be an IP.
|
|
76
|
+
# Set TRUSTED_PROXY_COUNT only if your app runs behind trusted reverse proxies.
|
|
77
|
+
def get_ip(request)
|
|
78
|
+
raw = ENV['TRUSTED_PROXY_COUNT']
|
|
79
|
+
raw = '0' if raw.nil? || raw.empty?
|
|
80
|
+
begin
|
|
81
|
+
hops = Integer(raw)
|
|
82
|
+
rescue ArgumentError
|
|
83
|
+
hops = -1
|
|
84
|
+
end
|
|
85
|
+
raise 'Cannot derive client IP: TRUSTED_PROXY_COUNT is not a valid hop count' if hops < 0
|
|
86
|
+
|
|
87
|
+
parts = (request.env['HTTP_X_FORWARDED_FOR'] || '').split(',').map(&:strip)
|
|
88
|
+
candidate = if hops == 0
|
|
89
|
+
request.env['REMOTE_ADDR']
|
|
90
|
+
elsif hops <= parts.length
|
|
91
|
+
parts[parts.length - hops]
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
raise IPAddr::Error if candidate.nil? || candidate.include?('/')
|
|
95
|
+
IPAddr.new(candidate)
|
|
96
|
+
candidate
|
|
97
|
+
rescue IPAddr::Error
|
|
98
|
+
raise 'Cannot derive client IP: no valid IP for this TRUSTED_PROXY_COUNT'
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The Client SDK expects the verification's fields in camelCase
|
|
102
|
+
def verification_json(verification)
|
|
103
|
+
{ id: verification.id, status: verification.status, outcome: verification.outcome,
|
|
104
|
+
nextSteps: verification.next_steps, stepData: verification.step_data }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
post '/api/orders' do
|
|
108
|
+
content_type :json
|
|
109
|
+
order = JSON.parse(request.body.read)['order']
|
|
110
|
+
|
|
111
|
+
checkpoint_response = composer.checkpoint(
|
|
112
|
+
checkpoint_name: 'PLACE_ORDER',
|
|
113
|
+
event: Composer::CheckpointEvent.new(ip: get_ip(request), data: { order: order }),
|
|
114
|
+
source_token: request.env['HTTP_X_DODGEBALL_SOURCE_TOKEN'],
|
|
115
|
+
session_id: session.id.to_s,
|
|
116
|
+
user_id: session[:user_id],
|
|
117
|
+
use_verification_id: request.env['HTTP_X_DODGEBALL_VERIFICATION_ID']
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
if composer.allowed?(checkpoint_response)
|
|
121
|
+
placed_order = database.create_order(order)
|
|
122
|
+
status 200
|
|
123
|
+
{ order: placed_order }.to_json
|
|
124
|
+
elsif composer.denied?(checkpoint_response)
|
|
125
|
+
# Return the verification so the frontend can show the reason
|
|
126
|
+
status 403
|
|
127
|
+
{ verification: verification_json(checkpoint_response.verification) }.to_json
|
|
128
|
+
elsif composer.running?(checkpoint_response)
|
|
129
|
+
# The workflow needs more input from the user (such as MFA or KYC)
|
|
130
|
+
status 202
|
|
131
|
+
{ verification: verification_json(checkpoint_response.verification) }.to_json
|
|
132
|
+
else
|
|
133
|
+
# No verdict: decide whether to return the error, proceed, retry, or reject. See Errors.
|
|
134
|
+
status 500
|
|
135
|
+
{ message: checkpoint_response.errors.map(&:message) }.to_json
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
// In your frontend, with the Client SDK
|
|
142
|
+
const placeOrder = async (order, previousVerificationId = null) => {
|
|
143
|
+
const sourceToken = await dodgeball.getSourceToken();
|
|
144
|
+
|
|
145
|
+
const endpointResponse = await axios.post("/api/orders", { order }, {
|
|
146
|
+
headers: {
|
|
147
|
+
"x-dodgeball-source-token": sourceToken,
|
|
148
|
+
"x-dodgeball-verification-id": previousVerificationId
|
|
149
|
+
},
|
|
150
|
+
// Let the 202 and 403 responses through instead of throwing
|
|
151
|
+
validateStatus: (status) => [200, 202, 403].includes(status)
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
if (endpointResponse.status === 200) {
|
|
155
|
+
setIsOrderPlaced(true);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
dodgeball.handleVerification(endpointResponse.data.verification, {
|
|
160
|
+
onVerified: async (verification) => {
|
|
161
|
+
// The user completed the extra step: call your API again with the verification ID
|
|
162
|
+
await placeOrder(order, verification.id);
|
|
163
|
+
},
|
|
164
|
+
onApproved: async () => {
|
|
165
|
+
setIsOrderPlaced(true);
|
|
166
|
+
},
|
|
167
|
+
onDenied: async (verification) => {
|
|
168
|
+
setIsOrderDenied(true);
|
|
169
|
+
},
|
|
170
|
+
onError: async (error) => {
|
|
171
|
+
setError(error);
|
|
172
|
+
setIsPlacingOrder(false);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## API
|
|
179
|
+
|
|
180
|
+
### Configuration
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
The SDK requires a secret API key as the first argument to the constructor.
|
|
185
|
+
|
|
186
|
+
```ruby
|
|
187
|
+
composer = Composer::ComposerClient.new('secret-api-key...')
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Optionally, you can pass a hash of configuration options as the second argument. Keys must be symbols.
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
composer = Composer::ComposerClient.new('secret-api-key...', {
|
|
194
|
+
api_version: Composer::ApiVersion::V1,
|
|
195
|
+
api_url: 'https://api.dodgeballhq.com',
|
|
196
|
+
log_level: Composer::ComposerLogLevel::ERROR
|
|
197
|
+
})
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
| Option | Default | Description |
|
|
201
|
+
| --- | --- | --- |
|
|
202
|
+
| `api_version` | `v1` | The Composer API version to use. |
|
|
203
|
+
| `api_url` | `https://api.dodgeballhq.com` | The base URL of the Spreedly Composer API. Useful for sending requests to different environments such as `https://api.sandbox.dodgeballhq.com`. |
|
|
204
|
+
| `log_level` | `INFO` | `TRACE`, `INFO`, `ERROR` or `NONE`. The SDK logs errors, plus redacted request and response payloads at `TRACE`, so `INFO` and `ERROR` currently log the same thing. The level is shared by every client in the process. See [Safe logging](#safe-logging). |
|
|
205
|
+
| `request_timeout_ms` | `30000` | How long each stage of an HTTP request (connecting, or waiting for data) may take, in milliseconds. Applies to every call the SDK makes. A value that isn't a positive integer is logged and replaced with the default. See [Timeouts](#timeouts). |
|
|
206
|
+
| `enabled` | `true` | When `false`, the SDK makes no API calls: `checkpoint` returns an approved response and `event` returns without sending anything. |
|
|
207
|
+
|
|
208
|
+
### Safe logging
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
The SDK writes JSON log lines to standard output. At the default `INFO` level it writes no request or response payloads: a successful checkpoint logs nothing, and `ERROR` reports request failures. Payload logging is opt-in through `TRACE`, and even there the fields that describe your end users or carry provider credentials (`ip`, `data`, `stepData` and `nextSteps`) are replaced with `[REDACTED]`, so a verbose log shows the shape of a request or response without carrying PII. Turn `TRACE` on only in environments where you are comfortable with that extra detail.
|
|
213
|
+
|
|
214
|
+
### Identifiers
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
`checkpoint` and `event` both take these. Provide a `session_id`, a `source_token`, or both.
|
|
219
|
+
|
|
220
|
+
| Parameter | Required | Description |
|
|
221
|
+
| --- | --- | --- |
|
|
222
|
+
| `source_token` | Either this or `session_id` | A token representing the device making the request. Obtained from the [Client SDK](https://www.npmjs.com/package/@spreedly/composer-sdk-client). |
|
|
223
|
+
| `session_id` | Either this or `source_token` | The current session ID of the request. |
|
|
224
|
+
| `user_id` | No | The ID of the user in your database, once you know it (for example, after registration). |
|
|
225
|
+
| `use_verification_id` | No | `checkpoint` only. The ID of a verification the user already completed. See [useVerification](#useverification). |
|
|
226
|
+
|
|
227
|
+
### Call a Checkpoint
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
Checkpoints represent key moments of risk in an application and are at the core of how Composer works. A checkpoint can represent any activity deemed to be a risk. Some common examples include: login, placing an order, redeeming a coupon, posting a review, changing bank account information, making a donation, transferring funds, creating a listing.
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
checkpoint_response = composer.checkpoint(
|
|
235
|
+
checkpoint_name: 'CHECKPOINT_NAME',
|
|
236
|
+
event: Composer::CheckpointEvent.new(
|
|
237
|
+
ip: '127.0.0.1',
|
|
238
|
+
data: {
|
|
239
|
+
transaction: { amount: 100, currency: 'USD' },
|
|
240
|
+
payment_method: { token: 'ghi789' }
|
|
241
|
+
}
|
|
242
|
+
),
|
|
243
|
+
session_id: 'session_def456'
|
|
244
|
+
)
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Along with the [identifiers](#identifiers), `checkpoint` takes:
|
|
248
|
+
|
|
249
|
+
| Parameter | Required | Description |
|
|
250
|
+
| --- | --- | --- |
|
|
251
|
+
| `checkpoint_name` | Yes | The name of the checkpoint to call. |
|
|
252
|
+
| `event` | Yes | A `Composer::CheckpointEvent`. |
|
|
253
|
+
| `event.ip` | No | The IP address of the device where the request originated. Recommended whenever an end user is present; leave it out for checkpoints with no user, such as renewals. |
|
|
254
|
+
| `event.data` | No | Data your checkpoint's workflow uses. Defaults to `{}`. |
|
|
255
|
+
| `options` | No | A hash with `sync` (defaults to `false`) and `timeout` (how long the SDK keeps polling an unresolved verification, in milliseconds; defaults to 10 minutes). See [Timeouts](#timeouts). |
|
|
256
|
+
|
|
257
|
+
### Timeouts
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
Two settings control timing and neither one caps the other. `request_timeout_ms` limits how long each stage of an HTTP request may take: connecting, or waiting for data. It applies to every call the SDK makes. It doesn't cap the request as a whole, so a response that keeps arriving slowly can take longer. The `timeout` in checkpoint options limits how long the SDK keeps polling for an unresolved verification, and defaults to 10 minutes. The SDK always polls at least once.
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
composer = Composer::ComposerClient.new('secret-api-key...', {
|
|
265
|
+
request_timeout_ms: 5000 # Give up if connecting, or waiting for data, takes longer than 5 seconds
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
checkpoint_response = composer.checkpoint(
|
|
269
|
+
checkpoint_name: 'CHECKPOINT_NAME',
|
|
270
|
+
event: Composer::CheckpointEvent.new(ip: '127.0.0.1'),
|
|
271
|
+
session_id: 'session_def456',
|
|
272
|
+
options: {
|
|
273
|
+
timeout: 10_000 # Stop polling after 10 seconds and return whatever we have
|
|
274
|
+
}
|
|
275
|
+
)
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
This example uses the default `sync: false`, which we recommend. The first request returns right away with a pending verification, and the SDK polls until it resolves, so each request is short and `request_timeout_ms` only guards against a hung connection. With `sync: true`, the first request waits on the server until the workflow finishes, so `request_timeout_ms` must be longer than your slowest workflow. Otherwise a slow but healthy checkpoint comes back as a failure.
|
|
279
|
+
|
|
280
|
+
When the polling budget runs out, the verification is simply still pending, which is not an error: `running?` returns `true`, and you can resume later by passing the verification ID to `use_verification_id`. The exception is when the last poll before the budget ran out failed: you get that failure instead, with `success` `false`, and `has_error?` returns `true`.
|
|
281
|
+
|
|
282
|
+
### Checkpoint Responses
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
Calling a checkpoint creates a verification in Composer. Its `status` and `outcome` determine how your application should proceed. Use the [utility methods](#utility-methods) rather than reading these fields directly.
|
|
287
|
+
|
|
288
|
+
```ruby
|
|
289
|
+
checkpoint_response.success # => true or false
|
|
290
|
+
checkpoint_response.errors # => array of Composer::ApiError (code, message)
|
|
291
|
+
checkpoint_response.version # => 'v1'
|
|
292
|
+
checkpoint_response.verification # => Composer::Verification, or nil
|
|
293
|
+
checkpoint_response.verification.id # => String
|
|
294
|
+
checkpoint_response.verification.status # => String
|
|
295
|
+
checkpoint_response.verification.outcome # => String
|
|
296
|
+
checkpoint_response.verification.step_data # => Hash
|
|
297
|
+
checkpoint_response.verification.next_steps # => Array
|
|
298
|
+
checkpoint_response.is_timeout # => true or false
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
#### Response fields
|
|
302
|
+
|
|
303
|
+
| Property | Description |
|
|
304
|
+
| --- | --- |
|
|
305
|
+
| `success` | `false` when no verification result could be produced. See [Errors](#errors). A verification that failed while running still has `success: true`. |
|
|
306
|
+
| `errors` | `Composer::ApiError` objects with a `message`, and a `code` when the API sends one. Usually empty when `success` is `true`. |
|
|
307
|
+
| `version` | The version of the Composer API that was used to make the request. |
|
|
308
|
+
| `verification` | The verification. Can be `nil` when `success` is `false`. |
|
|
309
|
+
| `verification.id` | The ID of the verification that was created. |
|
|
310
|
+
| `verification.status` | `COMPLETE`, `PENDING`, `BLOCKED` or `FAILED`. |
|
|
311
|
+
| `verification.outcome` | `APPROVED`, `DENIED`, `PENDING`, `ERROR`, `USAGE_LIMIT` or `API_LIMIT`. |
|
|
312
|
+
| `verification.step_data` | Data from the workflow's steps. |
|
|
313
|
+
| `verification.next_steps` | Steps the Client SDK runs next, such as MFA. Send them to your frontend as `nextSteps`, as the [Usage](#usage) example does, so the Client SDK can start them right away. |
|
|
314
|
+
| `is_timeout` | `true` when polling stopped after 3 failed polls in a row. See `timeout?` below. |
|
|
315
|
+
|
|
316
|
+
#### Statuses and outcomes
|
|
317
|
+
|
|
318
|
+
| `status` | `outcome` | Utility method | Meaning |
|
|
319
|
+
| --- | --- | --- | --- |
|
|
320
|
+
| `COMPLETE` | `APPROVED` | `allowed?` | Let the request proceed. |
|
|
321
|
+
| `COMPLETE` | `DENIED` | `denied?` | Deny the request. Return the verification to the frontend to show the reason. |
|
|
322
|
+
| `BLOCKED` | `PENDING` | `running?` | The workflow is waiting for input from the user, such as MFA or KYC. See [useVerification](#useverification). |
|
|
323
|
+
| `PENDING` | `PENDING` | `running?` | Still processing when the polling budget ran out. Resume later with `use_verification_id`. |
|
|
324
|
+
| `COMPLETE` | `PENDING` | `undecided?` | The workflow finished without reaching a verdict. Your application decides how to proceed. |
|
|
325
|
+
| `FAILED` | `ERROR` | `has_error?` | The workflow failed while running, for example a third-party service was unavailable. |
|
|
326
|
+
| `FAILED` | `USAGE_LIMIT` | `has_error?` | Your account reached its usage limit. |
|
|
327
|
+
| `FAILED` | `API_LIMIT` | `has_error?` | Your account is sending too many requests right now, or is deactivated. |
|
|
328
|
+
|
|
329
|
+
### Utility Methods
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
Each method takes a checkpoint response and returns a boolean. The same checks are available on the response itself (`checkpoint_response.allowed?` and so on).
|
|
334
|
+
|
|
335
|
+
| Method | Returns `true` when |
|
|
336
|
+
| --- | --- |
|
|
337
|
+
| `composer.allowed?(checkpoint_response)` | The request is allowed to proceed. |
|
|
338
|
+
| `composer.denied?(checkpoint_response)` | The request is denied. |
|
|
339
|
+
| `composer.running?(checkpoint_response)` | No verdict yet: the workflow is still processing or waiting on the user. |
|
|
340
|
+
| `composer.undecided?(checkpoint_response)` | The workflow finished without a verdict. |
|
|
341
|
+
| `composer.has_error?(checkpoint_response)` | `success` is `false`, or the workflow failed while running. `error?` is the same check. |
|
|
342
|
+
| `composer.timeout?(checkpoint_response)` | Polling stopped after 3 failed polls in a row. Running out of the polling `timeout` is not a timeout in this sense; see [Timeouts](#timeouts). |
|
|
343
|
+
|
|
344
|
+
### Errors
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
Only invalid input raises. Everything else comes back as a response, so a checkpoint never raises because of the network or the API. The errors in this table are all `Composer::ComposerError`.
|
|
349
|
+
|
|
350
|
+
| What happened | Raises? | What you get | Utility method |
|
|
351
|
+
| --- | --- | --- | --- |
|
|
352
|
+
| Missing secret key, or an invalid `api_version` or `log_level` | Yes, from the constructor: `Composer::MissingConfigError` or `Composer::InvalidConfigError` | | |
|
|
353
|
+
| Blank `checkpoint_name`, `nil` `event`, or neither `session_id` nor `source_token` | Yes, from `checkpoint`: `Composer::MissingParameterError` | | |
|
|
354
|
+
| The API rejected the request, for example there is no published workflow for the checkpoint name, or a usage or API limit was reached | No | `success: false`, the API's message in `errors` | `has_error?` |
|
|
355
|
+
| Network error, request timeout, or a response the SDK can't read | No | `success: false`, a `500` in `errors` | `has_error?` |
|
|
356
|
+
| 3 polls failed in a row | No | `success: false`, a `503` in `errors` | `has_error?`, `timeout?` |
|
|
357
|
+
| The workflow failed while running | No | `success: true`, status `FAILED`, outcome `ERROR` | `has_error?` |
|
|
358
|
+
| Invalid input to `event` | Never | Nothing; the SDK logs it (see [Track an Event](#track-an-event)) | |
|
|
359
|
+
|
|
360
|
+
When `success` is `false` there is no verdict at all. Don't treat it as a denial; decide whether to retry, proceed, or reject.
|
|
361
|
+
|
|
362
|
+
### useVerification
|
|
363
|
+
|
|
364
|
+
---
|
|
365
|
+
|
|
366
|
+
Sometimes the workflow needs more input from the user before it can reach a verdict. For example, if the user must complete MFA, the checkpoint response has `status` `BLOCKED` and `outcome` `PENDING`, and `running?` returns `true`. Return the verification to your frontend and pass it to the Client SDK's `handleVerification()` method, which gathers the input from the user. When the user is done, the frontend calls your API again with the verification ID, and your API passes it to `use_verification_id`. The checkpoint then uses that verification instead of creating a new one, so the user isn't asked twice. The [Usage](#usage) example shows the full flow.
|
|
367
|
+
|
|
368
|
+
**Important Note:** To prevent replay attacks, each verification ID can only be passed to `use_verification_id` once.
|
|
369
|
+
|
|
370
|
+
### Track an Event
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
You can track additional information about a user's journey by submitting tracking events from your server. This information will be added to the user's profile and is made available to checkpoints.
|
|
375
|
+
|
|
376
|
+
```ruby
|
|
377
|
+
composer.event(
|
|
378
|
+
event: Composer::TrackEvent.new(
|
|
379
|
+
type: 'ADD_TO_CART',
|
|
380
|
+
data: { product: { id: 'sku_123', quantity: 1 } }
|
|
381
|
+
),
|
|
382
|
+
session_id: 'session_def456'
|
|
383
|
+
)
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Along with the [identifiers](#identifiers), `event` takes:
|
|
387
|
+
|
|
388
|
+
| Parameter | Required | Description |
|
|
389
|
+
| --- | --- | --- |
|
|
390
|
+
| `event` | Yes | A `Composer::TrackEvent`. |
|
|
391
|
+
| `event.type` | Yes | A name for where the user is in their journey. Can be any non-blank string you choose. |
|
|
392
|
+
| `event.data` | No | Data to track. Defaults to `{}`. |
|
|
393
|
+
| `event.event_time` | No | When the event happened, in milliseconds since the epoch. Defaults to now. |
|
|
394
|
+
|
|
395
|
+
`event` doesn't raise for invalid input and returns `nil`, so tracking can't break your request. `notify` and `track` are aliases. Problems are logged at `ERROR` instead:
|
|
396
|
+
|
|
397
|
+
- If `event` is `nil`, `event.type` is blank, or neither `session_id` nor `source_token` is provided, the event is not sent and the SDK logs `Composer event was not sent: <reason>`.
|
|
398
|
+
- If the API rejects the event, the SDK logs `Composer event was not accepted by the API (HTTP <status>).`
|
|
399
|
+
- If the request fails (network error or timeout), the SDK logs `There was an error making a request.`
|
|
400
|
+
|
|
401
|
+
## Development
|
|
402
|
+
|
|
403
|
+
### Versioning
|
|
404
|
+
|
|
405
|
+
The SDK version is declared in `lib/composer/version.rb` (`VERSION`). The gemspec and the `Composer-SDK-Version` header both read it.
|
|
406
|
+
|
|
407
|
+
### Testing
|
|
408
|
+
|
|
409
|
+
The project uses RSpec for testing.
|
|
410
|
+
|
|
411
|
+
To run tests:
|
|
412
|
+
|
|
413
|
+
```sh
|
|
414
|
+
bundle install
|
|
415
|
+
bundle exec rspec
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Each run writes a coverage report to `coverage/index.html`.
|
|
419
|
+
|
|
420
|
+
### Code Style
|
|
421
|
+
|
|
422
|
+
This project uses RuboCop for code style enforcement:
|
|
423
|
+
|
|
424
|
+
```sh
|
|
425
|
+
bundle exec rubocop
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Testing with the examples
|
|
429
|
+
|
|
430
|
+
To try local SDK changes end to end, clone [Composer examples](https://github.com/spreedly/composer-examples) next to this repository, point the `server/ruby` example at your checkout in its `Gemfile`, and run it as the examples README describes:
|
|
431
|
+
|
|
432
|
+
```ruby
|
|
433
|
+
gem 'spreedly-composer-sdk-server', path: '../../../composer-sdk-server-ruby'
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Restart the example after each change.
|
|
437
|
+
|
|
438
|
+
## License
|
|
439
|
+
|
|
440
|
+
This SDK is released under the Apache-2.0 License. See [LICENSE](LICENSE) for details, and [THIRD-PARTY-LICENSES](THIRD-PARTY-LICENSES) for the licenses of its dependencies.
|
|
441
|
+
|
|
442
|
+
## Support
|
|
443
|
+
|
|
444
|
+
For support, please contact:
|
|
445
|
+
|
|
446
|
+
- Email: support@spreedly.com
|
|
447
|
+
- Documentation: https://developer.spreedly.com
|