Subscription Checkout Integration Flutter Guide
This integration allows you to integrate with a Cashfree Subscription checkout in your app.
The Flutter SDK is hosted on https://pub.dev/ you can get the sdk here. The example app is located here.
Setting Up SDK
Our 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:
dependencies:
flutter_cashfree_pg_sdk: ^2.2.7
Step 1: Creating a Subscription
The first step in the Subscription Checkout integration is to create a subscription. You need to do this before any payment can be processed. You can add an endpoint to your server which creates this subscription and is used for communication with your frontend.
Subscription creation must happen from your backend (as this API uses your secret key). Please do not call this directly from your mobile application. You can use below-mentioned backend SDKs
Once you create subscription, you will get the subscription_id
and subscription_session_id
Step 2: Opening the Subscription Checkout Payment Page
Once the subscription is created, the next step is to open the payment page so the customer can make the payment.
To complete the payment, we can follow the following steps:
- Enable Subscription flow flag in Android Manifest
- Create a
CFSubscriptionSession
object.
- Create a
CFTheme
object (Optional).
- Create a
CFSubscriptionPayment
object.
- Set payment callback.
- Initiate the payment using the payment object created from [step 3]
We go through these step by step below.
Enable Subscription flow flag
Add the Below entry to your Android manifest file. If you don’t enable this flag, SDK won’t provide a payment callback
<meta-data
android:name="cashfree_subscription_flow_enable"
android:value="true" />
Create a Subscription Session
This object contains essential information about the subscription, including the subscription session ID (subscription_session_id
) and subscription ID (subscription_id
) obtained from creation step. It also specifies the environment (sandbox or production).
try {
var subscriptionSession = CFSubscriptionSessionBuilder().setEnvironment(environment).setSubscriptionId(subscriptionId).setSubscriptionSessionId(subscriptionSessionId).build();
return subsriptionSession;
} on CFException catch (e) {
print(e.message);
}
Customize Theme (Optional)
You can customize the appearance of the checkout screen using CFTheme
. This step is optional but can help maintain consistency with your app’s design.
You can set the NavigationBarBackgroundColor
which will set the color for status bar & cashfree loader
var theme = CFThemeBuilder()
.setNavigationBarBackgroundColorColor("#ffffff")
.setNavigationBarTextColor("#ffffff")
.build();
Create Subscription Payment Object
Use CFSubscriptionPaymentBuilder
to create the payment object. This object accepts a CFSubscriptionSession
, like the one created in the previous step.
var cfSubscriptionCheckout = CFSubscriptionPaymentBuilder()
.setSession(subscriptionSession!)
.setTheme(theme)
.build();
Setup payment callback
You need to set up callback handlers to handle events after payment processing. This must be set before calling doPayment
.
void onSubscriptionVerify(String subscriptionId) {
print("Verify Subscription ===> $subscriptionId");
}
void onSubscriptionFailure(CFErrorResponse errorResponse, String data) {
print("Failure in subscription flow");
}
var cfPaymentGatewayService = CFPaymentGatewayService();
@override
void initState() {
super.initState();
cfPaymentGatewayService.setCallback(onSubscriptionVerify, onSubscriptionFailure);
}
Step 3: Sample Code
import 'package:flutter/material.dart';
import 'package:flutter_cashfree_pg_sdk/api/cferrorresponse/cferrorresponse.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfsubscriptioncheckoutpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpaymentgateway/cfpaymentgatewayservice.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfsession/cfsubssession.dart';
import 'package:flutter_cashfree_pg_sdk/api/cftheme/cftheme.dart';
import 'package:flutter_cashfree_pg_sdk/utils/cfenums.dart';
import 'package:flutter_cashfree_pg_sdk/utils/cfexceptions.dart';
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var cfPaymentGatewayService = CFPaymentGatewayService();
CFEnvironment environment = CFEnvironment.SANDBOX;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
TextButton(
onPressed: () => susbcriptionCheckout(),
child: const Text("Subscription Web Checkout Flow")),
],
),
),
),
);
}
susbcriptionCheckout() async {
try {
cfPaymentGatewayService.setCallback(onSubscriptionVerify, onSubscriptionFailure);
var subsriptionSession = CFSubscriptionSessionBuilder()
.setEnvironment(environment)
.setSubscriptionId(subscriptionId)
.setSubscriptionSessionId(subscriptionSessionId)
.build();
var theme = CFThemeBuilder()
.setNavigationBarBackgroundColorColor("#ffffff")
.setNavigationBarTextColor("#ffffff")
.build();
var cfsubscriptionCheckout = CFSubscriptionPaymentBuilder()
.setSession(subsriptionSession)
.setTheme(theme)
.build();
cfPaymentGatewayService.doPayment(cfsubscriptionCheckout);
} on CFException catch (e) {
print(e.message);
}
}
void onSubscriptionVerify(String subscriptionId) {
print("Verify Subscription ===> $subscriptionId");
}
void onSubscriptionFailure(CFErrorResponse errorResponse, String data) {
print(errorResponse.getMessage());
}
}
Step 4: Confirming the Payment
After the payment is completed, you need to confirm whether the payment was successful by checking the subscription status. Once the payment finishes, the user will be redirected back to your activity.
You must always verify payment status from your backend. Before delivering the goods or services, please ensure you call check the subscription status from your backend. Ensure you check the subscription status from your server endpoint.
Testing
You should now have a working checkout button that redirects your customer to Cashfree Subscription Checkout. If your integration isn’t working:
- Open the Network tab in your browser’s developer tools.
- Click the button and check the console logs.
- Use console.log(session) inside your button click listener to confirm the correct error returned.