How do I know which plan a user has selected using Stripe Checkout.js?

I list all our plans using the subscribe button as follows (django template syntax):

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

Then I create a client / subscription in response to this form: POSTED:

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()

But at this point I do not have a plan identifier to return to Stripe ..: /

Am I doing all this wrong?

+4
source share
1 answer

The entire Stripe script check does this by inserting a token into a hidden field in your form and then submit the entire form to your server. If you need other information, such as a plan, you should include this in your form:

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

Now you can access the plan through request.POST['plan'].

+4

Source: https://habr.com/ru/post/1608475/


All Articles