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

Flutter Hyperswitch: Simplifying payment integration for Flutter apps with seamless API interactions and customizable UI.

example/lib/main.dart

import 'dart:convert';
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_hyperswitch/flutter_hyperswitch.dart';
import 'package:http/http.dart' as http;

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

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

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

class _MyAppState extends State<MyApp> {
  final String _endpoint =
      Platform.isAndroid ? "http://10.0.2.2:5252" : "http://localhost:5252";
  final _flutterHyperswitchPlugin = FlutterHyperswitch();
  bool isButtonEnabled = false;
  String _response = '';

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

  Future<void> initPlatformState() async {
    try {
      var response =
          await http.get(Uri.parse("$_endpoint/create-payment-intent"));

      if (response.statusCode == 200) {
        print(response.body);

        Map<String, dynamic> responseBody = jsonDecode(response.body);
        Map<String, dynamic> initPaymentSheetResponse =
            await _flutterHyperswitchPlugin.initPaymentSheet(PaymentSheetParams(
                    publishableKey: responseBody['publishableKey'],
                    clientSecret: responseBody['clientSecret'])) ??
                {};

        if (initPaymentSheetResponse["type"] == "success") {
          setState(() {
            isButtonEnabled = true;
          });
        }
      } else {
        _response = "API Call Failed";
      }
    } catch (error) {
      _response = "API Call Failed";
    }
  }

  Future<void> onPress() async {
    Map<String, dynamic> presentPaymentSheetResponse =
        await _flutterHyperswitchPlugin.presentPaymentSheet() ?? {};

    print(presentPaymentSheetResponse);

    if (!mounted) return;

    setState(() {
      _response = presentPaymentSheetResponse["message"];
      if (presentPaymentSheetResponse["type"] != "cancelled") {
        isButtonEnabled = false;
        initPlatformState();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(
              child: ElevatedButton(
                  onPressed: isButtonEnabled ? onPress : null,
                  child: Text(
                      isButtonEnabled ? "Open Payment Sheet" : "Loading ...")),
            ),
            const SizedBox(height: 20),
            Center(
              child: Text(_response),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
0
points
1.28k
downloads

Publisher

verified publisherhyperswitch.io

Weekly Downloads

Flutter Hyperswitch: Simplifying payment integration for Flutter apps with seamless API interactions and customizable UI.

Homepage

License

unknown (license)

Dependencies

flutter, http, plugin_platform_interface

More

Packages that depend on flutter_hyperswitch