:credit_card: Paystack Plugin for Flutter

:rocket: Installation

To use this plugin, add paystack_flutter_sa as a dependency in your pubspec.yaml file.

Then initialize the plugin preferably in the initState of your widget.

import 'package:paystack_flutter_sa/paystack_flutter_sa.dart';

class _PaymentPageState extends State<PaymentPage> {
  var publicKey = '[YOUR_PAYSTACK_PUBLIC_KEY]';
  final plugin = PaystackPlugin();

  @override
  void initState() {
    plugin.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 payment 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.

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 = 'customer@email.com';
    CheckoutResponse response = await plugin.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.

plugin.checkout() returns the state and details of the payment in an instance of CheckoutResponse .

It is recommended that when plugin.checkout() returns, the payment should be verified on your backend.

2. :star: Charge Card

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

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 plugin.chargeCard() function for payment:

  PaymentCard _getCardFromUI() {
    // Using just the must-required parameters.
    return PaymentCard(
      number: cardNumber,
      cvc: cvv,
      expiryMonth: expiryMonth,
      expiryYear: expiryYear,
    );
  }

  _chargeCard(String accessCode) async {
    var charge = Charge()
      ..accessCode = accessCode
      ..card = _getCardFromUI();

    final response = await plugin.chargeCard(context, charge: charge);
    // Use the response
  }

2. Initialize Locally

Just send the payment details to plugin.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 = 'user@email.com'
        ..reference = _getReference()
        ..putCustomField('Charged From', 'Flutter PLUGIN');
      _chargeCard();

Support

If you find any bugs or issues while using the plugin, please register an issues on GitHub. You can also contact us at mistrytrushar88@gmail.com.

Want to contribute

If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), feel free to send your pull request.

Author

This paystack_flutter_sa plugin for Flutter is developed by Trushar Mistry.

Buy Me A Coffee

Buy Me A Coffee