pesepay 0.3.0 pesepay: ^0.3.0 copied to clipboard
Pesepay helps businesses in Africa get paid by anyone, anywhere in the world
Pesepay #
Pesepay helps businesses in Africa get paid by anyone, anywhere in the world
This is still WIP! Try out and contribute where you can.
Installation 💻 #
❗ In order to start using Pesepay you must have the Dart SDK installed on your machine.
Add pesepay
to your pubspec.yaml
:
dependencies:
pesepay:
Install it:
dart pub get
Usage 🔥 #
Import package #
import 'package:pesepay/pesepay.dart';
Declare and initialize #
final pesepay = Pesepay(
integrationKey: '',
encryptionKey: '',
resultUrl: '',
returnUrl: '',
);
Get list of active currencies #
final List<Currency> currencies = await Pesepay.getActiveCurrencies();
Get list of payment methods for selected currency #
final List<PaymentMethod> methods = await Pesepay.getPaymentMethodsByCurrency(currency);
Perform Web Transaction #
This relies on the returned web redirectUrl
that customers can use to complete the transaction
First step would be to create the transaction:
final Transaction transaction = pesepay.createTransaction(
amount: 1,
currencyCode: 'ZWL',
transactionDescription: 'Bag of potatoes',
transactionReference: '111-222-333'
)
Then process the transaction:
final TransactionResponse response = await pesepay.initiateWebTransaction(transaction);
If the above execution results in any error either within the package itself or from the Pesepay server side you should except a PesepayException
. So it would be helpful to handle that.
Perform Seamless Transaction #
First step here would be to create the transaction:
final SeamlessTransaction seamlessTransaction = pesepay.createSeamlessTransaction(
customerName: 'Cool Name',
customerEmail: 'yourmail@email.com',
customerPhone: '0777111111',
amount: 1,
currencyCode: 'ZWL',
transactionDescription: 'Banana Peel',
transactionReference: '111-222-333',
paymentMethodCode: paymentMethodCode,
);
Process the seamless transaction:
final TransactionResponse response = await pesepay.initiateSeamlessTransaction(transaction);
Check transaction status #
final TransactionResponse response = await pesepay.checkTransactionStatus(pollUrl);
Serverless Checkout ? #
Instead of using delays to check transaction status you can also stream
the TransactionResponse
using the poll url.
pesepay.streamTransactionResponse(..)
takes a requiredpollUrl
string and optionalstreamInterval
in seconds which is the interval to poll the url, default to20
sec- You can stream status and show current transaction status on UI to user with a
StreamBuilder(..)
like below:
final String pollUrl = response.pollUrl;
// in Widget build(..) method
// you can do something like
StreamBuilder(
stream: pesepay.streamTransactionResponse(pollUrl),
builder: (context, AsyncSnapshot<TransactionResponse> snapshot) {
if(snapshot.hasData) {
final TransactionResponse response = snapshot.data!;
return response.paid ? SuccessWidget() : OtherWidgetsForErrorOrWaiting();
} else {
return CircularProgressIndicator();
}
}
)
Full Usage Example #
void main() async {
try {
final pesepay = Pesepay(
integrationKey: '',
encryptionKey: '',
resultUrl: '',
returnUrl: '',
);
final Transaction transaction = pesepay.createTransaction(
amount: 1,
currencyCode: 'ZWL',
transactionDescription: 'Bag of potatoes',
transactionReference: '111-222-333'
);
final TransactionResponse response = await pesepay.initiateWebTransaction(transaction);
// Add a delay before checking status, maybe 20-30 seconds
await Future.delayed(const Duration(seconds: 30))
// Check status
final TransactionResponse pollResponse = await pesepay.checkTransactionStatus(response.pollUrl);
if (pollResponse.paid) {
// well you've done it
print('I deserve drinks 🍻');
} else {
// not yet pal
print('👎🏿')
}
} on PesepayException catch (e) {
print(e.message);
} catch (e) {
// hell
}
}