diff --git a/package.json b/package.json index 2ff1644..ee3dc0f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "@stripe/react-stripe-js": "1.1.2", "@stripe/stripe-js": "1.5.0", "dotenv": "latest", - "micro": "^9.3.4", "micro-cors": "^0.1.1", "next": "latest", "react": "^16.12.0", @@ -22,7 +21,6 @@ "use-shopping-cart": "2.1.0" }, "devDependencies": { - "@types/micro": "^7.3.3", "@types/micro-cors": "^0.1.0", "@types/node": "^13.1.2", "@types/react": "^16.9.17", diff --git a/pages/api/webhooks/index.ts b/pages/api/webhooks/index.ts index 4a9b3cc..c10f579 100644 --- a/pages/api/webhooks/index.ts +++ b/pages/api/webhooks/index.ts @@ -1,4 +1,3 @@ -import { buffer } from 'micro'; import Cors from 'micro-cors'; import { NextApiRequest, NextApiResponse } from 'next'; @@ -21,6 +20,22 @@ const cors = Cors({ allowMethods: ['POST', 'HEAD'], }); +const buffer: (req: NextApiRequest) => Promise = (req) => { + return new Promise((resolve, reject) => { + const body: Array = []; + req + .on('data', (chunk) => { + body.push(chunk); + }) + .on('end', () => { + resolve(Buffer.concat(body)); + }) + .on('error', (err) => { + reject(err); + }); + }); +}; + const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { if (req.method === 'POST') { const buf = await buffer(req); @@ -29,11 +44,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { let event: Stripe.Event; try { - event = stripe.webhooks.constructEvent( - buf.toString(), - sig, - webhookSecret - ); + event = stripe.webhooks.constructEvent(buf, sig, webhookSecret); } catch (err) { // On error, log and return the error message. console.log(`❌ Error message: ${err.message}`);