easy_pay 0.0.4 copy "easy_pay: ^0.0.4" to clipboard
easy_pay: ^0.0.4 copied to clipboard

EasyPaySDK

example/lib/main.dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:easy_pay/easy_pay.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final EasyPay _easyPay = EasyPay();
  String? _sessionKey;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      final version = await _easyPay.getPlatformVersion() ?? 'Unknown platform version';
      setState(() {
        _platformVersion = version;
      });
    } catch (e) {
      print('Failed to get platform version: $e');
    }
  }

  void showSnackbar(String message) {
    final snackBar = SnackBar(content: Text(message));
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }


  Future<void> startTransaction() async {
    try {
      var status = await Permission.location.status;
      if (!status.isGranted) {
        status = await Permission.location.request();
        if (!status.isGranted) {
          showSnackbar('Location permission denied. Kindly allow the permission.');
          return;
        }
      }

      final response = await _easyPay.registerDevice('tpnKey', 'merchantKey');
      print("Transaction successful: $response");

      Map<String, dynamic> decodedResponse = jsonDecode(response ?? '{}');
      String? sessionKey = decodedResponse['session_key'];

      if (sessionKey != null) {
        print('Session Key: $sessionKey');
        var uuid = Uuid();

        Map<String, dynamic> paymentData = {
          'type': 'SALE',
          'amount': '1',
          'isreceiptsneeded': false,
          'showapprovalscreen': false,
          'Customobject': {
            'customeremail': 'graghu@denovosystem.com',
            'PhoneNumber': '919840720372',
            'CreditType': '1',
            'NumberOfPayments': '1',
            'ExtraData': '[]',
            'HolderID': '1',
            'TransactionUniqueIdForQuery': uuid.v1()
          }
        };

        String jsonRequest = jsonEncode(paymentData);

        final paymentResponse = await _easyPay.startPayment( sessionKey, jsonRequest);
        print('Payment initiated: $paymentResponse');

      } else {
        print('Session key not found in response.');
      }

    } catch (e) {
      showSnackbar('Failed to start transaction: $e');
    }
  }

  Future<void> makeRequest() async {
    try {
      final response = await _easyPay.requestWith('merchantKey');
      print("Request successful: $response");
    } catch (e) {
      showSnackbar('Failed to make request: $e');
    }
  }

  Future<void> performTransaction() async {
    try {
      final response = await _easyPay.transactionWith(100.0, 1);
      print("Transaction successful: $response");
    } catch (e) {
      showSnackbar('Failed to perform transaction: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion\n'),
              if (Platform.isAndroid)
                ElevatedButton(
                onPressed: () => startTransaction(),
                child: const Text('Start Transaction'),
              ),
              if (Platform.isIOS)
                ElevatedButton(
                onPressed: () => makeRequest(),
                child: const Text('Make Request'),
              ),
              if (Platform.isIOS)
                ElevatedButton(
                  onPressed: () => performTransaction(),
                  child: const Text('Perform Transaction'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}