Flutter支付功能插件abc_pay的使用
Flutter支付功能插件abc_pay的使用
用于安卓、IOS APP 进行农行掌银支付
Android | iOS |
---|---|
SDK 16+ | 9.0+ |
使用
dependencies:
abc_pay: ^1.0.1
示例
import 'package:abc_pay/abc_pay.dart';
/// 进行农行掌银支付
void doAbcPay() async {
final canuse = await AbcPay.isAvaiable();
if (!canuse) {
/// 农业银行掌上银行未安装
return;
}
AbcPay.doPay(
callbackId: "callbackId",
tokenId: "tokenId",
appId: "appId", // 安卓必传
);
}
配置
iOS
在工程 Info.plist
文件内添加如下配置项
<key>LSApplicationQueriesSchemes</key>
<array>
<string>bankabc</string>
</array>
Android
在工程 AndroidManifest.xml
文件内添加如下配置项
<queries>
<package android:name="com.android.bankabc" />
</queries>
# 示例代码
```dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:abc_pay/abc_pay.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> {
String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = (await AbcPay.isAvaiable()) as String;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
更多关于Flutter支付功能插件abc_pay的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter支付功能插件abc_pay的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
abc_pay
是一个用于 Flutter 应用的支付插件,支持多种支付方式,如支付宝、微信支付等。为了在你的 Flutter 应用中使用 abc_pay
插件,你需要按照以下步骤进行配置和使用。
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 abc_pay
插件的依赖:
dependencies:
flutter:
sdk: flutter
abc_pay: ^1.0.0 # 请使用最新的版本号
然后运行 flutter pub get
来获取依赖。
2. 配置支付平台
根据你使用的支付平台(支付宝、微信支付等),你需要进行相应的配置。
2.1 支付宝配置
对于支付宝,你需要在 AndroidManifest.xml
文件中添加以下配置:
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:exported="false"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>
2.2 微信支付配置
对于微信支付,你需要在 AndroidManifest.xml
文件中添加以下配置:
<activity
android:name="com.tencent.mm.opensdk.openapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>
3. 初始化支付插件
在你的 Flutter 应用中初始化 abc_pay
插件:
import 'package:abc_pay/abc_pay.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('ABC Pay Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// 调用支付功能
_startPayment();
},
child: Text('Pay Now'),
),
),
),
);
}
void _startPayment() async {
// 初始化支付插件
await AbcPay.initialize(
appId: 'your_app_id',
partnerId: 'your_partner_id',
privateKey: 'your_private_key',
);
// 发起支付请求
final result = await AbcPay.pay(
orderId: 'your_order_id',
amount: '100.00',
subject: 'Test Payment',
body: 'This is a test payment',
);
// 处理支付结果
if (result['status'] == 'success') {
print('Payment successful');
} else {
print('Payment failed: ${result['message']}');
}
}
}