StripeClientDemo
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v9View latest version
This is a Stripe Checkout implementation that uses the actual price displayed on the page instead of a predefined price ID. It combines client-side and server-side approaches to create a seamless checkout experience.
- The page loads with a product display and checkout button
- When the user clicks "Proceed to Checkout":
- The code extracts the price directly from the page ($19.99)
- It sends this price to a server-side endpoint (/create-checkout-session)
- The server creates a Stripe Checkout Session with this exact amount
- The client redirects to Stripe's hosted checkout page
- After payment, the user is redirected back to the success or cancel URL
- The page displays a success or cancellation message based on the URL parameters
-
Environment Variables:
- Set your
STRIPE_SECRET_KEYin Val Town environment variables - Replace the placeholder publishable key (
pk_test_TYooMQauvdEDq54NiTphI7jx) with your actual Stripe publishable key in the HTML
- Set your
-
Advantages of This Approach:
- Uses the displayed price directly from the UI
- No need to create and manage price IDs in Stripe dashboard
- Prices can be changed in the UI without updating Stripe
- More secure than client-side only approaches
-
Security Considerations:
- Never include your Stripe secret key in client-side code
- The publishable key is safe to include in client-side code
- Server-side validation ensures price integrity
To customize this implementation:
- Update the product details and pricing in the HTML
- The checkout will automatically use whatever price is displayed on the page
- Customize the success and cancel messages
- Adjust the styling as needed using Tailwind classes
- Uses Stripe's Checkout Sessions API on the server
- Extracts the price directly from the UI
- Converts the price to cents (Stripe's required format)
- Creates a dynamic product with the specified price
- Handles errors gracefully with user feedback
