Flutter支付集成插件payaza的使用
Flutter支付集成插件payaza的使用
Payaza的SDK使得在应用中开始接受客户的支付变得非常简单。通过简单的步骤集成支付SDK,您可以轻松地开始接受付款。
开始使用
要在您的应用中开始收集支付,请在pubspec.yaml
文件中添加payaza
依赖项:
dependencies:
payaza: ^0.0.1
使用方法
在使用之前,请初始化SDK:
// 初始化SDK
Payaza.init('<public key>');
// 运行应用
runApp(const MyApp());
以下是一个完整的示例,展示了如何集成Payaza支付功能:
import 'package:example/my_custom_form.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:payaza/payaza.dart';
void main() {
// 初始化Payaza SDK
Payaza.init('PZ78-PKLIVE-FA9BB0D3-592F-4F8D-BA29-85F8B267F4A8');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/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](/user/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: [
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');
}
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](/user/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),
),
);
}
}
更多关于Flutter支付集成插件payaza的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复