beans_merchant_sdk 1.0.0 copy "beans_merchant_sdk: ^1.0.0" to clipboard
beans_merchant_sdk: ^1.0.0 copied to clipboard

Beans Merchant API Library

example/lib/main.dart

import 'dart:async';

import 'package:beans_merchant_sdk/beans_merchant_sdk.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      darkTheme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color.fromRGBO(87, 55, 179, 1),
          brightness: Brightness.dark,
        ),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late BeansMerchantSdk sdk;

  final apiKeyController = TextEditingController(
    text: "2B8F-1D7E-C3A1-9B2D",
  );
  final stellarAccountIdController = TextEditingController(
    text: "GCXQCEJD4T56UQVM3432BCUBD4ULYIZUDUFVGYYCZCOLL36UVNNLGBB5",
  );
  final amountController = TextEditingController();
  final memoController = TextEditingController();
  final maxAllowedPaymentsController = TextEditingController();
  final webhookUrlController = TextEditingController();
  final customEnvironmentController = TextEditingController();

  String selectedEnvironment = 'staging';
  List<String> environments = [
    'staging',
    'production',
  ];

  List<DropdownMenuItem<String>>? stellarCurrenciesDropdownItems;
  String? selectedCurrency;

  String? successMessage;
  String? errorMessage;

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

  void _initSdk() {
    String apiDomain;
    switch (selectedEnvironment) {
      case 'staging':
        apiDomain = BeansMerchantSdkDomain.staging;
        break;
      case 'production':
        apiDomain = BeansMerchantSdkDomain.production;
        break;
      default:
        apiDomain = BeansMerchantSdkDomain.staging;
    }

    sdk = BeansMerchantSdk(
      apiDomain: apiDomain,
      apiKey: apiKeyController.text,
    );

    unawaited(
      _loadInitialData(),
    );
  }

  Future<void> _loadInitialData() async {
    if (apiKeyController.text.isEmpty ||
        stellarAccountIdController.text.isEmpty) {
      return;
    }
    errorMessage = '';
    setState(() {});

    try {
      final data = await sdk.fetchStellarCurrencies(
        stellarAccountIdController.text,
      );
      stellarCurrenciesDropdownItems = [
        for (final currency in data.stellarCurrencies)
          DropdownMenuItem<String>(
            value: currency.id,
            child: Text('${currency.name} (${currency.code})'),
          ),
      ];
      setState(() {});
    } catch (e) {
      errorMessage = 'Error fetching stellar currencies: $e';
      setState(() {});
    }
  }

  Future<void> generateQrCode() async {
    final theme = Theme.of(context);

    errorMessage = '';
    setState(() {});

    try {
      final data = await sdk.generateSvgQrCode(
        stellarAccountIdController.text,
        selectedCurrency!,
        double.parse(amountController.text),
        memoController.text,
        maxAllowedPayments: maxAllowedPaymentsController.text.isEmpty
            ? null
            : int.parse(maxAllowedPaymentsController.text),
        webhookUrl: webhookUrlController.text.isEmpty
            ? null
            : webhookUrlController.text,
      );
      final svgString = data.svgQrCode
          .replaceAll('#FFFFFF', theme.colorScheme.surface.toString())
          .replaceAll('#000000', '#FFFFFF');
      showDialog(
        // ignore: use_build_context_synchronously
        context: context,
        builder: (context) {
          return AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('QR Code generated successfully'),
                SvgPicture.string(svgString),
              ],
            ),
          );
        },
      );
    } catch (error) {
      errorMessage = 'Error generating QR Code: $error';
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Merchant SDK example'),
      ),
      body: Center(
        child: ConstrainedBox(
          constraints: const BoxConstraints(
            maxWidth: 600,
          ),
          child: Padding(
            padding: const EdgeInsets.all(32.0),
            child: ListView(
              children: [
                DropdownButtonFormField2(
                  decoration: InputDecoration(
                    labelText: 'Select Environment',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  value: selectedEnvironment,
                  items: [
                    for (final env in environments)
                      DropdownMenuItem<String>(
                        value: env,
                        child: Text(env),
                      ),
                  ],
                  onChanged: (_) {
                    apiKeyController.clear();
                    stellarAccountIdController.clear();
                    _loadInitialData();
                  },
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: apiKeyController,
                  decoration: const InputDecoration(
                    labelText: 'API Key',
                    border: OutlineInputBorder(),
                  ),
                  onChanged: (_) {
                    stellarAccountIdController.clear();
                    _loadInitialData();
                  },
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: stellarAccountIdController,
                  decoration: const InputDecoration(
                    labelText: 'Destination Stellar Account ID',
                    border: OutlineInputBorder(),
                  ),
                  onChanged: (_) {
                    _loadInitialData();
                  },
                ),
                const SizedBox(height: 16),
                DropdownButtonFormField2<String>(
                  decoration: InputDecoration(
                    labelText: 'Select Currency',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  value: selectedCurrency,
                  items: stellarCurrenciesDropdownItems,
                  onChanged: (value) {
                    selectedCurrency = value;
                    setState(() {});
                  },
                  hint: const Text('Select Currency'),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: amountController,
                  decoration: const InputDecoration(
                    labelText: 'Amount',
                    border: OutlineInputBorder(),
                  ),
                  keyboardType: TextInputType.number,
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: memoController,
                  decoration: const InputDecoration(
                    labelText: 'Memo',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: maxAllowedPaymentsController,
                  decoration: const InputDecoration(
                    labelText:
                        'Max allowed payments (optional) (unlimited is -1) (defaults is 1)',
                    border: OutlineInputBorder(),
                  ),
                  keyboardType: TextInputType.number,
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: webhookUrlController,
                  decoration: const InputDecoration(
                    labelText: 'Webhook URL (optional)',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: generateQrCode,
                  child: const Text('Generate QR Code'),
                ),
                if (errorMessage != null) ...[
                  const SizedBox(height: 16),
                  Text(
                    errorMessage!,
                    style: TextStyle(
                      color: Theme.of(context).colorScheme.error,
                    ),
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}
0
likes
0
points
85
downloads

Documentation

Documentation

Publisher

verified publisherbeansapp.com

Weekly Downloads

Beans Merchant API Library

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on beans_merchant_sdk