@1st-flock/donations-embed-node 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.
- package/LICENSE +21 -0
- package/README.md +236 -0
- package/dist/index.cjs +648 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +381 -0
- package/dist/index.d.ts +381 -0
- package/dist/index.js +625 -0
- package/dist/index.js.map +1 -0
- package/dist/webhooks-DBoxlrQm.d.cts +438 -0
- package/dist/webhooks-DBoxlrQm.d.ts +438 -0
- package/dist/webhooks.cjs +181 -0
- package/dist/webhooks.cjs.map +1 -0
- package/dist/webhooks.d.cts +1 -0
- package/dist/webhooks.d.ts +1 -0
- package/dist/webhooks.js +178 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 1st Flock, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# @1st-flock/donations-embed-node
|
|
2
|
+
|
|
3
|
+
Server-side Node SDK for the 1st Flock Donations Embed API. Typed client for donations and recurring schedules, plus a constant-time HMAC-SHA256 webhook verifier. Zero runtime dependencies; ships ESM and CJS.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@1st-flock/donations-embed-node)
|
|
6
|
+
[](https://bundlephobia.com/package/@1st-flock/donations-embed-node)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @1st-flock/donations-embed-node
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @1st-flock/donations-embed-node
|
|
15
|
+
# or
|
|
16
|
+
yarn add @1st-flock/donations-embed-node
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires Node 18.17 or newer (Bun and Deno work too with their Node-compat shims). No third-party runtime dependencies — just Node's built-in `fetch` and `crypto`.
|
|
20
|
+
|
|
21
|
+
## Quickstart
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Client } from "@1st-flock/donations-embed-node";
|
|
25
|
+
|
|
26
|
+
const client = new Client({ apiKey: process.env.FLOCK_SECRET_KEY! });
|
|
27
|
+
|
|
28
|
+
// List the most recent 25 donations.
|
|
29
|
+
const page = await client.listDonations({ limit: 25 });
|
|
30
|
+
for (const donation of page.donations) {
|
|
31
|
+
console.log(donation.id, donation.gross_amount_cents, donation.status);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Run this against a sandbox key (`sk_test_…`) and any donation made through the embed (e.g. with sandbox card `4111 1111 1111 1111`, exp `10/29`, CVV `123`) shows up here within a few seconds.
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
Generate keys in the 1st Flock account portal under **Donations Setup → Embed Keys** ([full guide](https://1stflock.com/developers/donations-embed/)). The server SDK requires a **secret** key:
|
|
40
|
+
|
|
41
|
+
- **Secret keys** (`sk_live_…`, `sk_test_…`) — server-only. Never ship a secret key to a browser or any client-rendered code. Treat them like any other production credential — store in a secrets manager, rotate on suspicion of compromise.
|
|
42
|
+
- **Webhook signing secret** — issued separately when you create or rotate a webhook endpoint. Used by `verifyWebhook` to authenticate inbound deliveries. Store alongside the secret key.
|
|
43
|
+
|
|
44
|
+
The constructor rejects publishable keys (`pk_…`) at construction time with a clear `TypeError` so misconfiguration surfaces synchronously rather than as a confusing 401.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const client = new Client({
|
|
48
|
+
apiKey: process.env.FLOCK_SECRET_KEY!,
|
|
49
|
+
// Optional overrides:
|
|
50
|
+
baseUrl: "https://api.sandbox.1stflock.com", // also auto-resolved by sk_test_ prefix
|
|
51
|
+
retries: 3, // retries on 5xx + network errors (default 3)
|
|
52
|
+
timeout: 30_000, // per-request timeout in ms (default 30_000)
|
|
53
|
+
logger: console, // anything with debug(message, ...args)
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Test mode
|
|
58
|
+
|
|
59
|
+
Pass a sandbox key (`sk_test_…`) — the SDK routes to the sandbox cluster automatically based on the `_test_` prefix. No real money moves. Same code path, same wire format, same webhook events. Donations show up in the hub with a **TEST** badge and never appear in the production ledger.
|
|
60
|
+
|
|
61
|
+
## Common tasks
|
|
62
|
+
|
|
63
|
+
### List + paginate donations
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
let cursor: string | undefined;
|
|
67
|
+
do {
|
|
68
|
+
const page = await client.listDonations({ cursor, limit: 200 });
|
|
69
|
+
for (const donation of page.donations) {
|
|
70
|
+
await syncToLedger(donation);
|
|
71
|
+
}
|
|
72
|
+
cursor = page.next_cursor ?? undefined;
|
|
73
|
+
} while (cursor);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Filter by fund or donor email:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const buildingFundOnly = await client.listDonations({ fundId: "general" });
|
|
80
|
+
const samsHistory = await client.listDonations({ donorEmail: "sam@example.com" });
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Fetch a single donation
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const donation = await client.getDonation("8a1b9c4d-1234-4321-9abc-def012345678");
|
|
87
|
+
console.log(donation.confirmation_id, donation.gross_amount_cents);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Cross-organization access deliberately collapses to `404` (`NotFoundError`) so you cannot use the API to confirm whether a UUID belongs to another tenant.
|
|
91
|
+
|
|
92
|
+
### Refund a donation
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { Client, idempotencyKey } from "@1st-flock/donations-embed-node";
|
|
96
|
+
|
|
97
|
+
// Full refund:
|
|
98
|
+
await client.refundDonation(donationId);
|
|
99
|
+
|
|
100
|
+
// Partial refund with an idempotency key (safe to retry on network blip):
|
|
101
|
+
const refund = await client.refundDonation(
|
|
102
|
+
donationId,
|
|
103
|
+
{ amountCents: 1000, reason: "Donor adjustment" },
|
|
104
|
+
{ idempotencyKey: idempotencyKey("refund") },
|
|
105
|
+
);
|
|
106
|
+
console.log(refund.processor_refund_id, refund.refund_amount_cents);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The `idempotencyKey()` helper returns a tagged UUID like `"refund:1f2dab36-2a55-4f11-bdc8-2a2a4a95b001"`. Replays of the same key return the original response without re-contacting the gateway.
|
|
110
|
+
|
|
111
|
+
### List + cancel recurring schedules
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const page = await client.listRecurring({ status: "active", limit: 100 });
|
|
115
|
+
for (const recurring of page.recurring) {
|
|
116
|
+
if (donorOptedOut(recurring.donor_email)) {
|
|
117
|
+
await client.cancelRecurring(recurring.id, {
|
|
118
|
+
idempotencyKey: idempotencyKey("cancel-recurring"),
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Fire a synthetic webhook (integration test)
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
const result = await client.testWebhook("donation.completed");
|
|
128
|
+
console.log("test event id:", result.event_id);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The synthetic event's `data.object.synthetic` flag is set so downstream code can branch on it.
|
|
132
|
+
|
|
133
|
+
## Webhook verification
|
|
134
|
+
|
|
135
|
+
Verify every inbound delivery with `verifyWebhook` — constant-time HMAC-SHA256 comparison plus a 5-minute timestamp tolerance to defeat replays.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import express from "express";
|
|
139
|
+
import { verifyWebhook, WebhookError } from "@1st-flock/donations-embed-node";
|
|
140
|
+
|
|
141
|
+
const app = express();
|
|
142
|
+
|
|
143
|
+
app.post(
|
|
144
|
+
"/webhooks/donations",
|
|
145
|
+
express.raw({ type: "application/json" }), // give us the raw body bytes
|
|
146
|
+
(req, res) => {
|
|
147
|
+
const signature = req.header("Flock-Signature") ?? "";
|
|
148
|
+
try {
|
|
149
|
+
const event = verifyWebhook(req.body, signature, process.env.FLOCK_WEBHOOK_SECRET!);
|
|
150
|
+
switch (event.type) {
|
|
151
|
+
case "donation.completed":
|
|
152
|
+
enqueueReceipt(event.data.object);
|
|
153
|
+
break;
|
|
154
|
+
case "donation.refunded":
|
|
155
|
+
reverseReceipt(event.data.object);
|
|
156
|
+
break;
|
|
157
|
+
case "key.frozen":
|
|
158
|
+
alertOnCall(event.data.object);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
res.status(204).end();
|
|
162
|
+
} catch (err) {
|
|
163
|
+
// Always 401 — never confirm which check failed.
|
|
164
|
+
if (err instanceof WebhookError) {
|
|
165
|
+
return res.status(401).send("invalid signature");
|
|
166
|
+
}
|
|
167
|
+
throw err;
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Pass the **exact body bytes** the platform delivered. Re-serializing the JSON before verifying will invalidate the signature.
|
|
174
|
+
|
|
175
|
+
`verifyWebhook` raises typed exceptions on failure (`WebhookFormatError`, `WebhookTimestampError`, `WebhookSignatureError` — all inherit from `WebhookError`) so you can log distinct failure modes even though the HTTP response is the same `401` in every case.
|
|
176
|
+
|
|
177
|
+
For consumers that need only the verifier (and don't want to load the API client surface), import from the dedicated subpath:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import { verifyWebhook } from "@1st-flock/donations-embed-node/webhooks";
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Errors
|
|
184
|
+
|
|
185
|
+
Every API call raises a typed exception on a non-2xx response.
|
|
186
|
+
|
|
187
|
+
| HTTP status | Class |
|
|
188
|
+
| --- | --- |
|
|
189
|
+
| 400 / 422 | `ValidationError` |
|
|
190
|
+
| 401 | `AuthError` |
|
|
191
|
+
| 403 | `ForbiddenError` |
|
|
192
|
+
| 404 | `NotFoundError` |
|
|
193
|
+
| 409 | `ConflictError` |
|
|
194
|
+
| 410 | `GoneError` |
|
|
195
|
+
| 429 | `RateLimitError` (carries `retryAfterSeconds`) |
|
|
196
|
+
| 502 | `BadGatewayError` |
|
|
197
|
+
| 503 | `ServiceUnavailableError` |
|
|
198
|
+
| Other | `ApiError` |
|
|
199
|
+
|
|
200
|
+
All inherit from `ApiError` and expose `statusCode`, `code`, `message`, `details`, and `requestId`.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
import { ApiError, RateLimitError, ConflictError } from "@1st-flock/donations-embed-node";
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
await client.refundDonation(donationId, { amountCents: 1000 });
|
|
207
|
+
} catch (err) {
|
|
208
|
+
if (err instanceof RateLimitError) {
|
|
209
|
+
await sleep((err.retryAfterSeconds ?? 1) * 1000);
|
|
210
|
+
return retry();
|
|
211
|
+
}
|
|
212
|
+
if (err instanceof ConflictError) {
|
|
213
|
+
return console.warn("Already refunded:", donationId);
|
|
214
|
+
}
|
|
215
|
+
if (err instanceof ApiError) {
|
|
216
|
+
console.error(`API error ${err.statusCode} ${err.code}: ${err.message}`);
|
|
217
|
+
}
|
|
218
|
+
throw err;
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Full documentation
|
|
223
|
+
|
|
224
|
+
The full reference — every endpoint, every error code, the webhook event catalogue with payload schemas, and operations guidance — lives at [1stflock.com/developers/donations-embed/](https://1stflock.com/developers/donations-embed/).
|
|
225
|
+
|
|
226
|
+
## Vendor neutrality
|
|
227
|
+
|
|
228
|
+
This SDK never names the underlying payment processor, bank-link service, or any other third-party vendor in customer-facing strings. Donations carry vendor-neutral identifiers (`processor_transaction_id`, `processor_refund_id`, `processor_subscription_id`); errors route through 1st Flock's own taxonomy. The underlying integrations are an implementation detail and may change without notice — your code never has to.
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
MIT — see [LICENSE](./LICENSE).
|
|
233
|
+
|
|
234
|
+
## Reporting issues
|
|
235
|
+
|
|
236
|
+
File issues at [gitlab.com/1st-flock/donations/-/issues](https://gitlab.com/1st-flock/donations/-/issues) with the label `donations-embed-node`.
|