-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
536 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v224 | ||
v226 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env -S npm run-script run | ||
|
||
/* | ||
ATTENTION: this file exists to support github/stripe/stripe-node example test infrastructure | ||
You don't need to copy this file to your NextJS project. | ||
*/ | ||
|
||
import child_process from 'child_process'; | ||
|
||
const process = child_process.exec('npm run dev --verbose'); | ||
process.stdout.on('data', (line) => { | ||
const match = /url: (.*)/gm.exec(line); | ||
if (match) { | ||
console.log(`Webhook endpoint available at ${match[1]}/api/webhooks`); | ||
} | ||
console.log(line); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "webhook-signing-example-nextjs", | ||
"version": "1.0.0", | ||
"description": "NextJS webhook parsing sample", | ||
"repository": {}, | ||
"main": "./main.ts", | ||
"scripts": { | ||
"run": "ts-node-transpile-only ./main.ts", | ||
"prepare": "../prepare.sh", | ||
"dev": "next dev --port 0" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"dotenv": "^8.2.0", | ||
"next": "^13.1.6", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"stripe": "^11.9.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^13.1.4", | ||
"@types/react": "18.0.27", | ||
"eslint": "^8.33.0", | ||
"typescript": "^4.8.3", | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Stripe from 'stripe'; | ||
import {NextApiRequest, NextApiResponse} from 'next'; | ||
|
||
const handler = async ( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
): Promise<void> => { | ||
const stripe = new Stripe(process.env.STRIPE_WEBHOOK_SECRET, { | ||
apiVersion: '2022-11-15', | ||
}); | ||
|
||
const webhookSecret: string = process.env.STRIPE_WEBHOOK_SECRET; | ||
|
||
if (req.method === 'POST') { | ||
const sig = req.headers['stripe-signature']; | ||
|
||
let event: Stripe.Event; | ||
|
||
try { | ||
const body = await buffer(req); | ||
event = stripe.webhooks.constructEvent(body, sig, webhookSecret); | ||
} catch (err) { | ||
// On error, log and return the error message | ||
console.log(`❌ Error message: ${err.message}`); | ||
res.status(400).send(`Webhook Error: ${err.message}`); | ||
return; | ||
} | ||
|
||
// Successfully constructed event | ||
console.log('✅ Success:', event.id); | ||
|
||
// Cast event data to Stripe object | ||
if (event.type === 'payment_intent.succeeded') { | ||
const stripeObject: Stripe.PaymentIntent = event.data | ||
.object as Stripe.PaymentIntent; | ||
console.log(`💰 PaymentIntent status: ${stripeObject.status}`); | ||
} else if (event.type === 'charge.succeeded') { | ||
const charge = event.data.object as Stripe.Charge; | ||
console.log(`💵 Charge id: ${charge.id}`); | ||
} else { | ||
console.warn(`🤷♀️ Unhandled event type: ${event.type}`); | ||
} | ||
|
||
// Return a response to acknowledge receipt of the event | ||
res.json({received: true}); | ||
} else { | ||
res.setHeader('Allow', 'POST'); | ||
res.status(405).end('Method Not Allowed'); | ||
} | ||
}; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
const buffer = (req: NextApiRequest) => { | ||
return new Promise<Buffer>((resolve, reject) => { | ||
const chunks: Buffer[] = []; | ||
|
||
req.on('data', (chunk: Buffer) => { | ||
chunks.push(chunk); | ||
}); | ||
|
||
req.on('end', () => { | ||
resolve(Buffer.concat(chunks)); | ||
}); | ||
|
||
req.on('error', reject); | ||
}); | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.