Orbit
Orbit

Reputation: 2385

Error when creating a customer using test card

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?

Upvotes: 0

Views: 72

Answers (1)

floatingLomas
floatingLomas

Reputation: 8737

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.

Upvotes: 1

Related Questions