New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

next-stripe-helper

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-stripe-helper - npm Package Compare versions

Comparing version 1.1.19 to 1.1.20

{
"name": "next-stripe-helper",
"version": "1.1.19",
"version": "1.1.20",
"description": "Easily add Stripe boilerplate code to Nextjs, like webhook handling, and subscription updates. This package provides a thin wrapper around the Stripe API, and makes integrating Stripe and NextJS a lot faster!",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -21,3 +21,3 @@ # next-stripe-helper

Ensure you have set the `STRIPE_SECRET_LIVE` or `STRIPE_SECRET` environment variables in your `.env.local` (or other environment-specific `.env` files). With Next.js, you can access these environment variables using `process.env`.
Ensure you have set the `STRIPE_SECRET_LIVE` or `STRIPE_SECRET` environment variables in your `.env.local` (or other environment-specific `.env` files).

@@ -387,6 +387,10 @@ Ensure you have set the `STRIPE_WEBHOOK_SECRET_LIVE` or `STRIPE_WEBHOOK_SECRET` environment variables in your `.env.local` (or other environment-specific `.env` files).

In the Next.js environment, API routes provide a solution to build your backend functionality. The `next-stripe-helper` comes equipped with a webhook handler specifically designed for easy integration with Next.js API routes.
In the Next.js 13 environment, API routes provide a solution to build your backend functionality. The `next-stripe-helper` comes equipped with a webhook handler specifically designed for easy integration with Next.js API routes.
If you add the webhookHandler to an api route, your Database will automatically stay in sync with Stripe Products, Prices, and Subscriptions.
If you add the webhookHandler to an api route, your Database can automatically stay in sync with Stripe Products, Prices, and Subscriptions.
First create your DB functions (upsertProductRecord, upsertPriceRecord, manageSubscriptionStatusChange, manageCustomerDetailsChange), then use them with the webhookHandler function in an api endpoint.
You can find example functions below that use MongoDb, but it can be used with any DB type.
### Usage

@@ -406,3 +410,3 @@

import { webhookHandler } from 'next-stripe-helper';
import { manageSubscriptionStatusChange, upsertPriceRecord, upsertProductRecord } from '@/lib/stripe';
import { manageSubscriptionStatusChange, manageCustomerDetailsChange, upsertPriceRecord, upsertProductRecord } from '@/lib/stripe';
import { headers } from "next/headers"

@@ -423,2 +427,3 @@

manageSubscriptionStatusChange,
manageCustomerDetailsChange,
{ body, signature }

@@ -437,10 +442,12 @@ );

Here's an example of Stripe Helper Functions for a MongoDB Database.
Examples of Webhook functions you could use with a MongoDB Database.
``` javascript
import clientPromise from '@/lib/mongodb'
import { ObjectId } from 'mongodb'
import clientPromise from '@/lib/mongodb'; //assuming you have a funciton to get your clientPromise for your DB.
import { ObjectId } from 'mongodb';
const dbName = process.env.MONGODB_DB
const dbName = process.env.MONGODB_DB; //assuming you are using the db name.
//this will GET the active Stripe products and related Prices in your DB
export const getActiveProductsWithPrices = async () => {

@@ -473,2 +480,3 @@ try {

//this will GET the active Stripe products in your DB
export const getActiveApiProductsWithPrices = async () => {

@@ -507,3 +515,3 @@ try {

//this will keep the active Stripe products updated in your DB
export const upsertProductRecord = async (product) => {

@@ -533,2 +541,3 @@ const productData = {

//this will keep the active Stripe prices updated in your DB
export const upsertPriceRecord = async (price) => {

@@ -563,2 +572,3 @@ const priceData = {

//this will keep your user billing details updated in your DB
const copyBillingDetailsToCustomer = async (uuid, payment_method) => {

@@ -586,2 +596,3 @@ const customer = payment_method.customer

//this will keep the customer subscriptions updated in your DB
export const manageSubscriptionStatusChange = async (

@@ -644,4 +655,2 @@ subscriptionId,

// console.log('subscriptionItems',subscriptionItems)
const subscriptionData = {

@@ -686,4 +695,2 @@ _id: subscription.id,

console.log('manageSubscriptionStatusChange: update: ', result)
if (result.upsertedCount || result.modifiedCount) {

@@ -707,41 +714,10 @@ console.log(

}
```
## Sync with Stripe
## Syncing Stripe Products with Your Local Database
To ensure that your local database reflects your current Stripe products, you can utilize the provided syncing utility. First create DB functions, then use them with the syncWithStripe function in an api endpoint. You can then create a simple button in your app that triggers the sync function.
Note: If you add the webhookHandler shown above to an api route, your Database will automatically stay in sync with Stripe Products, Prices, and Subscriptions.
### Integration Steps:
1. **Add the Syncing Endpoint**:
Set up an API route in your Next.js project that uses the sync utility:
```javascript
// pages/api/syncStripe.js
import { syncWithStripe } from 'next-stripe-helper';
const dbOperations = {
getAllLocalProducts: async () => { /* fetch all products from the DB */ },
addProduct: async (product) => { /* add new product to the DB */ },
updateProduct: async (product) => { /* update existing product in the DB */ },
};
export default async (req, res) => {
if (req.method === 'POST') {
const result = await syncWithStripe(dbOperations);
if (result.success) {
res.status(200).json({ message: result.message });
} else {
res.status(500).json({ error: result.error, details: result.details });
}
} else {
res.status(405).end(); // Method Not Allowed
}
};
//this will keep the customer id and thier paymentID updated in your DB
export async function manageCustomerDetailsChange(
customerId: string,
defaultPaymentMethodId: string,
) {
// Your code to update the user document in your database
}
```

@@ -751,3 +727,3 @@

1. **Environment Variables**: Ensure you have set the `STRIPE_WEBHOOK_SECRET_LIVE` or `STRIPE_WEBHOOK_SECRET` environment variables in your `.env.local` (or other environment-specific `.env` files). With Next.js, you can access these environment variables using `process.env`.
1. **Environment Variables**: Ensure you have set the `STRIPE_WEBHOOK_SECRET_LIVE` or `STRIPE_WEBHOOK_SECRET` environment variables in your `.env.local` (or other environment-specific `.env` files). With Next.js, you can access these environment variables using `process.env`. Remember best practice is giving the api key the least amount of access needed.

@@ -758,3 +734,3 @@ 2. **Stripe Dashboard**: Configure your Stripe dashboard to send webhooks to `https://your-domain.com/api/stripe-webhook`.

- As with the general use-case, you need to provide the `upsertProduct`, `upsertPrice`, and `manageSubscriptionChange` callback functions. These functions will handle the various events as they occur on Stripe.
- As with the general use-case, you need to provide the `upsertProduct`, `upsertPrice`, `manageSubscriptionChange`, and `manageCustomerDetailsChange` callback functions. These functions will handle the various events as they occur on Stripe.

@@ -765,3 +741,3 @@ - Always handle errors gracefully. The provided webhook handler has built-in error handling, but you may want to extend or customize this for your specific needs.

When deploying your Next.js application, make sure to include your Stripe webhook secret in your production environment variables or secrets management solution.
When deploying your Next.js application, make sure to include your Stripe webhook secret in your production environment variables or secrets management solution. Never expose the secret or api keys.

@@ -768,0 +744,0 @@ ## Error Handling