beam-community / stripity-stripe

An Elixir Library for Stripe
Other
965 stars 344 forks source link

Add conn as expected second argument in StripeHandler handle_event callbacks #834

Closed jmpressman closed 4 months ago

jmpressman commented 4 months ago

Problem Statement

Currently the handle_event callbacks that get defined in a StripeHandler only accept a Stripe.Event. This unfortunately means that we can't leverage the conn during the event handling.

More specifically, I am hoping to trigger email invitations after a checkout has been completed. These invitations will contain a link that directs to an onboarding flow. These links are generated using the Phoenix.VerifiedRoutes.url function which requires a conn.

defmodule MyApp.StripeHandler do
  @behaviour Stripe.WebhookHandler

  @impl true
  def handle_event(%Stripe.Event{type: "checkout.session.completed"} = event) do
    # do stuff
  end
end

As it stands I will have to extend the Stripe.WebhookPlug locally in order for this flow to work, which is fine in the short term, but not ideal in the long term.

Solution Brainstorm

Adding the conn as an expected argument for the handle_event callbacks allow us to leverage the conn during event handling.

defmodule MyApp.StripeHandler do
  @behaviour Stripe.WebhookHandler

  @impl true
  def handle_event(%Stripe.Event{type: "invoice.paid"} = event, conn) do
    # do stuff with conn
    invite_url = url(conn, ~p"/some/onboaring/path")
  end
end

The downside here is that the conn could potentially be misused (i.e. to prematurely send a response) within the event handler. I'm not sure of the best way to guard against that.

jmpressman commented 4 months ago

I just realized that this isn't actually necessary, at least for the need I mentioned in the Problem statement. Closing.