Skip to content

Commit

Permalink
Updated Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wilburx9 committed Oct 3, 2018
1 parent 933e32b commit bc520e6
Showing 1 changed file with 51 additions and 64 deletions.
115 changes: 51 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
# Paystack Plugin for Flutter
# :credit_card: Paystack Plugin for Flutter

[![pub package](https://img.shields.io/pub/v/flutter_paystack.svg)](https://pub.dartlang.org/packages/flutter_paystack)

A Flutter plugin for making payments via Paystack Payment Gateway. Completely supports Android and iOS.

## Installation
## :rocket: Installation
To use this plugin, add `flutter_paystack` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).

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

``` dart
import 'package:flutter_paystack/flutter_paystack.dart'
class PaymentPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _PaymentPageState();
}
class _PaymentPageState extends State<PaymentPage> {
var paystackPublicKey = '[YOUR_PAYSTACK_PUBLIC_KEY]';
var paystackSecretKey = '[YOUR_PAYSTACK_SECRET_KEY]';
@override
void initState() {
PaystackPlugin.initialize(publicKey: paystackPublicKey);
super.initState();
}
@override
Widget build(BuildContext context) {
// Return your widgets
PaystackPlugin.initialize(
publicKey: paystackPublicKey, secretKey: paystackSecretKey);
// secretKey is not needed if you're not using checkout as a payment method.
//
}
}
```

No other configuration required - the plugin should work out of the box.
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.
2. **Charge Card**: This is a longer approach; you handle all callbacks and UI states on your own.

### 1. :star2: Checkout (Recommended)
You initialize a charge object with just an amount, email & accessCode or reference.
Pass an `accessCode` only when you have [initialized the transaction](https://developers.paystack.co/reference#initialize-a-transaction) from your end otherwise, pass `reference`;

Except you want the user to use a preferred checkout method, pass a one of your choosing.

## Making Payments
```dart
Charge charge = Charge()
..amount = 10000
..reference = _getReference()
// or ..accessCode = _getAccessCodeFrmInitialization()
..email = 'customer@email.com';
bool success = await PaystackPlugin.checkout(
context,
method: _method,
charge: charge,
);
```

`PaystackPlugin.checkout()` returns `true` for a successful payment otherwise, it returns `false`.

### 2. :star: Charge Card
You can choose to initialize the payment locally or via Paystack's backend.

### 1. Initialize Via Paystack (Recommended)
1.a. This starts by making a HTTP POST request to `https://api.paystack.co/transaction/initialize`
with the amount(in kobo), reference, etc in the request body and your paystack secret key in request header.
The request looks like this:
#### A. Initialize Via Paystack (Recommended)

``` dart
// Required imports
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
initTransaction() async {
var url = 'https://api.paystack.co/transaction/initialize';
Map<String, String> headers = {
'Authorization': 'Bearer $[YOUR_PAYSTACK_SECRET_KEY]',
};
Map<String, String> body = {
'reference': '[YOUR_GENERATED_REFERENCE]',
'amount': 500000.toString(), // Amount must always be in kobo
'email': 'user@email.com'
};
http.Response response =
await http.post(url, body: body, headers: headers);
// Charge card
_chargeCard(response.body);
}
```
Please check the [official documentation](https://developers.paystack.co/reference#initialize-a-transaction) for the full details of payment initialization.
1.a. This starts by making a HTTP POST request to [paystack](https://developers.paystack.co/reference#initialize-a-transaction).

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:
Expand Down Expand Up @@ -137,7 +127,7 @@ You can then create a `Charge` object with the access code and card details. The



### 2. Initialize Locally
#### 2. Initialize Locally
Just send the payment details to `PaystackPlugin.chargeCard`
```dart
// Set transaction params directly in app (note that these params
Expand All @@ -154,12 +144,14 @@ Just send the payment details to `PaystackPlugin.chargeCard`
```


## Validating Card Details
You are expected to build the UI for your users to enter their payment details.
## :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 **TextFormField**s inside a **Form** widget. Please check this article on
[validating forms on Flutter](https://medium.freecodecamp.org/how-to-validate-forms-and-user-input-the-easy-way-using-flutter-e301a1531165)
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.
Expand All @@ -178,32 +170,27 @@ Method to check if the card is valid. Always do this check, before charging the
This method returns an estimate of the string representation of the card type(issuer).


## chargeCard
Charging with the **PaystackPlugin** is quite straightforward. It requires the following arguments.
1. `context`: your UI **BuildContext**. It's used by the plugin for showing dialogs for the user to take a required action, e.g inputting OTP.
2. `charge`: You provide the payment details (`PaymentCard`, `amount` `email` etc) to an instance of the `Charge` object.
3. `beforeValidate`: Pre-validation callback.
4. `onSuccess`: callbacks for a successful payment.
4. `onError`: callback for when an error occurs in the transaction. Provides you with a reference to the error object.


## Verifying Transactions
## :heavy_check_mark: Verifying Transactions
This is quite easy. Just send a HTTP GET request to `https://api.paystack.co/transaction/verify/$[TRANSACTION_REFERENCE]`.
Please, check the [official documentaion](https://developers.paystack.co/reference#verifying-transactions) on verifying transactions.

## Testing your implementation
## :helicopter: Testing your implementation
Paystack provides tons of [payment cards](https://developers.paystack.co/docs/test-cards) for testing.

## Running Example project
## :arrow_forward: Running Example project
For help getting started with Flutter, view the online [documentation](https://flutter.io/).

An [example project](https://github.com/wilburt/flutter_paystack/tree/master/example) has been provided in this plugin.
Clone this repo and navigate to the **example** folder. Open it with a supported IDE or execute `flutter run` from that folder in terminal.

## Contributing, Issues and Bug Reports
## :pencil: Contributing, :disappointed: Issues and :bug: Bug Reports
The project is open to public contribution. Please feel very free to contribute.
Experienced an issue or want to report a bug? Please, [report it here](https://github.com/wilburt/flutter_paystack/issues). Remember to be descriptive.
Experienced an issue or want to report a bug? Please, [report it here](https://github.com/wilburt/flutter_paystack/issues). Remember to be as descriptive as possible.

## Credits
## :trophy: Credits
Thanks to the authors of Paystack [iOS](https://github.com/PaystackHQ/paystack-ios) and [Android](https://github.com/PaystackHQ/paystack-android) SDKS. I leveraged on their work to bring this plugin to fruition.

## :fire: Apps Using this plugin
1. **EasyDispatch** - [Play Store](https://play.google.com/store/apps/details?id=com.ncktech.easydispatch) | [App Store](https://itunes.apple.com/us/app/apple-store/id1413887430)

To to add your app here, just shoot me an email on faradaywilly(at)gmail.com.

0 comments on commit bc520e6

Please sign in to comment.