buzapay_sdk 0.1.1 copy "buzapay_sdk: ^0.1.1" to clipboard
buzapay_sdk: ^0.1.1 copied to clipboard

Flutter SDK for Buzapay Payment Gateway. Accept payments with Card, Bank Transfer and Stablecoins (USDT/USDC).

example/lib/main.dart

import 'package:buzapay_sdk/buzapay_sdk.dart';
import 'package:flutter/material.dart';

void main() {
  // Initialize Buzapay SDK
  Buzapay.initialize(
    config: BuzapayConfig(
      merchantId: 'YOUR_MERCHANT_ID',
      environment: BuzapayEnvironment.sandbox,
      enableLogging: true, // Set to false in production
    ),
  );

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Buzapay SDK Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF1A1A2E)),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _formKey = GlobalKey<FormState>();
  final _emailController = TextEditingController(text: 'customer@example.com');
  final _phoneController = TextEditingController(text: '08012345678');
  final _amountController = TextEditingController(text: '5000');

  String _selectedCurrency = 'NGN';
  String _statusMessage = '';
  bool _isLoading = false;

  @override
  void dispose() {
    _emailController.dispose();
    _phoneController.dispose();
    _amountController.dispose();
    super.dispose();
  }

  void _showMessage(String message, {bool isError = false}) {
    setState(() => _statusMessage = message);
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: isError ? Colors.red : Colors.green,
      ),
    );
  }

  Future<void> _handleCheckout() async {
    if (!_formKey.currentState!.validate()) return;

    setState(() => _isLoading = true);

    try {
      final result = await Buzapay.instance.checkout(
        context: context,
        amount: int.parse(_amountController.text),
        currency: _selectedCurrency,
        email: _emailController.text,
        phoneNumber: _phoneController.text,
        redirectUrl: 'https://example.com/payment/callback',
        narration: 'Payment from Buzapay SDK Example',
        onSuccess: (reference) {
          _showMessage('Payment successful! Reference: $reference');
        },
        onError: (error) {
          _showMessage('Payment failed: ${error.message}', isError: true);
        },
        onCancel: () {
          _showMessage('Payment cancelled', isError: true);
        },
      );

      if (result == PaymentResult.success) {
        debugPrint('Payment completed successfully');
      }
    } on BuzapayException catch (e) {
      _showMessage('Error: ${e.message}', isError: true);
    } finally {
      if (mounted) {
        setState(() => _isLoading = false);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Buzapay SDK Example'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // Logo/Header
              const Icon(
                Icons.payment,
                size: 80,
                color: Color(0xFF1A1A2E),
              ),
              const SizedBox(height: 16),
              const Text(
                'Test the Buzapay payment SDK',
                style: TextStyle(color: Colors.grey),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 32),

              // Email field
              TextFormField(
                controller: _emailController,
                decoration: const InputDecoration(
                  labelText: 'Email',
                  prefixIcon: Icon(Icons.email),
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.emailAddress,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter email';
                  }
                  if (!value.contains('@')) {
                    return 'Please enter a valid email';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),

              // Phone field
              TextFormField(
                controller: _phoneController,
                decoration: const InputDecoration(
                  labelText: 'Phone Number',
                  prefixIcon: Icon(Icons.phone),
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.phone,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter phone number';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),

              // Amount and Currency row
              Row(
                children: [
                  Expanded(
                    flex: 2,
                    child: TextFormField(
                      controller: _amountController,
                      decoration: const InputDecoration(
                        labelText: 'Amount (minor units)',
                        prefixIcon: Icon(Icons.money),
                        border: OutlineInputBorder(),
                        helperText: 'e.g., 5000 = ₦50.00',
                      ),
                      keyboardType: TextInputType.number,
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return 'Enter amount';
                        }
                        if (int.tryParse(value) == null) {
                          return 'Invalid number';
                        }
                        return null;
                      },
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: DropdownButtonFormField<String>(
                      initialValue: _selectedCurrency,
                      decoration: const InputDecoration(
                        labelText: 'Currency',
                        border: OutlineInputBorder(),
                      ),
                      items: const [
                        DropdownMenuItem(value: 'NGN', child: Text('NGN')),
                        DropdownMenuItem(value: 'USD', child: Text('USD')),
                      ],
                      onChanged: (value) {
                        setState(() => _selectedCurrency = value!);
                      },
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 32),

              // Pay button
              SizedBox(
                height: 56,
                child: ElevatedButton(
                  onPressed: _isLoading ? null : _handleCheckout,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF1A1A2E),
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: _isLoading
                      ? const SizedBox(
                          width: 24,
                          height: 24,
                          child: CircularProgressIndicator(
                            strokeWidth: 2,
                            valueColor: AlwaysStoppedAnimation(Colors.white),
                          ),
                        )
                      : const Text(
                          'Pay with Buzapay',
                          style: TextStyle(fontSize: 18),
                        ),
                ),
              ),
              const SizedBox(height: 24),

              // Status message
              if (_statusMessage.isNotEmpty)
                Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors.grey.shade100,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    _statusMessage,
                    textAlign: TextAlign.center,
                  ),
                ),

              const SizedBox(height: 32),

              // Alternative: Using BuzapayCheckoutButton
              const Divider(),
              const SizedBox(height: 16),
              const Text(
                'Or use the pre-built button:',
                style: TextStyle(fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 16),
              Center(
                child: BuzapayCheckoutButton(
                  amount: 5000,
                  currency: 'NGN',
                  email: 'customer@example.com',
                  phoneNumber: '08012345678',
                  redirectUrl: 'https://example.com/callback',
                  generatePaymentLink: (request) =>
                      Buzapay.instance.generatePaymentLink(
                    email: request.email,
                    phoneNumber: request.phoneNumber,
                    merchantReference: request.merchantReference,
                    amount: request.amount,
                    currency: request.currency,
                    narration: request.narration,
                    redirectUrl: request.redirectUrl,
                  ),
                  onSuccess: (ref) =>
                      _showMessage('Button payment success: $ref'),
                  onError: (e) => _showMessage(
                      'Button payment error: ${e.message}',
                      isError: true),
                  buttonText: 'Quick Pay ₦50.00',
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
160
points
27
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

Flutter SDK for Buzapay Payment Gateway. Accept payments with Card, Bank Transfer and Stablecoins (USDT/USDC).

Repository (GitHub)
View/report issues

Topics

#payments #payment-gateway #fintech #checkout

License

MIT (license)

Dependencies

flutter, http, webview_flutter

More

Packages that depend on buzapay_sdk