surfboard_ttp 1.2.1 copy "surfboard_ttp: ^1.2.1" to clipboard
surfboard_ttp: ^1.2.1 copied to clipboard

PlatformAndroid

Payments SDK for Surfboard Payments enabling payments for Flutter projects.

example/README.md

Example #

The below example shows example for initialisation, terminal registration, order creation and payment initiation

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:payment_example/service/evn.dart';
import 'package:payment_example/service/local_storage.dart';
import 'package:surfboard_ttp/surfboard_ttp.dart';
import 'package:surfboard_ttp/models/order_model/order_models.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await LocalStorage.initialize();
  initializeSDK();
  runApp(const MyApp());
}

initializeSDK() {
  SurfboardTTP stoked = SurfboardTTP(
      apiUrl: apiUrl,
      ttpBundleId: ttpBundleId,
      terminalId:
          LocalStorage.posTerminalIdBox?.get(StorageKeys.posTerminalId.name) ??
              '',
      partnerId: partnerId,
      merchantId: merchantId,
      storeId: storeId,
      appId: appId);
  stoked.setAuthToken(authToken: authToken);
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'SDK DEMO',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const Testing());
  }
}

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

  @override
  State<Testing> createState() => _TestingState();
}

class _TestingState extends State<Testing> with WidgetsBindingObserver {
  final Terminal terminal = Terminal();
  String transactionId = '';

  StreamController<AppLifecycleState> appLifecycleStream =
      StreamController<AppLifecycleState>();
  @override
  void initState() {
    super.initState();
    String posTerminalId =
        LocalStorage.posTerminalIdBox?.get(StorageKeys.posTerminalId.name) ??
            '';
    if (posTerminalId.isEmpty) {
      registerTerminal();
    }
    WidgetsBinding.instance.addObserver(this);
  }

  registerTerminal() {
    try {
      terminal.registerTerminal(appLifecycleStream.stream).then((value) {
        if (value.isNotEmpty) {
          LocalStorage.posTerminalIdBox
              ?.put(StorageKeys.posTerminalId.name, value);
        }
      });
    } catch (e) {
      debugPrint('registration failed $e');
    }
  }

  handleResume() {
    try {
      if (_orderId != 'No order found' && _orderId != 'completed') {
        payment.getTransactionDetails().then((value) {
          setState(() {
            transactionId = value.transactionId;
            if (transactionId.isNotEmpty) {
              _orderId = 'completed';
            }
          });
        });
      }
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  Payment payment = Payment();
  @override
  void didChangeAppLifecycleState(AppLifecycleState appState) {
    switch (appState) {
      case AppLifecycleState.resumed:
        appLifecycleStream.add(AppLifecycleState.resumed);
        handleResume();
        break;
      case AppLifecycleState.inactive:
        appLifecycleStream.add(AppLifecycleState.inactive);
        break;
      case AppLifecycleState.paused:
        appLifecycleStream.add(AppLifecycleState.paused);
        break;
      case AppLifecycleState.detached:
        appLifecycleStream.add(AppLifecycleState.detached);
        break;
    }
  }

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

  LineItem orderLines = LineItem(
    id: '1234',
    quantity: 1,
    name: 'item1',
    itemAmount: ItemAmount(
      regular: 1000,
      total: 1000,
      currency: Currency.sek,
      tax: [
        Tax(
          amount: 100,
          percentage: 10,
          type: TaxType.vat,
        ),
      ],
    ),
  );

  String _orderId = 'No order found';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'PaymentSDK',
          style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        ),
      ),
      body: Center(
        child: ListView(
          children: <Widget>[
            Text('current order: $_orderId'),
            if (transactionId.isNotEmpty) Text('transactionId: $transactionId'),
            ElevatedButton(
                onPressed: () {
                  Order order = Order(orderType: OrderType.purchase);
                  order.addLineItem(orderLines).createOrder().then((value) {
                    setState(() {
                      _orderId = value.orderId;
                    });
                  });
                },
                child: const Text('Create Order')),
            ElevatedButton(
                onPressed: () {
                  payment.initiatePayment(
                    _orderId,
                  );
                },
                child: const Text('Initiate Payment'))
          ],
        ),
      ),
    );
  }
}
4
likes
120
points
3
downloads

Publisher

verified publishersurfboardpayments.com

Weekly Downloads

Payments SDK for Surfboard Payments enabling payments for Flutter projects.

Homepage

Documentation

API reference

License

unknown (license)

Dependencies

android_id, android_intent_plus, device_apps, device_info_plus, flutter, http, package_info_plus

More

Packages that depend on surfboard_ttp