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

A Flutter plugin for Zoho Payments Checkout.

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ZohoPayments SDK Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF0066CC)),
        useMaterial3: true,
      ),
      home: const HomeScreen(),
    );
  }
}

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final _sdk = ZohoPaymentsFlutterSdk();

  // WARNING: Replace with your actual credentials for testing.
  // Never commit real API keys to version control.
  static const _apiKey = 'YOUR_API_KEY';
  static const _accountId = 'YOUR_ACCOUNT_ID';
  static const _paymentSessionId = 'YOUR_PAYMENT_SESSION_ID';
  static const _domain = ZohoPaymentsDomain.india;
  static const _environment = ZohoPaymentsEnvironment.live;

  bool _loading = false;
  ZohoPaymentsResult? _result;

  Future<void> _openCheckout() async {
    setState(() {
      _loading = true;
      _result = null;
    });

    try {
      await _sdk.initialize(apiKey: _apiKey, accountId: _accountId);

      final result = await _sdk.showCheckout(
        const ZohoPaymentsCheckoutOptions(paymentSessionId: _paymentSessionId),
        domain: _domain,
        environment: _environment,
      );

      if (mounted) {
        setState(() {
          _result = result;
          _loading = false;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _result = ZohoPaymentsFailure(code: 'ERROR', message: e.toString());
          _loading = false;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ZohoPayments SDK Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                        'API Key: ${_apiKey.length > 4 ? '${'*' * (_apiKey.length - 4)}${_apiKey.substring(_apiKey.length - 4)}' : _apiKey}'),
                    const SizedBox(height: 4),
                    const Text('Account ID: $_accountId'),
                    const SizedBox(height: 4),
                    Text('Domain: ${_domain.name}'),
                    const SizedBox(height: 4),
                    Text('Environment: ${_environment.name}'),
                    const SizedBox(height: 4),
                    const Text('Session ID: $_paymentSessionId'),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 24),
            FilledButton.icon(
              onPressed: _loading ? null : _openCheckout,
              icon: _loading
                  ? const SizedBox(
                      width: 18,
                      height: 18,
                      child: CircularProgressIndicator(
                        strokeWidth: 2,
                        color: Colors.white,
                      ),
                    )
                  : const Icon(Icons.open_in_new),
              label: Text(_loading ? 'Opening Checkout…' : 'Open Checkout'),
              style: FilledButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 14),
              ),
            ),
            if (_result != null) ...[
              const SizedBox(height: 24),
              _ResultCard(result: _result!),
            ],
          ],
        ),
      ),
    );
  }
}

class _ResultCard extends StatelessWidget {
  const _ResultCard({required this.result});

  final ZohoPaymentsResult result;

  @override
  Widget build(BuildContext context) {
    if (result is ZohoPaymentsSuccess) {
      final s = result as ZohoPaymentsSuccess;
      return Card(
        color: Colors.green.shade50,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
          side: BorderSide(color: Colors.green.shade300),
        ),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Icon(Icons.check_circle, color: Colors.green.shade700),
                  const SizedBox(width: 8),
                  Text(
                    'Payment Successful',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.green.shade700,
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
              const Divider(height: 20),
              _ResultRow(label: 'Payment ID', value: s.paymentId),
              _ResultRow(label: 'Signature', value: s.signature),
              if (s.mandateId != null)
                _ResultRow(label: 'Mandate ID', value: s.mandateId!),
            ],
          ),
        ),
      );
    } else {
      final f = result as ZohoPaymentsFailure;
      return Card(
        color: Colors.red.shade50,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
          side: BorderSide(color: Colors.red.shade300),
        ),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Icon(Icons.error_outline, color: Colors.red.shade700),
                  const SizedBox(width: 8),
                  Text(
                    'Payment Failed',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.red.shade700,
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
              const Divider(height: 20),
              _ResultRow(label: 'Code', value: f.code),
              _ResultRow(label: 'Message', value: f.message),
            ],
          ),
        ),
      );
    }
  }
}

class _ResultRow extends StatelessWidget {
  const _ResultRow({required this.label, required this.value});

  final String label;
  final String value;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 100,
            child: Text(
              '$label:',
              style: const TextStyle(fontWeight: FontWeight.w600),
            ),
          ),
          Expanded(child: Text(value, softWrap: true)),
        ],
      ),
    );
  }
}