0

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?

Orbit
  • 2,985
  • 9
  • 49
  • 106

1 Answers1

1

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.

floatingLomas
  • 8,553
  • 2
  • 21
  • 27
  • `token.id` is not empty, and contains the token id passed from the client Checkout flow. – Orbit Sep 29 '17 at 02: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 02: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 02:48