1

I am working on payment gateway integration and had to call the orders api. But i keep getting the error

{"error":{"code":"BAD_REQUEST_ERROR","description":"Please provide your api key for authentication purposes."}}

My whole section of code

const functions = require('firebase-functions');
    var express = require('express');
    var cors = require('cors');
    var request = require('request');
    const crypto = require('crypto');
    var app = express();
    app.use(cors({origin:true}));

    app.post("/",(req,res)=>{
      const amount = req.body.amount;

    const key = '----insert your key here----';
    const key_secret = '----- insert key secret here ----';

      var options = { method: 'POST',
        url: 'https://api.razorpay.com/v1/orders',
        headers: 
        {
            Authorization: 'Basic' + new Buffer(key + ":" + key_secret).toString("base64")},     
        form: 
        { amount: amount,
          currency: 'INR',
          receipt: "Receipt #20",
          payment_capture : 1
        } 
        };

      request(options,  (error, response, body)=> {
          if (error) throw new Error(error);
          res.send(body);

        });
    })



    exports.razorpaymentApi = functions.region('asia-east2').https.onRequest(app);

I have replaced key and key_secret with my original api key and secret. Can you tell me where i am going wrong. Thanks

Supritha
  • 45
  • 8
  • 1
    why do u use new Buffer to pass ur key_srecret ? and check ur browser network tab what its get for `Authorization` value – sayalok Sep 25 '19 at 07:50
  • I am beginner to nodejs, found the code from one of samples. Instead of new Buffer what do u think i should modify the line of code to? Because i tried using postman client, it works there – Supritha Sep 25 '19 at 08:06
  • Similar to https://stackoverflow.com/a/59005454/4703530 – VipinKundal Mar 31 '21 at 07:56

2 Answers2

1

I modified header as

headers: 
    {
        "authorization" : "Basic xxxxMyEncodedString"
    },

This worked for me.

Supritha
  • 45
  • 8
-1

try this

"authorization" = (new Buffer(key + ":" + key_secret, 'base64')).toString('utf8');

i refered this https://www.dotnetcurry.com/nodejs/1231/basic-authentication-using-nodejs