payaza 1.0.0 payaza: ^1.0.0 copied to clipboard
Payaza’s SDK makes it easy for you to start accepting payments from your customers when they visit your applications.
import 'package:example/my_custom_form.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:payaza/payaza.dart';
void main() {
// Initialize Payza with merchant key
// Payaza.init('PZ78-PKLIVE-FA9BB0D3-592F-4F8D-BA29-85F8B267F4A8');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Payaza Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Payaza Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Widget adaptiveAction(
{required BuildContext context,
required VoidCallback onPressed,
required Widget child}) {
final ThemeData theme = Theme.of(context);
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return TextButton(onPressed: onPressed, child: child);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return CupertinoDialogAction(onPressed: onPressed, child: child);
}
}
Future<void> showAlert(
{required String title, required String message}) async {
return await showAdaptiveDialog(
context: context,
builder: (BuildContext context) => AlertDialog.adaptive(
title: Text(title),
content: Text(message),
actions: <Widget>[
adaptiveAction(
context: context,
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('OK', style: TextStyle(color: Color(0xFF2357d1))),
),
],
),
);
}
void handleSuccess(PayazaSuccessResponse response) async {
await showAlert(
message: response.data.payazaReference ?? '',
title: 'Payment Successful');
}
void handleError(PayazaErrorResponse response) async {
await showAlert(message: response.data.message, title: 'Error');
// if (context.mounted) {
// Navigator.of(context).pop();
// }
}
void handleClose() {
print('Payaza widget was closed');
}
void onSubmit(FormData data) {
Payaza.init(data.merchantKey);
Payaza.createTransaction(
context,
config: PayazaConfig(
amount: data.amount,
connectionMode: PayazaConnectionMode.LIVE_CONNECTION_MODE,
email: data.email,
firstName: data.firstName,
lastName: data.lastName,
phoneNumber: data.phoneNumber,
transactionReference: 'PY_${DateTime.now().toIso8601String()}',
currencyCode: data.currency),
onSuccess: handleSuccess,
onError: handleError,
onClose: handleClose,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: MyCustomForm(onSubmit: onSubmit),
),
);
}
}