monnify_payment_sdk 1.0.0 monnify_payment_sdk: ^1.0.0 copied to clipboard
Flutter plugin for Monnify payment gateway. Supports Android and iOS.
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
import 'package:flutter/services.dart';
import 'package:monnify_payment_sdk/application_mode.dart';
import 'package:monnify_payment_sdk/monnify_payment_sdk.dart';
import 'package:monnify_payment_sdk/payment_method.dart';
import 'package:monnify_payment_sdk/transaction.dart';
import 'package:monnify_payment_sdk/transaction_response.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late BuildContext mContext;
final _monnifyPaymentSdkPlugin = MonnifyPaymentSdk();
@override
void initState() {
super.initState();
initializeSdk();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _monnifyPaymentSdkPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
Future<void> initializeSdk() async {
try {
if (await _monnifyPaymentSdkPlugin.initialize(
apiKey: 'Add your api key',
contractCode: 'add your contract code',
applicationMode: ApplicationMode.TEST)) {
_showToast("SDK initialized!");
}
} on PlatformException catch (e, s) {
print("Error initializing sdk");
print(e);
print(s);
_showToast("Failed to init sdk!");
}
}
Future<void> initPayment() async {
TransactionResponse transactionResponse;
try {
transactionResponse = await _monnifyPaymentSdkPlugin.initializePayment(
transaction: Transaction(
2000.0,
"NGN",
"Customer Name",
"mail.cus@tome.er",
getRandomString(15),
"Description of payment",
metaData: {"ip": "196.168.45.22", "device": "mobile"},
paymentMethods: [PaymentMethod.CARD, PaymentMethod.ACCOUNT_TRANSFER],
/*incomeSplitConfig: [SubAccountDetails("MFY_SUB_319452883968", 10.5, 500, true),
SubAccountDetails("MFY_SUB_259811283666", 10.5, 1000, false)]*/
));
_showToast(
"${transactionResponse.transactionStatus}\n${transactionResponse.paymentReference}\n${transactionResponse.transactionReference}\n${transactionResponse.amountPaid}\n${transactionResponse.amountPayable}\n${transactionResponse.paymentDate}\n${transactionResponse.paymentMethod}");
} on PlatformException catch (e, s) {
print("Error initializing payment");
print(e);
print(s);
_showToast("Failed to init payment!");
}
}
String getRandomString(int length) {
const _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
return String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
}
void _showToast(String message) {
final scaffold = ScaffoldMessenger.of(mContext);
scaffold.showSnackBar(
SnackBar(
content: Text(message),
action: SnackBarAction(
label: 'CLOSE', onPressed: scaffold.hideCurrentSnackBar),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Monnify Plugin Sample'),
),
body: Builder(
builder: (context) {
mContext = context;
return Center(
child: TextButton(
child: Text("PAY"),
onPressed: () => initPayment(),
),
);
},
),
),
);
}
}