<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="333318" data-post-id="333318">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="travisf" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/travisf/120/18564_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  travisf
                      <span class="op-star" title="Thread Starter">
                        <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                      </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I finally got to the bottom of this, I had, occasionally, used an Ngrok tunnel as an endpoint for the webhook but mostly I was using <code>localhost</code> I didn’t realize that when you ran the Stripe CLI locally it gave you a specific <code>signing_secret</code> when the session started:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&gt; Ready! You are using Stripe API Version [2020-08-27]. Your webhook signing secret is whsec_..... (^C to quit)
</code></pre>
<p>I was using the signing secret from another endpoint I had setup for Ngrok.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333318" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-333318" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333318"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="352449" data-post-id="352449">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="justincjohnson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/justincjohnson/120/23636_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  justincjohnson
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For anyone else who runs into this, the problem for me had to do with <a href="https://github.com/mtrudel/bandit/blob/b8b56515d89a8b552c90b5f0d1d181d2bb30ff50/lib/bandit.ex#L128" rel="noopener nofollow ugc">this setting</a> in Bandit.</p>
<p>I was able to get around the problem by changing my config to the following.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">config :my_app_web, MyAppWeb.Endpoint,
  # ...
  http_1_options: [max_request_line_length: 50_000]
</code></pre>
<p>I haven’t thought deeply about the size to use, but this got around the error I was experiencing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="352449" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-352449" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352449"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="352465" data-post-id="352465">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="justincjohnson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/justincjohnson/120/23636_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  justincjohnson
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Never mind.  I thought it fixed it but it was intermittent.  Back to the drawing board.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="352465" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-352465" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352465"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="354010" data-post-id="354010">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="wceolin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wceolin/120/28444_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  wceolin
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Was your post deleted? It’s returning 404.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="354010" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-354010" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="354010"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="354057" data-post-id="354057">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sorentwo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sorentwo/120/37360_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sorentwo
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Oban Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>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.</p>
<p>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 <code>body_reader</code> module will do it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># body_reader.ex
defmodule MyApp.BodyReader do
  def read_body(conn, _opts) do
    # You may want to only do this for certain paths
    with {:ok, raw_body, conn} &lt;- Plug.Conn.read_body(conn) do
      {:ok, raw_body, Plug.Conn.put_private(conn, :raw_body, raw_body)}
    end
  end
end
</code></pre>
<p><a href="https://hexdocs.pm/plug/1.16.1/Plug.Parsers.html#module-custom-body-reader" rel="noopener nofollow ugc">Configure</a> <code>Plug.Parsers</code> to use the new body_reader module:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library(),
    body_reader: {MyApp.BodyReader, :read_body, []}
</code></pre>
<p>Then define a private function to verify the signature in your controller:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyAppWeb.StripeController do
  use MyAppWeb, :controller

  plug :verify_signature

  # actions go here

  defp verify_signature(conn, _opts) do
    case get_req_header(conn, "stripe-signature") do
      [header] -&gt;
        ["t=" &lt;&gt; time, "v1=" &lt;&gt; sigv | _] = String.split(header, ",")
        signing_secret = Application.fetch_env!(:my_app, :stripe_signing_secret)

        hmac =
          :hmac
          |&gt; :crypto.mac(:sha256, signing_secret, [time, ".", conn.private.raw_body])
          |&gt; Base.encode16(case: :lower)

        if Plug.Crypto.secure_compare(hmac, sigv) do
          conn
        else
          conn
          |&gt; send_resp(400, "Invalid Signature")
          |&gt; halt()
        end

      _ -&gt;
        conn
        |&gt; send_resp(400, "Missing Signature")
        |&gt; halt()
    end
  end
end
</code></pre>
<p>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 (<code>whsec_test</code>), a fake timestamp, and <code>:raw_body</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @signature "t=123456789,v1=34e0846d2ae20d2fcde8c391d069223f72d0518eaf511de69f785470938e1505"

  setup %{conn: conn} do
    conn =
      conn
      |&gt; put_req_header("stripe-signature", @signature)
      |&gt; put_private(:raw_body, ~s({"fake":"body"}))

    {:ok, conn: conn}
  end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="354057" data-batch-url="/posts/batch_likers">
                        8
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-354057" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="354057"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="356116" data-post-id="356116">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="SteveL" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/SteveL/120/35804_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  SteveL
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just a quick note about testing the <code>body_reader</code> that 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:</p>
<ul>
<li>You must set a <code>content-type</code> header, and</li>
<li>You have to send your post request as a string and not a map. Sending as a map will bypass the body reader.</li>
</ul>
<p>My test ended up looking something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    test "invalid hmac signature", %{conn: conn} do
      conn = 
        conn
        |&gt; Plug.Conn.put_req_header("content-type", "application/json")
        |&gt; post(~p"/api/webhooks", "{}")

      assert response(conn, 400) == "Invalid signature"
    end
</code></pre>
<p>Hope that helps someone stop banging their head on the desk as much as I did!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="356116" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/stripe-payments-webhook-handler/63761/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-356116" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="356116"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>