Flutter支付集成插件yjy_viva_wallet_pos的使用
Flutter支付集成插件yjy_viva_wallet_pos的使用
Viva Wallet POS Flutter
这是用于Android与Viva Wallet POS集成的Flutter插件。
可以从以下链接下载POS应用程序:
https://play.google.com/store/apps/details?id=com.vivawallet.spoc.payapp
为了获取演示版本的访问权限,必须使用Viva Wallet Pos App的演示版本。请联系VivaWallet以获取演示访问权限。
此为初始版本,目前正在进行测试。
目前仅支持Android平台。
欢迎在github上为该项目贡献您的力量。
安装
使用此包作为库
1. 添加依赖项
将以下内容添加到项目的pubspec.yaml
文件中:
dependencies:
yjy_viva_wallet_pos: ^0.0.1
2. 安装包
可以通过命令行安装包:
运行以下命令:
flutter packages get
使用入门
使用库
请查看示例项目以了解如何使用库的基本功能。
可以参考官方开发者页面:
https://developer.vivawallet.com/apis-for-point-of-sale/card-terminal-apps/android-app/
插件中的所有函数和参数名称均与官方文档一致。
示例代码:
import 'package:yjy_viva_wallet_pos/viva_wallet_pos.dart';
VivaWalletPos pos = VivaWalletPos.instance;
try {
TransactionResponse response = await pos.sale(
clientTransactionId: 'Invoice 1234',
amount: 10.00,
showRating: false,
showReceipt: true,
showTransactionResult: false,
);
_resultMessage(response.message);
} catch (e) {
debugPrint(e.toString());
}
支持的方法
- ✅ activatePos
- ✅ setMode
- ✅ setPrintingSettings
- ✅ sendLogs
- ✅ sale
- ✅ cancel
- ✅ abort
示例代码
以下是完整的示例代码,展示如何在Flutter应用中使用该插件。
文件:example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:yjy_viva_wallet_pos/viva_wallet_pos.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _pos = VivaWalletPos.instance;
String _message = 'Select action';
Color _color = Colors.green;
[@override](/user/override)
void initState() {
super.initState();
}
void _resultMessage(String message, {bool error = false}) {
setState(() {
_message = message;
_color = error ? Colors.red : Colors.green;
});
}
void _activatePos() async {
try {
ActivationResponse response = await _pos.activatePos(apikey: 'API KEY', apiSecret: 'API SECREET');
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _sale() async {
try {
TransactionResponse response = await _pos.sale(
clientTransactionId: 'Invoice 1234',
amount: 10.00,
showRating: false,
showReceipt: false,
showTransactionResult: false,
);
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _cancel() async {
try {
TransactionResponse response = await _pos.cancel(
amount: 10.00,
showRating: false,
showReceipt: false,
showTransactionResult: false,
);
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _abort() async {
try {
AbortResponse response = await _pos.abort();
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _setMode() async {
try {
SetModeResponse response = await _pos.setMode(
mode: ApplicationMode.attended,
pin: '1234',
);
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _setPrintingSettings() async {
try {
SetPrintingSettingsResponse response = await _pos.setPrintingSettings(
businessDescriptionEnabled: true,
businessDescriptionType: BusinessDescriptionType.storeName,
);
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
void _sendLogs() async {
try {
SendLogsResponse response = await _pos.sendLogs();
_resultMessage(response.message);
} catch (e) {
_resultMessage(e.toString(), error: true);
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('VW Pos Plugin example'),
),
body: ListView(children: <Widget>[
Padding(
padding: const EdgeInsets.all(10),
child: Container(
height: 80,
color: _color,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: Center(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(_message),
),
),
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Activate POS', style: TextStyle(fontSize: 18)),
onPressed: () async {
_activatePos();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Sale 10.00', style: TextStyle(fontSize: 18)),
onPressed: () {
_sale();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Cancel - refund 10.00', style: TextStyle(fontSize: 18)),
onPressed: () {
_cancel();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Abort request', style: TextStyle(fontSize: 18)),
onPressed: () {
_abort();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Set Mode', style: TextStyle(fontSize: 18)),
onPressed: () {
_setMode();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Send logs', style: TextStyle(fontSize: 18)),
onPressed: () {
_sendLogs();
},
),
),
Container(
height: 70,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: ElevatedButton(
child: const Text('Set printing settings', style: TextStyle(fontSize: 18)),
onPressed: () {
_setPrintingSettings();
},
),
),
]),
),
);
}
}
更多关于Flutter支付集成插件yjy_viva_wallet_pos的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付集成插件yjy_viva_wallet_pos的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
yjy_viva_wallet_pos
是一个用于在 Flutter 应用中集成 Viva Wallet POS 支付的插件。通过这个插件,开发者可以方便地在应用中实现支付功能。以下是如何使用 yjy_viva_wallet_pos
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 yjy_viva_wallet_pos
插件的依赖:
dependencies:
flutter:
sdk: flutter
yjy_viva_wallet_pos: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:yjy_viva_wallet_pos/yjy_viva_wallet_pos.dart';
3. 初始化插件
在使用插件之前,通常需要对其进行初始化。你可以通过以下方式初始化插件:
void initVivaWalletPOS() async {
await YjyVivaWalletPos.init(
apiKey: 'your_api_key', // 你的 Viva Wallet API Key
merchantId: 'your_merchant_id', // 你的 Merchant ID
posId: 'your_pos_id', // 你的 POS ID
);
}
4. 发起支付
接下来,你可以使用插件提供的功能来发起支付。以下是一个简单的示例:
void makePayment() async {
try {
PaymentResponse response = await YjyVivaWalletPos.makePayment(
amount: 1000, // 支付金额,单位为分
currency: 'EUR', // 货币代码
description: 'Test Payment', // 支付描述
);
if (response.isSuccess) {
print('Payment successful: ${response.transactionId}');
} else {
print('Payment failed: ${response.errorMessage}');
}
} catch (e) {
print('Error: $e');
}
}