@havenpay/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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 7Haven
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.
22
+
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # Havenpay Server SDK
2
+
3
+ Backend-only TypeScript SDK for Havenpay merchant integrations.
4
+
5
+ This package is for trusted server runtimes only. It uses secret API keys, server-side webhook verification, and management APIs that must never be imported by `@havenpay/web` or `@havenpay/react-native`.
6
+
7
+ ## Implemented Surface
8
+
9
+ ```ts
10
+ import { Havenpay } from '@havenpay/server'
11
+
12
+ const _havenpay = new Havenpay({
13
+ secretKey: process.env.HAVENPAY_SECRET_KEY!,
14
+ apiVersion: '2026-05-24.haven',
15
+ })
16
+
17
+ const _intent = await havenpay.paymentIntents.create({
18
+ projectId: 'proj_...',
19
+ amount: 1000,
20
+ currency: 'usd',
21
+ customer: { phone: '+263771234567' },
22
+ mobileMoney: { provider: 'fake', phone: '+263771234567' },
23
+ idempotencyKey: crypto.randomUUID(),
24
+ })
25
+ ```
26
+
27
+ Available resources:
28
+
29
+ - `accounts.create/list/update`
30
+ - `apiKeys.create/list/revoke`
31
+ - `balanceTransactions.list`
32
+ - `events.list/replay`
33
+ - `paymentIntents.create/retrieve`
34
+ - `projects.create/list/update/archive/getConfig`
35
+ - `providerAccounts.create/list/rotate/revoke`
36
+ - `refunds.create/retrieve/list`
37
+ - `webhookEndpoints.create/list/update/delete`
38
+ - `webhooks.constructEvent`
39
+
40
+ ## Error Handling
41
+
42
+ Non-2xx REST responses throw `HavenpayApiError`. The class extends `Error` and preserves the legacy message format:
43
+
44
+ ```text
45
+ Havenpay API Error: <status> <body>
46
+ ```
47
+
48
+ It also exposes:
49
+
50
+ - `status`
51
+ - `requestId`
52
+ - `body`
53
+ - `error`
54
+ - `type`
55
+ - `code`
56
+
57
+ ```ts
58
+ import { HavenpayApiError } from '@havenpay/server'
59
+
60
+ try {
61
+ await havenpay.paymentIntents.retrieve('pi_missing')
62
+ }
63
+ catch (error) {
64
+ if (error instanceof HavenpayApiError) {
65
+ console.error(error.status, error.requestId, error.code)
66
+ }
67
+ }
68
+ ```
69
+
70
+ Configured request timeouts throw `HavenpayTimeoutError`:
71
+
72
+ ```ts
73
+ import { HavenpayTimeoutError } from '@havenpay/server'
74
+
75
+ try {
76
+ await havenpay.paymentIntents.retrieve('pi_slow')
77
+ }
78
+ catch (error) {
79
+ if (error instanceof HavenpayTimeoutError) {
80
+ console.error(error.timeoutMs)
81
+ }
82
+ }
83
+ ```
84
+
85
+ ## Retry Policy
86
+
87
+ Retries are disabled by default. Configure `retry` to retry network failures and HTTP 5xx responses with exponential backoff:
88
+
89
+ ```ts
90
+ const _havenpay = new Havenpay({
91
+ secretKey: process.env.HAVENPAY_SECRET_KEY!,
92
+ retry: {
93
+ maxAttempts: 3,
94
+ initialDelayMs: 100,
95
+ maxDelayMs: 2000,
96
+ },
97
+ })
98
+ ```
99
+
100
+ - Safe reads may retry.
101
+ - Mutating requests retry only when they carry an `Idempotency-Key`.
102
+ - Do not retry validation, authentication, authorization, tenant-scope, or not-found errors without changing the request or credentials.
103
+
104
+ ## API-Version Pinning
105
+
106
+ Set `apiVersion` to send `X-Haven-API-Version` on server SDK REST requests:
107
+
108
+ ```ts
109
+ const _havenpay = new Havenpay({
110
+ secretKey: process.env.HAVENPAY_SECRET_KEY!,
111
+ apiVersion: '2026-05-24.haven',
112
+ })
113
+ ```
114
+
115
+ ## Timeouts
116
+
117
+ Set `timeoutMs` to apply a per-attempt timeout to server SDK REST requests:
118
+
119
+ ```ts
120
+ const _havenpay = new Havenpay({
121
+ secretKey: process.env.HAVENPAY_SECRET_KEY!,
122
+ timeoutMs: 30000,
123
+ })
124
+ ```
125
+
126
+ Caller-provided abort signals are preserved by the shared request helper and are not converted into timeout errors.
127
+
128
+ ## Observability
129
+
130
+ Set `onRequestEvent` to receive redacted server SDK request lifecycle events:
131
+
132
+ ```ts
133
+ const _havenpay = new Havenpay({
134
+ secretKey: process.env.HAVENPAY_SECRET_KEY!,
135
+ onRequestEvent: (event) => {
136
+ console.info(event.type, event.operationName, event.requestId, event.status)
137
+ },
138
+ })
139
+ ```
140
+
141
+ Events include method, path, generated operation name, attempt, status, error kind, and API response request ID only. They do not include headers, request bodies, API keys, idempotency keys, provider credentials, webhook secrets, or client secrets.
142
+
143
+ ## Webhook Verification
144
+
145
+ `webhooks.constructEvent` verifies the exact raw request body with the endpoint webhook secret:
146
+
147
+ ```ts
148
+ const _event = havenpay.webhooks.constructEvent({
149
+ rawBody,
150
+ signature: request.headers.get('havenpay-signature') ?? '',
151
+ secret: process.env.HAVENPAY_WEBHOOK_SECRET!,
152
+ })
153
+ ```
154
+
155
+ ## Boundaries
156
+
157
+ - Do not use this package in browser or React Native code.
158
+ - Do not pass provider credentials or webhook secrets to client SDKs.
159
+ - Do not claim live provider support unless the provider adapter and tests prove it.
160
+ - Public client integrations should use `projectId` plus a short-lived payment `clientSecret` through the Web or React Native SDKs.