How to correctly create a stripe source from a stripe token

829 views Asked by At

I am attempting to create a stripe source server-side from a stripe token received client-side. When using console.log(), the token is the correct one retrieved from the client-side, but the error message I am receiving refers to a token that I did not even pass into the Stripe source promise.

Client side js

 stripe.createToken(card).then(function(result) {
          if (result.error) {
            // Inform the user if there was an error.
            var errorElement = document.getElementById('card-errors');
            errorElement.textContent = result.error.message;
          } else {
             document.getElementById('token').setAttribute('value',result.token.id);
             document.getElementById('type').setAttribute('value',result.token.type);
             myForm.submit();

          }
        });

and on my server I have the following...

router.post("/purchase",middleware.isLoggedIn, function(req,res){
    let  tokenId = req.body.token;
    let type = req.body.type;
    console.log(tokenId);

        stripe.sources.create({
            token: tokenId,
            type: req.body.type
        }).then((source) => {
            console.log(source);
        }).catch((e) =>{
            console.log(tokenId);
            console.log(e);
        });
    });
});

I get the following result from the conole.log(tokenId) statement

tok_1CdT3H2eZvKYlo2CNGY0W0JJ

but my error message from console.log(e) refers to a token beginning with 'card' that was not passed into the promise, which is the source of my confusion.

    { Error: Invalid token id: card_1CdT3H2eZvKYlo2Cv1PzIBCv
    at Error._Error (/home/ec2-user/environment/node_modules/stripe/lib/Error.js:12:17)
    at Error.Constructor (/home/ec2-user/environment/node_modules/stripe/lib/utils.js:124:13)
    at Error.Constructor (/home/ec2-user/environment/node_modules/stripe/lib/utils.js:124:13)
    at Function.StripeError.generate (/home/ec2-user/environment/node_modules/stripe/lib/Error.js:57:12)
    at IncomingMessage.<anonymous> (/home/ec2-user/environment/node_modules/stripe/lib/StripeResource.js:170:39)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)
  type: 'StripeInvalidRequestError',
  stack: 'Error: Invalid token id: card_1CdT3H2eZvKYlo2Cv1PzIBCv\n    at Error._Error (/home/ec2-user/environment/node_modules/stripe/lib/Error.js:12:17)\n    at Error.Constructor (/home/ec2-user/environment/node_modules/stripe/lib/utils.js:124:13)\n    at Error.Constructor (/home/ec2-user/environment/node_modules/stripe/lib/utils.js:124:13)\n    at Function.StripeError.generate (/home/ec2-user/environment/node_modules/stripe/lib/Error.js:57:12)\n    at IncomingMessage.<anonymous> (/home/ec2-user/environment/node_modules/stripe/lib/StripeResource.js:170:39)\n    at emitNone (events.js:91:20)\n     t IncomingMessage.emit (events.js:185:7)\n    at endReadableNT (_stream_readable.js:974:12)\n    at _combinedTickCallback (internal/process/next_tick.js:80:11)\n    at process._tickDomainCallback (internal/process/next_tick.js:128:9)',
  rawType: 'invalid_request_error',
  code: 'invalid_token_id',
  param: undefined,
  message: 'Invalid token id: card_1CdT3H2eZvKYlo2Cv1PzIBCv',
  detail: undefined,
  raw: 
   { code: 'invalid_token_id',
     message: 'Invalid token id: card_1CdT3H2eZvKYlo2Cv1PzIBCv',
     type: 'invalid_request_error',
     headers: 
      { server: 'nginx',
        date: 'Sat, 16 Jun 2018 01:18:04 GMT',
        'content-type': 'application/json',
        'content-length': '155',
        connection: 'close',
        'access-control-allow-credentials': 'true',
        'access-control-allow-methods': 'GET, POST, HEAD, OPTIONS, DELETE',
        'access-control-allow-origin': '*',
        'access-control-expose-headers': 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required',
        'access-control-max-age': '300',
        'cache-control': 'no-cache, no-store',
        'request-id': 'req_EfxhCBwQpauiVP',
        'stripe-version': '2017-08-15' },
     statusCode: 400,
     requestId: 'req_EfxhCBwQpauiVP' },
  headers: 
   { server: 'nginx',
     date: 'Sat, 16 Jun 2018 01:18:04 GMT',
     'content-type': 'application/json',
     'content-length': '155',
     connection: 'close',
     'access-control-allow-credentials': 'true',
     'access-control-allow-methods': 'GET, POST, HEAD, OPTIONS, DELETE',
     'access-control-allow-origin': '*',
     'access-control-expose-headers': 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required',
     'access-control-max-age': '300',
     'cache-control': 'no-cache, no-store',
     'request-id': 'req_EfxhCBwQpauiVP',
     'stripe-version': '2017-08-15' },
  requestId: 'req_EfxhCBwQpauiVP',
  statusCode: 400 }
1

There are 1 answers

0
koopajah On

Technically you can but it's likely a lot easier to just create a Source client-side directly instead of creating a token. Elements can create a Source, so does Checkout withe source callback.

If you really want to do this server-side, just pass the token in the token parameter when you create the Card Source.