This backend service integrates PostgreSQL (via Prisma ORM) and Stripe payments for managing user subscriptions and premium content access.
Make sure you have the following installed on your local machine:
- Node.js (v18 or higher recommended)
- PostgreSQL Database (or a remote PostgreSQL connection URL)
- Stripe CLI (for local webhook forwarding and testing)
Navigate to the project root directory and install the package dependencies:
npm installCreate a .env file in the root directory (you can copy .env.example) and configure your values:
PORT=5000
APP_URL="http://localhost:5000"
# Database Connection URL (PostgreSQL)
DATABASE_URL="postgres://username:password@hostname:port/database?sslmode=require"
# Password Hashing
BCRYPT_SALT_ROUNDS=10
# JWT Auth Configuration
JWT_ACCESS_SECRET="your-jwt-access-secret"
JWT_ACCESS_EXPIRY="1d"
JWT_REFRESH_SECRET="your-jwt-refresh-secret"
JWT_REFRESH_EXPIRY="7d"
# Stripe API Keys (Retrieve from Stripe Dashboard in Test Mode)
STRIPE_PRODUCT_PRICE_ID="price_1Tp..."
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."Prisma in this project is configured to use the multi-file schema feature (prisma.config.ts), with models split inside the prisma/model directory.
Run the following command to apply all existing migrations to your database:
npx prisma migrate deploy(Note: If you have modified the schemas in prisma/model and want to create a new migration, run npx prisma migrate dev --name <migration_name> instead.)
Generate the type-safe Prisma client:
npx prisma generateRuns the application in watch mode using tsx:
npm run devLaunches a browser interface to view and edit database tables at http://localhost:5555:
npm run prisma:studioTo test checkout payments and subscription updates locally:
-
Download and Log In to Stripe CLI: Follow instructions to install Stripe CLI, then authorize it:
stripe login
-
Start the Webhook Forwarder: Start forwarding Stripe events to your local server:
npm run stripe:webhook
This will output a line like:
> Ready! Your webhook signing secret is: whsec_xxxxxxxxxx -
Update Webhook Secret: Copy the
whsec_...secret and paste it as theSTRIPE_WEBHOOK_SECRETinside your.envfile. -
Trigger Webhook Events: Open a separate terminal to trigger events manually or test checkout through the API:
- To trigger checkout completion:
stripe trigger checkout.session.completed - To cancel a subscription:
stripe subscriptions cancel <sub_id>
- To trigger checkout completion:
We've added testing scripts under the scratch/ directory to help you quickly mock data:
-
Check Database Records: Verify what users and subscriptions currently exist:
npx tsx scratch/check_db.ts
-
Mock Active Premium Subscriptions: Instantly make all database users premium so they can create premium posts:
npx tsx scratch/make_premium.ts
-
Clear Subscriptions: Wipe out subscription logs so you can run a clean checkout test from scratch:
npx tsx scratch/clear_subscriptions.ts