Flutter支付集成插件fincra_payment的使用
Flutter支付集成插件fincra_payment的使用
Fincra Payment Package
一个用于集成Fincra支付结算的Flutter包,使开发者能够通过借记卡和信用卡进行支付,并在支付成功后提供自定义网络调用的钩子。
特性
- 轻松集成Fincra支付API。
- 支持通过借记卡和信用卡付款。
- 支付成功后的自定义网络调用钩子。
- 可定制的支付流程以满足各种应用需求。
开始使用
前提条件
在使用此包之前,请确保已具备以下条件:
-
已设置好Flutter项目。如果没有,请运行以下命令创建并进入项目:
flutter create my_flutter_app cd my_flutter_app
-
拥有Fincra账户及必要的API密钥。您可以在此处注册账户。
安装
在pubspec.yaml
文件中添加以下依赖项:
dependencies:
fincra_payment: ^1.0.0
然后运行以下命令以获取该包:
flutter pub get
使用方法
以下是一个简单的示例,展示如何使用Fincra支付包:
import 'package:fincra_payment/fincra_payment.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// 应用程序的根组件
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Fincra Payment'),
);
}
}
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> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: ElevatedButton(
onPressed: () {
FincraCheckout.launchFincra(
context,
publicKey: "******************************", // 测试环境使用测试密钥,生产环境使用正式密钥
amount: '100', // 支付金额(单位为分)
name: "Ayanfe Afolabi", // 用户姓名
phoneNumber: '+2347031276982', // 用户电话号码
currency: "NGN", // 币种
email: 'ayanfesolutions@gmail.com', // 用户邮箱
feeBearer: "customer", // 支付手续费承担方:"customer" 或 "buyer"
onSuccess: (data) async {
print('Payment Successful on test mode'); // 支付成功后的回调
// 在此处执行支付成功后的逻辑
},
onError: (data) {
print('Payment Failed'); // 支付失败时的回调
},
onClose: () {
Navigator.of(context).pop(); // 关闭支付窗口
print('Payment Close'); // 支付关闭时的回调
},
);
},
child: const Text('Make Payment'), // 按钮文本
),
),
);
}
}
更多关于Flutter支付集成插件fincra_payment的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付集成插件fincra_payment的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
fincra_payment
是一个用于在 Flutter 应用中集成 Fincra 支付功能的插件。Fincra 是一个支付平台,允许开发者轻松地集成支付功能到他们的应用中。以下是如何在 Flutter 项目中使用 fincra_payment
插件的步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 fincra_payment
插件的依赖。
dependencies:
flutter:
sdk: flutter
fincra_payment: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 初始化插件
在你的 Flutter 应用中,你需要在启动时初始化 fincra_payment
插件。通常,你可以在 main.dart
文件中进行初始化。
import 'package:fincra_payment/fincra_payment.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Fincra 支付插件
await FincraPayment.initialize(
apiKey: 'YOUR_API_KEY', // 替换为你的 Fincra API Key
environment: FincraEnvironment.sandbox, // 使用沙盒环境进行测试
);
runApp(MyApp());
}
3. 发起支付
在你的应用中,你可以使用 FincraPayment
类来发起支付请求。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:fincra_payment/fincra_payment.dart';
class PaymentScreen extends StatelessWidget {
Future<void> initiatePayment() async {
try {
final paymentResponse = await FincraPayment.initiatePayment(
amount: 1000, // 支付金额
currency: 'NGN', // 货币代码
reference: 'unique_reference', // 唯一参考号
customerEmail: 'customer@example.com', // 客户邮箱
customerName: 'John Doe', // 客户姓名
description: 'Payment for goods', // 支付描述
);
// 处理支付响应
if (paymentResponse.status == 'success') {
print('Payment successful: ${paymentResponse.message}');
} else {
print('Payment failed: ${paymentResponse.message}');
}
} catch (e) {
print('Error initiating payment: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fincra Payment'),
),
body: Center(
child: ElevatedButton(
onPressed: initiatePayment,
child: Text('Pay with Fincra'),
),
),
);
}
}
4. 处理支付回调
你可以通过监听支付回调来处理支付结果。fincra_payment
插件通常会返回一个包含支付状态和消息的响应对象。
if (paymentResponse.status == 'success') {
// 支付成功
} else {
// 支付失败
}
5. 测试与生产环境
在开发阶段,你可以使用 Fincra 的沙盒环境进行测试。当你准备好发布应用时,记得将环境切换到生产环境。
await FincraPayment.initialize(
apiKey: 'YOUR_API_KEY',
environment: FincraEnvironment.production, // 切换到生产环境
);
6. 错误处理
确保在支付过程中处理可能出现的错误,例如网络问题、无效的 API Key 等。
try {
final paymentResponse = await FincraPayment.initiatePayment(...);
} catch (e) {
print('Error: $e');
}