Flutter支付功能插件zsdk的使用
Flutter支付功能插件zsdk的使用
特点
功能 | iOS | Android |
---|---|---|
支付 | ✅ | ✅ |
获取支付状态 | ✅ | ✅ |
设置支付参数 | ✅ | ✅ |
重置支付参数 | ✅ | ✅ |
检查支付设备状态 | ✅ | ✅ |
#### 注意事项
目前此插件仅支持通过TCP/IP连接到支付设备。
iOS设置
target 'Runner' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
Android设置
无需设置
#### 如何使用
##### 添加依赖
```yaml
# 在你的flutter项目依赖中添加以下行
zsdk: ^2.0.0+11
然后运行flutter pub get
以下载库源码。
初始化zsdk对象
final zsdk = ZSDK();
开始支付校准
zsdk.doPaymentCalibrationOverTCPIP(
address: '10.0.0.100',
port: 9100 //可选
);
获取支付状态
zsdk.getPaymentStatusOverTCPIP(
address: '10.0.0.100',
port: 9100 //可选
).then(value) {
final paymentStatus = PaymentResponse.fromMap(value).status;
};
设置支付参数
zsdk.setPaymentParametersOverTCPIP(
address: '10.0.0.100',
port: 9100, //可选
parameters: PaymentParameters(
amount: 100,
currency: Currency.USD,
cardType: CardType.VISA,
// 其他参数...
)
).then(value) {
final paymentResponse = PaymentResponse.fromMap(value);
if(paymentResponse.errorCode == ErrorCode.SUCCESS) {
// 做一些操作
} else {
Status status = paymentResponse.statusInfo.status;
Cause cause = paymentResponse.statusInfo.cause;
// 做一些操作
}
}
重置支付参数
zsdk.setPaymentParametersOverTCPIP(
address: '10.0.0.100',
port: 9100, //可选
parameters: PaymentParameters.defaultParameters()
).then(value) {
final paymentResponse = PaymentResponse.fromMap(value);
if(paymentResponse.errorCode == ErrorCode.SUCCESS) {
// 做一些操作
} else {
Status status = paymentResponse.statusInfo.status;
Cause cause = paymentResponse.statusInfo.cause;
// 做一些操作
}
}
检查支付设备状态
zsdk.checkPaymentDeviceStatusOverTCPIP(
address: '10.0.0.100',
port: 9100, //可选
).then(value) {
final paymentResponse = PaymentResponse.fromMap(value);
Status status = paymentResponse.statusInfo.status;
print(status);
if(paymentResponse.errorCode == ErrorCode.SUCCESS) {
// 做一些操作
} else {
Cause cause = paymentResponse.statusInfo.cause;
print(cause);
}
}
完整示例Demo
import 'package:flutter/material.dart';
import 'package:zsdk/zsdk.dart' as Payment;
import 'dart:io';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
final Payment.ZSDK zsdk = Payment.ZSDK();
MyApp({super.key});
[@override](/user/override)
State createState() => _MyAppState();
}
enum OperationStatus {
SENDING,
RECEIVING,
SUCCESS,
ERROR,
NONE,
}
class _MyAppState extends State<MyApp> {
final addressIpController = TextEditingController(text: "10.0.0.100");
final addressPortController = TextEditingController();
final amountController = TextEditingController();
final currencyController = TextEditingController();
final cardTypeController = TextEditingController();
Payment.PaymentParameters? parameters;
String? message;
OperationStatus paymentStatus = OperationStatus.NONE;
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('支付插件zsdk示例应用'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'支付操作',
style: TextStyle(fontSize: 18),
),
const Divider(
color: Colors.transparent,
),
Card(
elevation: 4,
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Column(
children: <Widget>[
const Text(
'支付金额',
style: TextStyle(fontSize: 16),
),
TextField(
controller: amountController,
decoration: const InputDecoration(labelText: "金额"),
),
const SizedBox(
height: 16,
),
const Text(
'货币类型',
style: TextStyle(fontSize: 16),
),
TextField(
controller: currencyController,
decoration: const InputDecoration(labelText: "货币类型"),
),
const SizedBox(
height: 16,
),
const Text(
'卡类型',
style: TextStyle(fontSize: 16),
),
TextField(
controller: cardTypeController,
decoration: const InputDecoration(labelText: "卡类型"),
),
],
),
),
),
const SizedBox(
height: 16,
),
Card(
elevation: 4,
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Column(
children: <Widget>[
const Text(
'支付设备地址',
style: TextStyle(fontSize: 16),
),
TextField(
controller: addressIpController,
decoration: const InputDecoration(labelText: "设备IP地址"),
),
TextField(
controller: addressPortController,
decoration: const InputDecoration(labelText: "设备端口(默认为9100)"),
),
const SizedBox(
height: 16,
),
Visibility(
visible: paymentStatus != OperationStatus.NONE,
child: Column(
children: <Widget>[
Text(
"$message",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: getOperationStatusColor(paymentStatus)),
),
const SizedBox(
height: 16,
),
],
),
),
ElevatedButton(
onPressed: paymentStatus == OperationStatus.SENDING
? null
: () => onClick('btnSetPaymentParameters'),
child: Text(
"设置支付参数".toUpperCase(),
textAlign: TextAlign.center,
),
),
],
),
),
),
Card(
elevation: 4,
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Column(
children: <Widget>[
const Text(
'检查支付设备状态',
style: TextStyle(fontSize: 16),
),
ElevatedButton(
onPressed: paymentStatus == OperationStatus.SENDING
? null
: () => onClick('btnCheckPaymentDeviceStatus'),
child: Text(
"检查状态".toUpperCase(),
textAlign: TextAlign.center,
),
),
],
),
),
),
],
),
),
),
);
}
Color getOperationStatusColor(OperationStatus status) {
switch (status) {
case OperationStatus.RECEIVING:
case OperationStatus.SENDING:
return Colors.blue;
case OperationStatus.SUCCESS:
return Colors.green;
case OperationStatus.ERROR:
return Colors.red;
default:
return Colors.black;
}
}
void updateParameters(Payment.PaymentParameters? newParameters) {
parameters = newParameters;
amountController.text = "${parameters?.amount ?? ""}";
currencyController.text = "${parameters?.currency ?? ""}";
cardTypeController.text = "${parameters?.cardType ?? ""}";
}
onClick(String id) async {
try {
switch (id) {
case 'btnSetPaymentParameters':
setState(() {
message = "设置支付参数...";
paymentStatus = OperationStatus.SENDING;
});
widget.zsdk
.setPaymentParametersOverTCPIP(
address: addressIpController.text,
port: int.tryParse(addressPortController.text),
parameters: Payment.PaymentParameters(
amount: double.parse(amountController.text),
currency: Currency.fromString(currencyController.text),
cardType: CardType.fromString(cardTypeController.text),
// 其他参数...
),
)
.then((value) {
setState(() {
paymentStatus = OperationStatus.SUCCESS;
message = "$value";
updateParameters(Payment.PaymentResponse.fromMap(value).parameters);
});
}, onError: (error, stacktrace) {
try {
throw error;
} on PlatformException catch (e) {
Payment.PaymentResponse paymentResponse;
try {
paymentResponse = Payment.PaymentResponse.fromMap(e.details);
message =
"${paymentResponse.message} ${paymentResponse.errorCode} ${paymentResponse.statusInfo.status} ${paymentResponse.statusInfo.cause}";
} catch (e) {
print(e);
message = e.toString();
}
} on MissingPluginException catch (e) {
message = "${e.message}";
} catch (e) {
message = e.toString();
}
setState(() {
paymentStatus = OperationStatus.ERROR;
});
});
break;
case 'btnCheckPaymentDeviceStatus':
setState(() {
message = "检查支付设备状态...";
paymentStatus = OperationStatus.RECEIVING;
});
widget.zsdk
.checkPaymentDeviceStatusOverTCPIP(
address: addressIpController.text,
port: int.tryParse(addressPortController.text),
)
.then((value) {
setState(() {
paymentStatus = OperationStatus.SUCCESS;
Payment.PaymentResponse? paymentResponse;
if (value != null) {
paymentResponse = Payment.PaymentResponse.fromMap(value);
}
message =
"${paymentResponse != null ? paymentResponse.toMap() : value}";
});
}, onError: (error, stacktrace) {
try {
throw error;
} on PlatformException catch (e) {
Payment.PaymentResponse paymentResponse;
try {
paymentResponse = Payment.PaymentResponse.fromMap(e.details);
message =
"${paymentResponse.message} ${paymentResponse.errorCode} ${paymentResponse.statusInfo.status} ${paymentResponse.statusInfo.cause}";
} catch (e) {
print(e);
message = e.toString();
}
} on MissingPluginException catch (e) {
message = "${e.message}";
} catch (e) {
message = e.toString();
}
setState(() {
paymentStatus = OperationStatus.ERROR;
});
});
break;
}
} catch (e) {
print(e);
showSnackBar(e.toString());
}
}
void showSnackBar(String message) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(message)));
}
}
更多关于Flutter支付功能插件zsdk的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复