flutter_culqi 0.5.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

flutter_culqi #

A Flutter Package to allow the integration of Android/iOS apps with the Culqi API.

How to install it? #

Add the package to pubspec.yaml:

flutter_culqi : ^0.5.2

Import and use it! :)

import 'package:culqi_flutter/culqi_flutter.dart';

What flutter_culqi do? #

This package comes with two main components:

  1. An easy way to request the token from Culqi
  2. A widget to make faster the integration

1. An easy way to request the token from Culqi: #

flutter_culqi provides you with a simple interface to request and get the token from Culqi. It's easy as:

  • Create an String variable called _publicKey, assign to it the public key obtained from your panel at Culqi.
      final String _publicKey = 'INSERT_YOUR_PUBLIC_KEY_HERE';
    
  • Create a CulqiCard object (all fields are required).
      CulqiCard card = CulqiCard(
          cardNumber: '4111111111111111',
          cvv: '123',
          expirationMonth: 9,
          expirationYear: 2020,
          email:'test@testmail.com'
      );
    
  • Create a CulqiTokenizer object. This will keep the CulqiCard created above.
      CulqiTokenizer tokenizer = CulqiTokenizer(card);
    
  • And finally request the CulqiToken. The _publicKey is the same we setted above, in the first step.
      var result = await tokenizer.getToken(publicKey: _publicKey);
      if(result is CulqiToken){
        print(result.token);
      }else if(result is CulqiError){
        print(result);
      }
    
    All the code can be found at the example folder.

2. A widget to make faster the integration: #

We may not want to implement the UI to request all the data (card number, expiration date, etc). Because of that, this packages comes with a widget (called CulqiPayment) that makes the job for us. So, how to use it?

  • The widget can be created like any other. As body of Scaffold for example.
    
      class TestWidget extends StatefulWidget {
          @override
          TestWidgetState createState() => TestWidgetState();
      }
    
      class TestWidgetState extends State<TestWidget>{
          // We need this key to get the fields inside the widget
          GlobalKey<CulqiPaymentState> _widgetKey = GlobalKey();
    
          // This is again the public key from Culqi
          final String _publicKey = 'INSERT_YOUR_PUBLIC_KEY_HERE';
    
          // ... More code
    
          @override
          Widget build(BuildContext context){
              return Scaffold(
                  body: ListView(
                      children: <Widget>[
                          // Here we create it and pass the _widgetKey
                          CulqiPayment(_widgetKey),
                          RaisedButton(
                              // This function will get the token
                              // with the fields from CulqiPayment
                              onPressed: getToken,
                              child: Text('Get Token')
                          )
                      ]
                  )
              );
          }
      }
    
  • To make clearer how to get the CulqiToken when using the CulqiPayment widget, here is the getToken() function called from the RaisedButton above.
      class TestWidgetState extends State<TestWidget>{
          // Here comes the above code
          // ...
          // Here ends the above code
    
          void getToken() async{
              CulqiCard _card = CulqiCard();
    
              // Here we are getting all the fields from the widget
              // through setInfoOn(CulqiCard _card) function
              bool success = _widgetKey.currentState.setInfoOn(_card);
    
              // The widget doesn't ask for email, because 
              // assumes the app already has the user email  
              // (from login for example). 
              // But in case is needed, in the example
              // folder at repository, can be found the same 
              // code but requesting the email.
              //
              // Here, for simplicity, we set the email
    
              _card.email = 'test@testemail.com';
    
              CulqiTokenizer _tokenizer = CulqiTokenizer(_card);
    
              var r = await tokenizer.getToken(publicKey:_publicKey);
    
              if(r is CulqiToken){
                  print(r.token);
              }else if(r is CulqiError){
                  print(r);
              }
          }
      }
    
    Again, all the code can be found at the example folder.

The CulqiPayment widget:

This widgets allows two optional parameters: locale and years.

  • locale is an String with value 'es' or 'en', meaning Spanish and English respectively. This value is used to show the widget in the corresponding language. If not provided or not supported language is provided, the default goes to 'es'.
  • years is a List<int>. Represents the year range to choose at expiration year. Default goes to [2020, 2021, 2022, 2023, 2024, 2025].

Suggestions and bugs #

Please file feature requests, suggestions and bugs at the issue tracker.

0.5.2 #

  • Added a better error handling (with more specific errors from Culqi).
  • Added some tests to check the specific errors from Culqi

0.5.1 #

  • Fixed some maintenance suggestions from pub.dev

0.5.0 #

  • Initial version, created by Limonadev.
  • Support for create token from Culqi
  • Provides the CulqiPayment widget

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:flutter_culqi/flutter_culqi.dart';


/// You need to paste your Culqi public key here
final String _publicKey = 'INSERT_YOUR_PUBLIC_KEY_HERE';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  /// This functions tests the creation of a [CulqiToken] given a [CulqiCard]
  void onlyApi() async{
    CulqiCard card = CulqiCard(
        cardNumber: '4111111111111111',
        cvv: '123',
        expirationMonth: 9,
        expirationYear: 2020,
        email:'test@testmail.com'
    );

    CulqiTokenizer tokenizer = CulqiTokenizer(card);

    var result = await tokenizer.getToken(publicKey: _publicKey);
    if(result is CulqiToken){
      print(result.token);
    }else if(result is CulqiError){
      print(result);
    }

  }

  MyApp(){
    onlyApi();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Culqi Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {

  GlobalKey<CulqiPaymentState> _key = GlobalKey();

  String _email;

  String _token = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Culqi Example'),
      ),
      body: ListView(
        children: <Widget>[
          /// This is a provided widget to make faster the Culqi integration. Is not mandatory to use it,
          /// you can create your own form to get the necessary fields to get a token.
          ///
          /// Of course, the creation of the token requires an email, but on the most cases, the email
          /// is the login method, so the app already has the user email. Because of that the
          /// [CulqiPayment] widget wont request the email, and you need to provide it to the [CulqiCard]
          CulqiPayment(_key, locale: 'en', years: [2020, 2021, 2022, 2023, 2024],),

          /// In this example, here we are requesting the email and save it on [_email].
          ListTile(
            title: Text('Email', style: TextStyle(fontSize: 12),),
            subtitle: TextField(
              onChanged: (value){
                _email = value;
              },
              decoration: InputDecoration(
                hintText: 'test@test.com'
              ),
            ),
          ),
          Container(
            alignment: Alignment.centerRight,
            child: RaisedButton(
              onPressed: getToken,
              child: Text('Get Token', style: TextStyle(color: Colors.white),),
              color: Colors.blue,
            ),
          ),
          Text(_token)
        ],
      ),
    );
  }


  /// This async function allows us to get the info from the [CulqiPayment] widget and check if all the fields
  /// were completed. If it happens, set the [_email] on the [_card].
  ///
  /// Finally, using the [CulqiTokenizer] we try to get the token.
  void getToken() async{
    CulqiCard _card = CulqiCard();
    bool success = _key.currentState.setInfoOn(_card);

    if(success){
      _card.email = _email;

      CulqiTokenizer _tokenizer = CulqiTokenizer(_card);

      var result = await _tokenizer.getToken(publicKey: _publicKey);
      if(result is CulqiToken){
        setState(() {
          _token = result.token;
        });
      }else if(result is CulqiError){
        setState(() {
          _token = result.errorMessage;
        });
      }
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_culqi: ^0.5.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_culqi/flutter_culqi.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • flutter_culqi that is a package requiring null.

Health suggestions

Format lib/flutter_culqi.dart.

Run flutter format to format lib/flutter_culqi.dart.

Format lib/src/card.dart.

Run flutter format to format lib/src/card.dart.

Format lib/src/culqi_payment_widget.dart.

Run flutter format to format lib/src/culqi_payment_widget.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/culqi_response.dart (Run flutter format to format lib/src/culqi_response.dart.)
  • lib/src/internationalization/translation.dart (Run flutter format to format lib/src/internationalization/translation.dart.)
  • lib/src/token.dart (Run flutter format to format lib/src/token.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.0+4 0.12.1
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
http_parser 3.1.4
meta 1.1.8 1.2.1
path 1.7.0
pedantic 1.9.0 1.9.1
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test