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

Hyperswitch SDK for Flutter.

example/lib/main.dart

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

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

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

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

class _MyAppState extends State<MyApp> {
  String _response = '';
  String _clientSecret = '';
  bool isButtonEnabled = false;
  final _flutterHyperswitchPlugin = FlutterHyperswitch();

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

  Future<void> initPlatformState() async {
    try {
      var response = await http.post(
        Uri.parse("https://bscq1qxggd.execute-api.us-east-1.amazonaws.com/default/create-payment-intent"),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: jsonEncode({
          'amount': 6541,
          'currency': 'USD',
          'confirm': false,
          'capture_method': 'automatic',
          'authentication_type': 'no_three_ds',
          'customer_id': 'hyperswitch_sdk_demo_id',
          'description': 'Joseph First Crypto',
          'shipping': {
            'address': {
              'line1': '1467',
              'line2': 'Harrison Street',
              'line3': 'Harrison Street',
              'city': 'San Fransico',
              'state': 'California',
              'zip': '94122',
              'country': 'US',
              'first_name': 'joseph',
              'last_name': 'Doe',
            },
            'phone': {
              'number': '8056594427',
              'country_code': '+91',
            },
          },
          'billing': {
            'address': {
              'line1': '1467',
              'line2': 'Harrison Street',
              'line3': 'Harrison Street',
              'city': 'San Fransico',
              'state': 'California',
              'zip': '94122',
              'country': 'US',
              'first_name': 'joseph',
              'last_name': 'Doe',
            },
            'phone': {
              'number': '8056594427',
              'country_code': '+91',
            },
          },
          'metadata': {
            'order_details': {
              'product_name': 'Apple iphone 15',
              'quantity': 1,
            },
          },
          'business_country': 'US',
          'business_label': 'default',
        }),
      );

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

        Map<String, dynamic> responseBody = jsonDecode(response.body);
        Map<String, dynamic> initPaymentSheetResponse =
            await _flutterHyperswitchPlugin.initPaymentSheet({'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({
          'configuration': {
            'allowsDelayedPaymentMethods': true,
            'merchantDisplayName': 'Example, Inc.',
            'allowsPaymentMethodsRequiringShippingAddress': false,
            'googlePay': {
              'environment': 'Test',
              'countryCode': 'US',
              'currencyCode': 'US',
            },
          },
          'country': 'US',
          'clientSecret': _clientSecret
        }) ??
        {};

    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

Hyperswitch SDK for Flutter.

Homepage

License

unknown (license)

Dependencies

flutter, http, plugin_platform_interface

More

Packages that depend on flutter_hyperswitch