Payments
Create Payment Intent
Create a Stripe payment intent for purchasing content
POST
Creates a Stripe payment intent to initiate the payment flow for content purchase. This endpoint verifies the content exists, checks for duplicate purchases, and creates a pending purchase record.
Authentication
Requires authentication. Include the JWT token in the Authorization header.Request Body
The MongoDB ObjectId of the content to purchase
Response
Stripe client secret for completing the payment on the frontend. Pass this to Stripe Elements or Stripe.js to collect payment details.
The purchase amount in USD (dollars, not cents)
Payment Intent Metadata
The created Stripe payment intent includes the following metadata:contentId- The MongoDB ObjectId of the contentuserId- The MongoDB ObjectId of the purchasing usercontentTitle- The title of the content being purchased
Payment Flow
- Client calls this endpoint with
contentId - Server validates content exists and is published
- Server checks user hasn’t already purchased the content
- Server creates Stripe payment intent with amount in cents (price × 100)
- Server creates pending Purchase record in database
- Client receives
clientSecretto complete payment - User completes payment using Stripe on frontend
- Stripe sends webhook to
/api/payments/webhook - Server updates Purchase status to
completed
Validation
- Content must exist and have status
published - User cannot purchase the same content twice
- User must be authenticated
Error Codes
| Status Code | Description |
|---|---|
| 200 | Payment intent created successfully |
| 400 | Content already purchased by user |
| 401 | Not authenticated |
| 404 | Content not found or not published |
| 500 | Server error or Stripe API error |
Related Endpoints
- Get Payment Status - Check status of a payment
- Webhook - Stripe webhook handler for payment events
Implementation Notes
- Currency is hardcoded to USD
- Stripe amounts are in cents (price is multiplied by 100)
- A pending Purchase record is created immediately
- The Purchase status is updated to
completedvia webhook when payment succeeds - Source:
src/controllers/paymentController.js:6-61