fix: pass raw request body to stripe constructEvent
This commit is contained in:
@@ -1,11 +1,17 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { Stripe } from "stripe";
|
import type { Readable } from "node:stream";
|
||||||
|
|
||||||
import { createNextApiContext } from "@kan/api/trpc";
|
import { createNextApiContext } from "@kan/api/trpc";
|
||||||
import * as workspaceRepo from "@kan/db/repository/workspace.repo";
|
import * as workspaceRepo from "@kan/db/repository/workspace.repo";
|
||||||
import { createStripeClient } from "@kan/stripe";
|
import { createStripeClient } from "@kan/stripe";
|
||||||
|
|
||||||
export const webCrypto = Stripe.createSubtleCryptoProvider();
|
async function buffer(readable: Readable) {
|
||||||
|
const chunks = [];
|
||||||
|
for await (const chunk of readable) {
|
||||||
|
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
||||||
|
}
|
||||||
|
return Buffer.concat(chunks);
|
||||||
|
}
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
@@ -24,14 +30,13 @@ export default async function handler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body = req.body;
|
const buf = await buffer(req);
|
||||||
|
const rawBody = buf.toString("utf8");
|
||||||
|
|
||||||
const event = await stripe.webhooks.constructEventAsync(
|
const event = stripe.webhooks.constructEvent(
|
||||||
body,
|
rawBody,
|
||||||
sig,
|
sig,
|
||||||
process.env.STRIPE_WEBHOOK_SECRET!,
|
process.env.STRIPE_WEBHOOK_SECRET!,
|
||||||
undefined,
|
|
||||||
webCrypto,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const { db } = await createNextApiContext(req);
|
const { db } = await createNextApiContext(req);
|
||||||
@@ -60,3 +65,9 @@ export default async function handler(
|
|||||||
return res.status(400).json({ message: "Webhook handler failed" });
|
return res.status(400).json({ message: "Webhook handler failed" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
api: {
|
||||||
|
bodyParser: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user