@nile-squad/nylonpay-ts 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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nile Squad
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,233 @@
1
+ # Nylon Pay TypeScript SDK
2
+
3
+ Server-side SDK for integrating Nylon Pay into merchant applications. Supports TypeScript and JavaScript (ESM and CJS).
4
+
5
+ This package is the reference implementation of the [Nylon Pay SDK Spec](https://github.com/nile-squad/specs/blob/main/nylon-pay/sdk-spec.md) — the canonical, language-agnostic contract for building Nylon Pay SDKs in any language.
6
+
7
+ [Full documentation](https://docs.nylonpay.nilesquad.com/docs)
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @nile-squad/nylonpay-ts
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```ts
18
+ import { createNylonPay } from "@nile-squad/nylonpay-ts";
19
+
20
+ const nylonpay = createNylonPay({
21
+ apiKey: "npk_...",
22
+ apiSecret: "nps_...",
23
+ });
24
+
25
+ const payment = await nylonpay.collectPayment({
26
+ amount: 10000,
27
+ currency: "UGX",
28
+ customer: { name: "Jane", phoneNumber: "+256700000000" },
29
+ description: "Order #1234",
30
+ });
31
+
32
+ payment.on("success", ({ transaction }) => fulfillOrder(transaction));
33
+ payment.on("failed", ({ error }) => notifyCustomer(error));
34
+ ```
35
+
36
+ ## Configuration
37
+
38
+ > Test vs. live mode is selected by your API key — a sandbox key routes to test
39
+ > providers, a live key processes real money. There is no `environment` option.
40
+
41
+ | Option | Required | Default | Description |
42
+ |---|---|---|---|
43
+ | `apiKey` | Yes | | Must start with `npk_` |
44
+ | `apiSecret` | Yes | | Must start with `nps_` |
45
+ | `baseUrl` | No | `https://api.nylonpay.io/api/services` | API endpoint |
46
+ | `timeoutMs` | No | `30000` | Request timeout in milliseconds |
47
+ | `maxRetries` | No | `3` | Retry count for failed requests |
48
+ | `maxPollIntervalMs` | No | `2000` | Polling interval for async payments |
49
+ | `maxPollDurationMs` | No | `300000` | Maximum polling duration in milliseconds |
50
+ | `maxPollAttempts` | No | `150` | Maximum polling attempts |
51
+
52
+ ## Operations
53
+
54
+ ### collectPayment
55
+
56
+ Initiate a payment collection. Returns a `PaymentInstance` with event-driven updates.
57
+
58
+ ```ts
59
+ const payment = await nylonpay.collectPayment({
60
+ amount: 10000,
61
+ currency: "UGX",
62
+ customer: { name: "Jane", phoneNumber: "+256700000000" },
63
+ description: "Order #1234",
64
+ method: "mobileMoney",
65
+ reference: "ORDER-123",
66
+ });
67
+
68
+ payment.on("success", ({ transaction }) => { /* ... */ });
69
+ payment.on("failed", ({ error }) => { /* ... */ });
70
+ ```
71
+
72
+ `reference` is optional and auto-generated if omitted.
73
+
74
+ ### collectPaymentAndResolve
75
+
76
+ Block until the collection reaches a terminal state. Single request and response, no client-side polling.
77
+
78
+ ```ts
79
+ const result = await nylonpay.collectPaymentAndResolve({
80
+ amount: 5000,
81
+ currency: "UGX",
82
+ customer: { name: "Jane", phoneNumber: "+256700000000" },
83
+ description: "Quick payment",
84
+ });
85
+
86
+ if (result.isOk) console.log("Paid:", result.value.reference);
87
+ ```
88
+
89
+ ### makePayout
90
+
91
+ Disburse funds to a destination account.
92
+
93
+ ```ts
94
+ const payout = await nylonpay.makePayout({
95
+ amount: 50000,
96
+ currency: "UGX",
97
+ customer: { name: "Jane", phoneNumber: "+256700000000" },
98
+ destination: { accountHolderName: "Jane Doe", accountNumber: "123456" },
99
+ description: "Refund for order #1234",
100
+ });
101
+
102
+ const tx = await payout.wait();
103
+ ```
104
+
105
+ ### makePayoutAndResolve
106
+
107
+ Block until the payout reaches a terminal state. Single request and response.
108
+
109
+ ```ts
110
+ const result = await nylonpay.makePayoutAndResolve({
111
+ amount: 50000,
112
+ currency: "UGX",
113
+ customer: { name: "Jane", phoneNumber: "+256700000000" },
114
+ destination: { accountHolderName: "Jane Doe", accountNumber: "123456" },
115
+ description: "Refund",
116
+ });
117
+ ```
118
+
119
+ ### getStatus
120
+
121
+ One-shot status check for a transaction. Does not poll, returns the current server-side state.
122
+
123
+ ```ts
124
+ const result = await nylonpay.getStatus({ reference: "ORDER-123" });
125
+ if (result.isOk) console.log(result.value.status);
126
+ ```
127
+
128
+ ### getTransaction
129
+
130
+ Look up a full transaction record by `id` or `reference`. At least one must be provided.
131
+
132
+ ```ts
133
+ const result = await nylonpay.getTransaction({ reference: "ORDER-123" });
134
+ if (result.isOk) console.log(result.value.failureReason);
135
+ ```
136
+
137
+ ### verifyPhone
138
+
139
+ Pre-validate a phone number and get the registered name.
140
+
141
+ ```ts
142
+ const result = await nylonpay.verifyPhone({ phoneNumber: "+256700000000" });
143
+ if (result.isOk && result.value.verified) {
144
+ console.log("Registered to:", result.value.customerName);
145
+ }
146
+ ```
147
+
148
+ ### createInvoice
149
+
150
+ Generate a hosted payment link. Card payments are only supported via this hosted flow.
151
+
152
+ ```ts
153
+ const result = await nylonpay.createInvoice({
154
+ amount: 25000,
155
+ currency: "UGX",
156
+ description: "Monthly subscription",
157
+ items: [{ name: "Pro Plan", quantity: 1, unitPrice: 25000 }],
158
+ redirectUrl: "https://myapp.com/thank-you",
159
+ });
160
+
161
+ if (result.isOk) sendEmail(result.value.url);
162
+ ```
163
+
164
+ ### verifyWebhookSignature
165
+
166
+ Verify incoming webhook payloads before processing.
167
+
168
+ ```ts
169
+ app.post("/webhooks", (req, res) => {
170
+ const isValid = nylonpay.verifyWebhookSignature({
171
+ payload: req.rawBody,
172
+ signature: req.headers["x-nylon-signature"],
173
+ secret: process.env.NYLONPAY_WEBHOOK_SECRET,
174
+ });
175
+
176
+ if (!isValid) return res.status(401).send("Invalid signature");
177
+ });
178
+ ```
179
+
180
+ ## PaymentInstance Events
181
+
182
+ `collectPayment` and `makePayout` return a `PaymentInstance` with event-driven updates.
183
+
184
+ | Event | Description |
185
+ |---|---|
186
+ | `processing` | Transaction is being processed |
187
+ | `success` | Transaction completed successfully |
188
+ | `failed` | Transaction failed |
189
+ | `cancelled` | Transaction was cancelled |
190
+ | `error` | Network or polling error |
191
+
192
+ ```ts
193
+ payment.on("success", ({ transaction }) => { /* ... */ });
194
+ payment.once("success", ({ transaction }) => { /* fires once */ });
195
+ payment.off("success", handler);
196
+
197
+ const tx = await payment.wait();
198
+ ```
199
+
200
+ ## Error Handling
201
+
202
+ All operations return `Result<T, string>` from [slang-ts](https://github.com/nile-squad/slang-ts). Use `parseError` to get structured error objects.
203
+
204
+ ```ts
205
+ import { parseError } from "@nile-squad/nylonpay-ts";
206
+
207
+ const result = await nylonpay.getStatus({ reference: "ORDER-123" });
208
+ if (!result.isOk) {
209
+ const error = parseError(result.error);
210
+ if (error.retryable) {
211
+ // Retry the request
212
+ }
213
+ }
214
+ ```
215
+
216
+ ## Supported Currencies
217
+
218
+ `USD`, `EUR`, `GBP`, `KES`, `UGX`, `TZS`, `RWF`
219
+
220
+ ## Development
221
+
222
+ Maintainer notes and pending work live in [`dev-note.md`](./dev-note.md).
223
+
224
+ ```sh
225
+ pnpm install
226
+ pnpm test # vitest
227
+ pnpm typecheck # tsc --noEmit
228
+ pnpm build # tsup
229
+ ```
230
+
231
+ ## License
232
+
233
+ MIT