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

unlisted

SDK pour les transactions financières. By Finasys Technologies

example/lib/main.dart

import 'package:finapay_sdk/core/models/sdk_result.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:finapay_sdk/finapay_sdk.dart';

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

// MyApp is the root widget of the application. It initializes the MaterialApp
// with the application's theme and routes.
class MyApp extends StatefulWidget {
  const MyApp({super.key});

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHome(),
    );
  }
}

// MyHome is the home screen of the application. It displays the platform version
// and provides buttons to perform various Finapay SDK operations such as registering
// a client's token, making a payment, deposit, withdrawal, and transfer.
class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  String _platformVersion = 'Unknown';
  FinapaySdk? _finapaySdkPlugin;
  SdkResult? registrationResult;

  @override
  void initState() {
    super.initState();
    _attemptSdkInstantiation();
    _initPlatformState();
  }

  void _attemptSdkInstantiation() {
    try {
      _finapaySdkPlugin = FinapaySdk();
    } on Exception catch (e) {
      if (kDebugMode) {
        debugPrint(e.toString());
      }
    }
  }

  void _initSdk() async {
    try {
      await FinapaySdk.init();
      setState(() {
        _finapaySdkPlugin = FinapaySdk();
      });
    } on Exception catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(e.toString())),
        );
      }
    }
  }

  // initPlatformState initializes the platform state by fetching the platform version
  // from the Finapay SDK plugin. It handles potential errors and updates the UI
  // with the fetched platform version.
  Future<void> _initPlatformState() async {
    if (_finapaySdkPlugin == null) return;
    String platformVersion;
    try {
      platformVersion = await _finapaySdkPlugin?.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: _finapaySdkPlugin == null
            ? Column(
                children: [
                  const Text("Le Sdk n'a pas encore été initialisé"),
                  ElevatedButton(
                    onPressed: _initSdk,
                    child: const Text("Initialiser le SDK"),
                  ),
                ],
              )
            : Column(
                children: [
                  Center(
                    child: Text('Running on: $_platformVersion\n'),
                  ),
                  // Register client's token
                  ElevatedButton(
                    onPressed: () async {
                      final result = await _finapaySdkPlugin!.regsiterClientWallet(
                          token:
                              "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJaZHdYZDhVTzh0NERPQjVlYm1FdVBWVjMtSm5nMEtHNjYzOWRKcElpN1ZNIn0.eyJleHAiOjE3NDcyNDAxNDUsImlhdCI6MTc0NzIzNjU0NSwianRpIjoiMDVhOTI4NDItNDAzNS00Y2Y5LTk2NDctYjU0Y2NmYTk3ZWQwIiwiaXNzIjoiaHR0cDovLzE5Mi4xNjguODguMjE6ODA4MC9yZWFsbXMvZmluYXBheSIsImF1ZCI6WyJyZWFsbS1tYW5hZ2VtZW50IiwiYnJva2VyIiwiYWNjb3VudCJdLCJzdWIiOiI5ZjI5NDA0Yi02YjFkLTQzYTItYWU5Ni0xYzM0ZTA1YmI0MWMiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJmaW5hcGF5LWFwaSIsInNpZCI6ImUyYTZmNGQ1LWZhMjUtNGRlNy05NWEyLWZiNWExMDcwODNjMSIsImFjciI6IjEiLCJhbGxvd2VkLW9yaWdpbnMiOlsiaHR0cDovL2xvY2FsaG9zdDo0MjAzIiwiaHR0cDovL2xvY2FsaG9zdDo0MjAxIl0sInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJkZWZhdWx0LXJvbGVzLWZpbmFwYXkiLCJvZmZsaW5lX2FjY2VzcyIsInVwZGF0ZV93YWxsZXQiLCJhZGRfd2FsbGV0IiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJyZWFsbS1tYW5hZ2VtZW50Ijp7InJvbGVzIjpbImNyZWF0ZS1jbGllbnQiXX0sImJyb2tlciI6eyJyb2xlcyI6WyJyZWFkLXRva2VuIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50Iiwidmlldy1hcHBsaWNhdGlvbnMiLCJ2aWV3LWNvbnNlbnQiLCJ2aWV3LWdyb3VwcyIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwiZGVsZXRlLWFjY291bnQiLCJtYW5hZ2UtY29uc2VudCIsInZpZXctcHJvZmlsZSJdfX0sInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgcGhvbmUgZW1haWwiLCJwaW5fY2hhbmdlIjpmYWxzZSwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJncm91cHMiOlsiY2xpZW50Il0sInByZWZlcnJlZF91c2VybmFtZSI6IjA1NjQyNTY4NjQifQ.ZvlmIGOCORhS8VJJfCRGkIFh3TiLI5H8s6eA2Cj27ov_xML9Sj4LRICkiDZnze-6SqndA5D-cKoY6cp6mI9wzxynf7QkxUYyRXRAv_kMaVWvyJc2k5IGGRbuQcAWp7SZLAcPR_wRDSyNyW5SYsoOYJHm9s1qJ9fb3HVav2yHPnDwgRXqW-yNRqbCxS2RidY5dYpsiZnHDStl9iPIDGlpB2t4WIEQ5UtnwISgyY5QFQibpC1bOMmuRPMECq6xz68G4SPi5p_lFPYDg__xxG1DkGIuA3hs-Na25JYCev6KQ-UluyGOD9X9Yzh71N9lPdqsF10E7KXWOBIaM54C_--xuw");

                      setState(() {
                        registrationResult = result;
                      });
                      if (registrationResult?.status == RequestStatus.success) {
                        debugPrint(
                            "HOTE : Enregistrement réussi : ${registrationResult?.reference}");

                        if (context.mounted) {
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                                content: Text("Enregistrement réussi")),
                          );
                        }
                      } else {
                        debugPrint(
                            "HOTE : Enregistrement échoué : ${registrationResult?.message}");
                        if (context.mounted) {
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                                content: Text("Enregistrement échoué ")),
                          );
                        }
                      }
                    },
                    child: const Text("Register client with token"),
                  ),
                  // Make new Payment
                  if (registrationResult?.status == RequestStatus.success) ...[
                    ElevatedButton(
                      onPressed: () async {
                        final paymentResult = await _finapaySdkPlugin!
                            .initPayment(context: context);

                        if (paymentResult.status == RequestStatus.success) {
                          debugPrint(
                              "HOTE : Paiement réussi : ${paymentResult.reference}");
                        } else {
                          debugPrint(
                              "HOTE : Paiement échoué : ${paymentResult.message}");
                        }
                      },
                      child: const Text("Make new Paiement"),
                    ),
                    // Make new Deposit
                    ElevatedButton(
                      onPressed: () async {
                        final depositResult = await _finapaySdkPlugin!
                            .initDeposit(context: context);

                        if (depositResult.status == RequestStatus.success) {
                          debugPrint(
                              "HOTE : Dépot réussi : ${depositResult.reference}");
                        } else {
                          debugPrint(
                              "HOTE : Dépot échoué : ${depositResult.message}");
                        }
                      },
                      child: const Text("Make new Deposit"),
                    ),
                    // Make new Withdraw

                    // Make new Transfert
                    ElevatedButton(
                      onPressed: () async {
                        final tranfertResult = await _finapaySdkPlugin!
                            .initTransfert(context: context);

                        if (tranfertResult.status == RequestStatus.success) {
                          debugPrint(
                              "HOTE : Transfert réussi : ${tranfertResult.reference}");
                        } else {
                          debugPrint(
                              "HOTE : Transfert échoué : ${tranfertResult.message}");
                        }
                      },
                      child: const Text("Make new Transfert"),
                    ),
                  ]
                ],
              ),
      ),
    );
  }
}