Search code examples
javascriptnode.jsexpressgoogle-cloud-functionsrazorpay

BAD_REQUEST_ERROR - Nodejs Request Method


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


Solution

  • try this

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

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