spare_link 0.1.0-beta.2 copy "spare_link: ^0.1.0-beta.2" to clipboard
spare_link: ^0.1.0-beta.2 copied to clipboard

Flutter SDK for Spare's redirect-based open-banking payment flows. Provides bank selection, consent orchestration, crash-safe session recovery, and sandbox/production support for KSA, UAE, and BAH.

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 all three SpareLink entry points:
///   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 {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    // Recover any payment session that was interrupted by a crash or force-quit.
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await SpareLink.resumeIfNeeded(
        config: SpareLinkResumeConfig(
          environment: PaymentEnvironment.sandbox,
          tenant: SpareApiTenant.uae,
        ),
        callbacks: SpareLinkCallbacks(
          onExit: (result) => print('recovered: ${result.status.name}'),
        ),
      );
    });
  }

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

  /// Forward every payment-return URI to the SDK.
  /// Replace `'yourapp'` with your registered URL scheme.
  @override
  Future<bool> didPushRouteInformation(
    RouteInformation routeInformation,
  ) async {
    final uri = routeInformation.uri;
    if (uri.scheme == 'yourapp') {
      SpareLink.handleRedirect(uri);
      return true;
    }
    return super.didPushRouteInformation(routeInformation);
  }

  Future<void> _startPayment() async {
    // Obtain `linkToken` from your backend before calling SpareLink.start.
    // See the Spare Link API docs for how to create a payment link token.
    final result = await SpareLink.start(
      context,
      config: SpareLinkConfig(
        linkToken: 'your-session-token',
        environment: PaymentEnvironment.sandbox,
        tenant: SpareApiTenant.uae,
      ),
      callbacks: SpareLinkCallbacks(
        onEvent: (event) => print('event: ${event.name}'),
        onSuccess: (exchangeCode, linkSessionId) =>
            print('success — exchangeCode: $exchangeCode'),
        onExit: (result) => print('exit: ${result.status.name}'),
      ),
    );

    switch (result) {
      case PaymentSuccess(:final exchangeCode):
        // Exchange the code with your backend to confirm the payment.
        print('Payment succeeded — exchange code: $exchangeCode');
      case PaymentCancelled():
        print('Payment cancelled by user');
      case PaymentFailed(:final errorCode):
        print('Payment failed — error: $errorCode');
      case PaymentAbandoned():
        print('Payment abandoned');
    }
  }

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

Publisher

unverified uploader

Weekly Downloads

Flutter SDK for Spare's redirect-based open-banking payment flows. Provides bank selection, consent orchestration, crash-safe session recovery, and sandbox/production support for KSA, UAE, and BAH.

Homepage
Repository (GitHub)
View/report issues

Topics

#payments #open-banking #fintech #sdk

License

unknown (license)

Dependencies

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

More

Packages that depend on spare_link