flutter_cashfree_pg_sdk 2.0.14+17 flutter_cashfree_pg_sdk: ^2.0.14+17 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';
import 'package:webview_flutter/webview_flutter.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();
late final WebViewCookieManager cookieManager = WebViewCookieManager();
var controller = WebViewController()
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) async {
if (url.startsWith("https://api.cashfree.com")) {
await WebViewCookieManager().clearCookies();
}
print(url);
},
onPageFinished: (String url) async {
print(url);
if (url == "https://centinelapi.cardinalcommerce.com/V2/Cruise/StepUp") {
await WebViewCookieManager().clearCookies();
}
},
onWebResourceError: (WebResourceError error) {
print(error.description);
},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse('https://api.cashfree.com/pg/view/gateway/session_mvMYudnJU-9Pzh6mgCa7Kak120yALu29TeTqEiFHklHtZrHI8j8iw1K9ZuKykWXnCr5huFKjHumhBcr7W_LlTo6Pb-FpaFpFtb2KPCWercwwb670b07e226b31d076584e83b183ca7a'));
@override
void initState() {
super.initState();
cfPaymentGatewayService.setCallback(verifyPayment, onError);
}
// void clearCookies() {
// cookieManager.clearCookies();
// }
@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");
}
String orderId = "order_6032UF2HoMn3QyaQaDKMeRGn1aVkug";
String paymentSessionId = "session_7zC5IvX35H9Lz4Zhx-ZHs7IdGsdqZOevJsuGQRSIV4n4KPFlXFfYg4Q6kKLGIRYkdNU_JnMC9leQRA3XhU9Sd2vtVmvKFoTNtoUUgncqqgE1";
void receivedEvent(String event_name, Map<dynamic, dynamic> meta_data) {
print(event_name);
print(meta_data);
}
// String orderId = "order_18482TC1GWfnEYW3gheFhy4mArfynXh";
// String paymentSessionId = "session_gMej8P4gvNUKLbd3fGWVw7Njg5fj3KK4We0HjCg6Tkzy5yZ8mkghdv7vKels1CJ8fBz9_aVpSoU8n5rqufVQrexzhLW0g0dzgdiTJwmrkZYn";
// String orderId = "order_18482OupTxSofcClBAlgqyYxUVceHo8";
// String paymentSessionId = "session_oeYlKCusKyW5pND4Swzn1rE2-gwnoM8MOC2nck9RjIiUQwXcPLWB3U1xHaaItb-uA9H1k6Fwziq9O63DWcfYGy_3B7rl1nDFo3MMeVqiYrBr";
CFEnvironment environment = CFEnvironment.PRODUCTION;
pay() async {
try {
var session = createSession();
List<CFPaymentModes> components = <CFPaymentModes>[];
components.add(CFPaymentModes.UPI);
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);
}
}
CFSession? createSession() {
try {
var session = CFSessionBuilder().setEnvironment(environment).setOrderId(orderId).setPaymentSessionId(paymentSessionId).build();
return session;
} on CFException catch (e) {
print(e.message);
}
return null;
}
newPay() async {
cfPaymentGatewayService = CFPaymentGatewayService();
cfPaymentGatewayService.setCallback((p0) async {
print(p0);
}, (p0, p1) async {
print(p0);
print(p1);
});
webCheckout();
}
webCheckout() async {
try {
var session = createSession();
var cfWebCheckout = CFWebCheckoutPaymentBuilder().setSession(session!).build();
cfPaymentGatewayService.doPayment(cfWebCheckout);
} on CFException catch (e) {
print(e.message);
}
}
}