Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs[snippets]: add stripe webhook integration #347

Merged
merged 7 commits into from May 18, 2024

Conversation

hideokamoto-stripe
Copy link
Contributor

I added a page introducing how to create an API using Hono to handle Stripe Webhook events.

Context

https://github.com/orgs/honojs/discussions/2518#discussioncomment-9170675

To add protection processing for the API endpoint, Stripe Webhooks require the raw request body. I created an article with sample code explaining how to obtain this for both Node.js and Cloudflare.

By adding this article, users using Stripe with Hono will find it easier to understand the integration method, and it will be easier to provide support in case of issues or discussions.

@yusukebe
Copy link
Member

yusukebe commented May 9, 2024

Hey @hideokamoto-stripe

Awesome! I've left one comment. Please check it.

@hideokamoto-stripe
Copy link
Contributor Author

Thank you @yusukebe -san!
I updated the file to add the section.

snippets/stripe-webhook.md Outdated Show resolved Hide resolved

app.post("/webhook", async (context) => {
const stripe = new Stripe(context.env.STRIPE_API_KEY);
- const event = await context.req.json();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this "Colored Diffs in Code Blocks"?

https://vitepress.dev/guide/markdown#colored-diffs-in-code-blocks


### Deploying to Cloudflare ( Workers / Pages )

When processing Stripe webhook events on Cloudflare Workers or Cloudflare Pages functions, the raw request body can be obtained from `context.req.text()`.
Copy link
Member

@yusukebe yusukebe May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both Cloudflare and Node.js can use the same code because the API for accessing the request body is the same according to Web Standard APIs. So, both can use context.req.text() or context.req.arrayBuffer().

const app = new Hono();

app.post("/webhook", async (context) => {
const stripe = new Stripe(context.env.STRIPE_API_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want it to support multi runtimes, it would be good to use env() in hono/adapter:
https://hono.dev/helpers/adapter

@hideokamoto-stripe
Copy link
Contributor Author

Hello @yusukebe -san,
I updated the code snippet according to your feedback. As a result, we can make it simpler than before. I also gained a deeper understanding of Hono through this process.

Please feel free to provide your comments about this update.
Thank you.


The API that processes webhook events is publicly accessible.

```javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ts is better than javascript or js.

@hideokamoto-stripe
Copy link
Contributor Author

@yusukebe
I've removed unnecessary code snippets 👍

const stripe = new Stripe(STRIPE_SECRET_API_KEY);
const event = await context.req.json(); // [!code --]
const signature = context.req.header('stripe-signature'); // [!code ++]
try { // [!code ++]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the blocks like the following!

// [!code ++]

@yusukebe
Copy link
Member

@hideokamoto-stripe

Thanks! I've left one more comment. Check it.

@hideokamoto-stripe
Copy link
Contributor Author

@yusukebe
Oh, I forgot to remove it.
I've removed it!

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@hideokamoto-stripe

Thanks! Let's ship it.

@yusukebe yusukebe merged commit 13d896a into honojs:main May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants