OTP-Assist
Steps to enable OTP auto-read and auto-submit on your Android app for payments that rely on OTP for completion.
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.
-
Create a
. -
Generate the
from the . To go live with the integration and start accepting real payments, generate Live Mode API Keys and replace them in the integration. -
Integrate with
.
Add the following line in your build.gradle
(app-level) file in the dependencies block:
dependencies{//other dependenciesimplementation "com.razorpay:otp-assist:1.0.0""//other dependencies}
1.
.2.
.3.
.4.
.In the S2S flow, since you have not integrated with any of Razorpay’s Checkout SDKs, you must create the RazorpayOtpAssist
object.
RazorpayOtpAssist(Activity activity, String apiKey)
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
Use the sample code given below:
RazorpayOtpAssist razorpayOtpAssist = RazorpayOtpAssist(activity, apiKey);
You can use one of the options below based on your requirement:
- : For card payments.
- : For native OTP 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.
void startSmsListener(WebView webView)
WebView
object
Used to load the URL provided by Razorpay for payment completion.
Use the sample code given below:
RazorpayOtpAssist razorpayOtpAssist = new RazorpayOtpAssist(PaymentActivity.this, "YOU_KEY_ID");// Other coderazorpayOtpAssist.startSmsListener(webview);// Other code
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.
public interface OtpListener {void onOtpReceived(String sender, String body, String otp);void onOtpConfirmed(String sender, String body, String otp);}
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.
Use the sample code given below:
RazorpayOtpAssist razorpayOtpAssist = new RazorpayOtpAssist(PaymentActivity.this, "YOU_KEY_ID");// Other coderazorpayOtpAssist.startSmsListener(new OtpListener() {@Overridepublic void onOtpReceived(String sender, String body, String otp) {// Fill {otp} in the input field}@Overridepublic 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
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.
void onActivityResultReceived(String requestCode, String resultCode, Intent data)
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.
Use the sample code given below:
@Overrideprotected 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);}}
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.
void reset()
Use the code given below:
//other coderazorpayOtpAssist.reset()//other code
Is this integration guide useful?