调用https请求对Stripe API收费时收到错误400

时间:2018-11-10 16:08:28

标签: node.js https stripe-payments stringify bad-request

我正在使用没有任何库/ npm的nodejs来使用测试api密钥对条带进行收费。

但是我总是收到400个状态码响应,并且不明白为什么,有人可以给我提示吗?

这是我的要求详细信息:

{ protocol: 'https:',
  hostname: 'api.stripe.com',
  method: 'POST',
  path: 'v1/charges',
  timeout: 5000,
  headers:
   { 'Content-Type': 'application/x-www-form-urlencoded',
     'Content-Length': 72 },
  auth: 'sk_test_JOXtNqPjvpFgLXMiwuWWKZxu:' }

这是我的有效负载(使用querystring.stringify):

amount=5000&currency=usd&description=Tiago_1541865841578&source=tok_amex

在此先感谢您的帮助!

这是代码,我自己执行请求的方法:

helpers.sendRequest = function(protocol, port, hostname, method, path, timeoutSeconds, contentType, postData){

   // Stringify the payload
   var stringPayload = querystring.stringify(postData);

   // Construct the request
   var requestDetails = {
     'protocol' : protocol+':',
     'hostname' : hostname,
     'method' : method,
     'path' : path,
     'port' : port,
     'auth': ('Bearer ' + Buffer.from(config.stripe.secretApiKeyTest).toString('base64') + ":"),
     'timeout' : timeoutSeconds * 1000,
     'headers' :{
       'Authorization': ('Bearer ' + Buffer.from(config.stripe.secretApiKeyTest).toString('base64') + ":"),
       'teste':'ola',
       "teste2":"ola2",
       'Content-Type':contentType,
       'Content-Length': Buffer.byteLength(stringPayload)
     }
   };

   console.log("Request Details:")
   console.log(requestDetails);
   console.log("Payload:")
   console.log(stringPayload);

   // Instantiate the request object (using either the http or https module)
   var _moduleToUse = protocol == 'http' ? http : https;
   var req = _moduleToUse.request(requestDetails, function(res){
       console.log(res.statusCode);

   });

   // Bind to the error event so it doesn't get thrown
   req.on('error',function(e){
     callback(err, e);
   });

   // Bind to the timeout event
   req.on('timeout',function(){
     callback(true, {'Error': 'The request took much time and got timeout.'})
   });

   // Add the payload
   req.write(stringPayload);

   // End the request
   req.end();
 };

这是我调用aux方法发送请求的地方:

var stripeRequestObject = {
                            amount: (totalPrice*100),
                            currency: 'usd',
                            description: userData.name+'_'+Date.now(),
                            source: stripeToken,
                        };

                        genericHelper.sendRequest('https',
                          443,
                          'api.stripe.com',
                          'POST',
                          'v1/charges',
                          5,
                          'application/x-www-form-urlencoded',
                          stripeRequestObject);

2 个答案:

答案 0 :(得分:2)

那是我的方法。

const requestDetails = {
            protocol: 'https:',
            hostname: 'api.stripe.com',
            port: 443,
            method: 'POST',
            path: '/v1/charges',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': Buffer.byteLength(stringPayload),
              Authorization: `Bearer ${config.stripe.testAPIkey.Secret}`
            }
          };

您的代码中有错字

'v1/charges'

应为

'/v1/charges'

答案 1 :(得分:1)

在身份验证中,您需要在令牌之前添加 Bearer ,这就是将令牌发送到API的方式。我试图在邮递员上执行请求,它可以工作,您可以使用axios或superagent在您的js文件中执行请求 enter image description here enter image description here