pixelpay_sdk 2.0.10 copy "pixelpay_sdk: ^2.0.10" to clipboard
pixelpay_sdk: ^2.0.10 copied to clipboard

PixelPay's tool that simplifies integrations with APIs from the PixelPay platform.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/services.dart';
import 'package:pixelpay_sdk/models/billing.dart' as pixelpay;
import 'package:pixelpay_sdk/models/item.dart' as pixelpay;
import 'package:pixelpay_sdk/models/order.dart' as pixelpay;
import 'package:pixelpay_sdk/models/card.dart' as pixelpay;
import 'package:pixelpay_sdk/models/settings.dart' as pixelpay;
import 'package:pixelpay_sdk/requests/sale_transaction.dart' as pixelpay;
import 'package:pixelpay_sdk/requests/auth_transaction.dart' as pixelpay;
import 'package:pixelpay_sdk/services/transaction.dart' as pixelpay;
import 'package:pixelpay_sdk/resources/locations.dart' as pixelpay;

final _rnd = Random();

const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';

String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
    length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));

pixelpay.Settings _settings = pixelpay.Settings();

pixelpay.Card _card = pixelpay.Card();
pixelpay.Billing _billing = pixelpay.Billing();
pixelpay.Item _item = pixelpay.Item();
pixelpay.Order _order = pixelpay.Order();

Map<String, dynamic> _countries = Map();
Map<String, dynamic> _states = Map();
Map<String, dynamic> _formats = Map();

pixelpay.Card setCardModel() {
  final card = pixelpay.Card();
  card.number = _testRepository[_selectedUseCase]["card"]["number"];
  card.cvv2 = _testRepository[_selectedUseCase]["card"]["cvv2"];
  card.expire_month = _testRepository[_selectedUseCase]["card"]["expire_month"];
  card.expire_year = DateTime.now().year + 1;
  card.cardholder = _testRepository[_selectedUseCase]["card"]["cardholder"];

  return card;
}

pixelpay.Billing setBillingModel() {
  final billing = pixelpay.Billing();
  billing.address = _testRepository[_selectedUseCase]["billing"]["address"];
  billing.country = _testRepository[_selectedUseCase]["billing"]["country"];
  billing.state = _testRepository[_selectedUseCase]["billing"]["state"];
  billing.city = _testRepository[_selectedUseCase]["billing"]["city"];
  billing.phone = _testRepository[_selectedUseCase]["billing"]["phone"];

  return billing;
}

pixelpay.Item setItemModel() {
  final item = pixelpay.Item();
  item.code = _testRepository[_selectedUseCase]["item"]["address"];
  item.title = _testRepository[_selectedUseCase]["item"]["title"];
  item.price = _testRepository[_selectedUseCase]["item"]["price"];
  item.qty = _testRepository[_selectedUseCase]["item"]["qty"];

  return item;
}

pixelpay.Order setOrderModel(pixelpay.Item item) {
  final order = pixelpay.Order();
  order.id = getRandomString(8);
  order.currency = _selectedCurrency;
  order.customer_email = _testRepository[_selectedUseCase]["order"]["customer_email"];
  order.customer_name = _testRepository[_selectedUseCase]["order"]["customer_name"];

  order.addItem(item);
  order.addExtra("test", "lorem ipsum");
  order.totalize();

  return order;
}

/// Test data repository
Map<String, dynamic> _testRepository = Map();

/// Selected use case
String? _selectedUseCase = '0';

List<String> _currencies = [
  'HNL',
  'USD',
  'GTQ',
  'NIO',
];

String? _selectedCurrency = 'HNL';

List<String> _installmentMonths = [
  '3',
  '6',
  '9',
  '12',
];

String? _selectedInstallmentMonth = '12';

List<String> _installmentType = [
  'intra',
  'extra',
];

String? _selectedInstallmentType = 'extra';

/// General style for buttons
final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
  backgroundColor: Colors.teal,
  textStyle: const TextStyle(fontSize: 20),
);

/// General style for inputs
InputDecoration inputStyle(String labelText) {
  return InputDecoration(
    labelText: labelText,
    labelStyle: const TextStyle(
      fontSize: 20,
      fontWeight: FontWeight.w800,
    ),
    border: const OutlineInputBorder(),
  );
}

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

  if (_testRepository.isEmpty) {
    String testingData = await rootBundle.loadString('assets/test.json', cache: true);
    _testRepository = jsonDecode(testingData);
    _card = setCardModel();
    _billing = setBillingModel();
    _item = setItemModel();
    _order = setOrderModel(_item);

    _countries = await pixelpay.Locations.countriesList();
    _states = await pixelpay.Locations.statesList("HN");
    _formats = await pixelpay.Locations.formatsList("HN");
  }
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: const Padding(
          padding: EdgeInsets.all(8.0),
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String _status_code = '---';
  String _message = '---';
  String _response = 'Empty';
  String _token = '';
  bool _withAuthentication = false;
  bool _useSandbox = true;
  bool _useInstallments = false;

  Map<String, String> _merchantSettings = {
    'endpoint': '',
    'auth_key': '',
    'auth_hash': '',
  };

  Future<void> sendTransaction() async {
    if (!mounted) return;

    setState(() {
      _message = 'Start transaction ...';
      _status_code = '---';
      _response = 'Empty';
    });

    try {
      configureCustomSettings();

      _order = setOrderModel(_item);

      final sale = pixelpay.SaleTransaction();
      sale.lang = 'en';

      if (_token.trim().isNotEmpty) {
        sale.setCardToken(_token);
      } else {
        sale.setCard(_card);
        sale.setBilling(_billing);
      }
      sale.setOrder(_order);

      if (_useInstallments) {  
        sale.setInstallment(int.tryParse(_selectedInstallmentMonth!) ?? 12, _selectedInstallmentType!);
        sale.withPointsRedeemAmount(1);
      }

      if (_withAuthentication) {
        sale.withAuthenticationRequest();
      }

      // Mandar objeto
      final service = pixelpay.Transaction(_settings);

      setState(() {
        _message = 'Sending transaction ...';
      });

      final response = await service.doSale(sale);

      if (response != null) {
        setState(() {
          _message = response.message ?? 'Empty message';
          _status_code = response.getStatus().toString();
          _response = response.toJson();
        });
      }
    } on PlatformException {
      setState(() {
        _message = 'Failed to get request data.';
      });
    } on Exception {
      setState(() {
        _message = 'Exception on code.';
      });
    }
  }

  Future<void> doAuthTransaction() async {
    if (!mounted) return;

    setState(() {
      _message = 'Start auth transaction ...';
      _status_code = '---';
      _response = 'Empty';
    });

    try {
      configureCustomSettings();

      _order = setOrderModel(_item);

      final sale = pixelpay.AuthTransaction();
      sale.lang = 'en';

      if (_token.trim().isNotEmpty) {
        sale.setCardToken(_token);
      } else {
        sale.setCard(_card);
        sale.setBilling(_billing);
      }

      sale.setOrder(_order);

      if (_withAuthentication) {
        sale.withAuthenticationRequest();
      }

      final service = pixelpay.Transaction(_settings);

      setState(() {
        _message = 'Sending auth transaction ...';
      });

      final response = await service.doAuth(sale);


      if (response != null) {
        setState(() {
          _message = response.message ?? 'Empty message';
          _status_code = response.getStatus().toString();
          _response = response.toJson();
        });
      }
    } on PlatformException {
      setState(() {
        _message = 'Failed to get request data.';
      });
    } on Exception {
      setState(() {
        _message = 'Exception on code.';
      });
    }
  }

  //Set the custom settings from the Merchant Options
  Future<void> configureCustomSettings() async {
      if (_useSandbox) {
        _settings.setupSandbox();
      } else {
        _settings.setupEndpoint(_merchantSettings['endpoint'].toString());
        _settings.setupCredentials(_merchantSettings['auth_key'].toString(), _merchantSettings['auth_hash'].toString());
      }
  }

  Future<void> resetOutput() async {
    if (!mounted) return;

    setState(() {
      _status_code = '---';
      _message = '---';
      _response = 'Empty';
    });
  }

  Future<void> showAssets() async {
    if (!mounted) return;

    setState(() {
      _status_code = 'Showing';
      _message = 'Assets';
      _response = 'FORMATS: ' + jsonEncode(_formats)
        +'\n\nSTATES: ' + jsonEncode(_states)
        +'\n\nCOUNTRIES: ' + jsonEncode(_countries);
    });
  }

  /// Custom button
  Widget customButton(String text, IconData icon, VoidCallback onPressedCallback) {
    return ElevatedButton(
      key: Key(text),
      style: buttonStyle,
      onPressed: onPressedCallback,
      child: textIconRow(text, icon),
    );
  }

  /// Text and icon in a row
  Widget textIconRow(String text, IconData icon) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Flexible(
          child: Text(text),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5),
          child: Icon(icon, size: 20),
        ),
      ],
    );
  }

  /// Custom switch field
  Widget customSwitchField(String labelText, bool switchValue, void Function(bool) onChangeCallback) {
    return SwitchListTile(
      title: Text(labelText),
      value: switchValue,
      onChanged: onChangeCallback,
    );
  }

  /// Custom text field with focus
  Widget customTextField(String? textEditing, String labelText, void Function(String?) onBlurCallback) {
    final TextEditingController controller = TextEditingController(text: textEditing);

    return Focus(
      onFocusChange: (hasFocus) {
        if (!hasFocus) {
          onBlurCallback(controller.text);
        }
      },
      child: TextField(
        decoration: inputStyle(labelText),
        controller: controller,
      ),
    );
  }

  /// Custom read only text field
  Widget customReadOnlyTextField(String textEditing, String labelText) {
    return TextField(
      maxLines: null,
      readOnly: true,
      controller: TextEditingController(text: textEditing),
      decoration: inputStyle(labelText),
    );
  }

  /// Custom Dropdown Button
  Widget customDropdownButton(String? textEditing, Map<String, dynamic> dataList, String labelText, void Function(String?) onChangedCallback) {
    return DropdownButtonFormField(
      key: Key(labelText),
      value: textEditing,
      decoration: inputStyle(labelText),
      isExpanded: true,
      icon: const Icon(Icons.expand_more),
      elevation: 16,
      items: dataList.keys.map<DropdownMenuItem<String>>((String key) {
        return DropdownMenuItem<String>(
          value: key,
          child: Text(dataList[key]["title"]),
        );
      }).toList(),
      onChanged: onChangedCallback,
    );
  }

  /// Custom Dropdown Button
  Widget customSimpleDropdownButton(String? textEditing, List<String> options, String labelText, void Function(String?) onChangedCallback) {
    return DropdownButtonFormField(
      key: Key(labelText),
      value: textEditing,
      decoration: inputStyle(labelText),
      isExpanded: true,
      icon: const Icon(Icons.expand_more),
      elevation: 16,
      items: options.map((String option) {
        return DropdownMenuItem<String>(
          value: option,
          child: Text(option),
        );
      }).toList(),
      onChanged: onChangedCallback,
    );
  }

  /// X Axis Separator
  Widget xAxisSeparator(double? width) {
    return SizedBox(width: width,);
  }

  /// Y Axis Separator
  Widget yAxisSeparator() {
    return const SizedBox(height: 14,);
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView( 
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          customSwitchField('Use Sandbox', _useSandbox, (value) { setState(() {_useSandbox = value;}); }),
          Visibility(
            visible: !_useSandbox,
            maintainState: true,
            child: Column(
              children: [
                Row(
                  children: <Widget>[
                    Expanded(child: customTextField(_merchantSettings['endpoint'], 'Endpoint', (value) { setState(() {_merchantSettings['endpoint'] = value!;}); })),
                    xAxisSeparator(10),
                    Expanded(child: customTextField(_merchantSettings['auth_key'], 'Key', (value) { setState(() {_merchantSettings['auth_key'] = value!;}); })),
                  ],
                ),
                yAxisSeparator(),
                customTextField(_merchantSettings['auth_hash'], 'Hash', (value) { setState(() {_merchantSettings['auth_hash'] = value!;}); }),
                yAxisSeparator(),
              ],
            ),
          ),
          customSwitchField('Enable Installments', _useInstallments, (value) { setState(() {_useInstallments = value;}); }),
          Visibility(
            visible: _useInstallments,
            maintainState: true,
            child: Column(
              children: [
                Row(
                  children: <Widget>[
                    Expanded(child: customSimpleDropdownButton(_selectedInstallmentMonth, _installmentMonths, 'Months', (value) { setState(() {_selectedInstallmentMonth = value;}); })),
                    xAxisSeparator(10),
                    Expanded(child: customSimpleDropdownButton(_selectedInstallmentType, _installmentType, 'Type', (value) { setState(() {_selectedInstallmentType = value;}); })),
                  ],
                ),
                yAxisSeparator(),
              ],
            ),
          ),
          yAxisSeparator(),
          customDropdownButton(
            _selectedUseCase,
            _testRepository,
            'Select card',
            (value) { setState(() {
              _selectedUseCase = value;
            });
              _card = setCardModel();
              _billing = setBillingModel();
              _item = setItemModel();
              _order = setOrderModel(_item);
          }),
          yAxisSeparator(),
          Row(
            children: [
              Expanded(child: customSimpleDropdownButton(_selectedCurrency, _currencies, 'Currency', (value) { setState(() {_selectedCurrency = value;}); })),
              xAxisSeparator(10),
              Expanded(
                child: customTextField(_order.amount.toString(), 'Amount', (value) { setState(() {
                    _item.price = double.parse(value!);
                    _order = setOrderModel(_item);
                  }); }
                ),
              ),
            ],
          ),
          yAxisSeparator(),
          customTextField(_card.number, 'Card Number', (value) { setState(() {_card.number = value;}); }),
          yAxisSeparator(),
          customTextField(_card.cardholder, 'Card Holder', (value) { setState(() {_card.cardholder = value;}); }),
          yAxisSeparator(),
          Row(
            children: <Widget>[
              Expanded(child: customTextField(_card.expire_month.toString(), 'Expire Month', (value) { setState(() {_card.expire_month = int.tryParse(value!);}); }),),
              xAxisSeparator(10),
              Expanded(child: customTextField((DateTime.now().year + 1).toString(), 'Expire Year', (value) { setState(() {_card.expire_year = int.tryParse(value!);}); }),),
              xAxisSeparator(10),
              Expanded(child: customTextField(_card.cvv2, 'CVC', (value) { setState(() {_card.cvv2 = value;}); }),),
            ],
          ),
          yAxisSeparator(),
          customTextField(_token, 'Token', (value) { setState(() {_token = value!;}); }),
          yAxisSeparator(),
          customSwitchField('Enable 3DS/EMV transaction', _withAuthentication, (value) { setState(() {_withAuthentication = value;}); }),
          yAxisSeparator(),
          Row(
            children: <Widget>[
              Expanded(child: customButton('SALE', Icons.attach_money, sendTransaction),),
              xAxisSeparator(5),
              Expanded(child: customButton('AUTH', Icons.lock_open, doAuthTransaction),),
            ],
          ),
          yAxisSeparator(),
          Row(children: <Widget>[
              Expanded(child: customButton('SHOW ASSETS', Icons.source, showAssets),),
              xAxisSeparator(5),
              Expanded(child: customButton('RESET', Icons.settings_backup_restore, resetOutput),),
          ],),
          yAxisSeparator(),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(_status_code, key: const Key('status_code')),
              xAxisSeparator(10),
              Text(_message, key: const Key('main_message'),),
            ],
          ),
          yAxisSeparator(),
          customReadOnlyTextField(_response, 'Response'),
          yAxisSeparator(),
        ],
      ),
    );
  }
}
3
likes
140
points
55
downloads

Publisher

unverified uploader

Weekly Downloads

PixelPay's tool that simplifies integrations with APIs from the PixelPay platform.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

crypto, flutter, json_annotation, sprintf, yaml

More

Packages that depend on pixelpay_sdk

Packages that implement pixelpay_sdk