Flutter支付集成插件zainpay的使用
Flutter支付集成插件zainpay的使用
Zainpay Flutter Package
Flutter库帮助您在Flutter移动应用中创建无缝的支付体验。
目录
需求
- Zainpay账户 API Keys
- 支持的Flutter版本 >= 1.17.0
安装
- 在您的
pubspec.yaml
文件中添加依赖项:dependencies: zainpay: 0.1.22
- 运行以下命令以获取依赖项:
flutter pub get
使用
初始化Zainpay实例
要创建一个实例,您应该调用Zainpay构造函数。该构造函数接受以下必需参数:
context
publicKey
isTest
它返回一个Zainpay实例,然后我们可以对该实例调用异步方法来请求每个功能。
final Zainpay zainpay = Zainpay(
context: context,
publicKey: getPublicKey(), // 替换为您的实际公钥
isTest: true // 设置为true以使用测试环境
);
处理响应
创建Zainbox
调用createZainbox(name, tags, callbackUrl, email)
方法会返回一个CreateZainboxResponse?
的Future,我们需要等待实际的响应。
final CreateZainboxResponse? response = await zainpay.createZainbox(name, tags, callbackUrl, email);
if (response != null) {
showLoading(response.status!); // 显示加载状态
debugPrint("${response.toJson()}"); // 打印响应信息
} else {
showLoading("No Response!"); // 如果没有响应,显示提示
}
更新Zainbox
调用updateZainbox(zainboxCode, name[optional], tags[optional], callbackUrl[optional], email[optional])
方法会返回一个CreateZainboxResponse?
的Future,我们需要等待实际的响应。
final CreateZainboxResponse? response = await zainpay.updateZainbox(zainboxCode, name, tags, callbackUrl, email);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取所有Zainbox
调用getAllZainboxes()
方法会返回一个GetAllZainboxesResponse?
的Future,我们需要等待实际的响应。
final GetAllZainboxesResponse? response = await zainpay.getAllZainboxes();
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取指定Zainbox的所有账户
调用getAllZainboxAccounts(zainboxCodeName)
方法会返回一个ZainboxAccountResponse?
的Future,我们需要等待实际的响应。
final ZainboxAccountResponse? response = await zainpay.getAllZainboxAccounts(zainboxCodeName);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取虚拟账户余额
调用getVirtualAccountBalance(accountNumber)
方法会返回一个VirtualAccountBalanceResponse?
的Future,我们需要等待实际的响应。
final VirtualAccountBalanceResponse? response = await zainpay.getVirtualAccountBalance(accountNumber);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取指定Zainbox的虚拟账户余额
调用getZainboxVirtualAccountBalance(zainboxCode)
方法会返回一个ZainboxVirtualAccountsBalancesResponse?
的Future,我们需要等待实际的响应。
final ZainboxVirtualAccountsBalancesResponse? response = await zainpay.getZainboxVirtualAccountBalance(zainboxCode);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取虚拟账户交易记录
调用getVirtualAccountTransactions(accountNumber, count[optional])
方法会返回一个VirtualAccountTransactionsResponse?
的Future,我们需要等待实际的响应。
final VirtualAccountTransactionsResponse? response = await zainpay.getVirtualAccountTransactions(accountNumber, count);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取银行列表
调用getBankList()
方法会返回一个BankListResponse?
的Future,我们需要等待实际的响应。
final BankListResponse? response = await zainpay.getBankList();
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取账户名称
调用getAccountName(bankCode, accountNumber)
方法会返回一个AccountNameResponse?
的Future,我们需要等待实际的响应。
final AccountNameResponse? response = await zainpay.getAccountName(bankCode, accountNumber);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
验证转账
调用verifyTransfer(txnRef)
方法会返回一个TransferVerificationResponse?
的Future,我们需要等待实际的响应。
final TransferVerificationResponse? response = await zainpay.verifyTransfer(txnRef);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
验证存款
调用verifyDeposit(txnRef)
方法会返回一个DepositVerificationResponse?
的Future,我们需要等待实际的响应。
final DepositVerificationResponse? response = await zainpay.verifyDeposit(txnRef);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
获取Zainbox交易历史
调用getZainboxTransactionHistory(zainboxCode, count[optional])
方法会返回一个ZainboxTransactionHistoryResponse?
的Future,我们需要等待实际的响应。
final ZainboxTransactionHistoryResponse? response = await zainpay.getZainboxTransactionHistory(zainboxCode, count);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
按日期获取Zainbox收款汇总
调用getZainboxCollectionSummaryByDate(zainboxCode, {dateFrom, dateTo})
方法会返回一个ZainboxCollectionSummaryByDateResponse?
的Future,我们需要等待实际的响应。
final ZainboxCollectionSummaryByDateResponse? response = await zainpay.getZainboxCollectionSummaryByDate(zainboxCode, {dateFrom, dateTo});
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
注意:
dateTo
和 dateFrom
是可选参数。
创建虚拟账户
调用createVirtualAccount(fullName, email, mobileNumber, zainboxCode)
方法会返回一个CreateVirtualAccountResponse?
的Future,我们需要等待实际的响应。
final CreateVirtualAccountResponse? response = await zainpay.createVirtualAccount(fullName, email, mobileNumber, zainboxCode);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
创建结算计划
调用createSettlement(name, scheduleType, schedulePeriod, settlementList, status, zainboxCode)
方法会返回一个CreateSettlementResponse?
的Future,我们需要等待实际的响应。
final CreateSettlementResponse? response = await zainpay.createSettlement(name, scheduleType, schedulePeriod, settlementList, status, zainboxCode);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
转账资金
调用transferFund(destinationAccountNumber, destinationBankCode, amount, txnRef, narration, sourceAccountNumber, sourceBankCode, zainboxCode)
方法会返回一个FundTransferResponse?
的Future,我们需要等待实际的响应。
final FundTransferResponse? response = await zainpay.transferFund(destinationAccountNumber, destinationBankCode, amount, txnRef, narration, sourceAccountNumber, sourceBankCode, zainboxCode);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
更新虚拟账户状态
调用updateVirtualAccountStatus(status, accountNumber, zainboxCode)
方法会返回一个UpdateVirtualAccountStatusResponse?
的Future,我们需要等待实际的响应。
final UpdateVirtualAccountStatusResponse? response = await zainpay.updateVirtualAccountStatus(status, accountNumber, zainboxCode);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
初始化卡支付
调用initializeCardPayment(amount, txnRef, mobileNumber, zainboxCode, email, callbackUrl)
方法会返回一个CardPaymentResponse?
的Future,我们需要等待实际的响应。
final CardPaymentResponse? response = await zainpay.initializeCardPayment(amount, txnRef, mobileNumber, zainboxCode, email, callbackUrl);
if (response != null) {
showLoading(response.status!);
debugPrint("${response.toJson()}");
} else {
showLoading("No Response!");
}
更多关于Flutter支付集成插件zainpay的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付集成插件zainpay的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
ZainPay 是一个用于 Flutter 应用程序的支付集成插件,允许开发者轻松地将支付功能集成到他们的应用中。以下是如何在 Flutter 项目中使用 ZainPay 插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 ZainPay 插件的依赖。
dependencies:
flutter:
sdk: flutter
zainpay: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 初始化 ZainPay
在你的 Flutter 项目中,首先需要初始化 ZainPay。通常,你可以在 main.dart
文件中进行初始化。
import 'package:flutter/material.dart';
import 'package:zainpay/zainpay.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 ZainPay
await ZainPay.initialize(
apiKey: 'YOUR_API_KEY', // 替换为你的 API Key
environment: Environment.sandbox, // 使用沙盒环境进行测试
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ZainPay Example',
home: PaymentScreen(),
);
}
}
3. 发起支付
在你的支付页面中,你可以使用 ZainPay 插件来发起支付请求。
import 'package:flutter/material.dart';
import 'package:zainpay/zainpay.dart';
class PaymentScreen extends StatelessWidget {
Future<void> _makePayment() async {
try {
final response = await ZainPay.makePayment(
amount: 1000, // 支付金额
currency: 'NGN', // 货币类型
email: 'user@example.com', // 用户邮箱
phoneNumber: '1234567890', // 用户手机号
reference: 'unique_reference', // 唯一参考号
callbackUrl: 'https://example.com/callback', // 回调 URL
);
if (response.status == 'success') {
// 支付成功
print('Payment successful: ${response.transactionId}');
} else {
// 支付失败
print('Payment failed: ${response.message}');
}
} catch (e) {
// 处理异常
print('Error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ZainPay Payment'),
),
body: Center(
child: ElevatedButton(
onPressed: _makePayment,
child: Text('Make Payment'),
),
),
);
}
}
4. 处理支付回调
ZainPay 支持通过回调 URL 来通知支付结果。你需要在服务器端处理回调请求,并根据支付结果更新订单状态。
5. 测试与发布
在开发过程中,你可以使用 ZainPay 的沙盒环境进行测试。确保在发布应用之前切换到生产环境,并使用真实的 API Key。
await ZainPay.initialize(
apiKey: 'YOUR_PRODUCTION_API_KEY',
environment: Environment.production,
);