我正在使用 Prisma 和 next.js 14,并且我已经实现了 Stripe 支付 API。 这是我在这里的 API,我遇到错误
ReferenceError: prisma is not defined
我该如何解决这个问题?
import { NextApiRequest, NextApiResponse } from "next"
import { buffer } from "stream/consumers"
import Stripe from "stripe"
export const config = {
api:{
bodyParser: false
}
}
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string,{
apiVersion: '2023-10-16'
})
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
){
const buf = await buffer(req)
const sig = req.headers['stripe-signature']
if(!sig){
return res.status(400).send("Missing the stripe signature!")
}
let event: Stripe.Event
try{
event = stripe.webhooks.constructEvent(
buf, sig, process.env.STRIPE_WEBHOOK_SECRET!
)
}catch(err){
return res.status(400).send("Webhook error" + err)
}
switch(event.type){
case 'charge.succeeded':
const charge: any = event.data.object as Stripe.Charge;
if(typeof charge.payment_intent === 'string'){
await prisma?.order.update({
where: {paymentIntentId: charge.payment_intent},
data: {status: "complete", address: charge.shipping?.address},
});
}
break;
default:
console.log("Unhandled event type:" + event.type)
}
res.json({received: true})
}
这是我的文件夹结构。
这是错误。
Unhandled event type:payment_intent.succeeded
⨯ pages\api\stripe-webhook.ts (41:16) @ handler
⨯ ReferenceError: prisma is not defined
at handler (webpack-internal:///(api)/./pages/api/stripe-webhook.ts:38:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
page: '/api/stripe-webhook'
}
39 | const charge: any = event.data.object as Stripe.Charge;
40 | if(typeof charge.payment_intent === 'string'){
> 41 | await prisma?.order.update({
| ^
42 | where: {paymentIntentId: charge.payment_intent},
43 | data: {status: "complete", address: charge.shipping?.address},
44 | });
○ Compiling /_error ...
✓ Compiled /_error in 2.7s (736 modules)
在您的代码中,您根本没有导入和实例化
Prisma
。
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient();
请参阅快速入门文档了解更多信息。