我如何知道用户使用Stripe的Checkout.js选择的计划?

Pau*_*ars 4 django checkout stripe-payments

我用以下订阅按钮列出了我们所有的计划(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 %}
Run Code Online (Sandbox Code Playgroud)

然后我创建客户/订阅以响应此表单被POST:

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()
Run Code Online (Sandbox Code Playgroud)

但在那时我没有计划ID传回Stripe.:/.

我这样做各种错吗?

Dan*_*man 5

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

<form method="POST" action=".">
    {% csrf_token %}
    <input type="hidden" name="plan" value="{{ plan.id }}">
    <script....>
</form>
Run Code Online (Sandbox Code Playgroud)

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

  • 来自[文档](https://stripe.com/docs/checkout):"值得注意的是Checkout实际上不会产生费用 - 它只会创建令牌.您可以使用这些令牌在服务器上创建实际费用". (2认同)