bbps_sdk_flutter 0.0.2 copy "bbps_sdk_flutter: ^0.0.2" to clipboard
bbps_sdk_flutter: ^0.0.2 copied to clipboard

Flutter plugin for Juspay BBPS (Bharat BillPay System) bill payments

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';

import 'package:uuid/uuid.dart';
import 'package:bbps_sdk_flutter/bbps_sdk_flutter.dart';

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

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

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

class _MyAppState extends State<MyApp> {
  String _lastEvent = 'No events';
  late StreamSubscription<BbpsEvent> _eventSubscription;
  bool _isInitialized = false;

  String _selectedAction = 'BBPS_PAYMENT';

  final List<String> _actions = [
    'BBPS_PAYMENT',
    'SET_TXN_STATUS',
    'BBPS_BILLERS_LIST',
    'BBPS_LIST_TXN',
    'BBPS_LIST_PENDING_BILLS',
    'GENERATE_KEY',
  ];

  // Demo values
  final String _agentId = 'YB71YB72MOB511066132';
  final String _mobile = '9889993924';
  final String _sampleToken =
      'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IllCNzFZQjcyTU9CNTExMDY2MTMyIn0.eyJtb2JpbGUiOiI5MDEwMjAzMDQwIiwiZGV2aWNlX2lkIjoiZGV2aWNlMDAxIiwiaWF0IjoxNzc5Mjc0OTIwfQ.G9grwYRWPAQTuWVxykrwNl23iCebv55HMbEo7pg7jcVV69NhVZiMXWsPmIFG2wlYYWZJBbQ5yPH40lqSRaOfkENbugcku7eGel2WNulDvCKiZmnqmtKNloj11LE4Ka-IbFghAEid1ONLDgYixtzR7kN8nzQSrmltQOnK1z3nUN6-a7OacFJ0bH2Wnz0cmZ_iTQk4flnCHbQuOVF-5XG6OzvRdRgGE1_-C7lsCMGmyRgBaDUjM1c8qQptn3bLLgs9h-MlBFp4Std-6NW77nNz6aaYAE_s0ovJ0N2dunsLDcr11XHaxS2Lng5ysQSzeVYihby3r_vLkhFR0rgpEK3FDw';
  final Uuid _uuid = Uuid();

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

  @override
  void dispose() {
    _eventSubscription.cancel();
    super.dispose();
  }

  void _listenToEvents() {
    _eventSubscription = BbpsFlutter.eventStream.listen((event) {
      setState(() {
        _lastEvent = '${event.event}: ${jsonEncode(event.payload)}';
      });
      debugPrint('BBPS Event: ${event.event}, Payload: ${event.payload}');
    });
  }

  Future<void> _createService() async {
    try {
      await BbpsFlutter.createService(params: {'clientId': 'stock'});
      _showSnackBar('Service Created Successfully');
    } catch (e) {
      _showSnackBar('Error creating service: $e');
    }
  }

  Future<void> _initiate() async {
    try {
      await BbpsFlutter.initiate(
        params: {
          'action': 'initiate',
          'agentId': _agentId,
          'mobile': _mobile,
          'deviceId': '356152103690000',
          'clientId': 'stock',
          'issuingCou': 'yes_biz',
        },
      );
      setState(() {
        _isInitialized = true;
      });
      _showSnackBar('Initiated Successfully');
    } catch (e) {
      _showSnackBar('Error initiating: $e');
    }
  }

  Map<String, dynamic> _buildPayload(String action) {
    switch (action) {
      case 'BBPS_PAYMENT':
        return {
          'action': action,
          'agentId': _agentId,
          'authToken': _sampleToken,
        };

      case 'SET_TXN_STATUS':
        final paymentParam = {};
        final paymentParams = [paymentParam];
        final lastTxnRefId = _uuid.v4();

        final paymentDetails = {
          'payeeName': 'Neha Jain',
          'txnAmount': '100.00',
          'txnRefId': lastTxnRefId,
          'paymentMode': 'UPI',
          'custConvFee': '0',
          'paymentParams': paymentParams,
        };

        final data = {
          'response': 'Success',
          'note': 'Sending Money',
          'bbpsTxnId': 'demo-txn-id-${DateTime.now().millisecondsSinceEpoch}',
          'paymentDetails': paymentDetails,
        };

        return {'mobile': _mobile, 'action': 'SET_TXN_STATUS', 'data': data};

      case 'BBPS_BILLERS_LIST':
        final category = {
          'subCategories': <String>[],
          'categoryName': 'Broadband Postpaid',
          'categoryIcon': '',
        };
        return {'action': action, 'category': category};

      case 'BBPS_LIST_TXN':
        return {'action': action, 'offset': 0, 'limit': 15};

      case 'BBPS_LIST_PENDING_BILLS':
        return {'action': action};

      case 'GENERATE_KEY':
        return {
          'action': action,
          'agentId': _agentId,
          'authToken': _sampleToken,
        };

      default:
        return {'action': action};
    }
  }

  Future<void> _process() async {
    try {
      final payload = _buildPayload(_selectedAction);
      debugPrint('PAWAN >>> Process payload: ${jsonEncode(payload)}');
      final result = await BbpsFlutter.process(params: payload);
      _showSnackBar('Process result: $result');
    } catch (e) {
      _showSnackBar('Error processing: $e');
    }
  }

  Future<void> _onBackPressed() async {
    try {
      final result = await BbpsFlutter.onBackPressed();
      _showSnackBar('Back pressed handled: $result');
    } catch (e) {
      _showSnackBar('Error: $e');
    }
  }

  Future<void> _terminate() async {
    try {
      await BbpsFlutter.terminate();
      setState(() {
        _isInitialized = false;
      });
      _showSnackBar('BBPS Terminated');
    } catch (e) {
      _showSnackBar('Error: $e');
    }
  }

  void _showSnackBar(String message) {
    if (!mounted) return;
    final scaffoldMessenger = ScaffoldMessenger.maybeOf(context);
    if (scaffoldMessenger != null) {
      scaffoldMessenger.showSnackBar(SnackBar(content: Text(message)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('BBPS Flutter Example')),
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                'Status: ${_isInitialized ? 'Initialized' : 'Not Initialized'}',
                style: TextStyle(
                  fontSize: 14,
                  color: _isInitialized ? Colors.green : Colors.red,
                ),
              ),
              const SizedBox(height: 16),
              const Text(
                'Last Event:',
                style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
              ),
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Text(_lastEvent, style: const TextStyle(fontSize: 12)),
              ),
              const SizedBox(height: 24),
              ElevatedButton(
                onPressed: _createService,
                child: const Text('Create Service'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _isInitialized ? null : _initiate,
                child: const Text('Initiate'),
              ),
              const SizedBox(height: 16),
              const Divider(),
              const Text(
                'Process Action:',
                style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              DropdownButtonFormField<String>(
                value: _selectedAction,
                decoration: const InputDecoration(
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.symmetric(
                    horizontal: 12,
                    vertical: 8,
                  ),
                ),
                items: _actions.map((action) {
                  return DropdownMenuItem(
                    value: action,
                    child: Text(action, style: const TextStyle(fontSize: 13)),
                  );
                }).toList(),
                onChanged: _isInitialized
                    ? (value) {
                        setState(() {
                          _selectedAction = value!;
                        });
                      }
                    : null,
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _isInitialized ? _process : null,
                child: Text('Process: $_selectedAction'),
              ),
              const SizedBox(height: 16),
              const Divider(),
              ElevatedButton(
                onPressed: _isInitialized ? _onBackPressed : null,
                child: const Text('On Back Pressed'),
              ),
              const SizedBox(height: 8),
              ElevatedButton(
                onPressed: _isInitialized ? _terminate : null,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.red,
                  foregroundColor: Colors.white,
                ),
                child: const Text('Terminate'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
160
points
117
downloads

Documentation

API reference

Publisher

verified publisherapp-dev.juspay.in

Weekly Downloads

Flutter plugin for Juspay BBPS (Bharat BillPay System) bill payments

Homepage
Repository (GitHub)
View/report issues

License

AGPL-3.0 (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on bbps_sdk_flutter

Packages that implement bbps_sdk_flutter