xendit-fn 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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +442 -0
  3. package/lib/index.cjs +1588 -0
  4. package/lib/index.d.cts +9574 -0
  5. package/lib/index.d.ts +7 -0
  6. package/lib/index.d.ts.map +1 -0
  7. package/lib/index.esm.d.ts +9574 -0
  8. package/lib/index.esm.js +1544 -0
  9. package/lib/sdk/axios.d.ts +3 -0
  10. package/lib/sdk/axios.d.ts.map +1 -0
  11. package/lib/sdk/card/index.d.ts +2 -0
  12. package/lib/sdk/card/index.d.ts.map +1 -0
  13. package/lib/sdk/card/schema.d.ts +586 -0
  14. package/lib/sdk/card/schema.d.ts.map +1 -0
  15. package/lib/sdk/common.d.ts +28 -0
  16. package/lib/sdk/common.d.ts.map +1 -0
  17. package/lib/sdk/customer/index.d.ts +7 -0
  18. package/lib/sdk/customer/index.d.ts.map +1 -0
  19. package/lib/sdk/customer/schema.d.ts +5933 -0
  20. package/lib/sdk/customer/schema.d.ts.map +1 -0
  21. package/lib/sdk/ewallet/create.d.ts +173 -0
  22. package/lib/sdk/ewallet/create.d.ts.map +1 -0
  23. package/lib/sdk/ewallet/schema.d.ts +783 -0
  24. package/lib/sdk/ewallet/schema.d.ts.map +1 -0
  25. package/lib/sdk/index.d.ts +2201 -0
  26. package/lib/sdk/index.d.ts.map +1 -0
  27. package/lib/sdk/invoice/index.d.ts +8 -0
  28. package/lib/sdk/invoice/index.d.ts.map +1 -0
  29. package/lib/sdk/invoice/schema.d.ts +2198 -0
  30. package/lib/sdk/invoice/schema.d.ts.map +1 -0
  31. package/lib/sdk/payment-method/index.d.ts +7 -0
  32. package/lib/sdk/payment-method/index.d.ts.map +1 -0
  33. package/lib/sdk/payment-method/schema.d.ts +990 -0
  34. package/lib/sdk/payment-method/schema.d.ts.map +1 -0
  35. package/lib/utils/errors.d.ts +58 -0
  36. package/lib/utils/errors.d.ts.map +1 -0
  37. package/lib/utils/index.d.ts +6 -0
  38. package/lib/utils/index.d.ts.map +1 -0
  39. package/lib/utils/pagination.d.ts +134 -0
  40. package/lib/utils/pagination.d.ts.map +1 -0
  41. package/lib/utils/rate-limit.d.ts +90 -0
  42. package/lib/utils/rate-limit.d.ts.map +1 -0
  43. package/lib/utils/type-guards.d.ts +13 -0
  44. package/lib/utils/type-guards.d.ts.map +1 -0
  45. package/lib/utils/webhook.d.ts +101 -0
  46. package/lib/utils/webhook.d.ts.map +1 -0
  47. package/package.json +83 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 xpriori·𞋣𞋥
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,442 @@
1
+ # Xendit SDK for TypeScript/JavaScript
2
+
3
+ [![npm version](https://badge.fury.io/js/xendit-fn.svg)](https://badge.fury.io/js/xendit-fn)
4
+ [![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=flat&logo=typescript&logoColor=white)](https://typescriptlang.org/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ A modern, production-ready TypeScript SDK for the Xendit payment gateway. Framework-agnostic and designed for any JavaScript/TypeScript project.
8
+
9
+ ## Features
10
+
11
+ ✓ **Production-Ready**
12
+ - Built-in rate limiting and retry logic
13
+ - Webhook signature verification
14
+ - Comprehensive error handling
15
+ - Pagination utilities
16
+
17
+ ✓ **Developer Experience**
18
+ - Full TypeScript support with type safety
19
+ - Runtime validation using Zod schemas
20
+ - Tree-shakeable exports
21
+ - Framework-agnostic design
22
+
23
+ ✓ **Complete API Coverage**
24
+ - Customer Management
25
+ - E-wallet Payments
26
+ - Payment Methods
27
+ - Invoice Management
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ # Using bun (recommended)
33
+ bun add xendit-fn
34
+
35
+ # Using npm
36
+ npm install xendit-fn
37
+
38
+ # Using yarn
39
+ yarn add xendit-fn
40
+ ```
41
+
42
+ ## Quick Start
43
+
44
+ ```typescript
45
+ import { Xendit } from 'xendit-fn';
46
+
47
+ // Initialize with your secret key
48
+ const xendit = Xendit(process.env.XENDIT_SECRET_KEY!);
49
+
50
+ // Create a customer
51
+ const customer = await xendit.customer.create({
52
+ reference_id: 'customer_001',
53
+ type: 'INDIVIDUAL',
54
+ individual_detail: {
55
+ given_names: 'John',
56
+ surname: 'Doe',
57
+ },
58
+ email: 'john.doe@example.com',
59
+ mobile_number: '+639171234567',
60
+ });
61
+
62
+ // Create an e-wallet charge
63
+ const charge = await xendit.ewallet.charge({
64
+ reference_id: 'charge_001',
65
+ currency: 'PHP',
66
+ amount: 10000, // 100.00 PHP
67
+ checkout_method: 'ONE_TIME_PAYMENT',
68
+ channel_code: 'PH_GCASH',
69
+ channel_properties: {
70
+ success_redirect_url: 'https://yourapp.com/success',
71
+ failure_redirect_url: 'https://yourapp.com/failure',
72
+ },
73
+ });
74
+ ```
75
+
76
+ ## API Reference
77
+
78
+ ### Customer Management
79
+
80
+ #### Create Customer
81
+
82
+ ```typescript
83
+ const customer = await xendit.customer.create({
84
+ reference_id: 'unique_customer_id',
85
+ type: 'INDIVIDUAL', // or 'BUSINESS'
86
+ individual_detail: {
87
+ given_names: 'John',
88
+ surname: 'Doe',
89
+ nationality: 'PH',
90
+ date_of_birth: '1990-01-01',
91
+ },
92
+ email: 'john@example.com',
93
+ mobile_number: '+639171234567',
94
+ addresses: [{
95
+ country: 'PH',
96
+ street_line1: '123 Main St',
97
+ city: 'Manila',
98
+ postal_code: '1000',
99
+ is_primary: true,
100
+ }],
101
+ });
102
+ ```
103
+
104
+ #### Get Customer
105
+
106
+ ```typescript
107
+ // Get by ID
108
+ const customer = await xendit.customer.getById({
109
+ id: 'cust-12345'
110
+ });
111
+
112
+ // Get by reference ID
113
+ const customers = await xendit.customer.getByRefId({
114
+ reference_id: 'customer_001'
115
+ });
116
+ ```
117
+
118
+ #### Update Customer
119
+
120
+ ```typescript
121
+ const updatedCustomer = await xendit.customer.update({
122
+ id: 'cust-12345',
123
+ payload: {
124
+ email: 'newemail@example.com',
125
+ phone_number: '+639181234567',
126
+ },
127
+ });
128
+ ```
129
+
130
+ ### E-Wallet Payments
131
+
132
+ #### Create Charge
133
+
134
+ ```typescript
135
+ // One-time payment
136
+ const charge = await xendit.ewallet.charge({
137
+ reference_id: 'charge_001',
138
+ currency: 'PHP',
139
+ amount: 50000, // 500.00 PHP
140
+ checkout_method: 'ONE_TIME_PAYMENT',
141
+ channel_code: 'PH_GCASH',
142
+ channel_properties: {
143
+ success_redirect_url: 'https://yourapp.com/success',
144
+ failure_redirect_url: 'https://yourapp.com/failure',
145
+ },
146
+ customer_id: 'cust-12345', // optional
147
+ basket: [ // optional
148
+ {
149
+ reference_id: 'item_001',
150
+ name: 'Product Name',
151
+ category: 'Electronics',
152
+ currency: 'PHP',
153
+ price: 50000,
154
+ quantity: 1,
155
+ type: 'PRODUCT',
156
+ },
157
+ ],
158
+ });
159
+
160
+ // Tokenized payment
161
+ const tokenizedCharge = await xendit.ewallet.charge({
162
+ reference_id: 'charge_002',
163
+ currency: 'PHP',
164
+ amount: 25000,
165
+ checkout_method: 'TOKENIZED_PAYMENT',
166
+ payment_method_id: 'pm-12345',
167
+ channel_properties: {
168
+ success_redirect_url: 'https://yourapp.com/success',
169
+ },
170
+ });
171
+ ```
172
+
173
+ #### Get Charge
174
+
175
+ ```typescript
176
+ const charge = await xendit.ewallet.get({
177
+ id: 'ewc-12345'
178
+ });
179
+ ```
180
+
181
+ ### Supported Countries and Currencies
182
+
183
+ - **Philippines (PH)**: PHP
184
+ - **Indonesia (ID)**: IDR
185
+ - **Malaysia (MY)**: MYR
186
+ - **Thailand (TH)**: THB
187
+ - **Vietnam (VN)**: VND
188
+
189
+ ### Supported E-Wallet Channels
190
+
191
+ - **Indonesia**: OVO, DANA, LinkAja, ShopeePay, AstraPay, JENIUSPAY, SakuKu
192
+ - **Philippines**: PayMaya, GCash, GrabPay, ShopeePay
193
+ - **Vietnam**: Appota, MoMo, ShopeePay, VNPTWallet, ViettelPay, ZaloPay
194
+ - **Thailand**: WeChat Pay, LINE Pay, TrueMoney, ShopeePay
195
+ - **Malaysia**: Touch 'n Go, ShopeePay, GrabPay
196
+
197
+ ## Error Handling
198
+
199
+ The SDK provides comprehensive error handling with custom error types:
200
+
201
+ ```typescript
202
+ import { XenditApiError, ValidationError } from 'xendit-fn/errors';
203
+
204
+ try {
205
+ const customer = await xendit.customer.create(invalidData);
206
+ } catch (error) {
207
+ if (error instanceof ValidationError) {
208
+ console.error('Validation failed:', error.validationErrors);
209
+ } else if (error instanceof XenditApiError) {
210
+ console.error('API Error:', {
211
+ code: error.code,
212
+ message: error.message,
213
+ statusCode: error.statusCode,
214
+ details: error.details,
215
+ });
216
+ } else {
217
+ console.error('Unexpected error:', error);
218
+ }
219
+ }
220
+ ```
221
+
222
+ ## TypeScript Support
223
+
224
+ The SDK is built with TypeScript and provides full type definitions:
225
+
226
+ ```typescript
227
+ import type {
228
+ Customer,
229
+ CustomerResource,
230
+ EWalletChargeParams,
231
+ EWalletChargeResource
232
+ } from 'xendit-fn';
233
+
234
+ // All parameters and responses are fully typed
235
+ const createCustomer = async (data: Customer): Promise<CustomerResource> => {
236
+ return await xendit.customer.create(data);
237
+ };
238
+ ```
239
+
240
+ ## Development
241
+
242
+ ### Setup
243
+
244
+ ```bash
245
+ # Install dependencies
246
+ bun install
247
+
248
+ # Run type checking
249
+ bun run typecheck
250
+
251
+ # Run linting
252
+ bun run lint
253
+
254
+ # Run tests
255
+ bun run test
256
+ ```
257
+
258
+ ### Building
259
+
260
+ ```bash
261
+ # Build the package
262
+ bun run build
263
+
264
+ # Start development mode
265
+ bun run dev
266
+ ```
267
+
268
+ ### Environment Variables
269
+
270
+ For testing, create a `.env` file:
271
+
272
+ ```env
273
+ XENDIT_SK=your_xendit_secret_key_here
274
+ ```
275
+
276
+ ## Contributing
277
+
278
+ 1. Fork the repository
279
+ 2. Create a feature branch
280
+ 3. Make your changes
281
+ 4. Add tests for new functionality
282
+ 5. Run the test suite
283
+ 6. Submit a pull request
284
+
285
+ ## License
286
+
287
+ MIT License - see [LICENSE](LICENSE) file for details.
288
+
289
+ ## Related Projects
290
+
291
+ - [Xendit API Documentation](https://developers.xendit.co/)
292
+ - [TypeScript](https://typescriptlang.org/)
293
+ - [Zod](https://zod.dev/)
294
+
295
+ ## Production-Ready Features
296
+
297
+ ### Rate Limiting
298
+
299
+ The SDK includes built-in rate limiting to respect API limits:
300
+
301
+ ```typescript
302
+ import { Xendit } from 'xendit-fn';
303
+
304
+ const xendit = Xendit('your-api-key', {
305
+ rateLimit: {
306
+ maxRequests: 100, // Max requests per window
307
+ windowMs: 60000, // Time window (1 minute)
308
+ maxRetries: 3, // Retry attempts
309
+ baseRetryDelayMs: 1000 // Base delay for retries
310
+ }
311
+ });
312
+ ```
313
+
314
+ ### Webhook Handling
315
+
316
+ Secure webhook processing with signature verification:
317
+
318
+ ```typescript
319
+ import {
320
+ createWebhookProcessor,
321
+ WebhookHandlers,
322
+ parseWebhookEvent
323
+ } from 'xendit-fn';
324
+
325
+ // Create webhook processor
326
+ const webhookProcessor = createWebhookProcessor({
327
+ callbackToken: 'your-webhook-token',
328
+ // OR use HMAC verification
329
+ // hmacSecret: 'your-hmac-secret'
330
+ });
331
+
332
+ // Define event handlers
333
+ const handlers: WebhookHandlers = {
334
+ 'invoice.paid': async (event) => {
335
+ console.log('Invoice paid:', event.data);
336
+ // Process payment success
337
+ },
338
+ 'payment.failed': async (event) => {
339
+ console.log('Payment failed:', event.data);
340
+ // Handle payment failure
341
+ }
342
+ };
343
+
344
+ // Process webhook in your endpoint
345
+ app.post('/webhook', async (req, res) => {
346
+ const result = await webhookProcessor.processWebhook(
347
+ req.body,
348
+ req.headers,
349
+ handlers
350
+ );
351
+
352
+ if (result.success) {
353
+ res.status(200).send('OK');
354
+ } else {
355
+ res.status(400).send(result.error);
356
+ }
357
+ });
358
+ ```
359
+
360
+ ### Pagination Utilities
361
+
362
+ For endpoints that return paginated data:
363
+
364
+ ```typescript
365
+ import { fetchAllPages, iterateItems } from 'xendit-fn';
366
+
367
+ // Get all invoices across all pages
368
+ const allInvoices = await fetchAllPages(
369
+ axiosInstance,
370
+ '/invoices',
371
+ InvoiceSchema,
372
+ { limit: 100, maxItems: 1000 }
373
+ );
374
+
375
+ // Stream through items
376
+ for await (const invoice of iterateItems(
377
+ axiosInstance,
378
+ '/invoices',
379
+ InvoiceSchema
380
+ )) {
381
+ console.log('Processing invoice:', invoice.id);
382
+ }
383
+ ```
384
+
385
+ ### Complete API Coverage
386
+
387
+ #### Payment Methods
388
+
389
+ ```typescript
390
+ // Create payment method
391
+ const paymentMethod = await xendit.paymentMethod.create({
392
+ customer_id: 'customer-id',
393
+ type: 'DEBIT_CARD',
394
+ properties: {
395
+ id: 'card-token-from-xendit-js',
396
+ },
397
+ });
398
+
399
+ // List payment methods
400
+ const paymentMethods = await xendit.paymentMethod.list({
401
+ customer_id: 'customer-id',
402
+ });
403
+ ```
404
+
405
+ #### Invoice Management
406
+
407
+ ```typescript
408
+ // Create invoice
409
+ const invoice = await xendit.invoice.create({
410
+ external_id: 'invoice-001',
411
+ amount: 100000,
412
+ description: 'Payment for services',
413
+ invoice_duration: 3600,
414
+ customer: {
415
+ given_names: 'John',
416
+ email: 'customer@example.com',
417
+ },
418
+ success_redirect_url: 'https://yoursite.com/success',
419
+ failure_redirect_url: 'https://yoursite.com/failure',
420
+ });
421
+
422
+ // Expire invoice
423
+ const expiredInvoice = await xendit.invoice.expire({ id: 'invoice-id' });
424
+ ```
425
+
426
+ ### Environment Configuration
427
+
428
+ The SDK automatically detects test mode:
429
+
430
+ ```typescript
431
+ // Test mode (automatically detected)
432
+ const xendit = Xendit('xnd_development_...');
433
+ //
434
+
435
+
436
+ // Production mode
437
+ const xendit = Xendit('xnd_production_...');
438
+ ```
439
+
440
+ ---
441
+
442
+ **Note**: This is an unofficial SDK. For official support, please refer to [Xendit's official documentation](https://developers.xendit.co/).