【问题标题】:Getting error 400 when calling an https request to apply a charge on Stripe API调用 https 请求以对 Stripe API 收费时出现错误 400
【发布时间】:2019-04-13 21:55:34
【问题描述】:

我正在使用没有任何库/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);

【问题讨论】:

  • 我真的不明白你为什么不直接使用github.com/stripe/stripe-node。我知道您说过您“需要”在普通 Node 中执行此操作,购买 为什么?我很好奇。
  • 只是一个项目和学习:) 没有特别的原因!我知道有很多库和模块很有帮助^^

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


【解决方案1】:

在身份验证中,您需要在令牌之前添加 Bearer,这就是您将令牌发送到 API 的方式。我尝试在邮递员上发出请求并且它有效,您可以使用 axios 或 superagent 在您的 js 文件中执行请求

【讨论】:

  • 嗨@Pedro Silva 感谢您的回复,我在授权上添加了不记名标签,但请求仍然没有通过:(
  • 你能把你正在使用的完整代码发给我吗?同时,我会做一个小脚本来尝试请求
  • 嘿 Pedro 刚刚用详细的代码更新了问题,希望对您有所帮助,我会继续搜索并尝试找出解决方案...谢谢
  • 分析您的代码后,我意识到它太复杂了。所以我创建了一个简单的脚本,您可以适应您的代码,该脚本使用 superagent 执行请求,将您想要的所有数据作为参数传递并打印结果pastebin.com/tJCJTx3r 在此链接中,有一个带有代码的示例脚本。随意调整它以满足您的需求,如果您需要更多帮助,请告诉我。如果它解决了您的问题,请将答案标记为正确请:D
  • 我会尝试使用它们,如果可以的话我会在这里发布
【解决方案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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2021-12-13
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2016-08-04
    • 2016-12-08
    • 2021-03-22
    相关资源
    最近更新 更多