In this project, I built the backend for an e-commerce application using Spring Boot. The API includes endpoints for:
- Managing products
- Managing shopping carts
- Checking out
- Viewing order history
git clone https://github.com/IbnBaqqi/storeApi.git
cd storeApi- Rename the
.env.examplefile to.env. - Update the following environment variables inside .env:
Generate a secure random key using:
openssl rand -base64 32If openssl is not available, go to generate-random.org, click on Strings > API Tokens, and generate a secure token.
- Create a free account at stripe.com
- On your dashboard, go to Developers > API Keys. You can use the search bar for quick access.
- Copy the value of the Secret Key.
- Install the Stripe CLI: https://docs.stripe.com/stripe-cli
- Login and start the webhook listener:
stripe login
stripe listen --forward-to http://localhost:8080/checkout/webhook- Copy the signing secret from the terminal output and use it as the value for
STRIPE_WEBHOOK_SECRET_KEY.
This is a Maven project. To start the application, run:
./mvnw spring-boot:runIf you're on Windows:
mvnw.cmd spring-boot:runOnce running, the application will be available at:
http://localhost:8080
Swagger UI is available at:
http://localhost:8080/swagger-ui.htmlHere's a sample flow to help you understand how to interact with the API after starting the application.
GET /productsThe database is automatically populated with 10 sample products using a Flyway migration script.
POST /cartsThis will return the cart ID. You don't need to be logged in to create a cart.
Once you have a cart ID, you can add products to it by sending:
POST /carts/{cartId}/itemsRequest body example:
{
"productId": 1
}To check out, you have to register and login first:
POST /usersRequest body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "123456"
}POST /auth/login Request body:
{
"email": "john@example.com",
"password": "123456"
}Response body:
{
"token": "your-json-web-token"
}POST /checkout Headers
Authorization: Bearer your-json-web-tokenRequest body
{
"cartId": "your-cart-id"
}This endpoint returns a Stripe checkout URL. Open it in your browser to complete the payment using a test card:
Card: 4242 4242 4242 4242
Expiry: Any future date
CVC: Any 3 digitsOnce payment is completed, Stripe will trigger a webhook call to:
POST /checkout/webhook Our backend listens for this event and updates the order status in the database accordingly.
If any of the endpoints are not working Please create an issue.