Skip to content

IbnBaqqi/storeApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

148 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Store Api

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

🚀 Getting Started

1. Clone the Repository

git clone https://github.com/IbnBaqqi/storeApi.git
cd storeApi

2. Configure Environment Variables

  • Rename the .env.example file to .env.
  • Update the following environment variables inside .env:

JWT_SECRET

Generate a secure random key using:

openssl rand -base64 32

If openssl is not available, go to generate-random.org, click on Strings > API Tokens, and generate a secure token.

STRIPE_SECRET_KEY

  • 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.

STRIPE_WEBHOOK_SECRET_KEY

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.

▶️ Running the Project

This is a Maven project. To start the application, run:

./mvnw spring-boot:run

If you're on Windows:

mvnw.cmd spring-boot:run

Once running, the application will be available at:

http://localhost:8080

📚 API Documentation

Swagger UI is available at:

http://localhost:8080/swagger-ui.html

🧪 Example API Flow

Here's a sample flow to help you understand how to interact with the API after starting the application.

1. Get All Products

GET /products

The database is automatically populated with 10 sample products using a Flyway migration script.

2. Create a Shopping Cart

POST /carts

This will return the cart ID. You don't need to be logged in to create a cart.

3. Add Items to Cart

Once you have a cart ID, you can add products to it by sending:

POST /carts/{cartId}/items

Request body example:

{
  "productId": 1
}

4. Register a New User

To check out, you have to register and login first:

POST /users

Request body:

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "123456"
}

5. Login to Get an Access Token

POST /auth/login 

Request body:

{
  "email": "john@example.com",
  "password": "123456"
}

Response body:

{
  "token": "your-json-web-token"
}

6. Checkout

POST /checkout 

Headers

Authorization: Bearer your-json-web-token

Request 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 digits

7. Webhook & Order Status Update

Once 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.


Find a bug?

If any of the endpoints are not working Please create an issue.

About

E-Commerce API built with Java & Springboot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors