Stripe Payments
Accept payments with Stripe subscriptions
Stripe Payments
This guide shows how to set up subscription payments in Eden Stack using the @eden/payments package.
Prerequisites
Before you start:
- A Stripe account
- API keys from Stripe Dashboard
- At least one product with a price configured (for subscriptions)
Environment Setup
Add your Stripe keys to .env:
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...You'll get STRIPE_WEBHOOK_SECRET when setting up the webhook endpoint.
Creating Checkout Sessions
To start a payment, create a Checkout Session. The @eden/payments package exports createCheckoutSession configured for subscriptions:
import { createCheckoutSession } from "@eden/payments";
// In your API route
const session = await createCheckoutSession({
priceId: "price_123...", // From Stripe Dashboard
customerEmail: "user@example.com",
successUrl: "http://localhost:3000/success",
cancelUrl: "http://localhost:3000/cancel",
});
// Redirect user to session.urlThis function sets mode: "subscription" and configures card payments automatically.
Handling Webhooks
To know when payments complete or subscriptions update, listen to Stripe webhooks. Always verify the signature to ensure requests come from Stripe.
Use constructWebhookEvent from @eden/payments:
import { constructWebhookEvent } from "@eden/payments";
// In your webhook endpoint
app.post("/api/webhooks/stripe", async ({ request }) => {
const payload = await request.text();
const signature = request.headers.get("stripe-signature")!;
try {
const event = await constructWebhookEvent(payload, signature);
switch (event.type) {
case "checkout.session.completed":
// Handle successful payment
const session = event.data.object;
await activateSubscription(session.customer);
break;
case "customer.subscription.deleted":
// Handle cancellation
break;
}
return { received: true };
} catch (err) {
console.error("Webhook error:", err);
return new Response("Webhook Error", { status: 400 });
}
});For robust webhook handling, process events with background jobs. See the Background Jobs guide.
Testing with Stripe CLI
The easiest way to test webhooks locally:
-
Install and login:
stripe login -
Forward events to your local API:
stripe listen --forward-to localhost:3001/api/webhooks/stripe -
Trigger a test event:
stripe trigger checkout.session.completed
Copy the webhook signing secret (starts with whsec_) from the stripe listen output to your .env.
Next Steps
- Background Jobs - Process webhooks reliably
- Packages - See all package exports
Full documentation for Eden Stack users
This documentation is exclusively available to Eden Stack customers. Already purchased? Log in to access the full content.