@payclave/sdk-server 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @payclave/sdk-server
2
+
3
+ ## 0.1.0
4
+
5
+ Initial production-ready release.
6
+
7
+ ### Added
8
+
9
+ - `createPayclaveServerClient` for secret-key server integrations.
10
+ - Typed checkout session, invoice, payment, webhook endpoint, test webhook, and webhook delivery helpers.
11
+ - `Idempotency-Key` support for checkout-session and invoice creation.
12
+ - Abort and timeout support on every API request.
13
+ - Structured `PayclaveError` with `code`, `status`, `requestId`, `details`, and `cause`.
14
+ - Exported typed API, SDK, and webhook verification error-code vocabularies.
15
+ - Client-side guards for invalid base URLs, timeouts, identifiers, expiry windows, metadata, webhook URLs, event types, and non-serializable request bodies.
16
+ - Node-side Payclave webhook signature verification and event construction helpers.
17
+ - Dual ESM/CJS build with TypeScript declarations.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Payclave
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,162 @@
1
+ # @payclave/sdk-server
2
+
3
+ TypeScript server SDK for [Payclave](https://payclave.com) — non-custodial crypto checkout for merchants.
4
+
5
+ Use this package from your backend with a Payclave secret key. Never expose `sk_test_...` or `sk_live_...` keys in browser code.
6
+
7
+ Package name: [`@payclave/sdk-server`](https://www.npmjs.com/package/@payclave/sdk-server).
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @payclave/sdk-server
13
+ # or
14
+ bun add @payclave/sdk-server
15
+ ```
16
+
17
+ ## Create a checkout session
18
+
19
+ ```ts
20
+ import { createPayclaveServerClient } from "@payclave/sdk-server"
21
+
22
+ const payclave = createPayclaveServerClient({
23
+ secretKey: process.env.PAYCLAVE_SECRET_KEY!,
24
+ })
25
+
26
+ export async function POST(request: Request) {
27
+ const order = await request.json()
28
+
29
+ const session = await payclave.createCheckoutSession({
30
+ amount: order.total,
31
+ externalReference: order.id,
32
+ customerEmail: order.customerEmail,
33
+ successUrl: `https://merchant.example/orders/${order.id}/success`,
34
+ cancelUrl: "https://merchant.example/cart",
35
+ metadata: {
36
+ cartId: order.cartId,
37
+ },
38
+ idempotencyKey: order.id,
39
+ })
40
+
41
+ return Response.json({ checkoutUrl: session.checkoutUrl })
42
+ }
43
+ ```
44
+
45
+ ## API
46
+
47
+ ### `createPayclaveServerClient(options)`
48
+
49
+ | Option | Type | Description |
50
+ | ------------ | ------------------------- | ----------------------------------------------------------------- |
51
+ | `secretKey` | `string` | `sk_test_...` or `sk_live_...`. Required. |
52
+ | `apiBaseUrl` | `string` | Override the API base URL. Defaults to `https://api.payclave.com`. |
53
+ | `fetcher` | `(url, init) => Response` | Custom fetch implementation for tests or custom runtimes. |
54
+ | `timeoutMs` | `number` | Per-request timeout. |
55
+
56
+ Returns typed helpers:
57
+
58
+ - `createCheckoutSession(input)`
59
+ - `getCheckoutSession(id)`
60
+ - `createInvoice(input)`
61
+ - `getInvoice(id)`
62
+ - `getPayment(id)`
63
+ - `createWebhookEndpoint(input)`
64
+ - `createTestWebhook(input)`
65
+ - `listWebhookDeliveries(input?)`
66
+
67
+ ### Authentication
68
+
69
+ This SDK is for backend code only and sends `Authorization: Bearer sk_test_...` or `Authorization: Bearer sk_live_...` on every request. Use publishable `pk_test_...` or `pk_live_...` keys only with browser checkout integrations.
70
+
71
+ ### Checkout session input
72
+
73
+ | Field | Type | Description |
74
+ | ------------------ | ------------- | ---------------------------------------------------------------------------- |
75
+ | `amount` | `string` | Positive USDT decimal with up to 6 fractional digits, e.g. `"25.00"`. |
76
+ | `externalReference` | `string?` | Your order reference. |
77
+ | `customerEmail` | `string?` | Customer email for checkout records. |
78
+ | `successUrl` | `string?` | URL to return to after payment. |
79
+ | `cancelUrl` | `string?` | URL to return to when checkout is cancelled. |
80
+ | `metadata` | `object?` | Merchant-defined metadata. |
81
+ | `expiresInMinutes` | `number?` | Checkout expiry, 5 to 1440 minutes. |
82
+ | `idempotencyKey` | `string?` | Sent as `Idempotency-Key`; reuse only for retries of the same request. |
83
+ | `signal` | `AbortSignal?` | Cancel the request. |
84
+
85
+ ### Invoice input
86
+
87
+ `createInvoice` accepts `amount`, `externalReference`, `metadata`, `expiresInMinutes`, `idempotencyKey`, and `signal`. Reuse the same `idempotencyKey` only when retrying the same invoice creation request after a timeout, network failure, `RATE_LIMIT_EXCEEDED`, or 5xx response.
88
+
89
+ ### Webhook endpoints
90
+
91
+ `createWebhookEndpoint` accepts a `url`, optional `eventTypes`, and `signal`. Supported event types are exported as `PAYCLAVE_WEBHOOK_EVENT_TYPES` and currently include `invoice.paid`, `invoice.expired`, `payment.failed`, `payment.underpaid`, and `payment.overpaid`.
92
+
93
+ Payclave returns the endpoint `signingSecret` once when the endpoint is created. Store it securely and use it to verify incoming webhook deliveries.
94
+
95
+ ## Webhook signatures
96
+
97
+ Verify Payclave webhooks against the exact raw request body:
98
+
99
+ ```ts
100
+ import { constructPayclaveWebhookEvent } from "@payclave/sdk-server"
101
+
102
+ export async function POST(request: Request) {
103
+ const rawBody = await request.text()
104
+ const signature = request.headers.get("X-Payclave-Signature") ?? ""
105
+
106
+ const event = constructPayclaveWebhookEvent({
107
+ payload: rawBody,
108
+ signature,
109
+ secret: process.env.PAYCLAVE_WEBHOOK_SECRET!,
110
+ })
111
+
112
+ if (event.type === "invoice.paid") {
113
+ // Fulfill the order referenced by event.data.
114
+ }
115
+
116
+ return new Response(null, { status: 204 })
117
+ }
118
+ ```
119
+
120
+ `constructPayclaveWebhookEvent` throws `PayclaveError` when the signature, timestamp, or payload is invalid. Use `verifyPayclaveWebhookSignature` when you only need a boolean.
121
+
122
+ Webhook deliveries include `X-Payclave-Signature`, `X-Payclave-Timestamp`, `X-Payclave-Delivery`, and `X-Payclave-Event` headers. The signature format is `t=<unix_timestamp>,v1=<hex_hmac_sha256>`.
123
+
124
+ ## Errors
125
+
126
+ All client- and server-side failures throw a `PayclaveError`:
127
+
128
+ ```ts
129
+ import { PayclaveError } from "@payclave/sdk-server"
130
+
131
+ try {
132
+ await payclave.createCheckoutSession({ amount: "25.00" })
133
+ } catch (err) {
134
+ if (err instanceof PayclaveError) {
135
+ err.code
136
+ err.status
137
+ err.requestId
138
+ err.details
139
+ err.cause
140
+ }
141
+ }
142
+ ```
143
+
144
+ Built-in client codes include `INVALID_SECRET_KEY`, `INVALID_API_BASE_URL`, `INVALID_TIMEOUT`, `INVALID_ID`, `INVALID_AMOUNT`, `INVALID_EXPIRY`, `INVALID_METADATA`, `INVALID_IDEMPOTENCY_KEY`, `INVALID_LIMIT`, `INVALID_WEBHOOK_URL`, `INVALID_REQUEST_BODY`, `INVALID_RESPONSE`, `NETWORK_ERROR`, `TIMEOUT`, `ABORTED`, and webhook verification errors. Server-supplied API codes are passed through unchanged.
145
+
146
+ The package exports typed error-code lists for exhaustive handling and autocomplete:
147
+
148
+ - `PAYCLAVE_API_ERROR_CODES`
149
+ - `PAYCLAVE_SERVER_SDK_ERROR_CODES`
150
+ - `PAYCLAVE_WEBHOOK_VERIFICATION_ERROR_CODES`
151
+ - `PayclaveApiErrorCode`
152
+ - `PayclaveServerSdkErrorCode`
153
+ - `PayclaveWebhookVerificationErrorCode`
154
+ - `PayclaveErrorCode`
155
+
156
+ ## SDK identification
157
+
158
+ Every request includes `X-Payclave-Client: payclave-sdk-server/<version>` so the API can correlate bug reports to SDK versions.
159
+
160
+ ## License
161
+
162
+ MIT — see [LICENSE](./LICENSE).