Stripe-Elixir 如何获得 "payment source"

Stripe-Elixir How to get "payment source"

我正在使用这个包将 Stripe 合并到一个 phoenix 应用程序中:

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


我正在尝试创建订阅计划的客户。

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

客户已创建,但请求 returns 在尝试创建订阅时出错。错误是:

{
  "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"
  }
}

我不知道 API 希望我作为 "payment source" 发送什么,我也不清楚这是否应该在创建客户或创建订阅时发送。

我正在使用 JavaScript 嵌入式代码来创建付款弹出窗口:

  <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 9"
      data-locale="auto">
      </script>

  </form>

我不熟悉 Stripe Elixir,但您可以在 Stripe 上创建客户时传递 source,因此请尝试在您的创建调用中传递它。

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