Skip to content

coding404life/React-Stripe-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stripe Checkout Server (Express)

A minimal Express server that creates Stripe Checkout Sessions and returns the Checkout URL to a client (SPA or web app).

Prerequisites

  • Node.js 18+
  • A Stripe account and a Secret Key

Environment variables

Create a .env file in the project root:

STRIPE_PRIVATE_KEY=sk_test_...
CLIENT_URL=http://localhost:5173
  • STRIPE_PRIVATE_KEY: Your Stripe Secret Key (from the Stripe Dashboard).
  • CLIENT_URL: Your frontend/app origin used for CORS and success/cancel redirects.

Install

pnpm install
# or
npm install
# or
yarn install

Run

pnpm start
# or
npm start

Server starts on http://localhost:4242.

Endpoint

POST /create-checkout-session

Creates a Stripe Checkout Session from the provided cart items and returns the Checkout URL.

Request body (JSON):

{
  "items": [
    {
      "name": "T-Shirt",
      "image": "https://example.com/tshirt.png",
      "price": 1999,
      "amount": 2
    }
  ]
}
  • price is in the smallest currency unit (e.g., cents for USD).
  • amount is the quantity.

Response (JSON):

{ "url": "https://checkout.stripe.com/c/session_..." }

Example: calling from a client (fetch)

const res = await fetch("http://localhost:4242/create-checkout-session", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ items }),
});
const { url } = await res.json();
window.location.assign(url); // or window.location.replace(url)

Example: test with curl

curl -X POST http://localhost:4242/create-checkout-session \
  -H 'Content-Type: application/json' \
  -d '{
    "items": [
      {"name": "T-Shirt", "image": "https://example.com/tshirt.png", "price": 1999, "amount": 1}
    ]
  }'

CORS

The server allows requests from CLIENT_URL. Ensure your frontend runs at that origin (e.g., http://localhost:5173).

Success and cancel URLs

success_url and cancel_url use CLIENT_URL with query params ?success=true and ?canceled=true. Update in server.js if you need custom routes.

Notes

  • This server uses dynamic price_data; in production, prefer predefined Stripe Prices for accuracy and security.
  • Do not expose your Stripe Secret Key in the client.
  • Consider handling Stripe webhooks for post-payment fulfillment.

About

Stripe checkout server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published