如何从条带标记正确创建条带源

时间:2018-06-16 01:29:21

标签: node.js stripe-payments stripe-connect

我正在尝试从接收客户端的条带令牌创建条带源服务器端。使用console.log()时,令牌是从客户端检索的正确令牌,但我收到的错误消息是指我甚至没有传递到Stripe源承诺的令牌。

客户端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();

          }
        });

在我的服务器上我有以下内容......

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);
        });
    });
});

我从conole.log(tokenId)语句中获得以下结果

tok_1CdT3H2eZvKYlo2CNGY0W0JJ

但是我在console.log(e)中的错误消息是指一个以'card'开头但没有传递给promise的令牌,这是我混淆的原因。

    { 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 个答案:

答案 0 :(得分:0)

从技术上讲,你可以直接创建一个源客户端而不是创建一个令牌。元素可以create a Source,Checkout也可以source callback

如果您确实想要在服务器端执行此操作,请在创建卡片来源时在token parameter中传递令牌。