flutter_nmi 1.0.7 flutter_nmi: ^1.0.7 copied to clipboard
NMI Payment Integration.
example/lib/main.dart
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_beep/flutter_beep.dart';
import 'package:flutter_nmi/flutter_nmi.dart';
class NMIDevice {
static const kGeniusMini = "Genius Mini";
static const kBBPOS = "BBPOS";
static const kMiura = "Miura";
static const kOther = "Other";
}
class Action {
static const kTransaction = "Transaction";
static const kTMSUpdate = "TMSUpdate";
static const kPinPadStatus = "PinPadStatus";
}
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 FlutterNmi nmi;
String transactionStatus = "Click below button to make payment";
bool isLoading = false;
bool isTMSUpdate = false;
bool isPinPadStatus = false;
bool mShowWarningDialog = false;
BuildContext? mBuilderContext;
Timer? timer;
TextEditingController amountController = TextEditingController();
TextEditingController transactionIdController = TextEditingController();
@override
void initState() {
super.initState();
}
initialize() async {
setState(() {
isLoading = true;
});
var params = {
"action": Action.kTransaction,
"api_key": "VZueqyW43Ya8WgMD4Yx4G8qn6mED3yCq",
"sandbox": "1",
"signature_required": "1",
"no_signature_below_amount": "3",
"amount": "10",
"surcharge": "0",
"tip": "0",
"device_type": NMIDevice.kBBPOS,
"currency": "USD",
"transactionId": ""
};
//Service charge
nmi = FlutterNmi.instance(
onPinPadInitializationCompleted: onPinPadInitializationCompleted,
onPinPadInitializationFailed: onPinPadInitializationFailed,
onPinPadConnectionStarted: onPinPadConnectionStarted,
onPinPadConnected: onPinPadConnected,
onPinPadDisConnected: onPinPadDisConnected,
onPinPadConnectionFailed: onPinPadConnectionFailed,
onPinPadConfigurationUpdate: onPinPadConfigurationUpdate,
onPinPadTMSUpdateCompleted: onPinPadTMSUpdateCompleted,
onPinPadTMSUpdateFailed: onPinPadTMSUpdateFailed,
onTransactionStatusUpdate: onTransactionStatusUpdate,
onTransactionFailed: onTransactionFailed,
onTransactionCompleted: onTransactionCompleted,
onPinPadStatusUpdated: onPinPadStatusUpdated,
onTransactionTerminated: onTransactionTerminated,
);
mShowWarningDialog = true;
nmi.initialize(params);
}
requestTMSUpdate() {
setState(() {
isLoading = true;
});
isTMSUpdate = true;
var params = {
"action": Action.kTMSUpdate,
"api_key": "VZueqyW43Ya8WgMD4Yx4G8qn6mED3yCq",
"sandbox": "1",
"device_type": NMIDevice.kBBPOS,
};
nmi = FlutterNmi.instance(
onPinPadInitializationCompleted: onPinPadInitializationCompleted,
onPinPadInitializationFailed: onPinPadInitializationFailed,
onPinPadConnectionStarted: onPinPadConnectionStarted,
onPinPadConnected: onPinPadConnected,
onPinPadDisConnected: onPinPadDisConnected,
onPinPadConnectionFailed: onPinPadConnectionFailed,
onPinPadConfigurationUpdate: onPinPadConfigurationUpdate,
onPinPadTMSUpdateCompleted: onPinPadTMSUpdateCompleted,
onPinPadTMSUpdateFailed: onPinPadTMSUpdateFailed,
);
nmi.initialize(params);
}
selectPinPad() {
setState(() {
isLoading = true;
});
nmi = FlutterNmi.instance(
onPinPadInitializationCompleted: onPinPadInitializationCompleted,
onPinPadInitializationFailed: onPinPadInitializationFailed,
onPinPadConnectionStarted: onPinPadConnectionStarted,
onPinPadConnected: onPinPadConnected,
onPinPadDisConnected: onPinPadDisConnected,
onPinPadConnectionFailed: onPinPadConnectionFailed,
onPinPadConfigurationUpdate: onPinPadConfigurationUpdate,
onPinPadTMSUpdateCompleted: onPinPadTMSUpdateCompleted,
onPinPadTMSUpdateFailed: onPinPadTMSUpdateFailed,
);
nmi.selectPinPad();
}
getPinPadStatus() {
setState(() {
isLoading = true;
});
isPinPadStatus = true;
var params = {
"action": Action.kPinPadStatus,
"api_key": "VZueqyW43Ya8WgMD4Yx4G8qn6mED3yCq",
"sandbox": "1",
"device_type": NMIDevice.kBBPOS,
};
nmi = FlutterNmi.instance(
onPinPadInitializationCompleted: onPinPadInitializationCompleted,
onPinPadInitializationFailed: onPinPadInitializationFailed,
onPinPadConnectionStarted: onPinPadConnectionStarted,
onPinPadConnected: onPinPadConnected,
onPinPadDisConnected: onPinPadDisConnected,
onPinPadConnectionFailed: onPinPadConnectionFailed,
onPinPadConfigurationUpdate: onPinPadConfigurationUpdate,
onPinPadTMSUpdateCompleted: onPinPadTMSUpdateCompleted,
onPinPadTMSUpdateFailed: onPinPadTMSUpdateFailed,
onPinPadStatusUpdated: onPinPadStatusUpdated,
);
nmi.initialize(params);
}
requestRefund() {
setState(() {
isLoading = true;
});
var params = {
"api_key": "VZueqyW43Ya8WgMD4Yx4G8qn6mED3yCq",
"sandbox": "1",
"device_type": NMIDevice.kBBPOS,
"currency": "USD",
"transactionId": transactionIdController.text,
"amount": amountController.text,
};
nmi = FlutterNmi.instance(
onTransactionStatusUpdate: onTransactionStatusUpdate,
onTransactionFailed: onTransactionFailed,
onTransactionCompleted: onTransactionCompleted,
);
mShowWarningDialog = false;
nmi.requestRefund(params);
}
cancelTransaction() {
nmi.cancelTransaction();
}
onPinPadInitializationFailed() {
if (kDebugMode) {
print("Flutter ==== onPinPad Initialization Failed");
}
setState(() {
isLoading = false;
transactionStatus = "PinPad Initialization Failed";
});
}
onPinPadInitializationCompleted() {
if (kDebugMode) {
print("Flutter ==== onPinPad Initialization Completed");
}
setState(() {
//isLoading = false;
transactionStatus = "PinPad Initialization Completed";
});
}
onPinPadConnected(value) {
if (kDebugMode) {
print("Flutter ==== onPinPad Connection Completed $value");
}
setState(() {
transactionStatus = "PinPad Connected";
});
/*if (isTMSUpdate) {
nmi.requestTMSUpdate();
} else if (isPinPadStatus) {
nmi.getPinPadStatus();
}*/
}
onPinPadConnectionFailed() {
if (kDebugMode) {
print("Flutter ==== onPinPad Connection Failed");
}
setState(() {
isLoading = false;
transactionStatus = "PinPad Connection Failed";
});
}
onPinPadDisConnected() {
if (kDebugMode) {
print("Flutter ==== onPinPad DisConnected");
}
setState(() {
isLoading = false;
transactionStatus = "PinPad DisConnected";
});
}
onPinPadConfigurationUpdate(value) {
if (kDebugMode) {
print("Flutter ==== onPinPad Configuration Update");
}
setState(() {
transactionStatus = ((value as String).replaceAll("_", " "));
});
}
onPinPadConnectionStarted() {
if (kDebugMode) {
print("Flutter ==== onPinPad Connection Started");
}
setState(() {
transactionStatus = "Connecting to PIN Pad...";
});
}
onTransactionStatusUpdate(value) {
if (kDebugMode) {
print("Flutter ==== onPinPad Status Update");
}
setState(() {
transactionStatus = ((value as String).replaceAll("_", " "));
});
}
onPinPadTMSUpdateCompleted() {
if (kDebugMode) {
print("Flutter ==== TMS Update Completed");
}
setState(() {
isLoading = false;
transactionStatus = "TMS Update Completed";
});
}
onPinPadTMSUpdateFailed(value) {
if (kDebugMode) {
print("Flutter ==== TMS Update Failed $value");
}
setState(() {
isLoading = false;
transactionStatus = "TMS Update Failed. $value";
});
}
onTransactionFailed(value) {
if (kDebugMode) {
print("Flutter ==== Transaction Failed $value");
}
setState(() {
isLoading = false;
transactionStatus = "Transaction Failed \n$value";
});
}
onTransactionCompleted(value) {
if (kDebugMode) {
print("Flutter ==== Transaction Completed $value");
}
if (mShowWarningDialog) {
showWarningDialog(mBuilderContext!);
}
setState(() {
isLoading = false;
transactionStatus = "Transaction Completed";
});
}
onTransactionTerminated() {
if (kDebugMode) {
print("Flutter ==== Transaction Terminated");
}
setState(() {
isLoading = false;
transactionStatus = "Transaction Terminated";
});
}
onPinPadStatusUpdated(value) {
setState(() {
isLoading = false;
transactionStatus = "PinPad Status Updated";
});
if (kDebugMode) {
print("Flutter ==== Transaction Completed $value");
}
}
showWarningDialog(BuildContext context) {
timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
FlutterBeep.beep();
});
showDialog(
context: context,
builder: (_) {
Future.delayed(const Duration(seconds: 5), () {
timer!.cancel();
Navigator.of(context).pop();
});
return Dialog(
insetPadding: const EdgeInsets.symmetric(horizontal: 16),
elevation: 0,
backgroundColor: Colors.white,
child: StatefulBuilder(
builder: (BuildContext _, StateSetter _setState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.warning,
color: Colors.yellow,
size: 100,
),
],
),
Row(
children: [
Expanded(
child: Container(
margin: const EdgeInsets.only(
top: 20, left: 15, right: 15),
child: const Text(
"Please don't forget to remove the card",
style: TextStyle(fontSize: 20),
),
),
),
],
),
const SizedBox(
height: 10,
),
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Container(
margin: const EdgeInsets.only(top: 20, bottom: 20),
padding: const EdgeInsets.only(
top: 5, bottom: 5, right: 20, left: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
border: Border.all(
color: Colors.black,
width: 1,
),
boxShadow: const [
BoxShadow(
color: Colors.white,
spreadRadius: 5,
offset: Offset(0, 3),
),
],
),
child: const Text("Close"),
),
)
],
);
},
),
);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter NMI Demo'),
),
body: Builder(
builder: (context) {
mBuilderContext = context;
return SingleChildScrollView(
child: Column(
children: [
Visibility(
visible: isLoading,
child: Container(
margin: const EdgeInsets.only(top: 40),
child: const CircularProgressIndicator(
backgroundColor: Colors.grey,
color: Colors.purple,
strokeWidth: 5,
),
),
),
const SizedBox(
height: 20,
),
Text(transactionStatus),
const SizedBox(
height: 20,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Pay \$10"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
if (isLoading == false) {
initialize();
}
},
),
const SizedBox(
height: 20,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Request TMS Update"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
if (isLoading == false) {
requestTMSUpdate();
}
},
),
const SizedBox(
height: 20,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Select PinPad"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
if (isLoading == false) {
selectPinPad();
}
},
),
const SizedBox(
height: 20,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Cancel Transaction"),
color: Colors.amber,
),
onTap: () {
cancelTransaction();
},
),
const SizedBox(
height: 40,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Get PinPad Status"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
if (isLoading == false) {
getPinPadStatus();
}
},
),
const SizedBox(
height: 40,
),
Row(
children: [
const SizedBox(
width: 20,
),
Expanded(
child: TextField(
controller: amountController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Refund Amount',
),
),
),
const SizedBox(
width: 20,
),
Expanded(
child: TextField(
controller: transactionIdController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Transaction Id',
),
),
),
const SizedBox(
width: 20,
),
],
),
const SizedBox(
height: 20,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Request Refund"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
if (isLoading == false) {
if (amountController.text != "" &&
transactionIdController.text != "") {
requestRefund();
}
}
},
),
const SizedBox(
height: 20,
),
GestureDetector(
child: const Text("Open Dialog"),
onTap: () {
showWarningDialog(context);
},
)
/*const SizedBox(
height: 40,
),
GestureDetector(
child: Container(
padding: const EdgeInsets.all(20),
child: const Text("Open signature page"),
color: (isLoading == true) ? Colors.grey : Colors.amber,
),
onTap: () {
goToSignaturePad();
},
),*/
],
),
);
},
),
),
);
}
/*goToSignaturePad() {
nmi = FlutterNmi.instance();
nmi.goToSignaturePad();
print("Flutter ==== Open SignaturePad");
}*/
}