Flutter支付集成插件flutterwave的使用
Flutter支付集成插件flutterwave的使用
Flutterwave Flutter SDK #
我们已将项目迁移到[@Flutterwave](/user/Flutterwave)/Flutter!#
我们决定将此项目迁移到以改善您的库使用体验。我们没有计划在此项目上进行支持或发布额外的版本。
如果您正在您的Flutter/Dart项目中开始新的集成或打算更新现有的集成,请使用新的仓库。
- 了解如何将包添加到您的项目中这里。
- 从旧仓库迁移到新仓库。
完整示例代码
import 'package:flutter/material.dart';
import 'package:flutterwave/flutterwave.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutterwave Beta'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final formKey = GlobalKey<FormState>();
final amountController = TextEditingController();
final currencyController = TextEditingController();
final narrationController = TextEditingController();
final publicKeyController = TextEditingController();
final encryptionKeyController = TextEditingController();
final emailController = TextEditingController();
final phoneNumberController = TextEditingController();
String selectedCurrency = "";
bool isDebug = true;
[@override](/user/override)
Widget build(BuildContext context) {
this.currencyController.text = this.selectedCurrency;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(20, 10, 20, 10),
child: Form(
key: this.formKey,
child: ListView(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.amountController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
style: TextStyle(color: Colors.black),
decoration: InputDecoration(hintText: "Amount"),
validator: (value) => value.isNotEmpty ? null : "Amount is required",
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.currencyController,
textInputAction: TextInputAction.next,
style: TextStyle(color: Colors.black),
readOnly: true,
onTap: this._openBottomSheet,
decoration: InputDecoration(
hintText: "Currency",
),
validator: (value) => value.isNotEmpty ? null : "Currency is required",
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.publicKeyController,
textInputAction: TextInputAction.next,
style: TextStyle(color: Colors.black),
obscureText: true,
decoration: InputDecoration(
hintText: "Public Key",
),
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.encryptionKeyController,
textInputAction: TextInputAction.next,
style: TextStyle(color: Colors.black),
obscureText: true,
decoration: InputDecoration(
hintText: "Encryption Key",
),
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.emailController,
textInputAction: TextInputAction.next,
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: "Email",
),
validator: (value) => value.isNotEmpty ? null : "Email is required",
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: TextFormField(
controller: this.phoneNumberController,
textInputAction: TextInputAction.next,
style: TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: "Phone Number",
),
validator: (value) => value.isNotEmpty ? null : "Phone Number is required",
),
),
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: Row(
children: [
Text("Use Debug"),
Switch(
onChanged: (value) => {
setState(() {
isDebug = value;
})
},
value: this.isDebug,
),
],
),
),
Container(
width: double.infinity,
height: 50,
margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
child: RaisedButton(
onPressed: this._onPressed,
color: Colors.blue,
child: Text(
"Make Payment",
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
),
);
}
_onPressed() {
if (this.formKey.currentState.validate()) {
this._handlePaymentInitialization();
}
}
_handlePaymentInitialization() async {
final flutterwave = Flutterwave.forUIPayment(
amount: this.amountController.text.toString().trim(),
currency: this.currencyController.text,
context: this.context,
publicKey: this.publicKeyController.text.trim(),
encryptionKey: this.encryptionKeyController.text.trim(),
email: this.emailController.text.trim(),
fullName: "Test User",
txRef: DateTime.now().toIso8601String(),
narration: "Example Project",
isDebugMode: this.isDebug,
phoneNumber: this.phoneNumberController.text.trim(),
acceptAccountPayment: true,
acceptCardPayment: true,
acceptUSSDPayment: true
);
final response = await flutterwave.initializeForUiPayments();
if (response != null) {
this.showLoading(response.data.status);
} else {
this.showLoading("No Response!");
}
}
void _openBottomSheet() {
showModalBottomSheet(
context: this.context,
builder: (context) {
return this._getCurrency();
});
}
Widget _getCurrency() {
final currencies = [
FlutterwaveCurrency.UGX,
FlutterwaveCurrency.GHS,
FlutterwaveCurrency.NGN,
FlutterwaveCurrency.RWF,
FlutterwaveCurrency.KES,
FlutterwaveCurrency.XAF,
FlutterwaveCurrency.XOF,
FlutterwaveCurrency.ZMW
];
return Container(
height: 250,
margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
color: Colors.white,
child: ListView(
children: currencies
.map((currency) => ListTile(
onTap: () => {this._handleCurrencyTap(currency)},
title: Column(
children: [
Text(
currency,
textAlign: TextAlign.start,
style: TextStyle(color: Colors.black),
),
SizedBox(height: 4),
Divider(height: 1)
],
),
))
.toList(),
),
);
}
_handleCurrencyTap(String currency) {
this.setState(() {
this.selectedCurrency = currency;
this.currencyController.text = currency;
});
Navigator.pop(this.context);
}
Future<void> showLoading(String message) {
return showDialog(
context: this.context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
margin: EdgeInsets.fromLTRB(30, 20, 30, 20),
width: double.infinity,
height: 50,
child: Text(message),
),
);
},
);
}
}
更多关于Flutter支付集成插件flutterwave的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter支付集成插件flutterwave的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
Flutterwave 是一个非洲领先的支付网关,提供了多种支付方式的集成,包括信用卡、移动支付等。在 Flutter 应用中使用 Flutterwave 进行支付,可以通过集成 flutterwave
插件来实现。
以下是如何在 Flutter 应用中集成和使用 Flutterwave 支付插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 flutterwave
插件的依赖:
dependencies:
flutter:
sdk: flutter
flutterwave: ^3.0.0
然后运行 flutter pub get
来获取依赖。
2. 配置 Flutterwave
在 Flutterwave 官网上注册并获取你的 API 密钥。你需要在你的应用中使用这些密钥来初始化 Flutterwave。
3. 初始化 Flutterwave
在你的 Dart 文件中导入 flutterwave
插件,并初始化 Flutterwave:
import 'package:flutterwave/flutterwave.dart';
import 'package:flutterwave/models/requests/standard_request.dart';
final flutterwave = Flutterwave.forUIPayment(
context: context, // Your BuildContext
encryptionKey: "YOUR_ENCRYPTION_KEY", // Your Flutterwave Encryption Key
publicKey: "YOUR_PUBLIC_KEY", // Your Flutterwave Public Key
currency: "NGN", // Currency code (e.g., NGN, USD, etc.)
amount: "100", // Amount to charge
email: "user@example.com", // User's email
fullName: "User Name", // User's full name
txRef: "unique_transaction_reference", // Unique transaction reference
isDebugMode: true, // Set to true for test mode, false for production
phoneNumber: "1234567890", // User's phone number (optional)
acceptCardPayment: true, // Whether to accept card payments
acceptUSSDPayment: false, // Whether to accept USSD payments
acceptAccountPayment: false, // Whether to accept account payments
acceptFrancophoneMobileMoney: false, // Whether to accept Francophone mobile money
acceptGhanaPayment: false, // Whether to accept Ghana mobile money
acceptMpesaPayment: false, // Whether to accept Mpesa payments
acceptRwandaMoney: false, // Whether to accept Rwanda mobile money
acceptUgandaPayment: false, // Whether to accept Uganda mobile money
acceptZambiaPayment: false, // Whether to accept Zambia mobile money
);
4. 发起支付
使用 flutterwave
对象发起支付请求,并处理支付结果:
final response = await flutterwave.charge();
if (response != null) {
print("Transaction ID: ${response.transactionId}");
print("Status: ${response.status}");
print("Message: ${response.message}");
if (response.status == "success") {
// Payment was successful
print("Payment successful!");
} else {
// Payment failed or was cancelled
print("Payment failed or was cancelled.");
}
} else {
// No response received
print("No response from Flutterwave.");
}