#Jannik
1 messages ยท Page 1 of 1 (latest)
Hey there, I'm not sure what to make of this. Can you provide more context to explain the problem you're having?
hi
import { NextApiRequest, NextApiResponse } from "next";
import { NextRequest } from "next/server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
// https://github.com/stripe/stripe-node#configuration
apiVersion: "2022-11-15",
});
const webhookSecret: string = process.env.STRIPE_WEBHOOK_SECRET!;
console.log("webhookSecret", webhookSecret);
export async function POST(request: NextRequest) {
try {
console.log("HIT");
const buf = await request.text();
//@ts-ignore
const sig = request.headers["stripe-signature"]!;
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
buf.toString(),
sig,
webhookSecret
);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Unknown error";
// On error, log and return the error message.
if (err! instanceof Error) console.log(err);
console.log(`โ Error message: ${errorMessage}`);
return new Response("Webhook Error", { status: 400 });
}
// Successfully constructed event.
console.log("โ
Success:", event.id);
// Cast event data to Stripe object.
if (event.type === "payment_intent.succeeded") {
const paymentIntent = event.data.object as Stripe.PaymentIntent;
console.log(`๐ฐ PaymentIntent status: ${paymentIntent.status}`);
} else if (event.type === "payment_intent.payment_failed") {
const paymentIntent = event.data.object as Stripe.PaymentIntent;
console.log(
`โ Payment failed: ${paymentIntent.last_payment_error?.message}`
);
} else if (event.type === "charge.succeeded") {
console.log(`๐ต Charge id: ${charge.id}`);
} else {
console.warn(`๐คทโโ๏ธ Unhandled event type: ${event.type}`);
}
// Return a response to acknowledge receipt of the event.
return new Response("Success", { status: 200 });
} catch (error: any) {
console.log(error);
return new Response("Webhook Error", { status: 400 });
}
}
export const config = {
api: {
bodyParser: false,
},
};
error ```
Ok, and have you logged the values passed in to constructEvent? TO see what sig is?
BUF {
"id": "evt_3NBdtXBxbqzVV8bq19zyRjBy",
"object": "event",
"api_version": "2022-11-15",
"created": 1685020075,
"data": {
"object": {
"id": "pi_3NBdtXBxbqzVV8bq1jXwnd5P",
"object": "payment_intent",
"amount": 50000,
"amount_capturable": 0,
"amount_details": {
"tip": {
}
},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret": "pi_3NBdtXBxbqzVV8bq1jXwnd5P_secret_OFhBSh704tBd1qewyEUI6pHqE",
"confirmation_method": "automatic",
"created": 1685020075,
"currency": "eur",
"customer": null,
"description": null,
"invoice": null,
"last_payment_error": null,
"latest_charge": null,
"livemode": false,
"metadata": {
},
"next_action": null,
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
}
},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "requires_payment_method",
"transfer_data": null,
"transfer_group": null
}
},
"livemode": false,
"pending_webhooks": 2,
"request": {
"id": "req_LzjjXfUPBNo8j3",
"idempotency_key": "33166c20-2ce1-41de-8451-3c727391b322"
},
"type": "payment_intent.created"
}
undefined
sig is undefined
but why D
const sig = request.headers.get("stripe-signature") as string | string[]; this fixed it
thanks ๐
do you think you can provide an offical tutorial for nextjs app directory?
This is the problem, you need that signature
I'm not sure why the header isn't defined, but that seems to be related to how Next handles those requests
oki