fix: use @polar-sh/sdk validateEvent for webhook verification
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import "server-only";
|
||||
|
||||
import { Webhook } from "svix";
|
||||
import { validateEvent, WebhookVerificationError } from "@polar-sh/sdk/webhooks";
|
||||
|
||||
const POLAR_API_BASE = "https://api.polar.sh";
|
||||
const ACCESS_TOKEN = process.env.POLAR_ACCESS_TOKEN ?? "";
|
||||
@@ -52,19 +52,11 @@ export async function createPolarCheckout(params: {
|
||||
return res.json() as Promise<PolarCheckout>;
|
||||
}
|
||||
|
||||
// Svix kullanarak Polar webhook imzasını doğrula
|
||||
export function verifyPolarWebhook(
|
||||
// Polar resmi SDK ile webhook doğrulama ve parse
|
||||
// Hata fırlatırsa imza geçersiz demektir
|
||||
export function verifyAndParsePolarWebhook(
|
||||
headers: Record<string, string>,
|
||||
rawBody: string,
|
||||
): boolean {
|
||||
if (!WEBHOOK_SECRET) return false;
|
||||
try {
|
||||
// Svix whsec_ prefix bekler; polar_whs_ → whsec_ dönüşümü
|
||||
const secret = WEBHOOK_SECRET.replace(/^polar_whs_/, "whsec_");
|
||||
const wh = new Webhook(secret);
|
||||
wh.verify(rawBody, headers);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
): ReturnType<typeof validateEvent> {
|
||||
return validateEvent(rawBody, headers, WEBHOOK_SECRET);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user