spare_link 1.0.1 copy "spare_link: ^1.0.1" to clipboard
spare_link: ^1.0.1 copied to clipboard

Flutter SDK for Spare Link open-banking payments. Presents the payment flow, hands bank authorization to a real browser, and reports the payer back through your deep link.

example/lib/main.dart

// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:spare_link/spare_link.dart';

void main() => runApp(const ExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: HomePage());
  }
}

/// Demonstrates the instance-based SpareLink API:
///   1. [SpareLink.start]          — launch the payment flow
///   2. [SpareLink.handleRedirect] — forward deep-link return URIs
///   3. [SpareLink.resumeIfNeeded] — recover a session after a crash/cold start
class HomePage extends StatefulWidget {
  const HomePage({super.key});

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

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  late final SpareLink _spareLink;

  @override
  void initState() {
    super.initState();
    _spareLink = SpareLink(
      environment: PaymentEnvironment.sandbox,
      tenant: SpareApiTenant.uae,
    );
    _spareLink.events.listen((event) => print('event: $event'));
    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      final recovery = await _spareLink.resumeIfNeeded();
      switch (recovery) {
        case SessionRecovered(:final result):
          print('recovered: ${result.status.name}');
        case NoRecoveryNeeded(:final reason):
          print('no recovery: $reason');
      }
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _spareLink.dispose();
    super.dispose();
  }

  @override
  Future<bool> didPushRouteInformation(
    RouteInformation routeInformation,
  ) async {
    final uri = routeInformation.uri;
    if (uri.scheme == 'yourapp') {
      if (await _spareLink.handleRedirect(uri)) return true;
    }
    return super.didPushRouteInformation(routeInformation);
  }

  Future<void> _startPayment() async {
    // Your backend creates the payment request with `channel: 'sdk_flutter'`
    // and hands back the reference it returns.
    final session = await _spareLink.start(
      navigator: Navigator.of(context),
      reference: 'your-payment-request-reference',
    );

    final result = await session.result;

    // The deep link this came from is interceptable, so read the authoritative
    // record from your backend before telling the payer anything.
    debugPrint('${result.status.name} for ${result.reference}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('spare_link example')),
      body: Center(
        child: FilledButton(
          onPressed: _startPayment,
          child: const Text('Start Payment'),
        ),
      ),
    );
  }
}
1
likes
150
points
132
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter SDK for Spare Link open-banking payments. Presents the payment flow, hands bank authorization to a real browser, and reports the payer back through your deep link.

Homepage
Repository (GitHub)
View/report issues

Topics

#payments #open-banking #fintech #sdk

License

BSD-3-Clause (license)

Dependencies

device_info_plus, flutter, flutter_inappwebview, http, logging, shared_preferences, url_launcher, uuid

More

Packages that depend on spare_link