Alt description missing in image
Beta: Plugins coming soon!
@payments/stripeutilsserverstripe
Alt description missing in image

Stripe Server Utils

Thin wrappers around the Stripe SDK used by the Stripe payments driver.

utils/server/stripe.ts
import Stripe from 'stripe'
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)
export type { Stripe }
💡

The following utils are intentionally minimal. Prefer adding mapping/normalization in resolvers to keep SDK coupling localized.

listAll()

import { listAll } from '@payments/stripe/utils/server/stripe'

Iterates through Stripe list endpoints until all pages are fetched.

const items = await listAll(
  (params) => stripe.subscriptions.list(params),
  { customer: customerId, status }
)

findCustomerById()

import { findCustomerById } from '@payments/stripe/utils/server/stripe'

Retrieve a Stripe.Customer by ID.

const customer = await findCustomerById(customerId)

findCustomerByEmail()

import { findCustomerByEmail } from '@payments/stripe/utils/server/stripe'

Return the first customer matching email.

const existing = await findCustomerByEmail('jane@example.com')

findOrCreateCustomer()

import { findOrCreateCustomer } from '@payments/stripe/utils/server/stripe'

Lookup by email; if none found, create a new Stripe Customer.

const customer = await findOrCreateCustomer({
  email: user.email,
  name: 'Jane Doe',
  metadata: { userId: user.userId },
})

createCustomerCheckoutSession()

import { createCustomerCheckoutSession } from '@payments/stripe/utils/server/stripe'

Create a Checkout Session for a customer and return it.

const session = await createCustomerCheckoutSession({
  customerId,
  mode: 'subscription',
  lineItems: [{ price: 'price_XXXX', quantity: 1 }],
  successUrl,
  cancelUrl,
})

getCustomerCheckouts()

import { getCustomerCheckouts } from '@payments/stripe/utils/server/stripe'

List (and expand) all Checkout Sessions for a customer and attach lineItems.

const sessions = await getCustomerCheckouts(customerId, 'complete')

getCustomerSubscriptions()

import { getCustomerSubscriptions } from '@payments/stripe/utils/server/stripe'

List all subscriptions for a customer.

const subs = await getCustomerSubscriptions(customerId)

getCustomerInvoices()

import { getCustomerInvoices } from '@payments/stripe/utils/server/stripe'

List all invoices (expanded with lines).

const invoices = await getCustomerInvoices(customerId)

getCustomerPaymentIntents()

import { getCustomerPaymentIntents } from '@payments/stripe/utils/server/stripe'

List all payment intents for a customer.

const intents = await getCustomerPaymentIntents(customerId)

getCustomerCharges()

import { getCustomerCharges } from '@payments/stripe/utils/server/stripe'

List all charge objects for a customer.

const charges = await getCustomerCharges(customerId)

getPriceWithProduct()

import { getPriceWithProduct } from '@payments/stripe/utils/server/stripe'

Retrieve a price expanded with its product.

const price = await getPriceWithProduct('price_XXXX')

createCustomerPortalSession()

import { createCustomerCheckoutSession } from '@payments/stripe/utils/server/stripe'

Create a Billing Portal session for a customer.

const portal = await createCustomerPortalSession(customerId, returnUrl)