Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

paystack plugin for flutter

a robust flutter plugin for making payments via paystack payments gateway. completely supports android and ios

:rocket: installation

to use this plugin, add flutter_paystack as a dependency in your pubspec.yaml file.

then initialize the plugin preferably in the initstate of your widget.

import 'package:flutter_paystack/flutter_paystack.dart';

class _paymentpagestate extends state<paymentpage> {
  var publickey = '[your_paystack_public_key]';

  @override
  void initstate() {
    paystackplugin.initialize(
            publickey: publickey);
  }
}

no other configuration required—the plugin works out of the box.

:heavy_dollar_sign: making payments

there are two ways of making payments with the plugin.

  1. checkout: this is the easy way; as the plugin handles all the
    processes involved in making a payment (except transaction
    initialization and verification which should be done from your
    backend).
  2. charge card: this is a longer approach; you handle all callbacks
    and ui states.

1. :star2: checkout (recommended)

you initialize a charge object with an amount, email & accesscode or
reference. pass an accesscode only when you have
initialized the transaction
from your backend. otherwise, pass a reference.

charge charge = charge()
      ..amount = 10000
      ..reference = _getreference()
       // or ..accesscode = _getaccesscodefrminitialization()
      ..email = '[email protected]';
    checkoutresponse response = await paystackplugin.checkout(
      context context,
      method: checkoutmethod.card, // defaults to checkoutmethod.selectable
      charge: charge,
    );

please, note that an accesscode is required if the method is
checkoutmethod.bank or checkoutmethod.selectable.

paystackplugin.checkout() returns the state and details of the
payments in an instance of checkoutresponse .

it is recommended that when paystackplugin.checkout() returns, the
payments should be
verified
on your backend.

2. :star: charge card

you can choose to initialize the payment locally or via your backend.

a. initialize via your backend (recommended)

1.a. this starts by making a http post request to
paystack
on your backend.

1.b if everything goes well, the initialization request returns a response with an access_code.
you can then create a charge object with the access code and card details. the charge is in turn passed to the paystackplugin.chargecard() function for payment:

  paymentcard _getcardfromui() {
    // using just the must-required parameters.
    return paymentcard(
      number: cardnumber,
      cvc: cvv,
      expirymonth: expirymonth,
      expiryyear: expiryyear,
    );

    // using cascade notation (similar to java's builder pattern)
//    return paymentcard(
//        number: cardnumber,
//        cvc: cvv,
//        expirymonth: expirymonth,
//        expiryyear: expiryyear)
//      ..name = 'segun chukwuma adamu'
//      ..country = 'nigeria'
//      ..addressline1 = 'ikeja, lagos'
//      ..addresspostalcode = '100001';

    // using optional parameters
//    return paymentcard(
//        number: cardnumber,
//        cvc: cvv,
//        expirymonth: expirymonth,
//        expiryyear: expiryyear,
//        name: 'ismail adebola emeka',
//        addresscountry: 'nigeria',
//        addressline1: '90, nnebisi road, asaba, deleta state');
  }

  _chargecard(string accesscode) {
    var charge = charge()
      ..accesscode = accesscode
      ..card = _getcardfromui();

    paystackplugin.chargecard(context,
        charge: charge,
        beforevalidate: (transaction) => handlebeforevalidate(transaction),
        onsuccess: (transaction) => handleonsuccess(transaction),
        onerror: (error, transaction) => handleonerror(error, transaction));
  }

  handlebeforevalidate(transaction transaction) {
    // this is called only before requesting otp
    // save reference so you may send to server if error occurs with otp
  }

  handleonerror(object e, transaction transaction) {
    // if an access code has expired, simply ask your server for a new one
    // and restart the charge instead of displaying error
  }


  handleonsuccess(transaction transaction) {
    // this is called only after transaction is successful
  }

2. initialize locally

just send the payment details to paystackplugin.chargecard

      // set transaction params directly in app (note that these params
      // are only used if an access_code is not set. in debug mode,
      // setting them after setting an access code would throw an error
      charge charge = charge();
      charge.card = _getcardfromui();
      charge
        ..amount = 2000
        ..email = '[email protected]'
        ..reference = _getreference()
        ..putcustomfield('charged from', 'flutter plugin');
      _chargecard();

:wrench: :nut_and_bolt: validating card details

you are expected but not required to build the ui for your users to enter their payment details.
for easier validation, wrap the textformfields inside a form widget. please check this article on
validating forms on flutter
if this is new to you.

note: you don’t have to pass a card object to charge. the plugin will call-up a ui for the user to input their card.

you can validate the fields with these methods:

card.validnumber

this method helps to perform a check if the card number is valid.

card.validcvc

method that checks if the card security code is valid.

card.validexpirydate

method checks if the expiry date (combination of year and month) is valid.

card.isvalid

method to check if the card is valid. always do this check, before charging the card.

card.gettype

this method returns an estimate of the string representation of the card type(issuer).


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.