A TypeScript Express API that replays Stripe events and maintains state for various Stripe objects. This application provides a way to track and query the state of Stripe objects based on their event history.
- Event-based state management for Stripe objects
- RESTful API endpoints for various Stripe resources
- Background task service for polling Stripe events
- Webhook endpoint for real-time event processing
- TypeScript support with type safety
- PostgreSQL database with Drizzle ORM
- Express.js with middleware support
- Node.js (v14 or higher)
- PostgreSQL database
- Stripe API key
- Stripe webhook secret
Create a .env file in the root directory with the following variables:
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=your_stripe_webhook_secret
PORT=3000- Clone the repository:
git clone <repository-url>
cd replistripe- Install dependencies:
npm install- Set up the database:
npm run db:generate
npm run db:push- Start the development server:
npm run devPOST /webhooks- Receive and process Stripe webhook events- Validates Stripe signature using
STRIPE_WEBHOOK_SECRET - Saves events to database with proper type safety
- Returns 200 status on success
- Uses raw body parsing for signature verification
- Handles all Stripe event types automatically
- Validates Stripe signature using
GET /customers/:id- Get customer detailsGET /charges/:id- Get charge detailsGET /payment-intents/:id- Get payment intent detailsGET /setup-intents/:id- Get setup intent detailsGET /subscriptions/:id- Get subscription detailsGET /invoices/:id- Get invoice detailsGET /payment-methods/:id- Get payment method detailsGET /refunds/:id- Get refund detailsGET /disputes/:id- Get dispute detailsGET /balance-transactions/:id- Get balance transaction detailsGET /payouts/:id- Get payout detailsGET /webhook-endpoints/:id- Get webhook endpoint details
GET /products/:id- Get product detailsGET /prices/:id- Get price detailsGET /coupons/:id- Get coupon detailsGET /promotion-codes/:id- Get promotion code details
GET /tax-rates/:id- Get tax rate detailsGET /tax-ids/:id- Get tax ID details
GET /file-links/:id- Get file link details
GET /reviews/:id- Get review details
GET /transfer-reversals/:id- Get transfer reversal details
GET /application-fees/:id- Get application fee details
The application handles various Stripe event types for each resource:
customer.createdcustomer.updatedcustomer.deleted
charge.succeededcharge.failedcharge.refundedcharge.dispute.createdcharge.dispute.updatedcharge.dispute.closedcharge.dispute.funds_reinstatedcharge.dispute.funds_withdrawn
payment_intent.succeededpayment_intent.processingpayment_intent.payment_failedpayment_intent.canceledpayment_intent.createdpayment_intent.requires_actionpayment_intent.requires_capturepayment_intent.requires_confirmationpayment_intent.requires_payment_method
setup_intent.succeededsetup_intent.processingsetup_intent.requires_payment_methodsetup_intent.requires_confirmationsetup_intent.requires_actionsetup_intent.canceled
customer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedcustomer.subscription.trial_will_endcustomer.subscription.pending_update_appliedcustomer.subscription.pending_update_expired
invoice.createdinvoice.finalizedinvoice.paidinvoice.payment_failedinvoice.payment_action_requiredinvoice.upcominginvoice.marked_uncollectibleinvoice.voided
payment_method.attachedpayment_method.detachedpayment_method.updated
charge.refundedrefund.updated
charge.dispute.createdcharge.dispute.updatedcharge.dispute.closedcharge.dispute.funds_reinstatedcharge.dispute.funds_withdrawn
balance.available
payout.paidpayout.failedpayout.canceled
webhook_endpoint.createdwebhook_endpoint.updatedwebhook_endpoint.deleted
product.createdproduct.updatedproduct.deleted
price.createdprice.updatedprice.deleted
coupon.createdcoupon.updatedcoupon.deleted
promotion_code.createdpromotion_code.updated
tax_rate.createdtax_rate.updated
customer.tax_id.createdcustomer.tax_id.deleted
file.createdfile.updated
review.openedreview.closed
transfer_reversal.createdtransfer_reversal.updated
application_fee.createdapplication_fee.refunded
The application receives Stripe events from two sources:
-
Webhook Endpoint (
/webhooks)- Real-time event processing
- Validates Stripe signatures using environment variable
- Immediate event storage with type safety
- Handles all Stripe object types
- Uses raw body parsing for secure signature verification
-
Background Task
- Polls Stripe API for events
- Ensures no events are missed
- Handles webhook failures
- Provides backup event collection
src/
├── config/ # Configuration files
├── db/ # Database schema and migrations
├── routes/ # API route handlers
├── services/ # Business logic and services
└── index.ts # Application entry point
npm run dev- Start development servernpm run build- Build the applicationnpm start- Start production servernpm run db:generate- Generate database migrationsnpm run db:push- Push database schema changesnpm run db:studio- Open Drizzle Studio
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.