next-stripe-helper
Advanced tools
Comparing version 1.0.2-beta.0 to 1.0.3
{ | ||
"name": "next-stripe-helper", | ||
"version": "1.0.2-beta.0", | ||
"version": "1.0.3", | ||
"description": "Easily add Stripe boilerplate code to Nextjs, like webhook handling, and subscription updates. This package provides a think wrapper around the Stripe API.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -301,43 +301,30 @@ # next-stripe-helper | ||
import { webhookHandler } from 'stripe-next-helper'; | ||
import { webhookHandler } from 'next-stripe-helper'; | ||
import { manageSubscriptionStatusChange, upsertPriceRecord, upsertProductRecord } from '@/lib/stripe'; | ||
import { headers } from "next/headers" | ||
// Import or define upsertProduct, upsertPrice, and manageSubscriptionChange here. These are functions you create to handle the Database updates. | ||
// Ensure you have error handling and necessary functions. | ||
export async function POST(req) { | ||
try { | ||
// Validate if the request is legitimate (e.g., you might want to check if there's a user session, but for a stripe webhook, it might not be necessary.) | ||
try { | ||
const body = await req.text(); | ||
const signature = headers().get("Stripe-Signature"); | ||
const handleWebhook = webhookHandler( | ||
async (product) => { | ||
// upsertProduct logic here | ||
}, | ||
async (price) => { | ||
// upsertPrice logic here | ||
}, | ||
async (subscriptionId, customerId, isNew) => { | ||
// manageSubscriptionChange logic here | ||
} | ||
); | ||
if (!signature) { | ||
throw new Error('Stripe signature missing from headers'); | ||
} | ||
// Call the handler with the adapted request/response format | ||
const response = await handleWebhook(req, { | ||
status: (statusCode) => ({ statusCode }), | ||
json: (body) => new Response(JSON.stringify(body)), | ||
setHeader: (name, value) => {}, // You can extend this if needed | ||
end: (text) => new Response(text) | ||
}); | ||
await webhookHandler( | ||
upsertProductRecord, | ||
upsertPriceRecord, | ||
manageSubscriptionStatusChange, | ||
{ body, signature } | ||
); | ||
return response; | ||
} catch (error) { | ||
// Handle your errors. If it's a validation error, return a 422 status. Otherwise, return a 500 status. | ||
if (/* it's a validation error */) { | ||
return new Response(JSON.stringify(/* error details */), { status: 422 }); | ||
return new Response(null, { status: 200 }); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(JSON.stringify({ error: error.message }), { status: 500 }); | ||
} | ||
return new Response(null, { status: 500 }); | ||
} | ||
} | ||
``` | ||
@@ -351,5 +338,9 @@ | ||
const dbName = process.env.MONGODB_DB | ||
export const getActiveProductsWithPrices = async () => { | ||
try { | ||
const data = await clientPromise | ||
const client = await clientPromise | ||
const data = await client | ||
.db(dbName) | ||
.collection('products') | ||
@@ -379,4 +370,5 @@ .aggregate([ | ||
try { | ||
const data = await clientPromise | ||
const client = await clientPromise | ||
const data = await client | ||
.db(dbName) | ||
.collection('products') | ||
@@ -420,4 +412,5 @@ .aggregate([ | ||
} | ||
const result = await clientPromise | ||
const client = await clientPromise | ||
const result = await client | ||
.db(dbName) | ||
.collection('products') | ||
@@ -449,4 +442,5 @@ .updateOne( | ||
} | ||
const result = await clientPromise | ||
const client = await clientPromise | ||
const result = await client | ||
.db(dbName) | ||
.collection('prices') | ||
@@ -470,4 +464,4 @@ .updateOne( | ||
await stripe.customers.update(customer, { name, phone, address }) | ||
const { db } = await connectToDatabase() | ||
const result = await db.collection('users').findOneAndUpdate( | ||
const client = await clientPromise | ||
const result = await client.db(dbName).collection('users').findOneAndUpdate( | ||
{ _id: uuid }, | ||
@@ -494,5 +488,5 @@ { | ||
console.log(customerId) | ||
const customerData = await clientPromise | ||
const client = await clientPromise | ||
const customerData = await client | ||
.db(dbName) | ||
.collection('customers') | ||
@@ -577,4 +571,4 @@ .findOne({ stripe_customer_id: customerId }) | ||
} | ||
const result = await clientPromise | ||
const result = await client | ||
.db(dbName) | ||
.collection('subscriptions') | ||
@@ -581,0 +575,0 @@ .updateOne( |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
37379
658