flutterbudpay 0.0.9 copy "flutterbudpay: ^0.0.9" to clipboard
flutterbudpay: ^0.0.9 copied to clipboard

A Flutter plugin for making payments via BudPay Payment Gateway. Completely supports all platforms.

example/lib/main.dart

// ignore_for_file: library_private_types_in_public_api

// Import required packages.
import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutterbudpay/flutterbudpay.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'themeclass.dart';

// Define your backend URL and Budpay public key.
String backendUrl = '{YOUR_BACKEND_URL}';
String budpayPublicKey = '{YOUR_budpay_PUBLIC_KEY}';
const String appName = 'Budpay Example';

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

// MyApp is the main application widget.
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<MyApp> createState() => _MyAppState();

  // Function to get the state of the MyApp widget from any context.
  static _MyAppState of(BuildContext context) =>
      context.findAncestorStateOfType<_MyAppState>()!;
}

class _MyAppState extends State<MyApp> {
  // Default theme mode is system.
  ThemeMode _themeMode = ThemeMode.system;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: appName,
      debugShowCheckedModeBanner: false,
      themeMode: _themeMode,
      theme: ThemeClass.lightTheme,
      darkTheme: ThemeClass.darkTheme,
      home: const Newpage(),
    );
  }

  // Function to change the theme mode.
  void changeTheme(ThemeMode themeMode) {
    setState(() {
      _themeMode = themeMode;
    });
  }
}

// Check if the app is running in dark mode.
bool get isDarkMode {
  var brightness = SchedulerBinding.instance.window.platformBrightness;
  return brightness == Brightness.dark;
}

// Newpage is the main interface of the app.
class Newpage extends StatefulWidget {
  const Newpage({Key? key}) : super(key: key);
  @override
  _NewpageState createState() => _NewpageState();
}

class _NewpageState extends State<Newpage> {
  var currency = ["USD", "NGN"]; // Supported currencies.
  String dropdownvalue = 'NGN'; // Default currency.
  TextEditingController phoneController = TextEditingController(); // Controller for the amount text field.
  bool isloading = false; // Loading state for the checkout button.
  final plugin = Budpay(); // Instance of the Budpay plugin.
  final _formKey = GlobalKey<FormState>(); // Form key for validating the form.
  bool check = false; // Check state for the dark mode checkbox.

  @override
  void initState() {
    check = isDarkMode;
    plugin.initialize(
        publicKey: 'pk_test_oppvguyfnlna4f1eidl1bqrkxrxfszgdvgwq1q',
        secretKey: 'sk_test_genllkfsimb3wrqamv02hcqbr3fuo1alekr7cpy'); //Replace with your keys
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // final themeChange = Provider.of<DarkThemeProvider>(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color(0xff6C30F4),
        title: const Text("BUDPAY SDK SAMPLE"),
      ),
      body: Center(
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Checkbox(
                  value: check,
                  onChanged: (bool? value) {
                    setState(() {
                      check = !check;
                    });
                    if (!check) {
                      MyApp.of(context).changeTheme(ThemeMode.light);
                    } else {
                      MyApp.of(context).changeTheme(ThemeMode.dark);
                    }
                  }),
              const SizedBox(
                height: 20,
              ),
              DropdownButton(
                // Initial Value
                value: dropdownvalue,

                // Down Arrow Icon
                icon: const Icon(Icons.keyboard_arrow_down),

                // Array list of items
                items: currency.map((String items) {
                  return DropdownMenuItem(
                    value: items,
                    child: Text(items),
                  );
                }).toList(),
                // After selecting the desired option,it will
                // change button value to selected value
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownvalue = newValue!;
                  });
                },
              ),
              const SizedBox(
                height: 30,
              ),
              Container(
                decoration: const BoxDecoration(
                    color: Color(0xffF4F4F6),
                    borderRadius: BorderRadius.all(Radius.circular(15.0))),
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: TextFormField(
                  decoration: InputDecoration(
                    fillColor: const Color(0xffF4F4F6),
                    hintText: "1000",
                    hintStyle: TextStyle(
                        fontSize: 16.56,
                        fontWeight: FontWeight.w500,
                        color: isDarkMode ? null : Colors.black),
                    enabledBorder: const OutlineInputBorder(
                      borderRadius:
                           BorderRadius.all(Radius.circular(15.0)),
                      borderSide: BorderSide(color: Color(0xffF4F4F6)),
                    ),
                    focusedBorder: const OutlineInputBorder(
                      borderRadius:
                          BorderRadius.all(Radius.circular(15.0)),
                      borderSide: BorderSide(color: Color(0xffF4F4F6)),
                    ),
                    disabledBorder: const OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15.0)),
                      borderSide: BorderSide(color: Color(0xffF4F4F6)),
                    ),
                  ),
                  style: TextStyle(
                      fontSize: 16.56,
                      fontWeight: FontWeight.w500,
                      color: isDarkMode ? null : Colors.black),
                  controller: phoneController,
                  keyboardType: TextInputType.phone,
                  onChanged: (value) {
                    // widget.email(value);
                    if (value.isEmpty) {
                      setState(() {
                        // widget.emailfield(true);
                      });
                    } else {
                      setState(() {});
                    }
                  },
                  validator: (value) {
                    if (value!.isEmpty) {
                      return "This field can't be empty";
                    }

                    return null;
                  },
                ),
              ),
              const SizedBox(
                height: 50,
              ),
              GestureDetector(
                child: Container(
                  decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(15)),
                      color: Color(0xff6C30F4)),
                  height: 65,
                  width: MediaQuery.of(context).size.width - 70,
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  margin: const EdgeInsets.symmetric(
                      horizontal: 10, vertical: 10),
                  alignment: Alignment.center,
                  child: isloading
                      ? loadingWidget
                      : const Text(
                          "Checkout",
                          style: TextStyle(color: Colors.white, fontSize: 20),
                        ),
                ),
                onTap: () {
                  if (_formKey.currentState!.validate()) {
                    _handleCheckout(context);
                  }
                },
              )
            ],
          ),
        ),
      ),
    );
  }

  _handleCheckout(BuildContext context) async {
    setState(() => isloading = true);
    _formKey.currentState?.save();
    Charge charge = Charge()
      ..amount = int.parse(phoneController.text) // In base currency
      ..email = 'sdkapp@email.com'
      ..currency = dropdownvalue;
    // ..card = _getCardFromUI();

    charge.reference = _getReference();

    try {
      var response = await plugin.checkout(
        context,
        charge: charge,
      );
      log('Response = $response');
      setState(() => isloading = false);
      _updateStatus(response.reference, '$response');
    } catch (e) {
      setState(() => isloading = false);
      _showMessage("Check console for error");
      rethrow;
    }
  }

  String _getReference() {
    String platform;
    if (!kIsWeb) {
      if (Platform.isIOS) {
        platform = 'iOS';
      } else {
        platform = 'Android';
      }
    } else {
      platform = "WEB";
    }
    return 'ChargedFrom${platform}_${DateTime.now().millisecondsSinceEpoch}';
  }

  _updateStatus(String? reference, String message) {
    _showMessage('Reference: $reference \n Response: $message',
        const Duration(seconds: 7));
  }

  _showMessage(String message,
      [Duration duration = const Duration(seconds: 4)]) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text(message),
      duration: duration,
      action: SnackBarAction(
          label: 'CLOSE',
          onPressed: () =>
              ScaffoldMessenger.of(context).removeCurrentSnackBar()),
    ));
  }
}

const loadingWidget = SpinKitThreeBounce(
  color: Colors.white,
  size: 30.0,
);
5
likes
120
pub points
28%
popularity

Publisher

unverified uploader

A Flutter plugin for making payments via BudPay Payment Gateway. Completely supports all platforms.

Homepage

Documentation

API reference

License

MIT (LICENSE)

Dependencies

clipboard, device_info_plus, encrypt, flutter, gainer_crypto, google_fonts, http, intl, package_info_plus, platform_info, timer_count_down, webview_flutter

More

Packages that depend on flutterbudpay