Ad

Getting Error 400 When Calling An Https Request To Apply A Charge On Stripe API

I am using nodejs without any library/npm to make a charge on stripe using the test api key.

However I am always getting 400 status code response, and can't understand why, can someone give me an hint?

Here is my request details:

{ 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:' }

And here is my payload (using querystring.stringify):

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

Thank you in advance for any help!

Here is the code, the method where I do the request it self:

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

And here is where I call the aux method to send the request:

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

Answer

In the auth you need to add Bearer before the token, that's how you send tokens to API. I tried to do the request on postman and it works, you can use axios or superagent to perform the request in you js file enter image description hereenter image description here

Ad
source: stackoverflow.com
Ad