User API
Purchase Course
Initiate a course purchase by creating a Razorpay payment order
POST
Authentication
This endpoint requires authentication via Clerk. TheuserId is automatically extracted from the authentication token.
Request Body
The unique identifier of the course to purchase
Response
Indicates whether the request was successful
Unique identifier for the purchase record (used in payment verification)
Razorpay order ID for the payment
Razorpay key ID for client-side payment integration
Error message if success is false
Request Example
Response Examples
Error Codes
| Status Code | Description |
|---|---|
| 200 | Success or data not found (check success field) |
| 400 | Invalid request data - courseId is required |
| 401 | Unauthorized - Invalid or missing authentication token |
| 500 | Internal server error |
Purchase Flow
- Create Purchase Record: A purchase record is created with status
'created'in the database - Calculate Amount: Final amount is calculated after applying any discounts:
price - (discount * price / 100) - Create Razorpay Order: A Razorpay order is created with the purchase details
- Return Order Details: The client receives
purchaseId,orderId, andkeyIdto initiate payment - Payment Verification: After payment, use the
/api/user/verify-razorpayendpoint to complete the purchase
Important Notes
- The purchase record is created immediately with status
'created' - If Razorpay order creation fails, the purchase record is automatically deleted to keep the database clean
- The
purchaseIdmust be passed to the payment verification endpoint after successful payment - The amount is calculated using the course’s
coursePriceanddiscountfields - Currency defaults to the
CURRENCYenvironment variable or'INR'