我怎么知道用户选择了 Stripe 的 Checkout.js 什么计划?

How do I know what plan the user chose with Stripe's Checkout.js?

我用订阅按钮列出了我们所有的计划,如下所示(django 模板语法):

  {% for plan in plans %}
  <tr>
    <td>{{ plan.name }}</td>
    <td>£{{ plan.price_human }}</td>
    <td>
      <form method="POST" action=".">
        {% csrf_token %}
        <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="{{ public_key }}"
        data-image="/static/images/logo-n.png"
        data-name="Product Name"
        data-description="{{ plan.name }}"
        data-currency="{{ plan.currency }}"
        data-amount="{{ plan.price }}"
        data-locale="{{ request.LANGUAGE_CODE }}"
        data-email="{{ user.email }}"
        data-label="{% trans 'Subscribe' %}"
        data-panel-label="{% trans 'Subscribe' %}"
        data-allow-remember-me="false"
        >
        </script>
      </form>
    </td>
  </tr>
  {% endfor %}

然后我正在创建 customer/subscription 以响应发布的此表单:

class SubscribePageView(generic.TemplateView):
  def post(self, request, *args, **kwargs):
    stripe.api_key = settings.STRIPE_SECRET_KEY
    user = self.request.user
    token = request.POST.get('stripeToken')

    customer = stripe.Customer.create(
      source=token,
      plan=[[WHERE DOES THIS COME FROM??]],
      email=user.email,
    )
    user.customer_id = customer.id
    user.save()

但那时我没有传回给 Stripe 的计划 ID。 :/.

我这样做有错吗?

所有 Stripe 结帐脚本所做的就是将令牌插入表单中的隐藏字段,然后将整个表单提交到您的服务器。如果您需要任何其他信息,例如计划,您也应该将其包含在您的表格中:

<form method="POST" action=".">
    {% csrf_token %}
    <input type="hidden" name="plan" value="{{ plan.id }}">
    <script....>
</form>

现在您可以通过 request.POST['plan'] 访问该计划。