API ReferenceIntegrationsKnowledge Base

Razorpay Flutter Plugin for Android and iOS

The Razorpay Flutter plugin acts as a wrapper around our native Android and iOS SDKs.

Refer to our Standard Android SDK and Standard iOS SDK documentation for more information on these SDKs.

We recommend you read our Payment Flow document before proceeding with the integration.

Installation#

  1. Download the plugin from Pub.dev.

  2. Add the below code to dependencies in your app's pubspec.yml

    Copyrazorpay_flutter: 1.1.0

    Minimum Version Requirement:

    • For Android, ensure that the minimum API level for your app is 19 or higher.
    • For iOS, ensure that the minimum deployment target for your app is iOS 10.0 or higher. Also, don't forget to enable bitcode for your project.
  3. Run flutter packages get in the root directory of your app.

Usage#

Sample code to integrate can be found here.

Import package#

Use the below code to import the razorpay_flutter.dart file to your project.

Copyimport 'package:razorpay_flutter/razorpay_flutter.dart';

Create Razorpay instance#

Use the below code to create a Razorpay instance.

Copy_razorpay = Razorpay();

Attach Event Listeners#

The plugin uses event-based communication and emits events when payments fail or succeed.

The event names are exposed via the constants EVENT_PAYMENT_SUCCESS, EVENT_PAYMENT_ERROR and EVENT_EXTERNAL_WALLET from the Razorpay class.

Use the on(String event, Function handler) method on the Razorpay instance to attach event listeners.

Copy_razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess); _razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError); _razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);

The handlers would be defined in the class as:

Copyvoid _handlePaymentSuccess(PaymentSuccessResponse response) { // Do something when payment succeeds } void _handlePaymentError(PaymentFailureResponse response) { // Do something when payment fails } void _handleExternalWallet(ExternalWalletResponse response) { // Do something when an external wallet is selected }

To clear event listeners, use the clear method on the Razorpay instance.

Copy_razorpay.clear(); // Removes all listeners

Setup Options#

Copyvar options = { 'key': '<YOUR_KEY_ID>', 'amount': 100, //in the smallest currency sub-unit. 'name': 'Acme Corp.', 'description': 'Fine T-Shirt', 'prefill': { 'contact': '9123456789', 'email': 'gaurav.kumar@example.com' } };

For a complete list of options for the checkout form, refer to our standard checkout documentation.

Open Checkout#

Use the below code to open the Razorpay checkout.

Copy_razorpay.open(options);

API#

Documented below is the API package for the plugin.

Class#

Razorpay

Method (Open Checkout)#

The below method opens the checkout.

open(map<String, dynamic> options)

The options map has key as a required property. All other properties are optional. Refer to our standard checkout documentation for a complete list of options for the checkout form.

Method (Register Event Listeners)#

The below method registers event listeners for payment events.

on(String eventName, Function listener)

Method (Clear all Listeners)#

clear()

Error Codes#

The error codes have been exposed as integers by the Razorpay class.

The error code is available as the code field of the PaymentFailureResponse instance passed to the callback.

Error Code

Description

NETWORK_ERROR

There was a network error. For example, loss of internet connectivity.

INVALID_OPTIONS

An issue with options passed in Razorpay.open.

PAYMENT_CANCELLED

User cancelled the payment.

TLS_ERROR

Device does not support TLS v1.1 or TLS v1.2.

UNKNOWN_ERROR

An unknown error occurred.

Event Names#

The event names have been exposed as strings by the Razorpay class.

Event Name

Description

EVENT_PAYMENT_SUCCESS

The payment was successful.

EVENT_PAYMENT_ERROR

The payment was not successful.

EVENT_EXTERNAL_WALLET

An external wallet was selected.

PaymentSuccessResponse#

Field Name

data type Description

paymentId

string The ID for the payment.

orderId

string The order ID if the payment was for an order, otherwise null.

signature

string The signature to be used for payment verification. Only valid for orders, otherwise null.

PaymentFailureResponse#

Field Name

data type Description

code

integer The error code.

message

string The error message.

ExternalWalletResponse#

Field Name

Data Type Description

walletName

string The name of the external wallet selected.



Generate API Key#

Note:
You have to generate separate API Keys for the test and live modes. No money is deducted from your account in test mode.

To generate API key:

  1. Log into your Dashboard.
  2. Select the mode for which you want to generate the API key from the menu ribbon.
  3. Navigate to Settings.
  4. Click API Keys.
  5. Click Generate Key to generate key for the selected mode.

The Key Id and Key secret appear in a pop-out window. You also have the option to download the Key Details.

Watch the short animation below for more information.

Note:
After generating the keys from the Dashboard, download and save them securely. If you do not remember your API Keys, you need to re-generate it from the Dashboard and replace it wherever required.