payme_miniapp_flutter 1.7.1 copy "payme_miniapp_flutter: ^1.7.1" to clipboard
payme_miniapp_flutter: ^1.7.1 copied to clipboard

PayME Miniapp Flutter is plugin for application integrate with PayME Platform

example/lib/main.dart

import 'dart:math';

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

import 'package:payme_miniapp_flutter/payme_miniapp_flutter.dart';
import 'package:payme_miniapp_flutter/payme_miniapp_type.dart';

void main() async {
  await dotenv.load();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Your App Title',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  String? selectedService;
  String phoneNumber = '0795550300';
  String env = "SANDBOX";
  final TextEditingController _phoneNumberController = TextEditingController();

  List<Map<String, String>> services = [
    {"label": "POWE", "value": "POWE"},
    {"label": "WATE", "value": "WATE"},
    {"label": "ADSL", "value": "ADSL"},
    {"label": "TIVI", "value": "TIVI"},
    {"label": "MOBILE_CARD", "value": "MOBILE_CARD"},
    {"label": "MOBILE_TOPUP", "value": "MOBILE_TOPUP"},
    {"label": "GAME_CARD", "value": "GAME_CARD"},
    {"label": "PPMB", "value": "PPMB"}
  ];

  List<Map<String, String>> enviroment = [
    {"label": "PRODUCTION", "value": "PRODUCTION"},
    {"label": "SANDBOX", "value": "SANDBOX"},
  ];

  @override
  void initState() {
    super.initState();
    initPlatformState();
    _phoneNumberController.text = phoneNumber;

    init();

    PaymeMiniappFlutter.onResponse.listen((dynamic response) {
      if (response != null) {
        _showResponseDialog(response.toString(), "Response");
      }
    });

    // Listen to error events
    PaymeMiniappFlutter.onError.listen((dynamic error) {
      if (error != null) {
        _showResponseDialog(error.toString(), "Error");
      }
    });
  }

  Future<void> initPlatformState() async {
    if (!mounted) return;
  }

  Future<void> onOpenMiniApp() async {
    PayMEOpeningParam openingParam = PayMEOpeningParam.open(phone: phoneNumber);

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> onOpenService(String? service) async {
    PayMEOpeningParam openingParam = PayMEOpeningParam.service(
        phone: phoneNumber, data: PayMEServiceParam(service: service ?? ''));

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> init() async {
    env == "SANDBOX"
        ? await PaymeMiniappFlutter.init(
            dotenv.env['APP_ID_MERCHANT_SBX']!,
            dotenv.env['PUBLIC_KEY_MERCHANT_RSA_SBX']!,
            dotenv.env['PRIVATE_KEY_MERCHANT_RSA_SBX']!,
            locale: PayMELocale.VI,
            env: PayMEEnv.SANDBOX,
            mode: PayMEMode.MINIAPP_SANDBOX)
        : await PaymeMiniappFlutter.init(
            dotenv.env['APP_ID_MERCHANT']!,
            dotenv.env['PUBLIC_KEY_MERCHANT_RSA']!,
            dotenv.env['PRIVATE_KEY_MERCHANT_RSA']!,
            locale: PayMELocale.VI,
            env: PayMEEnv.PRODUCTION,
            mode: PayMEMode.MINIAPP_PRODUCT);
  }

  Future<void> getBalance() async {
    await PaymeMiniappFlutter.getBalance(phoneNumber);
  }

  Future<void> setLanguage(String language) async {
    await PaymeMiniappFlutter.setLanguage(language);
  }

  Future<void> onDeposit() async {
    PayMEOpeningParam openingParam =
        PayMEOpeningParam.deposit(phone: "0795550300", data: PayMEPayParam());

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> onWithdraw() async {
    PayMEOpeningParam openingParam =
        PayMEOpeningParam.withdraw(phone: "0795550300", data: PayMEPayParam());

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> onTransfer() async {
    PayMEOpeningParam openingParam =
        PayMEOpeningParam.transfer(phone: "0795550300", data: PayMEPayParam());

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> getAccountInfo() async {
    await PaymeMiniappFlutter.getAccountInfo(phoneNumber);
  }

  Future<void> onPayment(String transaction) async {
    // await PaymeMiniappFlutter.payment("155762501569");
    PayMEOpeningParam openingParam = PayMEOpeningParam.payment(
        data: PayMEPaymentDirectParam(transaction: transaction));

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> onTransferQR() async {
    // await PaymeMiniappFlutter.payment("155762501569");
    PayMEOpeningParam openingParam = PayMEOpeningParam.transferQR(
        phone: "0795550300",
        data: PayMETransferQRParam(
            amount: 20000,
            bankNumber: "9704000000000018",
            swiftCode: "SBITVNVX",
            cardHolder: "NGUYEN VAN A",
            note: "test"));

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  Future<void> pay() async {
    PayMEOpeningParam openingParam = PayMEOpeningParam.pay(
        phone: phoneNumber,
        data: PayMEPaymentParam(
            amount: 10000,
            transactionId: generateRandomSixDigitNumber().toString(),
            note: "note"));

    await PaymeMiniappFlutter.open(PayMEOpeningType.modal, openingParam);
  }

  int generateRandomSixDigitNumber() {
    // Create a Random object
    Random random = Random();

    // Generate a random 6-digit number
    int min = 100000;
    int max = 999999;
    return min + random.nextInt(max - min);
  }

  Future<void> _showResponseDialog(String response, String type) async {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(type),
          content: Text(response),
          actions: [
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('OK'),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          FocusManager.instance.primaryFocus?.unfocus();
        },
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: SafeArea(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  TextField(
                    controller: _phoneNumberController,
                    onChanged: (value) {
                      setState(() {
                        phoneNumber = value;
                      });
                    },
                    keyboardType: TextInputType.number,
                    decoration: const InputDecoration(
                        labelText: 'Nhập số điện thoại',
                        contentPadding: EdgeInsets.symmetric(horizontal: 16)),
                  ),
                  const SizedBox(height: 30),
                  Center(
                      child: Row(
                    children: [
                      DropdownButton(
                        padding: const EdgeInsets.symmetric(horizontal: 16),
                        value: env,
                        items: enviroment.map((Map<String, String> enviroment) {
                          return DropdownMenuItem<String>(
                            value: enviroment["value"]!,
                            child: Text(enviroment["label"]!),
                          );
                        }).toList(),
                        onChanged: (String? value) {
                          setState(() {
                            env = value!;
                          });
                        },
                      ),
                      ElevatedButton(
                        onPressed: init,
                        child: const Text('Change Enviroment'),
                      )
                    ],
                  )),
                  const SizedBox(height: 30),
                  ElevatedButton(
                    onPressed: onOpenMiniApp,
                    child: const Text('Open Mini App'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: () {
                      setLanguage("en");
                      onOpenMiniApp();
                    },
                    child: const Text('Set En Language'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: getBalance,
                    child: const Text('Get Balance'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: onDeposit,
                    child: const Text('Deposit 10.000đ'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: onWithdraw,
                    child: const Text('Withdraw 10.000đ'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: onTransfer,
                    child: const Text('Transfer 10.000đ'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: getAccountInfo,
                    child: const Text('Get Account Info'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: () => onPayment("155762501569"),
                    child: const Text('Payment'),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: () => onTransferQR(),
                    child: const Text('Transfer QR'),
                  ),
                  const SizedBox(height: 16),
                  Center(
                    child: Container(
                      constraints: BoxConstraints(
                          maxWidth: MediaQuery.of(context).size.width * 0.8),
                      padding: const EdgeInsets.all(16.0),
                      child: Row(
                        children: [
                          Expanded(
                            child: DropdownButton<String>(
                              value: selectedService,
                              items:
                                  services.map((Map<String, String> service) {
                                return DropdownMenuItem<String>(
                                  value: service["value"]!,
                                  child: Text(service["label"]!),
                                );
                              }).toList(),
                              onChanged: (String? newValue) {
                                setState(() {
                                  selectedService = newValue;
                                });
                              },
                              hint: const Text('Select a service'),
                            ),
                          ),
                          const SizedBox(width: 16),
                          ElevatedButton(
                            onPressed: () {
                              if (selectedService != null) {
                                // Add your logic to handle the selected service
                                onOpenService(selectedService);
                                debugPrint(
                                    'Selected Service: $selectedService');
                              } else {
                                debugPrint('Please select a service.');
                              }
                            },
                            child: const Text('Open'),
                          ),
                        ],
                      ),
                    ),
                  ),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: pay,
                    child: const Text('Pay'),
                  ),
                ],
              ),
            ),
          ),
        ));
  }
}
0
likes
130
points
30
downloads

Publisher

unverified uploader

Weekly Downloads

PayME Miniapp Flutter is plugin for application integrate with PayME Platform

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on payme_miniapp_flutter