An E-Commerce API built using Node JS, Express JS, Firebase & Stripe.
npm run dev
npx firebase emulators:start --project demo-e-commerce-api
npx stripe listen --forward-to localhost:3000/api/webhooks/stripe --events checkout.session.completed
http://127.0.0.1:4000
http://localhost:3000
This API uses Stripe Checkout to securely process payments. It uses a two-step flow :
- Order Creation : When a
POSTrequest is made to/api/orders, the backend securely calculates the total price from the database and creates a Stripe Checkout Session. It returns acheckout_urlin API response in Postman and saves the order in Firestore asPending. - Webhook Automation : When the user opens that
checkout_urlin the browser and completes the payment, Stripe sends a Webhook event (checkout.session.completed) to the backend. The server verifies the signature and automatically updates the order'spayment_statustoPaid.
Testing Payments :
Use the Stripe Card 4242 4242 4242 4242 (Dummy) with a future expiration date 12/26 (Dummy) and a 3-digit CVC 123 (Dummy) to simulate a successful transaction.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | / |
Home Page | No |
| GET / POST | /api/users |
Fetch All Users / Create User | No |
| GET / PUT / DELETE | /api/users/:id |
Manage Specific User | Admin (PUT / DELETE) |
| GET / POST | /api/categories |
Fetch All / Create Category | Admin (POST Only) |
| GET / PUT / DELETE | /api/categories/:id |
Manage Specific Category | Admin (PUT / DELETE) |
| GET / POST | /api/products |
Fetch All / Create Product | Admin (POST Only) |
| GET / PUT / DELETE | /api/products/:id |
Manage specific Product | Admin (PUT/DELETE) |
| GET / POST | /api/orders |
Fetch All / Create Order & Stripe URL | No |
| GET | /api/orders/:id |
Fetch specific Order | No |
| POST | /api/webhooks/stripe |
Stripe Webhook Listener | Stripe Signature |
To test protected routes (POST, PUT, DELETE), you must include the custom header :
- Key :
x-user-id - Value :
admin_1(For Admin Access) oruser_1(For Standard User Access)
Endpoint : POST /api/users
Headers : Not Required
Note : Do not include stripe_id in the body. The backend automatically generates it via Stripe.
Admin User :
{
"id": "admin_1",
"name": "John Doe",
"address": "Bangalore",
"phone": "9876543210",
"role": "Admin"
}
Normal User :
{
"id": "user_1",
"name": "Jane Doe",
"address": "Mumbai",
"phone": "9873216540",
"role": "User"
}
Endpoint : POST /api/categories
Headers : x-user-id: admin_1
{
"name": "Electronics",
"description": "Gadgets"
}
(Copy the id from the response to use in the Product payload).
Endpoint : POST /api/products
Headers : x-user-id: admin_1
{
"name": "Laptop",
"description": "MacBook Pro",
"price": 1999.99,
"category_ids": ["PASTE_YOUR_CATEGORY_ID_HERE"]
}
(Copy the id from the response to use in the Order payload).
Endpoint : POST /api/orders
Headers : Not Required
{
"buyer_id": "user_1",
"order_items": [
{
"product_id": "PASTE_YOUR_PRODUCT_ID_HERE",
"quantity": 2
}
]
}
(Copy the checkout_url from the response, paste it into your browser, and use the test card below to pay).
Use these dummy details on the Stripe Checkout page to simulate a successful transaction :
- E-Mail :
testuser@gmail.com(Or Any E-Mail) - Card Number :
4242 4242 4242 4242(Or Any 16 Digits) - Expiration Date :
12/26(Or Any Future Date) - CVC :
123(Or Any 3 Digits) - Name :
Test User(Or Any Name)