Skip to main content

card_flutter 1.0.24 copy "card_flutter: ^1.0.24" to clipboard
card_flutter: ^1.0.24 copied to clipboard

Accept a payment with one or multiple payment methods securely. Powered by Tap Payments.

example/lib/main.dart

import 'package:card_flutter/card_flutter.dart';
import 'package:flutter/material.dart';

/// The card configuration, shared by the preload and the card screen. The
/// preloaded form is only used when both pass equal values.
const Map<String, dynamic> cardConfiguration = {
  'operator': {
    'publicKey': 'pk_test_YhUjg9PNT8oDlKJ1aE2fMRz7',
  },
  'scope': 'Token',
  'order': {
    'reference': '',
    'amount': 1,
    'description': '',
    'currency': 'KWD',
    'id': '',
  },
  'customer': {
    'nameOnCard': '',
    'editable': true,
    'contact': {
      'phone': {
        'number': '011',
        'countryCode': '+20',
      },
      'email': 'test@gmail.com',
    },
    'name': [
      {
        'middle': 'middle',
        'last': 'last',
        'lang': 'en',
        'first': 'first',
      }
    ],
  },
  'purpose': 'CHARGE',
  'transaction': {
    'metadata': {
      'id': '',
    },
    'paymentAgreement': {
      'contract': {
        'id': '',
      },
      'id': '',
    },
  },
  'invoice': {
    'id': '',
  },
  'merchant': {
    'id': '1124340',
  },
  'features': {
    'customerCards': {
      'saveCard': true,
      'autoSaveCard': true,
    },
    'alternativeCardInputs': {
      'cardScanner': true,
      'cardNFC': true,
    },
    'acceptanceBadge': true,
  },
  'acceptance': {
    'supportedSchemes': [],
    'supportedFundSource': [],
    'supportedPaymentAuthentications': ['3DS'],
  },
  'fieldVisibility': {
    'card': {
      'cvv': true,
      'cardHolder': true,
    },
  },
  'interface': {
    'powered': true,
    'loader': true,
    'edges': 'flat',
    'colorStyle': 'colored',
    'theme': 'light',
    'cardDirection': 'dynamic',
    'locale': 'en',
  },
  'redirect': {
    'url': '',
  },
  'post': {
    'url': '',
  },
};

const String prefillCardNumber = "5123450000000008";
const String prefillCardExpiry = "01/39";

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

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

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const HomeScreen(),
    );
  }
}

/// Stands for the screen before payment, for example where the payer picks the
/// amount. It preloads the card form, so it is ready when the card screen opens.
class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
    _preload();
  }

  void _preload() {
    TapCardSDK.preload(
      sdkConfiguration: cardConfiguration,
      cardNumber: prefillCardNumber,
      cardExpiry: prefillCardExpiry,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Plugin example app')),
      body: Center(
        child: FilledButton(
          onPressed: () async {
            await Navigator.of(context).push(
              MaterialPageRoute(builder: (_) => const CardViewScreen()),
            );
            // The form was used; preload a fresh one for the next payment.
            _preload();
          },
          child: const Text('Pay with card'),
        ),
      ),
    );
  }
}

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

  @override
  State<CardViewScreen> createState() => _CardViewScreenState();
}

class _CardViewScreenState extends State<CardViewScreen> {
  dynamic mCardSDKResponse;

  bool generateToken = false;

  bool showTapTokenButton = false;

  @override
  void initState() {
    showTapTokenButton = false;
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text("hoo hook,"),
          TapCardViewWidget(
            sdkConfiguration: cardConfiguration,
            onReady: () {
              setState(() {
                showTapTokenButton = true;
              });
            },
            onFocus: () {
              setState(() {
                generateToken = false;
              });
            },
            onSuccess: (String? success) {
              setState(() {
                mCardSDKResponse = success.toString();
                generateToken = false;
              });
            },
            onValidInput: (String? validInput) {
              setState(() {
                mCardSDKResponse = validInput.toString();
                generateToken = false;
              });
            },
            onHeightChange: (String? heightChange) {
              setState(() {
                mCardSDKResponse = heightChange.toString();
              });
            },
            onBinIdentification: (String? binIdentification) {
              setState(() {
                mCardSDKResponse = binIdentification.toString();
              });
            },
            onChangeSaveCard: (String? saveCard) {
              setState(() {
                mCardSDKResponse = "saveCard $saveCard ".toString();
              });
            },
            onError: (String? error) {
              setState(() {
                mCardSDKResponse = error.toString();
                generateToken = false;
              });
            },
            generateToken: generateToken,
            cardNumber: prefillCardNumber,
            cardExpiry: prefillCardExpiry,
            showLoading: true,
          ),
          const SizedBox(height: 10),
          Visibility(
            visible: showTapTokenButton,
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12),
              child: FilledButton(
                onPressed: () {
                  setState(() {
                    generateToken = true;
                  });
                },
                style: FilledButton.styleFrom(
                  fixedSize: Size(
                    MediaQuery.sizeOf(context).width * 0.96,
                    50,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                onHover: (bool? value) {},
                child: const Text(
                  "Get Tap Token",
                ),
              ),
            ),
          ),
          const SizedBox(height: 10),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: SingleChildScrollView(
                child: SelectableText(
                  mCardSDKResponse == null
                      ? ""
                      : "SDK RESPONSE : $mCardSDKResponse",
                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    color: Colors.black,
                    fontSize: 12,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
7
likes
150
points
1.65k
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Accept a payment with one or multiple payment methods securely. Powered by Tap Payments.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on card_flutter

Packages that implement card_flutter