-
Notifications
You must be signed in to change notification settings - Fork 150
/
main.dart
500 lines (454 loc) · 15.8 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import 'package:flutter/material.dart';
import 'package:razorpay_flutter/model/upi_account.dart';
import 'package:razorpay_flutter/razorpay_flutter.dart';
import 'package:razorpay_flutter/model/Error.dart';
import 'package:flutter/cupertino.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
/*class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
// TPV Key - rzp_test_5sHeuuremkiApj
//Non-TPV key - rzp_test_0wFRWIZnH65uny
String keyId = "rzp_test_5sHeuuremkiApj";
String mobileNumber="";
late CheckOut checkOut ;
@override
void initState() {
checkOut = CheckOut(keyId);
super.initState();
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Pay with Razorpay',
),
ElevatedButton(onPressed: (){
Razorpay razorpay = Razorpay();
var options = {
'key': 'rzp_live_ILgsfZCZoFIKMb',
'amount': 100,
'name': 'Acme Corp.',
'description': 'Fine T-Shirt',
'retry': {'enabled': true, 'max_count': 1},
'send_sms_hash': true,
'prefill': {'contact': '8888888888', 'email': '[email protected]'},
'external': {
'wallets': ['paytm']
}
};
var options = {
'amount': 10000,
'currency': 'INR',
'prefill':{
'contact':'9877597717',
'email':'[email protected]'
},
'theme':{
'color':'#0CA72F'
},
'send_sms_hash':true,
'retry':{
'enabled':false,
'max_count':4
},
'key': 'rzp_test_5sHeuuremkiApj',
'order_id':'order_N0fmkHxFIp7wQh',
'disable_redesign_v15': false,
'experiments.upi_turbo':true,
'ep':'https://api-web-turbo-upi.ext.dev.razorpay.in/test/checkout.html?branch=feat/turbo/tpv'
};
razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, handlePaymentErrorResponse);
razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, handlePaymentSuccessResponse);
razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, handleExternalWalletSelected);
razorpay.open(options);
},
child: const Text("Pay with Razorpay")),
SizedBox(height: 10,),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 10),
child: TextField(
keyboardType: TextInputType.number,
textAlign: TextAlign.start,
decoration: InputDecoration(
hintText: 'Mobile Number',
),
onChanged: (newValue) => mobileNumber = newValue,
),
),
ElevatedButton(onPressed: (){
checkOut.upiTurbo.linkNewUpiAccount(customerMobile: mobileNumber,
color: "#ffffff",
onSuccess: (List<UpiAccount> upiAccounts) {
print("Successfully Onboarded Account : ${upiAccounts.length}");
},
onFailure:(Error error) { ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error : ${error.errorDescription}")));}
);
}, child: const Text("Pay with Turbo UPI")),
SizedBox(height: 8,),
ElevatedButton(onPressed: (){
checkOut.upiTurbo.manageUpiAccounts(customerMobile: mobileNumber,
color: "#ffffff",
onFailure:(Error error) { ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error : ${error.errorDescription}")));}
);
}, child: const Text("ManageUpiAccounts")),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void handlePaymentErrorResponse(PaymentFailureResponse response){
* PaymentFailureResponse contains three values:
* 1. Error Code
* 2. Error Description
* 3. Metadata
*
showAlertDialog(context, "Payment Failed", "Code: ${response.code}\nDescription: ${response.message}\nMetadata:${response.error.toString()}");
}
void handlePaymentSuccessResponse(PaymentSuccessResponse response){
* Payment Success Response contains three values:
* 1. Order ID
* 2. Payment ID
* 3. Signature
*
showAlertDialog(context, "Payment Successful", "Payment ID: ${response.paymentId}");
}
void handleExternalWalletSelected(ExternalWalletResponse response){
showAlertDialog(context, "External Wallet Selected", "${response.walletName}");
}
void showAlertDialog(BuildContext context, String title, String message){
// set up the buttons
Widget continueButton = ElevatedButton(
child: const Text("Continue"),
onPressed: () {},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text(title),
content: Text(message),
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}*/
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController keyController = TextEditingController();
final TextEditingController amountController = TextEditingController();
final TextEditingController orderIdController = TextEditingController();
final TextEditingController mobileNumberController = TextEditingController();
// TPV Key - rzp_test_5sHeuuremkiApj
//Non-TPV key - rzp_test_0wFRWIZnH65uny
//Checkout key - rzp_live_ILgsfZCZoFIKMb
String merchantKeyValue = "rzp_live_ILgsfZCZoFIKMb";
String amountValue = "100";
String orderIdValue = "";
String mobileNumberValue = "8888888888";
late Razorpay razorpay ;
@override
void initState() {
razorpay = Razorpay("rzp_test_qRGYYA5wZrpFvJ").initUpiTurbo();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Container(
child: Column(
children: [
RZPEditText(
controller: keyController,
textInputType: TextInputType.text,
hintText: 'Enter Key',
labelText: 'Merchant Key',
),
RZPEditText(
controller: amountController,
textInputType: TextInputType.number,
hintText: 'Enter Amount',
labelText: 'Amount',
),
RZPEditText(
controller: orderIdController,
textInputType: TextInputType.text,
hintText: 'Enter Order Id',
labelText: 'Order Id',
),
RZPEditText(
controller: mobileNumberController,
textInputType: TextInputType.number,
hintText: 'Enter Mobile Number',
labelText: 'Mobile Number',
),
Container(
margin: EdgeInsets.fromLTRB(12.0, 0, 12.0, 0),
child: Text(
'* Note - In case of TPV the orderId is mandatory.',
style: TextStyle(
fontStyle: FontStyle.italic,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: RZPButton(
widthSize: 200.0,
onPressed: () {
merchantKeyValue = keyController.text;
amountValue = amountController.text;
razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, handlePaymentErrorResponse);
razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, handlePaymentSuccessResponse);
razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, handleExternalWalletSelected);
razorpay.open(getPaymentOptions());
},
labelText: 'Standard Checkout Pay',
),
),
Expanded(
child: RZPButton(
widthSize: 200.0,
onPressed: () {
merchantKeyValue = keyController.text;
amountValue = amountController.text;
mobileNumberValue = mobileNumberController.text;
orderIdValue = orderIdController.text;
razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, handlePaymentErrorResponse);
razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, handlePaymentSuccessResponse);
razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, handleExternalWalletSelected);
razorpay.open(getTurboPaymentOptions());
},
labelText: 'Turbo Pay',
),
),
],
),
RZPEditText(
controller: mobileNumberController,
textInputType: TextInputType.number,
hintText: 'Enter Mobile Number',
labelText: 'Mobile Number',
),
RZPButton(
widthSize: 200.0,
labelText: "Link New Upi Account",
onPressed: () {
mobileNumberValue = mobileNumberController.text;
razorpay.upiTurbo.linkNewUpiAccount(customerMobile: mobileNumberValue,
color: "#ffffff",
onSuccess: (List<UpiAccount> upiAccounts) {
print("Successfully Onboarded Account : ${upiAccounts.length}");
},
onFailure:(Error error) { ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error : ${error.errorDescription}")));}
);
},
),
RZPButton(
widthSize: 200.0,
labelText: "Manage Upi Account",
onPressed: () {
mobileNumberValue = mobileNumberController.text;
razorpay.upiTurbo.manageUpiAccounts(customerMobile: mobileNumberValue,
color: "#ffffff",
onFailure:(Error error) { ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error : ${error.errorDescription}")));}
);
},
),
],
),
),
),
);
}
Map<String, Object> getPaymentOptions() {
return {
'key': '$merchantKeyValue',
'amount': int.parse(amountValue),
'name': 'Acme Corp.',
'description': 'Fine T-Shirt',
'retry': {'enabled': true, 'max_count': 1},
'send_sms_hash': true,
'prefill': {
'contact': '$mobileNumberValue',
'email': '[email protected]'
},
'external': {
'wallets': ['paytm']
}
};
}
Map<String, Object> getTurboPaymentOptions() {
return {
'amount': int.parse(amountValue),
'currency': 'INR',
'prefill':{
'contact':'$mobileNumberValue',
'email':'[email protected]'
},
'theme':{
'color':'#0CA72F'
},
'send_sms_hash':true,
'retry':{
'enabled':false,
'max_count':4
},
'key': '$merchantKeyValue',
'order_id':'$orderIdValue',
'disable_redesign_v15': false,
'experiments.upi_turbo':true,
'ep':'https://api-web-turbo-upi.ext.dev.razorpay.in/test/checkout.html?branch=feat/turbo/tpv'
};
}
//Handle Payment Responses
void handlePaymentErrorResponse(PaymentFailureResponse response){
/** PaymentFailureResponse contains three values:
* 1. Error Code
* 2. Error Description
* 3. Metadata
**/
showAlertDialog(context, "Payment Failed", "Code: ${response.code}\nDescription: ${response.message}\nMetadata:${response.error.toString()}");
}
void handlePaymentSuccessResponse(PaymentSuccessResponse response){
/** Payment Success Response contains three values:
* 1. Order ID
* 2. Payment ID
* 3. Signature
**/
showAlertDialog(context, "Payment Successful", "Payment ID: ${response.paymentId}");
}
void handleExternalWalletSelected(ExternalWalletResponse response){
showAlertDialog(context, "External Wallet Selected", "${response.walletName}");
}
void showAlertDialog(BuildContext context, String title, String message){
// set up the buttons
Widget continueButton = ElevatedButton(
child: const Text("Continue"),
onPressed: () {},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text(title),
content: Text(message),
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}
class RZPButton extends StatelessWidget {
String labelText;
VoidCallback onPressed;
double widthSize = 100.0;
RZPButton(
{required this.widthSize,
required this.labelText,
required this.onPressed});
@override
Widget build(BuildContext context) {
return Container(
width: widthSize,
height: 50.0,
margin: EdgeInsets.fromLTRB(12.0, 8.0, 8.0, 12.0),
child: ElevatedButton(
onPressed: onPressed,
child: Text(
labelText,
style: TextStyle(
color: Colors.white,
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.indigoAccent),
),
),
);
}
}
class RZPEditText extends StatelessWidget {
String hintText;
String labelText;
TextInputType textInputType;
TextEditingController controller;
RZPEditText(
{required this.textInputType,
required this.hintText,
required this.labelText,
required this.controller});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(12.0),
padding: EdgeInsets.fromLTRB(16.0, 0, 0, 0),
decoration: BoxDecoration(
border: Border.all(),
),
child: TextField(
controller: controller,
keyboardType: textInputType,
style: TextStyle(
color: Colors.black,
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: hintText,
labelText: labelText,
),
),
);
}
}