John_Shelby

John_Shelby

Saving incoming data from Stripe

Hello everyone,
I am trying to create a payment gateway using Stripe. My solution is based on this tutorial.

I think I have everything set up well, but I’m not sure if Stripe is returning the required data that I would like to store in a database and work with afterwards.

In the tutorial, after a successful payment is made in the terminal after the stripe listen command, the following is the output:

However, my output looks like this:

If the return values from webhook are correct anyway, which I’m not sure. How should I continue to work with them within the handle event to save them?

Thanks to IO.inspect I was expecting some data in the terminal, where for example %Stripe.Event{type: "invoice.paid"}

defmodule MyAppWeb.StripeHandler do
  import MyAppWeb.Gettext

  @behaviour Stripe.WebhookHandler

  @impl true
  def handle_event(%Stripe.Event{type: "invoice.paid"} = event) do
    IO.inspect(gettext("Payment Success"))
    IO.inspect(event)
    :ok
  end
end

I have added the handler in the endpoint

  plug Stripe.WebhookPlug,
    at: "/webhook/stripe",
    handler: MyAppWeb.StripeHandler,
    secret: {Application, :get_env, [:stripity_stripe, :stripe_webhook_secret]}

It’s about storing data like customer_id, emai, etc that I can see in the payment in my Stripe account.

I am still a newbie and this is my first attempt at a payment gateway. Thanks a lot for any advice.

Have a nice day. :slight_smile:

Marked As Solved

John_Shelby

John_Shelby

UPDATE:

The answer is that the data are sent back to the application.

Alternatively, ngrok can also be used.

Fot test you will need 3 terminals open:

  1. In the first terminal, ensure the Stripe CLI listener is running (stripe listen --forward-to localhost:4000/webhook/stripe)
  2. In the second terminal, ensure your app is running (mix phx.server). Remember you may need to run source .env in the terminal first
  3. In the third terminal, execute stripe trigger checkout.session.completed.

I am replacing step three by going through and filling in the data directly in the application.

My handle event for saving to the database now looks like this:

  @impl true
  def handle_event(%Stripe.Event{type: "invoice.paid"} = event) do
    IO.inspect(gettext("Payment Success"))
    IO.inspect(event)

    %{customer: customer_id, customer_email: email, amount_paid: amount} = event.data.object

    Purchases.create_customer(%{
      email: email,
      stripe_customer_id: customer_id,
      amount: amount
    })

    {:ok, "success"}
  end

Practically the whole issue with a link to a sample application is available in the tutorial at this link.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement