Flutter支付集成插件monnify_flutter_sdk_plus的使用
Flutter支付集成插件monnify_flutter_sdk_plus的使用
初始化
要使用此插件,在你的 pubspec.yaml
文件中添加 monnify_flutter_sdk_plus
作为依赖项。
dependencies:
monnify_flutter_sdk_plus: ^版本号
如何使用
该插件提供了两个API:
1. 初始化
初始化插件。这一步应该在小部件的 initState
中完成。
import 'package:monnify_flutter_sdk_plus/monnify_flutter_sdk_plus.dart';
class _MyAppState extends State<MyApp> {
[@override](/user/override)
void initState() {
super.initState();
MonnifyFlutterSdkPlus.initialize(
'YOUR_API_KEY',
'CONTRACTCODE',
ApplicationMode.TEST
);
}
}
2. 初始化支付
创建一个 Transaction
对象并传递给 initializePayment
函数。
Future<void> initPayment() async {
TransactionResponse transactionResponse =
await MonnifyFlutterSdkPlus.initializePayment(Transaction(
2000,
"NGN",
"Customer Name",
"mail.cus@tome.er",
"PAYMENT_REF",
"Description of payment",
metaData: {
"ip": "196.168.45.22",
"device": "mobile_flutter"
// 任何其他信息
},
paymentMethods: [PaymentMethod.CARD, PaymentMethod.ACCOUNT_TRANSFER],
incomeSplitConfig: [
SubAccountDetails("MFY_SUB_319452883968", 10.5, 500, true),
SubAccountDetails("MFY_SUB_259811283666", 10.5, 1000, false)
]
));
}
可选参数
- 支付方法:指定交易级别的支付方法。
- 子账户:在
incomeSplitConfig
中的子账户是在特定交易结算时接收资金的账户。 - 元数据:是一个包含单层键值对的映射,用于传递与交易相关的额外信息。
收到的 TransactionResponse
类字段
String paymentDate;
double amountPayable;
double amountPaid;
String paymentMethod;
String transactionStatus;
String transactionReference;
String paymentReference;
需要更多信息?
如需进一步了解Monnify的移动SDK,包括交易状态类型,请参阅:
完整示例
以下是一个完整的示例代码,展示了如何在Flutter应用中集成Monnify支付插件。
import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:monnify_flutter_sdk_plus/monnify_flutter_sdk_plus.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late BuildContext mContext;
[@override](/user/override)
void initState() {
super.initState();
initializeSdk();
}
Future<void> initializeSdk() async {
try {
if (await MonnifyFlutterSdkPlus.initialize(
'MK_TEST_G9YG93QQJA', '4551641593', 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 MonnifyFlutterSdkPlus.initializePayment(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](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Monnify 插件示例'),
),
body: Builder(
builder: (context) {
mContext = context;
return Center(
child: TextButton(
child: const Text("支付"),
onPressed: () => initPayment(),
),
);
},
),
),
);
}
}
更多关于Flutter支付集成插件monnify_flutter_sdk_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复