/
main.dart
246 lines (215 loc) · 10.2 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import 'package:flutter/material.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfcard/cfcardlistener.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfcard/cfcardwidget.dart';
import 'package:flutter_cashfree_pg_sdk/api/cferrorresponse/cferrorresponse.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfcard.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfcardpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfdropcheckoutpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfnetbanking.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfnetbankingpayment.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfupi.dart';
import 'package:flutter_cashfree_pg_sdk/api/cfpayment/cfupipayment.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:flutter_cashfree_pg_sdk/api/cfupi/cfupiutils.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();
CFCardWidget? cfCardWidget;
@override
void initState() {
super.initState();
cfPaymentGatewayService.setCallback(verifyPayment, onError);
final GlobalKey<CFCardWidgetState> myWidgetKey = GlobalKey<CFCardWidgetState>();
try {
var session = createSession();
cfCardWidget = CFCardWidget(key: myWidgetKey,
textStyle: null,
inputDecoration: InputDecoration(
hintText: 'XXXX XXXX XXXX XXXX',
contentPadding: const EdgeInsets.all(15.0), // Adjust padding as needed
counterText: "",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0), // Adjust the radius as needed
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0), // Adjust the radius as needed
borderSide: const BorderSide(
color: Colors.green, // Set your desired tint color here
width: 2.0, // Adjust the border width as needed
),
),
),
cardListener: cardListener,
cfSession: session,
);
} on CFException catch (e) {
print(e.message);
}
CFUPIUtils().getUPIApps().then((value) {
print("value");
print(value);
for(var i = 0; i < (value?.length ?? 0); i++) {
var a = value?[i]["id"] as String ?? "";
if(a.contains("phonepe")) {
selectedId = value?[i]["id"];
}
}
});
}
// 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: [
// Container(
// height: 500,
// width: 500,
// child: WebViewWidget(controller: controller),
// )
TextButton(onPressed: pay, child: const Text("Pay")),
TextButton(onPressed: webCheckout, child: const Text("Web Checkout")),
cfCardWidget!,
TextButton(onPressed: cardPay, child: const Text("Card Pay")),
TextButton(onPressed: upiCollectPay, child: const Text("UPI Collect Pay")),
TextButton(onPressed: upiIntentPay, child: const Text("UPI Intent Pay")),
TextButton(onPressed: netbankingPay, child: const Text("Netbanking Pay")),
],
),
),
),
);
}
// "var cardNumber = document.createElement('div');\ncardNumber.id = \"cardNumber\";\nvar cardCvv = document.createElement('div');\ncardCvv.id = \"cardCvv\";\nvar cardExpiry = document.createElement('div');\ncardExpiry.id = \"cardExpiry\";\nvar cardHolder = document.createElement('div');\ncardHolder.id = \"cardHolder\";\nvar payButton = document.createElement('button');\npayButton.id = \"payButton\";\n\n\nconst cashfree = await load({ \n mode: \"sandbox\", //or production\n });\n\n const cardComponent = cashfree.create(\"cardNumber\", {});\n cardComponent.mount(\"#cardNumber\");\n\n const cardCvv = cashfree.create(\"cardCvv\", {});\n cardCvv.mount(\"#cardCvv\");\n\n const cardExpiry = cashfree.create(\"cardExpiry\", {});\n cardExpiry.mount(\"#cardExpiry\");\n\n const cardHolder = cashfree.create(\"cardHolder\", {});\n cardHolder.mount(\"#cardHolder\");\n\n const showError = function(e){\n alert(e.message)\n }\n\n document.querySelector(\"#payBtn\").addEventListener(\"click\", async () => {\n cashfree.pay({\n paymentMethod: cardComponent,\n paymentSessionId: \"yourPaymentSession\",\n returnUrl: \"https://merchantsite.com/return?order_id={order_id}\",\n }).then(function (data) {\n if (data != null && data.error) {\n return showError(data.error)\n }\n });\n })"
void verifyPayment(String orderId) {
print("Verify Payment");
}
void onError(CFErrorResponse errorResponse, String orderId) {
print(errorResponse.getMessage());
print("Error while making payment");
}
void cardListener(CFCardListener cardListener) {
print("Card Listener triggered");
print(cardListener.getNumberOfCharacters());
print(cardListener.getType());
print(cardListener.getMetaData());
}
String orderId = "order_3242VhWIOoHkighuJbdpuhWdXE9Bsd";
String paymentSessionId = "session_PTi-Rhw3Pc4WqklJa66spUkB6BsJSo60sFvUf21KZLy0l_tzHNdGNH5dAc66grGxa_e6dJMGtr1ZpMldgAtDLF2MWauZvIYvKEDZL-fEZpi9";
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;
String selectedId = "";
upiCollectPay() async {
try {
var session = createSession();
var upi = CFUPIBuilder().setChannel(CFUPIChannel.COLLECT).setUPIID("suhasg6@ybl").build();
var upiPayment = CFUPIPaymentBuilder().setSession(session!).setUPI(upi).build();
cfPaymentGatewayService.doPayment(upiPayment);
} on CFException catch (e) {
print(e.message);
}
}
netbankingPay() async {
try {
var session = createSession();
var netbanking = CFNetbankingBuilder().setChannel("link").setBankCode(3003).build();
var netbankingPayment = CFNetbankingPaymentBuilder().setSession(session!).setNetbanking(netbanking).build();
cfPaymentGatewayService.doPayment(netbankingPayment);
} on CFException catch (e) {
print(e.message);
}
}
upiIntentPay() async {
try {
cfPaymentGatewayService.setCallback(verifyPayment, onError);
var session = createSession();
var upi = CFUPIBuilder().setChannel(CFUPIChannel.INTENT).setUPIID(selectedId).build();
var upiPayment = CFUPIPaymentBuilder().setSession(session!).setUPI(upi).build();
cfPaymentGatewayService.doPayment(upiPayment);
} on CFException catch (e) {
print(e.message);
}
}
cardPay() async {
try {
cfPaymentGatewayService.setCallback(verifyPayment, onError);
var session = createSession();
var card = CFCardBuilder().setInstrumentId("db178aff-b8cf-420e-b0ba-7af89f0d2263").setCardCVV("123").build();
var cardPayment = CFCardPaymentBuilder().setSession(session!).setCard(card).savePaymentMethod(true).build();
cfPaymentGatewayService.doPayment(cardPayment);
} on CFException catch (e) {
print(e.message);
}
}
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 oid = "order_18482a6vWX0q6kKeLLFroOegxXZuxcW";
var spi = "session_gQUSQATOLp88E-qRox3vB-H10IWAGgbnaDSNBpu6dxauSzitdg6cxu2GAzRYf-t4TL5aXs0BgvY6Og5eFilV4-JsNGUBsXU42Bv3kOjuLFVA";
var session = CFSessionBuilder().setEnvironment(environment).setOrderId(oid).setPaymentSessionId(spi).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 theme = CFThemeBuilder().setNavigationBarBackgroundColorColor("#ff00ff").setNavigationBarTextColor("#ffffff").build();
var cfWebCheckout = CFWebCheckoutPaymentBuilder().setSession(session!).setTheme(theme).build();
cfPaymentGatewayService.doPayment(cfWebCheckout);
} on CFException catch (e) {
print(e.message);
}
}
}