Skip to main content
Cashfree’s Flutter SDK provides a streamlined payment solution that integrates the payment gateway into your Flutter applications through a WebView-based checkout implementation, supporting both Android and iOS platforms.

Key benefits

The Flutter SDK offers the following advantages for your payment integration:
  • Simplified integration: Provides a pre-built SDK that delivers an optimised payment experience for Flutter applications.
  • Cross-platform support: Seamlessly supports Android (SDK version 19+) and iOS (deployment target 11+) platforms.
  • Secure and compliant: Securely handles payment processing with built-in security measures managed by the SDK.

Prerequisites

Ensure you complete the following tasks before starting the integration: The Flutter integration consists of three essential steps: The step-by-step guide for each step of the integration process is as follows:

Step 1: Create an order Server-side

To integrate the Cashfree Payment Gateway, you must first create an order. Complete this step before you process any payments. Configure an endpoint on your server to handle order creation. You can’t call this API from the client-side.
Create orders through your server as this API requires your secret key. Don’t call it directly from your mobile application.
API request for creating an order
Here’s a sample request for creating an order using your desired backend language. Cashfree offers backend SDKs to simplify the integration process.
import { Cashfree, CFEnvironment } from "cashfree-pg";

const cashfree = new Cashfree(
	CFEnvironment.PRODUCTION,
	"{Client ID}",
	"{Client Secret Key}"
);

function createOrder() {
	var request = {
		order_amount: "1",
		order_currency: "INR",
		customer_details: {
			customer_id: "node_sdk_test",
			customer_name: "",
			customer_email: "example@gmail.com",
			customer_phone: "9999999999",
		},
		order_meta: {
			return_url:
				"https://test.cashfree.com/pgappsdemos/return.php?order_id=order_123",
		},
		order_note: "",
	};

	cashfree
		.PGCreateOrder(request)
		.then((response) => {
			var a = response.data;
			console.log(a);
		})
		.catch((error) => {
			console.error("Error setting up order request:", error.response.data);
		});
}
After successfully creating an order, you will receive a unique order_id and payment_session_id that you need for subsequent steps. You can view all the complete API request and response for /orders here.

Step 2: Open the payment page Client-side

Web Checkout is a streamlined payment solution that integrates Cashfree’s payment gateway into your Flutter app through the SDK. This implementation uses a WebView to provide a secure, feature-rich payment experience. Your customers are presented with a familiar web interface where they can enter their payment details and complete their transaction seamlessly. All payment logic, UI components, and security measures are managed by the SDK, eliminating the need for complex custom implementation.

1. Set up the SDK

The Flutter SDK is hosted on pub.dev. You can get the SDK here. The Flutter SDK supports Android SDK version 19 and above and iOS minimum deployment target of 11 and above. Open your project’s pubspec.yaml and add the following under dependencies:
flutter_cashfree_pg_sdk: 2.2.9+47

iOS configuration

Add this to your application’s info.plist file:
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>phonepe</string>
  <string>tez</string>
  <string>paytmmp</string>
  <string>bhim</string>
  <string>amazonpay</string>
  <string>credpay</string>
</array>

2. Complete the payment

To complete the payment, follow these steps:
  1. Create a CFSession object.
  2. Create a Web Checkout Payment object.
  3. Set payment callback.
  4. Initiate the payment using the payment object created.

Create a session

This object contains essential information about the order, including the payment session ID (payment_session_id) and order ID (order_id) obtained from Step 1. It also specifies the environment (sandbox or production).
try {
    var session = CFSessionBuilder().setEnvironment(environment).setOrderId(orderId).setPaymentSessionId(paymentSessionId).build();
    return session;
} on CFException catch (e) {
    print(e.message);
}

Create a web checkout payment object

Use CFWebCheckoutPaymentBuilder to create the payment object. This object accepts a CFSession, like the one created in the previous step.
var cfWebCheckout = CFWebCheckoutPaymentBuilder()
  .setSession(session!)
  .build();

Set up the callback

You need to set up callback handlers to handle events after payment processing.
void verifyPayment(String orderId) {
    print("Verify Payment");
  }

  void onError(CFErrorResponse errorResponse, String orderId) {
    print(errorResponse.getMessage());
    print("Error while making payment");
  }

var cfPaymentGatewayService = CFPaymentGatewayService();

  @override
  void initState() {
    super.initState();
    cfPaymentGatewayService.setCallback(verifyPayment, onError);
}

Sample code

class _MyAppState extends State<MyApp> {

  var cfPaymentGatewayService = CFPaymentGatewayService();

  @override
  void initState() {
    super.initState();
    cfPaymentGatewayService.setCallback(verifyPayment, onError);
  }

  void verifyPayment(String orderId) {
    print("Verify Payment");
  }

  void onError(CFErrorResponse errorResponse, String orderId) {
    print(errorResponse.getMessage());
    print("Error while making payment");
  }

  webCheckout() async {
    try {
      var session = createSession();
      var cfWebCheckout = CFWebCheckoutPaymentBuilder().setSession(session!).build();
      cfPaymentGatewayService.doPayment(cfWebCheckout);
    } on CFException catch (e) {
      print(e.message);
    }

  }

  CFSession? createSession() {
    try {
      var session = CFSessionBuilder().setEnvironment(environment).setOrderId(orderId).setPaymentSessionId(paymentSessionId).build();
      return session;
    } on CFException catch (e) {
      print(e.message);
    }
    return null;
  }
}

Step 3: Confirm the payment Server-side

After the customer completes the payment, you must confirm the payment status. Once the payment finishes, the user redirects back to your activity. To verify an order you can call our /pg/orders endpoint from your backend. You can also use our SDK to achieve the same.
version := "2023-08-01"
response, httpResponse, err := cashfree.PGFetchOrder(&version, "<order_id>", nil, nil, nil)
if err != nil {
	fmt.Println(err.Error())
} else {
	fmt.Println(httpResponse.StatusCode)
	fmt.Println(response)
}
Always verify the order status before you deliver services to the customer. You can use the Get Order API for this. An order is successful when the order_status is PAID.

Testing

You should now have a working checkout button that redirects your customer to Cashfree payment page. If your integration isn’t working:
  1. Open the Network tab in your browser’s developer tools.
  2. Click the button and check the console logs.
  3. Use console.log(session) inside your button click listener to confirm the correct error returned.

Error codes

To confirm the error returned in your application, you can view the error codes that are exposed by the SDK.
The following are some of the error codes that are exposed by the SDK:
Error codesMessage
MISSING_CALLBACKThe callback is missing in the request.
ORDER_ID_MISSINGThe “order_id” is missing in the request.
CARD_EMI_TENURE_MISSINGThe “emi_tenure” is missing or invalid (It has to be greater than 0).
INVALID_UPI_APP_ID_SENTThe id sent is invalid. The value has to be one of the following: “tez://”,“phonepe://”,“paytm://”,“bhim://. Please refer the note in CFUPI class for more details
INVALID_PAYMENT_OBJECT_SENTThe payment object that is set does not match any payment mode. Please set the correct payment mode and try again.
WALLET_OBJECT_MISSINGThe CFWallet object is missing in the request
NETBANKING_OBJECT_MISSINGThe CFNetbanking object is missing in the request.
UPI_OBJECT_MISSINGThe CFUPI object is missing in the request.
CARD_OBJECT_MISSINGThe CFCard object is missing in the request.
INVALID_WEB_DATAThe url seems to be corrupt. Please reinstantiate the order.
SESSION_OBJECT_MISSINGThe “session” is missing in the request
PAYMENT_OBJECT_MISSINGThe “payment” is missing in the request
ENVIRONMENT_MISSINGThe “environment” is missing in the request.
ORDER_TOKEN_MISSINGThe “order_token” is missing in the request.
CHANNEL_MISSINGThe “channel” is missing in the request.
CARD_NUMBER_MISSINGThe “card_number” is missing in the request.
CARD_EXPIRY_MONTH_MISSINGThe “card_expiry_mm” is missing in the request.
CARD_EXPIRY_YEAR_MISSINGThe “card_expiry_yy” is missing in the request.
CARD_CVV_MISSINGThe “card_cvv” is missing in the request.
UPI_ID_MISSINGThe “upi_id” is missing in the request
WALLET_CHANNEL_MISSINGThe “channel” is missing in the wallet payment request
WALLET_PHONE_MISSINGThe “phone number” is missing in the wallet payment request
NB_BANK_CODE_MISSINGThe “bank_code” is missing in the request

Affiliate partner program

As a developer building payment experiences for your clients, you can earn additional income while providing them with industry-leading payment solutions.

Join the Cashfree affiliate partner program and get rewarded every time your clients use Cashfree.

What you get:

  • Earn up to 0.25% commission on every transaction.
  • Become a trusted fintech partner for your clients.
  • Access to a dedicated partner manager for expert support.

What your clients get:

  • Instant activation and go live in minutes.
  • Industry-best success rate across all payment modes.
  • Effortless acceptance of international payments in 140+ currencies.

Get started today. Become a partner now.