flutter_cashfree_pg_sdk 2.0.12+15 flutter_cashfree_pg_sdk: ^2.0.12+15 copied to clipboard
This is a new integration provided by Cashfree. If you are integrating with Cashfree PG for the first time, we recommend that you use this integration.
import 'package:flutter/material.dart';
import 'package:flutter_cashfree_pg_sdk/api/cferrorresponse/cferrorresponse.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfdropcheckoutpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfwebcheckoutpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpaymentcomponents/cfpaymentcomponent.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpaymentgateway/cfpaymentgatewayservice.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfsession/cfsession.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';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var cfPaymentGatewayService = CFPaymentGatewayService();
@override
void initState() {
super.initState();
cfPaymentGatewayService.setCallback(verifyPayment, onError, receivedEvent);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
TextButton(onPressed: pay, child: const Text("Pay")),
TextButton(onPressed: webCheckout, child: const Text("Web Checkout"))
],
),
),
),
);
}
void verifyPayment(String orderId) {
print("Verify Payment");
}
void onError(CFErrorResponse errorResponse, String orderId) {
print(errorResponse.getMessage());
print("Error while making payment");
}
void receivedEvent(String event_name, Map<dynamic, dynamic> meta_data) {
print(event_name);
print(meta_data);
}
String orderId = "order_18482OugsYDr411Aj2DzDrJP8g8Q4k6";
String paymentSessionId = "session_6U02SnmsBos4Ym2QRxkLQ9dS3HiB-XeQe9C__NZxFwGtsVtGUTPVX5iA9x5EdhTPnXXZxo7NHJeBSPDSshPqSdu88_3QSbD59S4ris1BIHAv";
// String orderId = "order_18482OupTxSofcClBAlgqyYxUVceHo8";
// String paymentSessionId = "session_oeYlKCusKyW5pND4Swzn1rE2-gwnoM8MOC2nck9RjIiUQwXcPLWB3U1xHaaItb-uA9H1k6Fwziq9O63DWcfYGy_3B7rl1nDFo3MMeVqiYrBr";
CFEnvironment environment = CFEnvironment.PRODUCTION;
CFSession? createSession() {
try {
var session = CFSessionBuilder().setEnvironment(environment).setOrderId(orderId).setPaymentSessionId(paymentSessionId).build();
return session;
} on CFException catch (e) {
print(e.message);
}
return null;
}
pay() async {
try {
var session = createSession();
List<CFPaymentModes> components = <CFPaymentModes>[];
var paymentComponent = CFPaymentComponentBuilder().setComponents(components).build();
var theme = CFThemeBuilder().setNavigationBarBackgroundColorColor("#FF0000").setPrimaryFont("Menlo").setSecondaryFont("Futura").build();
var cfDropCheckoutPayment = CFDropCheckoutPaymentBuilder().setSession(session!).setPaymentComponent(paymentComponent).setTheme(theme).build();
cfPaymentGatewayService.doPayment(cfDropCheckoutPayment);
} on CFException catch (e) {
print(e.message);
}
}
webCheckout() async {
try {
var session = createSession();
var cfWebCheckout = CFWebCheckoutPaymentBuilder().setSession(session!).build();
cfPaymentGatewayService.doPayment(cfWebCheckout);
} on CFException catch (e) {
print(e.message);
}
}
}