1.0.2 β€’ Published 9 months ago

@codesled/stripe-payments v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

@codesled/stripe-payments

Simple, composable Stripe checkout integration for Node.js applications. Create Stripe Checkout sessions and verify webhooks with minimal setup β€” no boilerplate, no complexity.

Framework-agnostic and dev-first. Designed to make payments frictionless to implement.

πŸš€ Part of the CodeSled developer toolkit β€” reusable modules that help you build apps faster.


Features

  • Create Stripe Checkout sessions with one function
  • Supports both one-time and subscription payments
  • Minimal setup with secure defaults
  • Verify Stripe webhook signatures
  • Compatible with Express, Next.js, or any Node backend
  • Plug-and-play β€” no need to read Stripe docs
  • Now supports manual Stripe key initialization with initStripe()

Installation

npm install @codesled/stripe-payments

You must have a Stripe account and access to secret keys.


Environment Setup

Create a .env file (or set env variables) with the following:

STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...

These values are found in your Stripe Dashboard.


Initialization

You must call initStripe() once before using createCheckoutSession() or verifyWebhookSignature().

Option 1 β€” Using .env (recommended):

require("dotenv").config();
const { initStripe } = require("@codesled/stripe-payments");

initStripe(); // uses STRIPE_SECRET_KEY from .env

Option 2 β€” Manual Stripe key:

const { initStripe } = require("@codesled/stripe-payments");

initStripe("sk_test_yourSecretKeyHere");

Usage Example

Creating a Checkout Session

const {
  initStripe,
  createCheckoutSession,
} = require('@codesled/stripe-payments');

initStripe(); // or pass the key manually

const sessionUrl = await createCheckoutSession({
  priceId: 'price_123',
  successUrl: 'https://yourapp.com/success',
  cancelUrl: 'https://yourapp.com/cancel',
  customerEmail: 'user@example.com',
  mode: 'payment' // "payment" for one-time or "subscription" for recurring
});

console.log('Redirect to:', sessionUrl);

Verifying Webhook Events

Use in a raw body parser route (e.g. in Express or Next.js API route):

const {
  initStripe,
  verifyWebhookSignature,
} = require('@codesled/stripe-payments');

initStripe(); // or initStripe("sk_test_...")

const event = verifyWebhookSignature(req.rawBody, req.headers['stripe-signature']);

// Handle event type
if (event.type === 'checkout.session.completed') {
  const session = event.data.object;
  console.log('Checkout complete:', session.id);
}

Functions

FunctionPurpose
initStripe()Initializes Stripe with your secret key
createCheckoutSession()Generate a hosted Stripe Checkout session URL
verifyWebhookSignature()Validate incoming webhook using Stripe secret

Notes

  • Supports both subscription mode and one-time mode via the mode option.
  • Requires raw body middleware for webhook validation (e.g., express.raw({ type: 'application/json' })).
  • initStripe() must be called before any other function.

Local Testing

Use Stripe CLI or Dashboard to trigger test webhooks:

stripe listen --forward-to localhost:3000/webhook

Why Use This Package?

Stripe is powerful, but overkill for simple needs. This package abstracts away Stripe’s verbose API and lets you:

  • 🚫 Avoid boilerplate
  • 🧐 Skip reading docs
  • 🧹 Reuse across all CodeSled apps

Focus on shipping β€” we handle the payment logic.


License

MIT β€” Free to use, share, and build on.


About CodeSled

CodeSled is a modular developer toolkit β€” reusable code blocks for core app functionality (auth, payments, CMS, and more). Build faster. Ship smarter.

Follow @codesled for updates.

1.0.2

9 months ago

1.0.1

9 months ago

0.1.0

9 months ago