我正确收到Stripe webhooks的Post请求,但在处理请求时遇到困难. 我是不是错过了什么?控制台.日志(log)不会打印到终端.

这是迄今为止我的代码,以及当从stripe收到checkout.session.completed时我想要处理的内容的概要.

./api/webhook/route.ts
import { createClient } from '@supabase/supabase-js';
import { headers } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';
import { Resend } from 'resend';
import EmailTemplate from '@/components/email-template';


const resend = new Resend(process.env.NEXT_RESEND_API_KEY);

const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);

const stripe = new Stripe(`${process.env.STRIPE_SECRET_KEY}`, {
  apiVersion: '2023-08-16',
});

//Handling incoming POST req
async function POST(request: NextRequest) {
  try {
    const body = await request.text();
    const endpointSecret = process.env.STRIPE_SECRET_WEBHOOK_KEY!;
    const sig = headers().get('stripe-signature') as string;
    // const sig = headers().get('Stripe-Signature') as string;
    let event: Stripe.Event;
    //read the event data from stripe
    try {
      event = stripe.webhooks.constructEvent(body, sig, endpointSecret);
    } catch (err) {
      return new Response(`Webhook Error: ${err}`, {
        status: 400,
      });
    }
    console.log(`Received event !webhook!: ${event?.type}`);
    // Handle the event: checkout.session.completed
    switch (event.type) {
      case 'checkout.session.completed':
        const session: any = event.data.object;
        console.log('!Checkout session completed!:', session.payment_status);

        // get customer name, email
        const customerEmail = session.customer_details.email;
        const customerName = session.customer_details.name;

        // create the customer in db
        const { data: customer, error: errorCustomer } = await supabase
          .from('Customer')
          .insert({ customerName, customerEmail })
          .select()
          .single();

        if (errorCustomer) throw new Error(errorCustomer.message);

       
        break;
      default:
    }
    return NextResponse.json({ received: true });
  } catch (error) {
    return NextResponse.json({ error }, { status: 400 });
  }
}

async function GET(request: NextApiRequest, response: NextApiResponse) {
  // Bad Request or how ever you want to respond.
  return response.status(400).json({ error: 'Bad Request' });
}

export { POST, GET };

提前感谢.

Stripe CLI post request webhooks are received - OK. [![enter image description here][1]][1]][1]][1]

已收到checkout. sessions.Completed- OK checkout. service.Completed会触发suspect和进一步的操作,尽管有正确的Post响应,但这些操作并未发生.

推荐答案

Noob代表我错误:

听错了路:

Stripe listen --forward-to本地主机:3000/api/checkout(NO)

stripe listen --forward-to本地主机:3000/api/webhook(是)

Reactjs相关问答推荐

优化、自定义登录页面、使用用户ID而不是邮箱

react 派生状态未正确更新

使用useReducer时,props 未从父级传递给子或孙级

TanStack/Reaction-Query在我的Vercel应用程序中不起作用

当列表中的项的顺序改变并且使用唯一键作为它们的索引时,Reaction如何看待呈现方面?

可以使用mode.css/mode.scss引用常规的类名吗?

如何使用Reaction Testing Library+Vitest正确更新单元测试中的输入?

如何从Reaction-Arborist树获取排序数据

REACTION-Easy-SORT返回.map错误

React-chartjs-2:红色和绿色之间的差距

字段是必需的,当它没有定义时

React - 拖放 UML

在 React 中使用复选框管理状态

未捕获的类型错误:useSelector 不是函数

页面加载时不显示数组中的数据

Cypress - 替换 React 项目中的变量

在嵌套的 Stack Navigator 中的 BottomTabBar 中导航会导致比以前更多的渲染

React Router 6.4CreateBrowserRouter子元素不显示

将 set statehook 函数传递给子路由组件.解构错误

链接的音译 Gatsby JS