> ## Documentation Index
> Fetch the complete documentation index at: https://razorpay-881012b3.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OTP-Assist

> Steps to enable OTP auto-read and auto-submit on your Android app for payments that rely on OTP for completion.

<div style={{display:"flex",flexWrap:"wrap",alignItems:"center",gap:"0.35rem 0.9rem",border:"1px solid rgba(128,128,128,0.28)",borderRadius:"0.5rem",padding:"0.45rem 0.75rem",margin:"0 0 1.25rem",fontSize:"0.875rem"}}>
  <span style={{fontWeight:600}}>Available in</span>
  <span>🇮🇳 India</span>
</div>

With Razorpay OTP-Assist, your customers gain a faster and enhanced checkout experience with Razorpay OTP auto-read and auto-submit. The system automatically reads the OTP received, with your customer’s consent, and submits it. It prevents errors and the users do not need to navigate or interact with additional elements to complete verification, making the process seamless.

## Prerequisites

* Create a [Razorpay account](https://dashboard.razorpay.com/signup).

* Generate the [API keys](/docs/api/authentication#generate-api-keys) from the Dashboard. To go live with the integration and start accepting real payments, generate Live Mode API Keys and replace them in the integration.

* Integrate with [Android Custom SDK](/docs/payments/payment-gateway/android-integration/custom).

## Dependencies

Add the following line in your `build.gradle`(app-level) file in the dependencies block:

```java Dependencies theme={null}
dependencies{
    //other dependencies
    implementation "com.razorpay:otp-assist:1.0.0""
    //other dependencies
}
```

## Integration Steps

**1.** [Initialise RazorpayOtpAssist Object](#step-1-initialise-razorpayotpassist-object). <br />

**2.** [OTP Auto-Submission](#step-2-otp-auto-submission). <br />

**3.** [Override onActivityResultReceived](#step-3-override-onactivityresultreceived). <br />

**4.** [Reset All Objects](#step-4-reset-all-objects). <br />

### Step 1: Initialise RazorpayOtpAssist Object

In the S2S flow, since you have not integrated with any of Razorpay’s Checkout SDKs, you must create the `RazorpayOtpAssist` object.

<CodeGroup>
  ```java Java theme={null}
  RazorpayOtpAssist(Activity activity, String apiKey)
  ```

  ```kotlin Kotlin theme={null}
  class RazorpayOtpAssist(activity: Activity, apiKey: String)
  ```
</CodeGroup>

`activity`
: `object` Activity object within which the RazorpayOtpAssist object is created. This activity object displays the UI timer for the OTP submit cancellation.

`apiKey`
: `string` API Key ID generated from the [Dashboard](/docs/api/authentication#generate-api-keys), used to ensure the feature is enabled.

#### Sample Code

Use the sample code given below:

<CodeGroup>
  ```java Java theme={null}
  RazorpayOtpAssist razorpayOtpAssist = RazorpayOtpAssist(activity, apiKey);
  ```

  ```kotlin Kotlin theme={null}
  val razorpayOtpAssist = RazorpayOtpAssist(activity, apiKey);
  ```
</CodeGroup>

### Step 2: OTP Auto-Submission

You can use one of the options below based on your requirement:

* [WebView-based Payments](#webview-based-payments): For card payments.
* [Business OTP Page-based Payments](#business-otp-page-based-payments): For native OTP payments.

#### WebView-based Payments

Since you load the URL provided by Razorpay in WebView for payment completion, this step allows us to auto-fill and auto-submit the OTP directly in WebView.

<CodeGroup>
  ```java Java theme={null}
  void startSmsListener(WebView webView)
  ```

  ```kotlin Kotlin theme={null}
  fun startSmsListener(webView: WebView)
  ```
</CodeGroup>

`WebView`
: `object` Used to load the URL provided by Razorpay for payment completion.

#### Sample Code

Use the sample code given below:

<CodeGroup>
  ```java Java theme={null}
  RazorpayOtpAssist razorpayOtpAssist = new RazorpayOtpAssist(PaymentActivity.this, "YOU_KEY_ID");
  // Other code

  razorpayOtpAssist.startSmsListener(webview);
  // Other code
  ```

  ```kotlin Kotlin theme={null}
  val razorpayOtpAssist = RazorpayOtpAssist(this@PaymentOptionsEditPayload, "YOU_KEY_ID")
  // Other code

  razorpayOtpAssist.startSmsListener(webView)
  // Other code
  ```
</CodeGroup>

#### Business OTP Page-based Payments

Razorpay offers Native OTP solutions where you can submit the OTP by using Razorpay APIs. To enable OTP auto-read and auto-submit of payments with this feature, we use a separate function that uses an interface to send the callback to you once the OTP is received.

<CodeGroup>
  ```java Java theme={null}
  public interface OtpListener {
      void onOtpReceived(String sender, String body, String otp);
      void onOtpConfirmed(String sender, String body, String otp);
  }
  ```

  ```kotlin Kotlin theme={null}
  interface OtpListener {
     fun onOtpReceived(sender: String, body: String, otp: String)
     fun onOtpConfirmed(sender: String, body: String, otp: String)
  }
  ```
</CodeGroup>

`OtpListener`
: `object` Acts as a callback, triggered when the OTP is received and parsed after the timer is displayed.

`onOtpReceived`
: Triggered when the message is received and the SDK extracts OTP. Values:

* `sender`: Sender of the message (default: razorpay).
* `body`: The entire message.
* `OTP`: OTP pin extracted from the message.

`onOtpConfirmed`
: Triggered when the timer for OTP Auto-submit is allowed/confirmed by the user. Values:

* `sender`: Sender of the message (default: razorpay).
* `body`: The entire message.
* `OTP`: OTP pin extracted from the message.

#### Sample Code

Use the sample code given below:

<CodeGroup>
  ```java Java theme={null}
  RazorpayOtpAssist razorpayOtpAssist = new RazorpayOtpAssist(PaymentActivity.this, "YOU_KEY_ID");
  // Other code

  razorpayOtpAssist.startSmsListener(new OtpListener() {
      @Override
      public void onOtpReceived(String sender, String body, String otp) {
          // Fill {otp} in the input field
      }

      @Override
      public void onOtpConfirmed(String sender, String body, String otp) {
          // This function is triggered after the RazorpayOtpAssist SDK displays the timer, which can be used to stop the auto-completion.
          // If the user cancels auto-submit, this function is not triggered.
      }
  });

  // Other code
  ```

  ```kotlin Kotlin theme={null}
  val razorpayOtpAssist = RazorpayOtpAssist(this@PaymentOptionsEditPayload, "YOU_KEY_ID")
  // Other code

  razorpayOtpAssist.startSmsListener(object : OtpListener {
      override fun onOtpReceived(sender: String, body: String, otp: String) {
          // Fill {otp} in the input field
      }

      override fun onOtpConfirmed(sender: String, body: String, otp: String) {
          // This function is triggered after the RazorpayOtpAssist SDK displays the timer, which can be used to stop auto-completion.
          // If the user cancels auto-submit, this function is not triggered.
      }
  })

  // Other code
  ```
</CodeGroup>

### Step 3: Override onActivityResultReceived

When the application does not use the `RECEIVE_SMS` permission, we use the `SmsRetreiverClient` API provided by Google, which enables the user to give a one-time consent for the application to read the incoming message.

The user’s response for the one-time consent is passed to the activity’s `onActivityResult` function. Since the SDK cannot override this, we request you send this data to us.

<CodeGroup>
  ```java Java theme={null}
  void onActivityResultReceived(String requestCode, String resultCode, Intent data)
  ```

  ```kotlin Kotlin theme={null}
  fun onActivityResultReceived(requestCode: String, resultCode: String, data: Intent)
  ```
</CodeGroup>

`requestCode`
: `string` Passed by `RazorpayOtpAssist` SDK when the `startActivityForResult` is triggered.

`resultCode`
: `string` Contains user-selected action.

`data`
: `intent` Contains data from the user-selected action.

#### Sample Code

Use the sample code given below:

<CodeGroup>
  ```java Java theme={null}
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == RazorpayOtpAssist.SMS_CONSENT_REQUEST && data != null) {
         razorpayOtpAsisst.onActivityResultReceived(requestCode, resultCode, data);
     }
  }
  ```

  ```kotlin Kotlin theme={null}
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == RazorpayOtpAssist.SMS_CONSENT_REQUEST && data != null) {
          razorpayOtpAssist.onActivityResultReceived(requestCode, resultCode, data)
      }
  }
  ```
</CodeGroup>

### Step 4: Reset All Objects

You can use this function to destroy all objects used by the `RazorpayOtpAssist` SDK to avoid leaks or when starting a new transaction with the same Razorpay object.

<CodeGroup>
  ```java Java theme={null}
  void reset()
  ```

  ```kotlin Kotlin theme={null}
  fun reset()
  ```
</CodeGroup>

#### Sample Code

Use the code given below:

```java Reset theme={null}
//other code
razorpayOtpAssist.reset()
//other code
```
