pass_mtx 0.0.14 copy "pass_mtx: ^0.0.14" to clipboard
pass_mtx: ^0.0.14 copied to clipboard

Pass Micro Transaction Module - Register your bank cards to PASS application and make your transaction the most convenient and fastest way.

example/lib/main.dart

import 'dart:convert';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:pass_mtx/pass_mtx.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";

  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 = "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 {
      // For Staging
      passInitResult =
          await _passMtxPlugin.stageInit(appId, clientId, privatePem, publicPem) ??
              'Unknown platform version';
      // FIXIT: For Production
      // 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) ?? '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> 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.
    try {
      passPurchaseResult = await _passMtxPlugin.makePassPurchase(
            "1", // userId
            "pass_inapp_txn_1", // orderId
            tokenController.text, // paymentToken
            "2509", // expDate
            amountController.text, // paymentAmount
            "DTB${DateTime.now().toIso8601String()}", // dbRefNo
            "pass txn test", // description
          ) ??
          'Make Purchase Failed';
    } on PlatformException {
      passPurchaseResult = 'Failed to get platform version.';
    }

    setState(() {
      responseController.text = passPurchaseResult;
    });
    debugPrint("PassPurchase result $passPurchaseResult");
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 0,
      length: 2,
      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),
              ),
            ],
          ),
        ),
        body: TabBarView(
          children: <Widget>[buildCardsExample, buildTxnView],
        ),
      ),
    );
  }

  Widget get buildTxnView => Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const Text("Pass 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(
                    "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(),
              ),
            ),
          ],
        ),
      );
}
3
likes
0
pub points
20%
popularity

Publisher

verified publisherdatabank.mn

Pass Micro Transaction Module - Register your bank cards to PASS application and make your transaction the most convenient and fastest way.

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on pass_mtx