Stripe POST req OK in CLI BUT how to handle webhook requests from Stripe – NextJS 14

I am receiving correctly Stripe webhooks POST req but having trouble handling the request.
Is there something I am missing? Console.logs don’t print to terminal.

Here is my code so far with the outline of what i want to handle when received checkout.session.completed from stripe.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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',
});
type ResponseData = {
message: string;
};
//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);
// update order in sb with paymentStatus + customerId
const { data: orderData, error: errorOrder } = await supabase
.from('Order')
.update({
customerId: customer.customerId,
paymentStatus: session.payment_status,
})
.eq('orderId', session.id)
.select();
if (errorOrder) throw new Error(errorOrder.message);
// create coupon and promotion code
if (session.amount_total !== 0) {
const coupon = await stripe.coupons.create({
name: 'business-100',
percent_off: 100,
duration: 'repeating',
duration_in_months: 1,
max_redemptions: 4,
});
if (!coupon) throw new Error('Coupon not created');
const promCode = await stripe.promotionCodes.create({
coupon: coupon.id,
max_redemptions: 4,
});
if (!promCode) throw new Error('Promotion code not created');
const { data, error: errorResend } = await resend.emails.send({
from: '[email protected]',
to: [customerEmail],
subject: 'Promotional code',
react: EmailTemplate({
firstName: `${customerName}`,
orderId: `${session.id}`,
promCode: `${promCode.code}`,
}),
text: `Hi there, here is your promotional code: ${promCode.code}`,
});
if (errorResend) throw new Error(errorResend.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 };
</code>
<code>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', }); type ResponseData = { message: string; }; //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); // update order in sb with paymentStatus + customerId const { data: orderData, error: errorOrder } = await supabase .from('Order') .update({ customerId: customer.customerId, paymentStatus: session.payment_status, }) .eq('orderId', session.id) .select(); if (errorOrder) throw new Error(errorOrder.message); // create coupon and promotion code if (session.amount_total !== 0) { const coupon = await stripe.coupons.create({ name: 'business-100', percent_off: 100, duration: 'repeating', duration_in_months: 1, max_redemptions: 4, }); if (!coupon) throw new Error('Coupon not created'); const promCode = await stripe.promotionCodes.create({ coupon: coupon.id, max_redemptions: 4, }); if (!promCode) throw new Error('Promotion code not created'); const { data, error: errorResend } = await resend.emails.send({ from: '[email protected]', to: [customerEmail], subject: 'Promotional code', react: EmailTemplate({ firstName: `${customerName}`, orderId: `${session.id}`, promCode: `${promCode.code}`, }), text: `Hi there, here is your promotional code: ${promCode.code}`, }); if (errorResend) throw new Error(errorResend.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 }; </code>
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',
});

type ResponseData = {
  message: string;
};

//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);

        // update order in sb with paymentStatus + customerId
        const { data: orderData, error: errorOrder } = await supabase
          .from('Order')
          .update({
            customerId: customer.customerId,
            paymentStatus: session.payment_status,
          })
          .eq('orderId', session.id)
          .select();

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

        // create coupon and promotion code
        if (session.amount_total !== 0) {
          const coupon = await stripe.coupons.create({
            name: 'business-100',
            percent_off: 100,
            duration: 'repeating',
            duration_in_months: 1,
            max_redemptions: 4,
          });

          if (!coupon) throw new Error('Coupon not created');

          const promCode = await stripe.promotionCodes.create({
            coupon: coupon.id,
            max_redemptions: 4,
          });

          if (!promCode) throw new Error('Promotion code not created');

          const { data, error: errorResend } = await resend.emails.send({
            from: '[email protected]',
            to: [customerEmail],
            subject: 'Promotional code',
            react: EmailTemplate({
              firstName: `${customerName}`,
              orderId: `${session.id}`,
              promCode: `${promCode.code}`,
            }),
            text: `Hi there, here is your promotional code: ${promCode.code}`,
          });

          if (errorResend) throw new Error(errorResend.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 };

Thank you in advance.

Stripe CLI post request webhooks are received – OK.
checkout.session.completed is received – OK
checkout.session.completed triggers supabase and further actions which are not happening despite correct POST reponses.

New contributor

Ehtz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật