Getting this error TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array

I am trying to use payment gateway API, and create subscription but I am getting this error in title. I have tried debug and everything just don’t understand what to do. No matter what I do I get the error.

Everything is fine the form is fine, just when click on submit that’s when I get this error.

I am running this on localhost on my PC.

This is the errror:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object

This is my app.js code :

const express = require("express");
const https = require('https');
const bodyParser = require("body-parser");
var request = require('request');



const app = express();

app.get("/", function(req, res) {

  // https.get(url, function(response) {
  //   console.log(response.statusCode);
  // })
  res.sendFile(__dirname + "/index.html");


})

app.use(express.static("public"));
app.use(bodyParser.urlencoded({
  extended: true
}));

app.post("/", function(req, res) {

  const customerEmailV = req.body.email;
  const customerPhoneV = req.body.pNumber;
  const subscriptionIdV = req.body.sID;
  const planIdV = "001";
  const returnUrlV = "https://google.com";
  const paymentOptionV = "card";
  const cardNumberV = req.body.cNumber;
  const card_expiryMonthV = req.body.expiryM;
  const card_expiryYearV = req.body.expiryYYYY;
  const card_cvvV = req.body.cvv;
  const card_holderV = req.body.cardHolderName;
  
  // console.log(customerEmail, customerPhone,subscriptionId,planId,returnUrl,paymentOption,cardNumber,card_expiryMonth);
  // console.log(card_expiryYear,card_holder,card_cvv);

  // const dataJ = {
  //   customerEmail: customerEmailV,
  //   customerPhone: customerPhoneV,
  //   returnUrl: returnUrlV,
  //   subscriptionId: subscriptionIdV,
  //   planId: planIdV,
  //   paymentOption: paymentOptionV,
  //   card_number: card_numberV,
  //   card_expiryMonth: card_expiryMonthV,
  //   card_expiryYear: card_expiryYearV,
  //   card_cvv: card_cvvV,
  //   card_holder: card_holderV
  // };

  const url = "https://test.cashfree.com/api/v2/subscriptions";

  const options = {
    'method': 'POST',
    'url': 'https://test.cashfree.com/api/v2/subscriptions',
    'headers': {
      'X-Client-Id': '9353760f74de0be3bf586b67d73539',
      'X-Client-Secret': 'ee7a9d1ddbb245572990f9ba7e6de3995a0a8cfd',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      "subscriptionId": subscriptionIdV,
      "planId": planIdV,
      "customerEmail": customerEmailV,
      "customerPhone": customerPhoneV,
      "expiresOn": "2022-05-30 23:59:59",
      "paymentOption": paymentOptionV,
      "card_number": cardNumberV,
      "card_expiryMonth": card_expiryMonthV,
      "card_expiryYear": card_expiryYearV,
      "card_cvv": card_cvvV,
      "card_holder": card_holderV,
      "firstChargeDate": "2021-10-06",
      "returnUrl": "https://google.com"
    })

  };


  const request = https.request(options, function(response) {
    response.on("data", function(data) {
      console.log(JSON.parse(data));
    });
  });

  request.write();
  request.end();

})


app.listen(3000, function(req, res) {
  console.log("The server is running at port 3000")
})

This is my HTML code:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">

  <!-- CSS -->
  <link rel="stylesheet" href="css/style.css">


  <title>Zingy Subscription</title>
</head>

<body>

  <section id="title">

    <!-- As a heading -->
    <nav class="navbar navbar-light bg-light">
      <div class="container-fluid">
        <span class="navbar-brand mb-0 h1">Zingy</span>
      </div>
    </nav>

  </section>

  <section id="form">


    <h1>Zingy Subscription</h1>
    <p>2,999Rs. Per Year</p>
    <form action="/", method="POST">
      <div class="mb-3">
        <input type="email" name="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email address" required autofocus>
        <!-- <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> -->
      </div>
      <div class="mb-3">
        <input type="text" name="pNumber" class="form-control" placeholder="Enter 10 digit Phone Number" required>
      </div>

      <div class="mb-3">
        <input type="text" name="sID" class="form-control" placeholder="Subscription ID" required>
      </div>

      <!-- <div class="mb-3">
        <input type="text" name="pID" class="form-control" placeholder="Plan ID" required>
      </div> -->
      <!-- <div class="mb-3">
        <input type="text" name="rURL" class="form-control" placeholder="Enter returnUrl" required>
      </div> -->
      <!-- <div class="mb-3">
        <input type="text" name="pOption" class="form-control" placeholder="paymentOption" required>
      </div> -->

      <div class="mb-3">
        <input type="text" name="cNumber" class="form-control" placeholder="Enter Card Number" required>
      </div><div class="mb-3">
        <input type="text" name="expiryM" class="form-control" placeholder="Enter Expiry Month" required>
      </div><div class="mb-3">
        <input type="text" name="expiryYYYY" class="form-control" placeholder="Enter Expiry Year" required>
      </div><div class="mb-3">
        <input type="text" name="cvv" class="form-control" placeholder="Enter CVV" required>
      </div><div class="mb-3">
        <input type="text" name="cardHolderName" class="form-control" placeholder="Enter Card Holder Name" required>
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>

  </section>

  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

  <!-- Option 2: Separate Popper and Bootstrap JS -->

  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-W8fXfP3gkOKtndU4JGtKDvXbO53Wy8SZCQHczT5FMiiqmQfUpWbYdTil/SxwZgAN" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv+l9liuYLaMQ/" crossorigin="anonymous"></script>



</body>

</html>

Answer

Got the answer.

Pro tip: You can use Postman to generate code for you in your preferred language .

const express = require('express');
const https = require('follow-redirects').https;
const bodyParser = require("body-parser");
const fs = require('fs');



const app = express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({
  extended: true
}));

app.get("/", function(req, res) {

  res.sendFile(__dirname + "/index.html");

})



app.post("/", function(req, res) {
  const customerEmailV = req.body.email;
  const customerPhoneV = req.body.pNumber;
  const subscriptionIdV = req.body.sID;
  const planIdV = "001";
  const returnUrlV = "https://google.com";
  const paymentOptionV = "card";
  const cardNumberV = req.body.cNumber;
  const card_expiryMonthV = req.body.expiryM;
  const card_expiryYearV = req.body.expiryYYYY;
  const card_cvvV = req.body.cvv;
  const card_holderV = req.body.cardHolderName;

  var options = {
    'method': 'POST',
    'hostname': 'test.cashfree.com',
    'path': '/api/v2/subscriptions',
    'headers': {
      'X-Client-Id': '9353760f74de0be3bf586b67d73539',
      'X-Client-Secret': 'ee7a9d1ddbb245572990f9ba7e6de3995a0a8cfd',
      'Content-Type': 'application/json'
    },
    'maxRedirects': 20
  };

  var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

var postData = JSON.stringify({
  "subscriptionId": subscriptionIdV,
  "planId": planIdV,
  "customerEmail": customerEmailV,
  "customerPhone": customerPhoneV,
  "expiresOn": "2022-05-30 23:59:59",
  "paymentOption": paymentOptionV,
  "card_number": cardNumberV,
  "card_expiryMonth": card_expiryMonthV,
  "card_expiryYear": card_expiryYearV,
  "card_cvv": card_cvvV,
  "card_holder": card_holderV,
  "firstChargeDate": "2021-10-06",
  "returnUrl": "https://google.com"
});

req.write(postData);

req.end();



   console.log(customerEmailV, customerPhoneV,subscriptionIdV,planIdV,returnUrlV,paymentOptionV,cardNumberV,card_expiryMonthV);
  // console.log("we got it");
})


app.listen(3000, function() {
  console.log("The server is running at port 3000");
})