Flutter支付集成插件flutter_edfapay_softpos_sdk的使用
Flutter支付集成插件flutter_edfapay_softpos_sdk的使用
帮助开发者轻松将EdfaPay SoftPos集成到Flutter移动应用中。
请参考以下链接:
让我们开始
维基与文档
https://github.com/edfapay/edfapay-softpos-sdk-examples/tree/flutter https://github.com/edfapay/edfapay-softpos-sdk-examples/wiki
### 示例代码
```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_edfapay_softpos_sdk/edfapay_softpos_sdk.dart';
import 'helper_methods.dart';
/* 添加插件 https://pub.dev/packages/hexcolor */
import 'package:hexcolor/hexcolor.dart';
const authCode = "a2FzaGlmLXNka3VzZXJAZWRmYXBheS5jb206d0lWS2dZTnFAeDUySjQ1";
const logoPath = "assets/images/edfa_logo.png";
const amountToPay = "01.010";
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _edfaPluginInitiated = false;
[@override](/user/override)
void initState() {
super.initState();
initiate();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.all(15),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 20),
Expanded(
flex: 2,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FractionallySizedBox(
widthFactor: 0.3,
child: Image.asset(logoPath),
),
SizedBox(height: 30),
const Text(
"SDK",
style: TextStyle(fontSize: 65, fontWeight: FontWeight.w700),
textAlign: TextAlign.center,
),
SizedBox(height: 10),
const Text(
"v0.0.1",
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
],
),
),
const Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.all(10),
child: Text(
"您正在启用您的Android应用程序,以便客户可以非常方便地进行支付。只需点击付款按钮并轻触带有NFC功能的Android手机即可。",
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w400, color: Colors.black45),
textAlign: TextAlign.center,
),
),
),
ElevatedButton(
onPressed: pay,
style: ButtonStyle(backgroundColor: MaterialStatePropertyAll(HexColor("06E59F"))),
child: const Text("Pay $amountToPay", style: TextStyle(color: Colors.black)),
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text(
"点击上面的按钮以测试10.00 SAR的卡处理。",
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
textAlign: TextAlign.center,
),
),
],
),
),
),
);
}
initiate() async {
EdfaPayPlugin.enableLogs(true);
EdfaPayPlugin.initiate(authCode: authCode, environment: Env.STAGING).then((value) {
if (value) {
setTheme();
}
setState(() {
_edfaPluginInitiated = value;
});
}).catchError((e) {
if (e is PlatformException) {
toast(e.message ?? e.code);
} else {
toast("Error Initializing SDK");
}
});
}
setTheme() async {
final logo = await assetsBase64(logoPath);
EdfaPayPlugin.theme
.setButtonBackgroundColor("#06E59F")
.setButtonTextColor("#000000")
.setPoweredByImage(logo)
.setHeaderImage(logo);
}
pay() async {
if (!_edfaPluginInitiated) {
toast("Edfapay插件未初始化。");
return;
}
final params = TxnParams(
amount: amountToPay,
transactionType: TransactionType.purchase,
);
EdfaPayPlugin.enableLogs(true);
EdfaPayPlugin.pay(
params,
onPaymentProcessComplete: (status, code, result, isProcessComplete) {
toast("卡支付过程完成");
print('>>> Payment Process Complete');
},
onServerTimeOut: () {
toast("服务器请求超时");
print('>>> Server Timeout');
print(' >>> 请求超时而执行交易后端');
},
onScanCardTimeOut: () {
toast("卡扫描超时");
print('>>> 扫描卡超时');
print(' >>> 卡扫描超时,设备上没有任何卡轻触');
},
onCancelByUser: () {
toast("用户取消");
print('>>> 用户取消');
print(' >>> 用户自行选择取消扫描/支付过程');
},
onError: (Exception error) {
toast(error.toString());
print('>>> 异常');
print(' >>> 扫描/支付过程中发生异常,请检查日志');
print(' >>> ${error.toString()}');
},
);
}
}
更多关于Flutter支付集成插件flutter_edfapay_softpos_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付集成插件flutter_edfapay_softpos_sdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_edfapay_softpos_sdk
是一个用于在 Flutter 应用中集成 EDFAPay SoftPOS 支付的插件。SoftPOS 是一种允许智能手机或平板电脑作为支付终端的技术,EDFAPay 提供了一种便捷的支付解决方案。
以下是如何在 Flutter 项目中使用 flutter_edfapay_softpos_sdk
的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 flutter_edfapay_softpos_sdk
插件的依赖。
dependencies:
flutter:
sdk: flutter
flutter_edfapay_softpos_sdk: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化 SDK
在使用 SDK 之前,你需要对其进行初始化。通常,你需要在应用的启动阶段进行初始化。
import 'package:flutter_edfapay_softpos_sdk/flutter_edfapay_softpos_sdk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 EDFAPay SoftPOS SDK
await FlutterEdfapaySoftposSdk.initialize(
apiKey: 'your_api_key',
merchantId: 'your_merchant_id',
environment: Environment.sandbox, // 或 Environment.production
);
runApp(MyApp());
}
3. 配置支付
接下来,你可以配置支付参数并进行支付。以下是一个简单的支付流程示例:
import 'package:flutter/material.dart';
import 'package:flutter_edfapay_softpos_sdk/flutter_edfapay_softpos_sdk.dart';
class PaymentScreen extends StatefulWidget {
[@override](/user/override)
_PaymentScreenState createState() => _PaymentScreenState();
}
class _PaymentScreenState extends State<PaymentScreen> {
String _paymentStatus = 'Pending';
Future<void> _processPayment() async {
try {
final paymentResult = await FlutterEdfapaySoftposSdk.processPayment(
amount: 100.0, // 支付金额
currency: 'USD', // 货币类型
description: 'Test Payment', // 支付描述
);
setState(() {
_paymentStatus = paymentResult.status;
});
if (paymentResult.status == 'Success') {
// 支付成功处理
} else {
// 支付失败处理
}
} catch (e) {
setState(() {
_paymentStatus = 'Error: ${e.toString()}';
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('EDFAPay Payment'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Payment Status: $_paymentStatus'),
SizedBox(height: 20),
ElevatedButton(
onPressed: _processPayment,
child: Text('Process Payment'),
),
],
),
),
);
}
}