@xenterprises/fastify-xstripe 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/API.md ADDED
@@ -0,0 +1,574 @@
1
+ # xStripe API Reference
2
+
3
+ Complete API documentation for xStripe example server endpoints supporting subscriptions and one-time payments.
4
+
5
+ ## Table of Contents
6
+
7
+ 1. [Customer Management](#customer-management)
8
+ 2. [Plans & Products](#plans--products)
9
+ 3. [Subscriptions](#subscriptions)
10
+ 4. [Payments](#payments)
11
+ 5. [Payment Methods](#payment-methods)
12
+ 6. [Utility Endpoints](#utility-endpoints)
13
+
14
+ ---
15
+
16
+ ## Customer Management
17
+
18
+ ### Create Customer
19
+
20
+ **POST** `/create-customer`
21
+
22
+ Create a new Stripe customer.
23
+
24
+ **Request Body:**
25
+ ```json
26
+ {
27
+ "email": "user@example.com",
28
+ "name": "John Doe"
29
+ }
30
+ ```
31
+
32
+ **Response:**
33
+ ```json
34
+ {
35
+ "customerId": "cus_ABC123..."
36
+ }
37
+ ```
38
+
39
+ **Status Codes:**
40
+ - `200` - Customer created successfully
41
+ - `500` - Error creating customer
42
+
43
+ ---
44
+
45
+ ## Plans & Products
46
+
47
+ ### List All Plans
48
+
49
+ **GET** `/plans`
50
+
51
+ Retrieve all available products with their pricing information.
52
+
53
+ **Query Parameters:** None
54
+
55
+ **Response:**
56
+ ```json
57
+ {
58
+ "plans": [
59
+ {
60
+ "productId": "prod_ABC123...",
61
+ "name": "Professional Plan",
62
+ "description": "Best for teams",
63
+ "images": ["https://..."],
64
+ "metadata": {
65
+ "features": "advanced"
66
+ },
67
+ "prices": [
68
+ {
69
+ "priceId": "price_ABC123...",
70
+ "amount": 29.99,
71
+ "currency": "USD",
72
+ "interval": "month",
73
+ "intervalCount": 1,
74
+ "trialPeriodDays": 7,
75
+ "nickname": "Monthly",
76
+ "metadata": {}
77
+ }
78
+ ]
79
+ }
80
+ ]
81
+ }
82
+ ```
83
+
84
+ **Status Codes:**
85
+ - `200` - Success
86
+ - `500` - Error retrieving plans
87
+
88
+ ### Get Specific Plan
89
+
90
+ **GET** `/plans/:productId`
91
+
92
+ Retrieve a specific product and all its prices.
93
+
94
+ **URL Parameters:**
95
+ - `productId` (string, required) - Stripe product ID (e.g., `prod_ABC123...`)
96
+
97
+ **Response:**
98
+ ```json
99
+ {
100
+ "productId": "prod_ABC123...",
101
+ "name": "Professional Plan",
102
+ "description": "Best for teams",
103
+ "images": [],
104
+ "metadata": {},
105
+ "prices": [
106
+ {
107
+ "priceId": "price_ABC123...",
108
+ "amount": 29.99,
109
+ "currency": "USD",
110
+ "interval": "month",
111
+ "intervalCount": 1,
112
+ "trialPeriodDays": 7,
113
+ "nickname": "Monthly"
114
+ }
115
+ ]
116
+ }
117
+ ```
118
+
119
+ **Status Codes:**
120
+ - `200` - Success
121
+ - `404` - Product not found
122
+ - `500` - Server error
123
+
124
+ ---
125
+
126
+ ## Subscriptions
127
+
128
+ ### Create Subscription Checkout
129
+
130
+ **POST** `/create-checkout-session`
131
+
132
+ Create a checkout session for recurring subscription.
133
+
134
+ **Request Body:**
135
+ ```json
136
+ {
137
+ "customerId": "cus_ABC123...",
138
+ "priceId": "price_ABC123..."
139
+ }
140
+ ```
141
+
142
+ **Response:**
143
+ ```json
144
+ {
145
+ "sessionId": "cs_test_ABC123...",
146
+ "url": "https://checkout.stripe.com/..."
147
+ }
148
+ ```
149
+
150
+ **Status Codes:**
151
+ - `200` - Session created
152
+ - `500` - Error creating session
153
+
154
+ ### List Customer Subscriptions
155
+
156
+ **GET** `/customer/:customerId/subscriptions`
157
+
158
+ Get all subscriptions for a customer.
159
+
160
+ **URL Parameters:**
161
+ - `customerId` (string, required) - Stripe customer ID
162
+
163
+ **Response:**
164
+ ```json
165
+ {
166
+ "subscriptions": [
167
+ {
168
+ "id": "sub_ABC123...",
169
+ "status": "active",
170
+ "planId": "price_ABC123...",
171
+ "planName": "Professional Monthly",
172
+ "amount": 29.99,
173
+ "currency": "USD",
174
+ "interval": "month",
175
+ "createdAt": "2024-12-29T10:00:00Z",
176
+ "currentPeriodEnd": "2025-01-29T10:00:00Z",
177
+ "cancelAtPeriodEnd": false,
178
+ "canceledAt": null
179
+ }
180
+ ],
181
+ "total": 1
182
+ }
183
+ ```
184
+
185
+ **Subscription Status Values:**
186
+ - `active` - Active subscription
187
+ - `trialing` - In trial period
188
+ - `past_due` - Payment failed
189
+ - `canceled` - Canceled by user
190
+ - `unpaid` - Unpaid invoices
191
+
192
+ **Status Codes:**
193
+ - `200` - Success
194
+ - `500` - Error retrieving subscriptions
195
+
196
+ ### Update Subscription
197
+
198
+ **POST** `/subscription/:id/update`
199
+
200
+ Update a subscription (change plan, apply coupon, modify quantity).
201
+
202
+ **URL Parameters:**
203
+ - `id` (string, required) - Subscription ID
204
+
205
+ **Request Body:**
206
+ ```json
207
+ {
208
+ "priceId": "price_XYZ789...",
209
+ "quantity": 1,
210
+ "coupon": "SAVE20",
211
+ "metadata": {
212
+ "custom_field": "value"
213
+ },
214
+ "prorationBehavior": "create_prorations"
215
+ }
216
+ ```
217
+
218
+ **Proration Behavior Options:**
219
+ - `create_prorations` (default) - Create prorated invoice
220
+ - `none` - No proration
221
+ - `always_invoice` - Always invoice immediately
222
+
223
+ **Response:**
224
+ ```json
225
+ {
226
+ "id": "sub_ABC123...",
227
+ "status": "active",
228
+ "priceId": "price_XYZ789...",
229
+ "amount": 49.99,
230
+ "nextBillingDate": "2025-01-29T10:00:00Z",
231
+ "message": "Subscription updated successfully"
232
+ }
233
+ ```
234
+
235
+ **Status Codes:**
236
+ - `200` - Updated successfully
237
+ - `500` - Error updating subscription
238
+
239
+ ### Cancel Subscription
240
+
241
+ **POST** `/cancel-subscription/:id`
242
+
243
+ Cancel a subscription immediately or at period end.
244
+
245
+ **URL Parameters:**
246
+ - `id` (string, required) - Subscription ID
247
+
248
+ **Request Body:**
249
+ ```json
250
+ {
251
+ "immediately": false
252
+ }
253
+ ```
254
+
255
+ **Parameters:**
256
+ - `immediately` (boolean, optional, default: false)
257
+ - `true` - Cancel immediately
258
+ - `false` - Cancel at end of billing period
259
+
260
+ **Response:**
261
+ ```json
262
+ {
263
+ "subscriptionId": "sub_ABC123...",
264
+ "status": "canceled"
265
+ }
266
+ ```
267
+
268
+ **Status Codes:**
269
+ - `200` - Canceled successfully
270
+ - `500` - Error canceling subscription
271
+
272
+ ---
273
+
274
+ ## Payments
275
+
276
+ ### Create One-Time Payment Checkout
277
+
278
+ **POST** `/create-payment-session`
279
+
280
+ Create a checkout session for one-time payment.
281
+
282
+ **Request Body:**
283
+ ```json
284
+ {
285
+ "customerId": "cus_ABC123...",
286
+ "priceId": "price_ABC123...",
287
+ "quantity": 2,
288
+ "metadata": {
289
+ "order_id": "12345"
290
+ }
291
+ }
292
+ ```
293
+
294
+ **Response:**
295
+ ```json
296
+ {
297
+ "sessionId": "cs_test_ABC123...",
298
+ "url": "https://checkout.stripe.com/..."
299
+ }
300
+ ```
301
+
302
+ **Status Codes:**
303
+ - `200` - Session created
304
+ - `500` - Error creating session
305
+
306
+ ---
307
+
308
+ ## Payment Methods
309
+
310
+ ### List Payment Methods
311
+
312
+ **GET** `/customer/:customerId/payment-methods`
313
+
314
+ Get all payment methods for a customer.
315
+
316
+ **URL Parameters:**
317
+ - `customerId` (string, required) - Stripe customer ID
318
+
319
+ **Response:**
320
+ ```json
321
+ {
322
+ "paymentMethods": [
323
+ {
324
+ "id": "pm_ABC123...",
325
+ "type": "card",
326
+ "isDefault": true,
327
+ "card": {
328
+ "brand": "visa",
329
+ "last4": "4242",
330
+ "expMonth": 12,
331
+ "expYear": 2025
332
+ },
333
+ "billingDetails": {
334
+ "name": "John Doe",
335
+ "email": "john@example.com"
336
+ },
337
+ "createdAt": "2024-12-29T10:00:00Z"
338
+ }
339
+ ],
340
+ "defaultPaymentMethodId": "pm_ABC123...",
341
+ "total": 1
342
+ }
343
+ ```
344
+
345
+ **Card Brands:**
346
+ - `visa`
347
+ - `mastercard`
348
+ - `amex`
349
+ - `discover`
350
+ - And others...
351
+
352
+ **Status Codes:**
353
+ - `200` - Success
354
+ - `500` - Error retrieving payment methods
355
+
356
+ ### Add Payment Method
357
+
358
+ **POST** `/customer/:customerId/payment-methods`
359
+
360
+ Attach a payment method to a customer.
361
+
362
+ **Note:** The payment method must first be created on the frontend using Stripe Elements or Stripe.js.
363
+
364
+ **URL Parameters:**
365
+ - `customerId` (string, required) - Stripe customer ID
366
+
367
+ **Request Body:**
368
+ ```json
369
+ {
370
+ "paymentMethodId": "pm_ABC123..."
371
+ }
372
+ ```
373
+
374
+ **Response:**
375
+ ```json
376
+ {
377
+ "id": "pm_ABC123...",
378
+ "type": "card",
379
+ "card": {
380
+ "brand": "visa",
381
+ "last4": "4242",
382
+ "expMonth": 12,
383
+ "expYear": 2025
384
+ },
385
+ "message": "Payment method added successfully"
386
+ }
387
+ ```
388
+
389
+ **Status Codes:**
390
+ - `200` - Added successfully
391
+ - `500` - Error adding payment method
392
+
393
+ ### Set Default Payment Method
394
+
395
+ **POST** `/customer/:customerId/payment-methods/:paymentMethodId/default`
396
+
397
+ Set a payment method as the default for invoices and subscriptions.
398
+
399
+ **URL Parameters:**
400
+ - `customerId` (string, required) - Stripe customer ID
401
+ - `paymentMethodId` (string, required) - Payment method ID
402
+
403
+ **Request Body:** (empty)
404
+
405
+ **Response:**
406
+ ```json
407
+ {
408
+ "customerId": "cus_ABC123...",
409
+ "defaultPaymentMethodId": "pm_ABC123...",
410
+ "message": "Default payment method updated successfully"
411
+ }
412
+ ```
413
+
414
+ **Status Codes:**
415
+ - `200` - Updated successfully
416
+ - `500` - Error updating payment method
417
+
418
+ ### Delete Payment Method
419
+
420
+ **DELETE** `/customer/:customerId/payment-methods/:paymentMethodId`
421
+
422
+ Remove a payment method from a customer.
423
+
424
+ **URL Parameters:**
425
+ - `customerId` (string, required) - Stripe customer ID
426
+ - `paymentMethodId` (string, required) - Payment method ID to delete
427
+
428
+ **Request Body:** (empty)
429
+
430
+ **Response:**
431
+ ```json
432
+ {
433
+ "paymentMethodId": "pm_ABC123...",
434
+ "message": "Payment method removed successfully"
435
+ }
436
+ ```
437
+
438
+ **Status Codes:**
439
+ - `200` - Deleted successfully
440
+ - `500` - Error deleting payment method
441
+
442
+ ---
443
+
444
+ ## Utility Endpoints
445
+
446
+ ### Get Subscription
447
+
448
+ **GET** `/subscription/:id`
449
+
450
+ Get details of a specific subscription.
451
+
452
+ **URL Parameters:**
453
+ - `id` (string, required) - Subscription ID
454
+
455
+ **Response:**
456
+ ```json
457
+ {
458
+ "id": "sub_ABC123...",
459
+ "status": "active",
460
+ "currentPeriodEnd": "2025-01-29T10:00:00Z",
461
+ "cancelAtPeriodEnd": false
462
+ }
463
+ ```
464
+
465
+ **Status Codes:**
466
+ - `200` - Success
467
+ - `404` - Subscription not found
468
+ - `500` - Server error
469
+
470
+ ### Health Check
471
+
472
+ **GET** `/health`
473
+
474
+ Check if the server and Stripe client are operational.
475
+
476
+ **Response:**
477
+ ```json
478
+ {
479
+ "status": "ok",
480
+ "stripe": true,
481
+ "timestamp": "2024-12-29T10:00:00Z"
482
+ }
483
+ ```
484
+
485
+ **Status Codes:**
486
+ - `200` - Server is healthy
487
+
488
+ ---
489
+
490
+ ## Error Handling
491
+
492
+ All endpoints follow consistent error response format:
493
+
494
+ ```json
495
+ {
496
+ "error": "Error message describing what went wrong"
497
+ }
498
+ ```
499
+
500
+ **Common HTTP Status Codes:**
501
+ - `200` - Success
502
+ - `400` - Bad request (invalid parameters)
503
+ - `404` - Resource not found
504
+ - `500` - Server error (Stripe API error or unexpected issue)
505
+
506
+ ---
507
+
508
+ ## Usage Examples
509
+
510
+ ### Complete Subscription Flow
511
+
512
+ ```bash
513
+ # 1. Create customer
514
+ curl -X POST http://localhost:3000/create-customer \
515
+ -H "Content-Type: application/json" \
516
+ -d '{
517
+ "email": "user@example.com",
518
+ "name": "John Doe"
519
+ }'
520
+
521
+ # Response: { "customerId": "cus_ABC123..." }
522
+
523
+ # 2. List available plans
524
+ curl http://localhost:3000/plans
525
+
526
+ # 3. Create checkout session
527
+ curl -X POST http://localhost:3000/create-checkout-session \
528
+ -H "Content-Type: application/json" \
529
+ -d '{
530
+ "customerId": "cus_ABC123...",
531
+ "priceId": "price_ABC123..."
532
+ }'
533
+
534
+ # Response: { "sessionId": "cs_test_...", "url": "https://..." }
535
+ # User completes payment at URL
536
+
537
+ # 4. List subscriptions
538
+ curl http://localhost:3000/customer/cus_ABC123.../subscriptions
539
+
540
+ # 5. Update plan
541
+ curl -X POST http://localhost:3000/subscription/sub_ABC123.../update \
542
+ -H "Content-Type: application/json" \
543
+ -d '{
544
+ "priceId": "price_XYZ789..."
545
+ }'
546
+
547
+ # 6. Manage payment methods
548
+ curl http://localhost:3000/customer/cus_ABC123.../payment-methods
549
+ ```
550
+
551
+ ### One-Time Payment Flow
552
+
553
+ ```bash
554
+ # 1. Create checkout for one-time payment
555
+ curl -X POST http://localhost:3000/create-payment-session \
556
+ -H "Content-Type: application/json" \
557
+ -d '{
558
+ "customerId": "cus_ABC123...",
559
+ "priceId": "price_ABC123...",
560
+ "quantity": 2
561
+ }'
562
+
563
+ # User completes payment at returned URL
564
+ ```
565
+
566
+ ---
567
+
568
+ ## Related Documentation
569
+
570
+ - [Stripe API Docs](https://stripe.com/docs/api)
571
+ - [Stripe Webhooks](https://stripe.com/docs/webhooks)
572
+ - [Subscription Guide](https://stripe.com/docs/billing/quickstart)
573
+ - [Payment Methods API](https://stripe.com/docs/api/payment_methods)
574
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,96 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2024-12-29
9
+
10
+ ### Added
11
+ - Initial release of xStripe Fastify v5 plugin
12
+ - Simplified, testable Stripe webhook handling
13
+ - 20+ built-in handlers for subscription lifecycle events
14
+ - Pure function-based handler architecture
15
+ - Stripe client decoration on Fastify instance
16
+ - Webhook signature verification and validation
17
+ - Custom handler override support
18
+ - Error handling and logging
19
+ - Type-safe operation with TypeScript support
20
+ - Helper functions for common Stripe operations
21
+ - Comprehensive error handling with graceful degradation
22
+ - 6 comprehensive tests
23
+
24
+ ### Features
25
+ - **Event-based architecture** - Clean handler separation
26
+ - **Easy to test** - Handlers are pure functions
27
+ - **Type-safe** - Full TypeScript support
28
+ - **Built-in handlers** - 20+ default event handlers
29
+ - **Production-ready** - Signature verification, error handling
30
+ - **Flexible** - Override any handler with custom logic
31
+ - **Readable** - Self-documenting code
32
+
33
+ ### Handlers Included
34
+ - Subscription lifecycle (created, updated, deleted)
35
+ - Invoice lifecycle (created, finalized, payment_failed, paid)
36
+ - Customer events (created, updated, deleted)
37
+ - Payment method events (attached, detached)
38
+ - Charge events (succeeded, failed, refunded)
39
+ - And 10+ more default handlers
40
+
41
+ ### Documentation
42
+ - README.md with API reference
43
+ - QUICK_START.md with step-by-step setup
44
+ - EXAMPLES.md with 15+ real-world scenarios
45
+ - TESTING.md with testing guide
46
+ - MIGRATION.md with upgrade guide
47
+
48
+ ### Security
49
+ - Webhook signature verification
50
+ - Request validation
51
+ - Error handling without data leaks
52
+ - Secure Stripe API key handling
53
+
54
+ ---
55
+
56
+ ## [Future Versions - Planned]
57
+
58
+ ### [1.1.0] - Planned
59
+ - Webhook retry logic
60
+ - Event filtering and routing
61
+ - Batch event handling
62
+ - Metrics collection hooks
63
+ - Advanced error recovery
64
+
65
+ ### [1.2.0] - Planned
66
+ - Webhook event queue for reliability
67
+ - Dead letter handling
68
+ - Event replay capabilities
69
+ - Audit logging
70
+
71
+ ### [2.0.0] - Planned (Breaking Changes)
72
+ - Payment intent workflows
73
+ - Connect account support
74
+ - Advanced billing scenarios
75
+ - Plugin ecosystem
76
+
77
+ ---
78
+
79
+ ## Dependencies
80
+ - **stripe**: ^16.12.0 (Stripe JavaScript SDK)
81
+ - **fastify-plugin**: ^5.0.0 (Fastify plugin wrapper)
82
+ - **fastify**: ^5.0.0+ (peer dependency)
83
+
84
+ ## System Requirements
85
+ - Node.js >= 20.0.0
86
+ - Fastify >= 5.0.0
87
+ - Stripe account with API keys
88
+
89
+ ## License
90
+ ISC
91
+
92
+ ## Author
93
+ Tim Mushen
94
+
95
+ ## Repository
96
+ https://gitlab.com/x-enterprises/fastify-plugins/fastify-xstripe