Stripe Not Returning Token (test mode)

457 views
Skip to first unread message

Chris Kavanagh

unread,
Jun 16, 2016, 7:20:22 PM6/16/16
to Django users
Stripe Not Returning Token (test mode)

I'm trying out Stripe in test mode on a site I'm building, and for some reason Stripe won't return a token as it's supposed to.
I can tell it's not returning a token, because I get a MultiValueDictKeyError when submitting the form. Plus, it won't print
the token out to the console, so it's obviously not returning one.

If I change the line ( token = request.POST['stripeToken'])  to  (token = request.POST.get('stripeToken')), I get the error  "
Request req_8eOcfP3bw2bzDd: Must provide source or customer." This way will actually create
a customer I can see in the Stripe Dashboard but won't create a charge, and of course
I get the error "must provide a source or customer."

Here's the link to Stripe form documentation along with javascript. . .
https://stripe.com/docs/custom-form

I've double checked the keys to make sure they are correct, and it's definitely in test mode. From the examples &
tutorials I've looked at, I don't see a reason it shouldn't be working. Any help is greatly appreciated, thanks Chris.

Anyway, here's the code:

#views.py

def checkout(request):
    publishable_key = settings.STRIPE_PUBLISHABLE_KEY
    stripe.api_key = settings.STRIPE_SECRET_KEY
    if request.method == "POST":
        #token = request.POST.get('stripeToken')
        token = request.POST['stripeToken']
        print token
        customer = stripe.Customer.create(description='test', source=token)
        print customer
        stripe.Charge.create(amount=500, currency='usd', source=token, description='test')
        #stripe.Charge.create(amount=500, currency='usd', customer=customer, description=customer_data['description'])
        return redirect('orders:thanks.html')
    context = {'publishable_key': publishable_key}
    return render(request, 'orders/checkout.html', context)





#template checkout,html

{% extends "base.html" %}

 {% block jQuery %}
 <script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">
    Stripe.setPublishableKey('{{ publishable_key }}');

    $(function() {
  var $form = $('#payment-form');
  $form.submit(function(event) {
    // Disable the submit button to prevent repeated clicks:
    $form.find('.submit').prop('disabled', true);

    // Request a token from Stripe:
    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from being submitted:
    return false;
  });
});

    function stripeResponseHandler(status, response) {
  // Grab the form:
  var $form = $('#payment-form');

  if (response.error) { // Problem!

    // Show the errors on the form:
    $form.find('.payment-errors').text(response.error.message);
    $form.find('.submit').prop('disabled', false); // Re-enable submission

  } else { // Token was created!

    // Get the token ID:
    var token = response.id;

    // Insert the token ID into the form so it gets submitted to the server:
    $form.append($('<input type="hidden" name="stripeToken">').val(token));

    // Submit the form:
    $form.get(0).submit();
  }
};
</script>

    {% endblock %}


 {% block content %}
 <h3 class="text-center">Credit Card Payment</h3>
 <div class="container">
<div class="row">
    <form method="post" action="." id="checkout-form">
        {% csrf_token %}
       
       
        <div class="form-group">
          <label class="control-label" for="card">Card</label>
          <div class="controls">
              <input type="text" id="card" class="form-control" data-stripe="number" />
          </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="">Expiration (MM/YYYY)</label>
            <div class="row">
                <div class="col-xs-2">
                    <input type="text" size="2" data-stripe="exp-month" class="form-control" />
                </div>
                <div class="col-xs-2">
                    <input type="text" size="4" data-stripe="exp-year" class="form-control" />
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="cvc">CVC</label>
            <div class="controls">
                <input type="text" id="cvc" size="4" class="form-control" data-stripe="cvc" />
            </div>
        </div>
        <div class="form-group">
            <div class="controls">
                <input type="submit" value="Checkout" class="btn btn-primary" />
            </div>
        </div>
    </form>
</div>
</div>
{% endblock %}



Request req_8eOcfP3bw2bzDd: Must provide source or customer.

MultiValueDictKeyError at /orders/checkout/

"'stripeToken'"

Kristofer Pettijohn

unread,
Jun 17, 2016, 8:50:10 PM6/17/16
to django...@googlegroups.com
This is how I did it.  This is using Stripe Checkout.

forms.py ... (you can omit the address pieces if you don't need them)

    class StripeForm(forms.Form):
        stripeToken = forms.CharField(max_length=80)
        stripeBillingName = forms.CharField(max_length=80, required=False)
        stripeBillingAddressLine1 = forms.CharField(max_length=80, required=False)
        stripeBillingAddressZip = forms.CharField(max_length=80, required=False)
        stripeBillingAddressState = forms.CharField(max_length=80, required=False)
        stripeBillingAddressCity = forms.CharField(max_length=80, required=False)
        stripeBillingAddressCountry = forms.CharField(max_length=80, required=False)


views.py ...

    stripe_form = StripeForm(request.POST or None)
    if request.method == 'POST' and stripe_form.is_valid():
        token = stripe_form.cleaned_data['stripeToken']
        customer = stripe.Customer.create(
            description=summary_dict['email_address'],
            source=token
        )



From: "Chris Kavanagh" <cka...@gmail.com>
To: "Django users" <django...@googlegroups.com>
Sent: Thursday, June 16, 2016 2:20:22 PM
Subject: Stripe Not Returning Token (test mode)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8596d735-c697-4734-a2a3-a6bbec0ee2e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Kavanagh

unread,
Jun 18, 2016, 1:24:00 AM6/18/16
to Digest Recipients
Thanks Kristofer, let me try that and see what happens. . .

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/kOYgvRGOSqw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Chris Kavanagh

unread,
Jun 18, 2016, 1:33:55 AM6/18/16
to Digest Recipients
Kristofen, could I see the template? The javascript/jQuery you're using in the template specifically?

 I think that's where the problem is.
I copied it directly from https://stripe.com/docs/custom-form, but it doesn't appear to be running the jQuery. I get an error,
js jquery referenceerror $ is not defined $(function() when I look at Firebug (in browser).

On Fri, Jun 17, 2016 at 4:49 PM, Kristofer Pettijohn <kris...@cybernetik.net> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/kOYgvRGOSqw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Chris Kavanagh

unread,
Jun 18, 2016, 1:38:39 AM6/18/16
to Digest Recipients
Oh, never mind, you're using stripe checkout. My bad. Let me try it your way.

Chris Kavanagh

unread,
Jun 18, 2016, 1:51:25 AM6/18/16
to Digest Recipients
One last question, if you don't mind. Did you have to use a {% csrf_token %} on your form? I read somewhere on stackoverflow that Stripe Checkout wouldn't accept the csrf token? Thanks again, Kristofen.

On Fri, Jun 17, 2016 at 4:49 PM, Kristofer Pettijohn <kris...@cybernetik.net> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/kOYgvRGOSqw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kristofer Pettijohn

unread,
Jun 18, 2016, 3:44:15 PM6/18/16
to django...@googlegroups.com
Yes, I am using CSRF on the form and do not have any issues with it.


      <form method="POST">

        {% csrf_token %}

        <script

          src="https://checkout.stripe.com/checkout.js" class="stripe-button"

          data-key="{{ stripe_publishable_key }}"

          data-amount="{{ stripe_amount }}"

          data-name="..."

          data-description="..."

          data-zip-code="true"

          data-billing-address="true"

          data-allow-remember-me="true"

          data-email="{{ summary_dict.email_address }}"

          data-locale="auto"

          data-label="Register with Card"

          data-zip-code="true">

        </script>

      </form>



From: "Chris Kavanagh" <cka...@gmail.com>
To: "Digest Recipients" <django...@googlegroups.com>
Sent: Friday, June 17, 2016 8:51:04 PM
Subject: Re: Stripe Not Returning Token (test mode)

Chris Kavanagh

unread,
Jun 18, 2016, 8:20:38 PM6/18/16
to Digest Recipients
I understand. I was able to get things working using Stripe Checkout (along with csrf). The Token was returning fine.
However, for some odd reason, using Stripe.js just wouldn't work. I could create a customer that
would show in the Dashboard, but would never return a Token.

Anyways, thanks again for all your help and your patience!

Dennis Mutyaba

unread,
Jun 7, 2017, 6:29:00 PM6/7/17
to Django users
hi chris 
  please i need your hand here i am geting the same problem
dennis
Reply all
Reply to author
Forward
0 new messages