-3

I am converting this cURL code to javascript.

curl -u <YOUR_KEY_ID>:<YOUR_KEY_SECRET> \
-X POST https://api.razorpay.com/v1/customers \
-H "Content-Type: application/json" \
-d '{
  "name": "Gaurav Kumar1"
  "email": "gaurav.kumar+1@example.com"
  "contact": "9123456781"
}'
<script>
    function createCustomer() {

 console.log("createCustomer");
     const url="https://api.razorpay.com/v1/customers";
     fetch(url, {
        method: 'post',
        headers: {
 "x-api-key": "MY_KEY_ID",
       "content-type": "application/json",
    "cache-control": "no-cache"
        },
        body: {
          "name": "Gaurav Kumar1",
            "email": "gaurav.kumar+1@example.com",
            "contact": "9123456781"
        }
})
        .then(response => response.json())
        .then((data) => {
        console.log("data",data)
                  })
}

</script>

when i check this in postman it is giving the correct response. But when i use this in my site it is giving the error.

POST https://api.razorpay.com/v1/customers 401 (Unauthorized) data : error: {code: "BAD_REQUEST_ERROR", description: "Please provide your api key for authentication purposes."}

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Please read the razorpay documentation on how to pass API Key. Hint, it uses basic auth – Ayush Gupta Nov 23 '19 at 07:02
  • Possible duplicate of [Basic authentication with fetch?](https://stackoverflow.com/questions/43842793/basic-authentication-with-fetch) – Ayush Gupta Nov 23 '19 at 07:05
  • Just a brownie point, having your authorization token in frontend is a serious security issue, you should call your server with the details of the Customer and let the server call razor pay APIs for you its a lot safer that way. – Ayyub Kolsawala Nov 23 '19 at 07:27
  • @pooja How did you solve this? – Priyanka Mar 23 '20 at 10:26

2 Answers2

3

You are missing the header Authorization, have a look at this I use jquery AJAX for it I get the code from postman itself.

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://api.razorpay.com/v1/customers",
  "method": "POST",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic cnpwX2xpdmVfa2V5OmFfbG9uZ19wYXNzd29yZA==",
    "User-Agent": "PostmanRuntime/7.13.0",
    "Accept": "*/*",
    "Cache-Control": "no-cache",
    "Postman-Token": "f1c3919b-d2b8-4646-8ce0-caa60f2dfc3a,9dd079e3-ddab-41a8-8b1d-5ac53c00e07b",
    "Host": "api.razorpay.com",
    "accept-encoding": "gzip, deflate",
    "content-length": "59",
    "Connection": "keep-alive",
    "cache-control": "no-cache"
  },
  "data": {
    "name": "Gaurav Kumar1",
    "email": "gaurav.kumar+1@example.com",
    "contact": "9123456781"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

Refer to the steps to Generate the Authorization Key and Secret this is from the official documentation.

https://razorpay.com/docs/assets/images/generate-api-keys.gif

Hope this helps :)

enter image description here

Ayyub Kolsawala
  • 809
  • 8
  • 15
  • how you generated Authorization, i didn't find it over Razorpay dasboard – Yash Jul 04 '20 at 13:48
  • I just pulled this from the docs this should help you generate the Key you need - https://razorpay.com/docs/api/. Also attaching a GIF from the document with the steps to generate the same. https://razorpay.com/docs/assets/images/generate-api-keys.gif – Ayyub Kolsawala Jul 04 '20 at 16:07
1

Here's my code.its work perfect

you have to encode keys and secret by the following methong :

key:key_secret

by base64 encrypt algorithym ,

then you will pass to authorization token for you api >>>

               fetch("https://api.razorpay.com/v1/orders", {
                 method: "POST",
                 headers: {
                   "Content-Type": "application/json",
                   Authorization: "Basic cnpwX3Rlc3RfMWNIbVVzREJRczhUVVY6WDRTRG95YVBqcHNTb3dac0JHQ29TdEs5",
                   Accept: "application/json",
                   "Cache-Control": "no-cache",
                   Host: "api.razorpay.com"
                 },
                 body: JSON.stringify({
                   amount: 50000,
                   currency: "INR",
                   receipt: "receipt#1",
                   payment_capture: 1
                 })
               })
                 .then(response => response.json())
                 .then(responseJson => {
                   console.log("responseJson", responseJson);
                   var order = responseJson;
                 });