Search code examples
stripe-paymentsstripe.netstripe.js

Error when trying to get Stripe Token


I hope someone can help. I am new to Stripe. I am trying to allow people to subscribe to a plan (say 5$/month). I have configured the stripe using the dashboard. Also got my publishable key. On the client side I am trying to get the token to send it to back end, so that I can create the subscription.

I have seen an old example (i.e stripe.js V2) where you can do what you see below and get the token:

     <script type="text/javascript" src="https://js.stripe.com/v2"></script>
 Stripe.setPublishableKey("MyPublishableKey");
 expiration = $('.cc-exp').payment('cardExpiryVal');
        Stripe.card.createToken({
            number: $('.cc-number').val(),
            cvc: $('.cc-cvc').val(),
            exp_month: (expiration.month || 0),
            exp_year: (expiration.year || 0)
        }, stripeResponseHandler);

Now I am using Stripe.js V3, and doing this:

    <script type="text/javascript" src="https://js.stripe.com/v3"></script>
var stripePublishableKey = @Html.Raw(Json.Encode(Model.StripePublishableKey));
    var stripe = Stripe(stripePublishableKey);
stripe.tokens.create({
                card: {
                    "number": '4242424242424242',
                    "exp_month": 12,
                    "exp_year": 2019,
                    "cvc": '123'
                }
            }, function(err, token) {
                // asynchronously called
            });

I am pretty sure the publishable key is working because when I was experimenting I could see some events to show up on Stripe dashboard.

The problem I am hitting is that I get the following error in the chrome console:

Uncaught TypeError: Cannot read property 'create' of undefined

I think this is the correct interface but not 100% sure:

If anyone can help, that will be great.

Thank you.

Behdad.


Solution

  • V3 (Elements) doesn't have the ability to create Tokens from raw card details - and you shouldn't be doing that as it has PCI consequences.

    You'll likely want to switch out your card fields for Elements here.