Stripe-Elixir How to get "payment source"

466 views Asked by At

I am using this package to incorporate Stripe into a phoenix app:

https://github.com/sikanhe/stripe-elixir


I am trying to create a customer that subscribes to a plan.

defmodule StripeTestWeb.PaymentController do
  use StripeTestWeb, :controller
  use Stripe.API

  def index(conn, %{"stripeEmail" => email} = _params) do
    {:ok, customer_data} = Stripe.Customer.create(%{email: email})

    Stripe.Subscription.create(%{
      customer: customer_data["id"],
      items: [%{plan: "plan_D9JehUOiyPGtgp"}]
    })

    render(conn, "index.html")
  end
end

The customer is created but the request returns an error when trying to create the subscription. The error is:

{
  "error": {
    "code": "resource_missing",
    "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
    "message": "This customer has no attached payment source",
    "type": "invalid_request_error"
  }
}

I don't know what the API expects me to send as "payment source" nor am I clear if this is supposed to be sent when creating the customer or when creating the subscription.

I am using the JavaScript embedded code to create the payment pop up:

  <form action="payment" method="POST">
      <input type="hidden" name="_csrf_token" value="<%= Plug.CSRFProtection.get_csrf_token()%>">

      <script
      src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key="pk_test_2vzqy4IW9zHAYyKSjDNZkd2l"
      data-amount="14900"
      data-name="Demo Site"
      data-description="Subscription $149"
      data-locale="auto">
      </script>

  </form>
1

There are 1 answers

1
AbM On BEST ANSWER

I am not familiar with Stripe Elixir but you can pass the source when creating a customer on Stripe, so try passing that with your create call.

defmodule StripeTestWeb.PaymentController do
  use StripeTestWeb, :controller
  use Stripe.API

  def index(conn, %{"stripeEmail" => email, "stripeToken" => stripe_token} = _params) do
    {:ok, customer_data} = Stripe.Customer.create(%{email: email, source: stripe_token})

    Stripe.Subscription.create(%{
      customer: customer_data["id"],
      items: [%{plan: "plan_D9JehUOiyPGtgp"}]
    })

    render(conn, "index.html")
  end
end