ninepay_sdk_flutter_demo 1.0.7-beta.1
ninepay_sdk_flutter_demo: ^1.0.7-beta.1 copied to clipboard
9Pay SDK Flutter
example/lib/main.dart
import 'dart:developer';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ninepay_sdk_flutter/ninepay_sdk_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> {
late NinepaySdkFlutter ninepaySdkFlutter;
@override
void initState() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
ninepaySdkFlutter = NinepaySdkFlutter(
merchantCode: '',
secretKey: '',
uId: null,
flavorEnv: NinepayEnv.STAGING,
phoneNumber: '',
// flavorEnv: NinepayEnv.SAND,
colorCode: '15AE62',
);
ninepaySdkFlutter.onEvent().listen((event) {
// Get type event
if (kDebugMode) {
print('LISTEN SUCCESS ${event["name_event"]}');
}
String typeEvent = event['name_event'];
switch (typeEvent) {
case 'backToAppFrom':
showToast(event.toString());
break;
case 'sdkDidComplete':
showToast('sdkDidComplete');
log(event);
break;
case 'getInfoSuccess':
showToast('GET INFO SUCCESS ${event['data']}');
break;
case 'onError':
showToast(event['message']);
// map {"error_code": errorCode, "message": message}
break;
case 'onLogoutSuccessful':
showToast('Đăng xuất thành công!');
break;
case 'onCloseSDK':
showToast('onCloseSDK');
break;
}
});
super.initState();
}
@override
void dispose() {
// cancel listener
ninepaySdkFlutter.cancel();
super.dispose();
}
void closeKeyboard() {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus!.unfocus();
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: closeKeyboard,
child: MaterialApp(
builder: BotToastInit(),
navigatorObservers: [BotToastNavigatorObserver()],
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: ListView(
children: [
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.LOGIN);
},
child: const Text('Click Open Login')),
const Text('Thanh toán (chỉ nhập URL payment)'),
TextFormField(
keyboardType: TextInputType.url,
onFieldSubmitted: (url) {
closeKeyboard();
if (url.isEmpty) {
showToast('URL không hợp lệ!');
return;
}
/// Pass url payment from backend
ninepaySdkFlutter.openPaymentOnSDK(url, PaymentMethod.DEFAULT, true);
},
decoration: const InputDecoration(hintText: 'URL payment'),
),
const SizedBox(height: 10),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.getUserInfo();
},
child: const Text('GET USER INFO')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.PHONE_CARD);
},
child: const Text('Open Phone card')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.TOPUP);
},
child: const Text('Open Phone Topup')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.DATA_CARD);
},
child: const Text('Open data card')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.SERVICE_CARD);
},
child: const Text('Open Service card')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.HISTORY);
},
child: const Text('Open history')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.DEPOSIT);
},
child: const Text('Open deposit')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.TRANSFER);
},
child: const Text('Open transfer')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.QR);
},
child: const Text('Open scan QR')),
// Hoa don
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.openSDKWithAction(action: NinepayActions.BILLING);
},
child: const Text('Open BILLING')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.close();
},
child: const Text('Close 9Pay')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.logout();
},
child: const Text('Logout 9Pay')),
MaterialButton(
onPressed: () async {
ninepaySdkFlutter.getListPaymentMethod(onSuccess: (p0) {}, onError: (p0) {});
},
child: const Text('Get payment method')),
],
),
),
),
),
);
}
void showToast(String? msg) {
if (msg == null) {
BotToast.showText(text: 'No data');
return;
}
BotToast.showText(text: msg);
log(msg);
}
}