travisf
I’m working through this tutorial trying to get payments working with Stripe.
I’ve implemented a Stripe webhook handler:
defmodule Postbox.StripeHandler do
@behaviour Plug
alias Plug.Conn
def init(config), do: config
def call(%{request_path: "/webhook/payments"} = conn, _params) do
signing_secret = Application.get_env(:stripity_stripe, :webhook_key)
[stripe_signature] = Plug.Conn.get_req_header(conn, "stripe-signature")
with {:ok, body, _} = Plug.Conn.read_body(conn),
{:ok, stripe_event} =
Stripe.Webhook.construct_event(body, stripe_signature, signing_secret) do
Plug.Conn.assign(conn, :stripe_event, stripe_event)
else
err ->
conn
|> Conn.send_resp(:bad_request, err)
|> Conn.halt()
end
end
end
I’ve copied my webhook secret (:webhook_key directly from the signing secret) but I’m still, continuously getting this error from Stripe: ** (MatchError) no match of right hand side value: {:error, "No signatures found matching the expected signature for payload"}.
This is a test account and I’m using Ngrok for the Webhook address.
Initial question aside I’m wondering if I’m just using Stripe.Checkout.Session do I need to even bother with Webhooks? Willl Stripe returning a successful URL be sufficient?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










First Post!- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
I don’t believe it’s directly related to your issue, but note that there’s a specific callout in the docs for
Plug.Conn.read_bodysaying NOT to discard theconn:Most Liked
sorentwo
I noticed that the post appears to be deleted earlier this week. Here’s a brief, working example of how to verify stripe webhooks in phoenix.
First, parsed JSON will strip some whitespace and alphabetize keys. To calculate the correct HMAC you need to stash the original unparsed request body. A small
body_readermodule will do it:Configure
Plug.Parsersto use the new body_reader module:Then define a private function to verify the signature in your controller:
That will do it! If you test your webhook controller, which you certainly should, you’ll need to inject a signature as well. The signature below is fake and computed using a test signing key (
whsec_test), a fake timestamp, and:raw_body:wojtekmach
This blog post might be helpful: https://dashbit.co/blog/how-we-verify-webhooks.
Lucassifoni
You need to access the body before it has been modified by a subsequent Plug.
Here is an example in Plug.Parsers docs that exposes this very use case
Edit : note that you can also do that with a custom Parser that runs before other Plug.Parsers. You can pattern match on the request path if you wish to avoid copying the raw body for other kind of requests.
Check for exhaustiveness with the docs, because I cannot verify it today.
After that, the
%{raw_body: r}map gets merged intoconn.params. You can access it by pattern matching on conn.params in your controller.def handle_webhook(%{params: %{raw_body: raw_body}} = conn, params) doThere are many ways to get to the desired result, but the goal is the same : preserve the raw request body.
Last Post!
SteveL
Just a quick note about testing the
body_readerthat took me a while to figure out. I had a hard time just getting the custom body reader to run in controller tests until I realized the following:content-typeheader, andMy test ended up looking something like:
Hope that helps someone stop banging their head on the desk as much as I did!