How Cashfree implemented one-time-passwords with Browserless

contents
Cashfree implemented one-time-passwords with Browserless

What was the goal of your automation?

Cashfree is now India’s leading payments and API banking solution. Trusted by 100,000 businesses, Cashfree processes more than US$20 billion annually.

The goal was to implement auto-enter OTP (one-time-password) for Card Payments without redirection to the Bank's Page. Customers of merchants now never leave their websites. Before implementing Browserless, there was complete browser redirection during payment.

Why did you choose Browserless for automation?

Browserless is very easy to use. It is very reliable. Their docker image is so flexible and powerful. If you use puppeteer then look no farther than browserless. It will manage chrome for you which can hog your servers if not done right. You worry about your logic and let the professionals on browerless handle chrome and its quirks.


function evaluate(otp) {
  //Fill bank's input element with otp
  document.getElementsByClassName("optPass")[0].value = otp;
  //call validate function in the page
  ValidateForm();
}
const waitForResponse = function(page, badURL, goodURL = "cashfree.com") {
  return new Promise((resolve, reject) => {
    page.on("response", (response) => {
      let newUrl = response.url();
      if (newUrl.includes(badURL)) {
        return resolve(false);
      } else if (newUrl.includes(goodURL)) {
        return resolve(true);
      }
    });
  });
};
async function submitOTP(otp) {
  try {
    let browser = await puppeteer.connect({
      browserWSEndpoint: "internal/browserless/endpoint",
    });
    let page = await browser.newPage();
    let errorDialogMessage = "";

    //register event to capture alert dialog box
    page.on("dialog", async (dialog) => {
      errorDialogMessage = dialog.message();
      await dialog.dismiss();
      return errorDialogMessage;
    });
    //submit otp
    await page.evaluate(this.evaluate, otp);

    if (errorDialogMessage !== "") {
      return constants.RESPONSE_MSG_OTP_FAIL;
    }
    //check otp submission. If OTP is okay we will redirect to cashfree.com
    let output = await Promise.race([
      waitForResponse(page, "www.mybank.com/ajax/validate"),
      page.waitForSelector(".error", {
        visible: true
      }),
    ]);

    if (output === true) {
      return "OTP_OK";
    } else {
      return "OTP_INVALID";
    }
  } catch (err) {
    return "OTP_INVALID";
  }
}

How to get started with Browserless

There are different ways to use our product.

  • Sign up for a free account and get an API key. You have 6 hours of usage for free! After that, you can pay as you go, and only pay per second that you use!
  • If you’ve already tested our service and want a dedicated machine for your requests, you might be interested in signing up for a dedicated account, this works best if your doing screencasting or have a heavy load of requests since you won’t be sharing resources.

If you’re using one of our hosted services; be that usage-based or capacity-based, just connect to our WebSocket securely with your token to start web scraping!

Share this article

Ready to try the benefits of Browserless?