pass_mtx 0.0.34
pass_mtx: ^0.0.34 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
example/lib/main.dart
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';
import 'package:webview_flutter/webview_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> {
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 cardExpireDateController = TextEditingController();
WebViewController? _webViewController;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_passMtxPlugin.generateCardRegistrationToken(loggedUserId, "https://example.com/card-callback/").then((result) {
if (result != null) {
Map<String, dynamic> jsonResult = json.decode(result);
String initialUrl = "https://ecomstg.pass.mn/openapi/inapp/card-form/?token_id=${jsonResult["ret"]["token_id"]}";
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.black)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {},
onPageFinished: (String url) {
},
onWebResourceError: (WebResourceError error) {
},
),
)
..loadRequest(Uri.parse(initialUrl), headers: {
'Inapp-Client-Id': "ClientID",
'Inapp-App-Id': '<APP_ID>'
});
}
});
});
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 = "ClientID";
// DATABANK талаас үүсгэж өгсөн PrivateKey
String privatePem = "PrivateKey";
// DATABANK талаас үүсгэж өгсөн PublicKey
String publicPem = "PublicKey";
// 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> 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 buildCardsExample() {
return _webViewController == null
? const Center(
child: Text("Loading"),
)
: WebViewWidget(controller: _webViewController!);
}
}