10

Github GitHub - ynnoj/next-stripe: Simplified server-side Stripe workflows in Ne...

 3 years ago
source link: https://github.com/ynnoj/next-stripe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

NextStripe

Simplified server-side Stripe workflows in Next.js

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

Getting Started

yarn add next-stripe@beta

Add the API route

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

import NextStripe from 'next-stripe'

export default NextStripe({
  secret_key: process.env.STRIPE_SECRET_KEY
})

Usage

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

Checkout Sessions

Create

Stripe API Docs

import { createCheckoutSession } from '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

Stripe API Docs

import { createPaymentIntent } from 'next-stripe/client'

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

Confirm

Stripe API Docs

import { confirmPaymentIntent } from 'next-stripe/client'

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

Retrieve

Stripe API Docs

import { retrievePaymentIntent } from 'next-stripe/client'

const paymentIntent = await retrievePaymentIntent('pi_id')

Update

Stripe API Docs

import { updatePaymentIntent } from 'next-stripe/client'

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

Billing Portal Sessions

Create

Stripe API Docs

import { createBillingPortalSession } from '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 inspred by NextAuth.
  • Thanks to Jamie Barton for the initial idea.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK