pass_mtx 0.0.27 pass_mtx: ^0.0.27 copied to clipboard
Pass Micro Transaction Module - Register your bank cards to PASS application and make your transaction the most convenient and fastest way. PASS.MN
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:intl/intl.dart';
import 'package:flutter/services.dart';
import 'package:pass_mtx/pass_mtx.dart';
import 'package:qr_flutter/qr_flutter.dart';
void main() {
runApp(
const MaterialApp(
home: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const String RESPONSE_OK = "ok";
static const String RESPONSE_NG = "ng";
String _passInitResult = 'Unknown';
String loggedUserId = "1";
// OTP , amount
String verificationMethod = "amount";
// Verification code
String verificationValue = "1010";
String? qrData;
String? orderId;
final _passMtxPlugin = PassMtx();
// Pass purchase
TextEditingController responseController = TextEditingController();
TextEditingController response2Controller = TextEditingController();
TextEditingController amountController = TextEditingController();
TextEditingController tokenController = TextEditingController();
TextEditingController cardNoController = TextEditingController();
TextEditingController cardCvvController = TextEditingController();
TextEditingController cardExpireDateController = TextEditingController();
TextEditingController cardHolderNameController = TextEditingController();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String passInitResult;
String appId = "<app_id>";
// DATABANK талаас үүсгэж өгсөн ClientID
String clientId = "<client_id>";
// DATABANK талаас үүсгэж өгсөн PrivateKey
String privatePem = "<private_key>";
// DATABANK талаас үүсгэж өгсөн PublicKey
String publicPem = "<public_key>";
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
if (kReleaseMode) {
passInitResult = await _passMtxPlugin.initProd(
appId, clientId, privatePem, publicPem) ??
'Unknown platform version';
} else {
passInitResult =
await _passMtxPlugin.init(appId, clientId, privatePem, publicPem) ??
'Unknown platform version';
}
} on PlatformException {
passInitResult = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_passInitResult = passInitResult;
response2Controller.text = _passInitResult;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> callSaveCard(String szPan, String szCvv, String szExpDate,
String szCardHolderName) async {
String pingResult;
String saveCardResult;
String verifyCardResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
pingResult = await _passMtxPlugin.getPassPing() ?? 'Get Ping Failed';
} on PlatformException {
pingResult = 'Failed to Ping.';
}
debugPrint("Ping Result $pingResult");
try {
saveCardResult = await _passMtxPlugin.saveCard(
loggedUserId, szPan, szCvv, szExpDate, szCardHolderName) ??
'Save Card Failed';
} on PlatformException {
saveCardResult = 'Failed to Save Card.';
}
Map<String, dynamic> jsonBody = json.decode(saveCardResult);
if (jsonBody.containsKey("status_code") &&
jsonBody["status_code"] == RESPONSE_OK) {
Map<String, dynamic> jsonRet = jsonBody["ret"];
if (jsonRet.containsKey("amount")) {
verificationValue = jsonRet["amount"];
verificationValue =
verificationValue.substring(verificationValue.length - 4);
try {
verifyCardResult = await _passMtxPlugin.verifyCard(
loggedUserId, verificationMethod, verificationValue) ??
'Verify Card Failed';
} on PlatformException {
verifyCardResult = 'Failed to Verify Card.';
}
debugPrint("Verify Card Result $verifyCardResult");
}
}
setState(() {
response2Controller.text = saveCardResult;
});
debugPrint("Save Card Result $saveCardResult");
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> callDeleteCard() async {
String deleteCardResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
deleteCardResult = await _passMtxPlugin.deleteCard(
loggedUserId, "f21a974d-b54f-4bd5-951b-6233ae7b8bd4") ??
'Get Card Failed';
} on PlatformException {
deleteCardResult = 'Failed to get platform version.';
}
setState(() {
response2Controller.text = deleteCardResult;
});
debugPrint("Deleted result $deleteCardResult");
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> callSetDefaultCard() async {
String setDefaultCardResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
setDefaultCardResult = await _passMtxPlugin.setDefaultCard(
loggedUserId, "9288e3be-99aa-45b2-a28e-47fc1702a9fc") ??
'Set Default Card Failed';
} on PlatformException {
setDefaultCardResult = 'Failed to get platform version.';
}
setState(() {
response2Controller.text = setDefaultCardResult;
});
debugPrint("Deleted result $setDefaultCardResult");
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> callLoadCardToken() async {
String getCardResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
getCardResult =
await _passMtxPlugin.getCard(loggedUserId) ?? 'Get Card Failed';
} on PlatformException {
getCardResult = 'Failed to get platform version.';
}
setState(() {
response2Controller.text = getCardResult;
});
debugPrint("GetCard result $getCardResult");
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> makePassPurchase() async {
if (amountController.text.isEmpty) {
return;
}
String passPurchaseResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
String amount = amountController.text;
String realAmount = (int.parse(amount) * 100).toString();
DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyyMMddHHmmss').format(now);
try {
passPurchaseResult = await _passMtxPlugin.makePassPurchase(
"1", // userId
"pass_inapp_txn_1", // orderId
tokenController.text, // paymentToken
"2509", // expDate
realAmount, // paymentAmount
"DTB$formattedDate", // dbRefNo
"pass txn test", // description
) ??
'Make Purchase Failed';
} on PlatformException {
passPurchaseResult = 'Failed to get platform version.';
}
setState(() {
responseController.text = passPurchaseResult;
});
debugPrint("PassPurchase result $passPurchaseResult");
}
Future<void> passGetQr() async {
String passGetQrResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
setState(() {
orderId = "INAPPV1${DateTime.now().millisecondsSinceEpoch}";
});
try {
passGetQrResult = await _passMtxPlugin.passGetQr(
"1", // userId
orderId ?? "", // orderId
tokenController.text, // paymentToken
"2509", // expDate
"3600", // ttl
) ??
'Get QR Failed';
setState(() {
qrData = passGetQrResult;
});
debugPrint("GetQR qr Data $qrData");
while (true) {
String orderEnqueryResult = await _passMtxPlugin.orderEnquery(
"1", // userId
orderId ?? "", // orderId
) ??
'Get Order enquery Failed';
debugPrint("OrderEnquery result $orderEnqueryResult");
if (orderEnqueryResult.contains("confirm_required")) {
bool? result = await _showMyDialog();
if (result ?? false) {
await confirmOrder();
return;
}
await cancelOrder();
setState(() {
qrData = null;
});
break;
} else if (orderEnqueryResult.contains("paid")) {
setState(() {
qrData = null;
});
break;
}
await Future.delayed(const Duration(seconds: 5));
}
} on PlatformException {
passGetQrResult = 'Failed to get platform version.';
}
debugPrint("GetQR result $passGetQrResult");
}
Future<void> cancelOrder() async {
String orderCancelResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
orderCancelResult = await _passMtxPlugin.orderCancel(
"1", // userId
orderId ?? "", // orderId
) ??
'OrderCancel Failed';
} on PlatformException {
orderCancelResult = 'Failed to get platform version.';
}
debugPrint("OrderCancel result $orderCancelResult");
}
Future<void> confirmOrder() async {
String orderConfirmResult;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
orderConfirmResult = await _passMtxPlugin.orderConfirm(
"1", // userId
orderId ?? "", // orderId
"",
"",
tokenController.text,
cardExpireDateController.text) ??
'OrderConfirm Failed';
} on PlatformException {
orderConfirmResult = 'Failed to get platform version.';
}
Map<String, dynamic> jsonData = json.decode(orderConfirmResult);
Map<String, dynamic> jsonRet = {};
if (jsonData.containsKey("ret")) {
jsonRet = jsonData["ret"];
if (jsonRet.containsKey("resp_code") && jsonRet["resp_code"] == "000") {
setState(() {
qrData = null;
});
}
}
debugPrint("OrderConfirm result $orderConfirmResult");
}
Future<bool?> _showMyDialog() async {
return showDialog<bool>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('CONFIRMATION'),
content: const SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Guilgee batalgaajuulna uu.'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Approve'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
initialIndex: 0,
length: 3,
child: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('Pass MTX Example'),
bottom: const TabBar(
tabs: <Widget>[
Tab(
icon: Icon(Icons.credit_card_outlined),
),
Tab(
icon: Icon(Icons.shopping_cart_outlined),
text: "Sale /direct/",
),
Tab(
icon: Icon(Icons.shopping_cart_outlined),
text: "Sale /cpm/",
),
],
),
),
body: TabBarView(
children: <Widget>[buildCardsExample, buildTxnView, buildCpmTxnView],
),
),
);
}
Widget get buildCpmTxnView => SingleChildScrollView(
child: Column(
children: [
const Text("Pass CPM Purchase Test"),
const SizedBox(
height: 30,
),
TextField(
controller: tokenController,
keyboardType: TextInputType.number,
maxLength: 100,
textInputAction: TextInputAction.done,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Payment Token',
),
),
const SizedBox(
height: 30,
),
TextField(
controller: cardExpireDateController,
keyboardType: TextInputType.number,
maxLength: 4,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Expire Date',
),
),
const SizedBox(
height: 30,
),
TextButton(
child: const Text("GET QR"),
onPressed: () {
FocusScope.of(context).unfocus();
passGetQr();
},
),
qrData != null
? Column(
children: [
QrImageView(
data: qrData ?? "",
version: QrVersions.auto,
size: 200.0,
),
const SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: const Text(
"CANCEL",
style: TextStyle(color: Colors.red),
),
onPressed: () {
cancelOrder();
},
),
],
)
],
)
: Container(),
],
),
);
Widget get buildTxnView => Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text("Pass Direct Purchase Test"),
const SizedBox(
height: 30,
),
TextField(
controller: tokenController,
keyboardType: TextInputType.number,
maxLength: 100,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Payment Token',
),
),
const SizedBox(
height: 10,
),
TextField(
controller: amountController,
keyboardType: TextInputType.number,
maxLength: 16,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Amount',
),
),
TextButton(
child: const Text("Pay"),
onPressed: () {
makePassPurchase();
},
),
const Text("Pass Purchase Response"),
const SizedBox(
height: 10,
),
TextField(
controller: responseController,
keyboardType: TextInputType.number,
maxLength: 1000,
readOnly: true,
maxLines: 10,
style: const TextStyle(fontSize: 12),
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
),
],
);
Widget get buildCardsExample => SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Wrap(
spacing: 20,
runSpacing: 20,
children: [
const SizedBox(
height: 5,
),
TextField(
controller: cardNoController,
keyboardType: TextInputType.number,
maxLength: 16,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Card Number',
),
),
TextField(
controller: cardHolderNameController,
maxLength: 26,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Card Holder Name',
),
),
TextField(
controller: cardCvvController,
keyboardType: TextInputType.number,
maxLength: 3,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'CVV',
),
),
TextField(
controller: cardExpireDateController,
keyboardType: TextInputType.number,
maxLength: 4,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Expire Date',
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
child: const Text("Save"),
onPressed: () {
callSaveCard(
cardNoController.text,
cardCvvController.text,
cardExpireDateController.text,
cardHolderNameController.text,
);
debugPrint(
"Card NO :${cardNoController.text}\nCVV:${cardCvvController.text}\nEXPDATE:${cardExpireDateController.text}\nHOLDER:${cardHolderNameController.text}");
}),
TextButton(
child: const Text("Load"),
onPressed: () {
callLoadCardToken();
},
),
TextButton(
child: const Text("Set Default"),
onPressed: () {
callSetDefaultCard();
},
),
TextButton(
child: const Text(
"Delete",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold),
),
onPressed: () {
callDeleteCard();
},
),
],
),
const Text("Request Response"),
const SizedBox(
height: 10,
),
TextField(
controller: response2Controller,
keyboardType: TextInputType.number,
maxLength: 1000,
readOnly: true,
maxLines: 10,
style: const TextStyle(fontSize: 12),
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
),
],
),
);
}