When attempting to begin a Subscription for a newly created Customer, I receive the following error from Stripe:

invalid_request_error Error: This customer has no attached payment source

The customer seems to be created just fine. I am using Stripe Checkout to collect the card token. For testing, I am using Stripe's 4242 4242 4242 4242 card number with random information. The token seems to be getting created and passed to my server just fine. Below is my server side code:

stripe.plans.retrieve(
    "basic-monthly",
    function(err, plan) {
        if (err) {
            console.error(err)
            res.sendStatus(500)
        } else {
            stripe.customers.create({
                email: owner,
                source: token.id,
            }, function(err, customer) {
                if (err) {
                    console.error(err)
                    res.sendStatus(500)
                  } else {
                    stripe.subscriptions.create({
                      customer: customer.id,
                      items: [
                        {
                          plan: "basic-monthly",
                          quantity: 1
                        },
                      ],
                    }, function(err, subscription) {
                      if (err) {
                        console.error(err)
                        console.log('@@@@@ UNABLE TO CREATE SUBSCRIPTION @@@@')
                        res.sendStatus(500)
                      } else {
                         console.log('Subscription created.')
                         console.dir(subscription)
                         res.sendStatus(200);
                      }     
                    });
                  }
                });
              }
            });

@@@@@ UNABLE TO CREATE SUBSCRIPTION @@@@ is logged, along with the errors described above. I understand what the error means, but I am not sure how it is occurring. As you can see above, I am passing in the Token Id when creating a customer, source: token.id,.

What is the issue here?

up vote 1 down vote accepted

The most likely cause here is that token.id is empty, so the Customer is being created without a Source. I'd suggest logging the contents of token and see what you get.

  • token.id is not empty, and contains the token id passed from the client Checkout flow. – Orbit Sep 29 '17 at 2:23
  • I was mistaken, I thought token.id was just fine as when I print it at the beginning of my route, it prints to the console as expected. I just added a log statement directly above the call to stripe.customers.create() and it is in fact somehow undefined. How is this possible? It's the exact same variable, and not being modified anywhere in my route. – Orbit Sep 29 '17 at 2:33
  • So turns out the token was being turned into a String via JSON.stringify() on the client. It printed to the console with all it's contents, but naturally token.id returned undefined. I appreciate the help. – Orbit Sep 29 '17 at 2:48

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.