Skip to content

💰 Simplified server-side Stripe workflows in Next.js

License

Notifications You must be signed in to change notification settings

NuroDev/next-stripe

 
 

Repository files navigation



💰
NextStripe




Simplified server-side Stripe workflows in Next.js

Tests Package Version Package Monthly Downloads

⚠️ PLEASE NOTE: This library is currently in beta and should be used in production with caution!

Getting Started

yarn add @nurodev/next-stripe

Add the API route

Create a [...nextstripe].js catch-all route in your project's pages/api/stripe directory.

⚠️ PLEASE NOTE: It is recommended you use a restricted key with limited API access with this library. These keys can be created and configured with the required access in the Stripe Dashboard.

import NextStripe from '@nurodev/next-stripe';

export default NextStripe({
	stripe_key: process.env.STRIPE_RESTRICTED_KEY,
	options: {
		// Optionally specify Stripe instance options
		// See: https://stripe.com/docs/js/initializing#init_stripe_js-options
	},
});

Usage

@nurodev/next-stripe/client exports helper functions to call the Next.js API routes.

Checkout Sessions

Create

Docs

import { createCheckoutSession } from '@nurodev/next-stripe/client';

const session = await createCheckoutSession({
	success_url: window.location.href,
	cancel_url: window.location.href,
	line_items: [{ price: 'price_id', quantity: 1 }],
	payment_method_types: ['card'],
	mode: 'payment',
});

PaymentIntents

Create

Docs

import { createPaymentIntent } from '@nurodev/next-stripe/client';

const paymentIntent = await createPaymentIntent({
	amount: 1000,
	currency: 'usd',
});

Confirm

Stripe API Docs

import { confirmPaymentIntent } from '@nurodev/next-stripe/client';

const paymentIntent = await confirmPaymentIntent('pi_id', {
	payment_method: 'pm_id',
});

Retrieve

Docs

import { retrievePaymentIntent } from '@nurodev/next-stripe/client';

const paymentIntent = await retrievePaymentIntent('pi_id');

Update

Docs

import { updatePaymentIntent } from '@nurodev/next-stripe/client';

const paymentIntent = await updatePaymentIntent('pi_id', {
	amount: 1000,
	currency: 'usd',
});

Billing Portal Sessions

Create

Docs

import { createBillingPortalSession } from '@nurodev/next-stripe/client';

const session = await createBillingPortalSession({
	customer: 'cus_id',
	return_url: window.location.href,
});

Acknowledgements

  • A lot of the patterns in this library were inspired by NextAuth.
  • Thanks to Jamie Barton for the initial idea.

Packages

No packages published

Languages

  • TypeScript 98.9%
  • JavaScript 1.1%