Flutter支付集成插件flutter_flexible_pay的使用
Flutter支付集成插件flutter_flexible_pay的使用
通过使用flutter_flexible_pay
插件,您可以轻松地在全球范围内通过Google Pay和Apple Pay进行Stripe支付。该插件支持iOS和Android平台。
使用说明
AndroidManifest
在您的AndroidManifest.xml
文件中添加以下元数据:
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
加载支付配置
首先,您需要加载支付配置。这通常在应用程序启动时完成。以下是如何加载支付配置的示例代码:
import 'package:flutter_flexible_pay/flutter_flexible_pay.dart';
/// 这个示例文件用于实现Stripe支付
/// 对于其他支付方式,移除"stripe:*"键并替换为"gatewayMerchantId"
/// 查看项目示例以查看example/assets文件夹中的payment_profile_google_pay.json和payment_profile_apple_pay.json的内容
Future<void> loadConfiguration() async {
Map<String, dynamic> profiles = {
'google': 'assets/configurations/payment_profile_google_pay.json',
'apple': 'assets/configurations/payment_profile_apple_pay.json',
};
FlutterFlexiblePay.setPaymentConfig(profiles);
}
[@override](/user/override)
void initState() {
super.initState();
loadConfiguration();
}
执行支付
接下来,您可以通过调用FlutterFlexiblePay.makePayment
方法来执行支付。以下是如何执行支付的示例代码:
_makePayment(dynamic product) async {
var environment = 'test'; // 或者 'production'
if (!(await FlutterFlexiblePay.isAvailable(environment))) {
_showToast(scaffoldContext, "Google或Apple Pay不可用!");
} else {
PaymentItem product = PaymentItem(
countryCode: "US",
currencyCode: "USD",
amount: product["amount"], // 字符串
label: product["name"], // 产品名称或标签
);
FlutterFlexiblePay.makePayment(product).then((Result result) {
if (result.status == ResultStatus.SUCCESS) {
_showToast(scaffoldContext, result.description);
}
if (result.status == ResultStatus.RESULT_CANCELED) {
_showToast(scaffoldContext, result.error);
}
if (result.status == ResultStatus.ERROR) {
_showToast(scaffoldContext, result.error);
}
if (result.status == ResultStatus.UNKNOWN) {
_showToast(scaffoldContext, '未知错误');
}
}).catchError((dynamic error) {
_showToast(scaffoldContext, error.toString());
});
}
}
创建自定义支付数据的文档
有关创建自定义支付数据的详细信息,请参阅以下链接:
完整示例代码
以下是完整的示例代码,展示了如何集成flutter_flexible_pay
插件:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_flexible_pay/flutter_flexible_pay.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late BuildContext scaffoldContext;
/// 这个示例文件用于实现Stripe支付
/// 对于其他支付方式,移除"stripe:*"键并替换为"gatewayMerchantId"
Future<void> preparePaymentConfig() async {
Map<String, dynamic> profiles = {
'google': 'assets/configurations/payment_profile_google_pay.json',
'apple': 'assets/configurations/payment_profile_apple_pay.json',
};
FlutterFlexiblePay.setPaymentConfig(profiles);
}
[@override](/user/override)
void initState() {
super.initState();
preparePaymentConfig();
}
_makeStripePayment() async {
var environment = 'test'; // 或者 'production'
if (!(await FlutterFlexiblePay.isAvailable(environment))) {
_showToast(scaffoldContext, "Google或Apple Pay不可用!");
} else {
PaymentItem product = PaymentItem(
countryCode: "US",
currencyCode: "USD",
amount: "0.80",
label: "Shirt",
);
/// 使用异步[FlutterFlexiblePay]进行支付
FlutterFlexiblePay.makePayment(product).then((Result result) {
if (result.status == ResultStatus.SUCCESS) {
_showToast(scaffoldContext, result.description);
}
if (result.status == ResultStatus.RESULT_CANCELED) {
_showToast(scaffoldContext, result.error);
}
if (result.status == ResultStatus.ERROR) {
_showToast(scaffoldContext, result.error);
}
if (result.status == ResultStatus.UNKNOWN) {
_showToast(scaffoldContext, '未知错误');
}
}).catchError((dynamic error) {
_showToast(scaffoldContext, error.toString());
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Flexible Pay'),
),
body: Builder(builder: (context) {
scaffoldContext = context;
return Center(
child: Column(
children: <Widget>[
TextButton(
onPressed: _makeStripePayment,
child: const Text('Make payment'),
),
],
),
);
}),
),
);
}
void _showToast(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
action: SnackBarAction(
label: 'UNDO',
onPressed: () {},
),
));
}
}
更多关于Flutter支付集成插件flutter_flexible_pay的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付集成插件flutter_flexible_pay的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_flexible_pay
是一个用于在 Flutter 应用程序中集成多种支付方式的插件。它支持多种支付平台,如支付宝、微信支付、Apple Pay、Google Pay 等。使用这个插件可以简化支付流程,开发者只需调用统一的 API 即可完成支付操作。
1. 安装插件
首先,在 pubspec.yaml
文件中添加 flutter_flexible_pay
依赖:
dependencies:
flutter:
sdk: flutter
flutter_flexible_pay: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
2. 配置支付平台
根据你需要的支付平台,进行相应的配置。以下是一些常见支付平台的配置示例:
支付宝配置
在 AndroidManifest.xml
中添加以下配置:
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
</activity>
<activity
android:name="com.alipay.sdk.app.H5AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
</activity>
微信支付配置
在 AndroidManifest.xml
中添加以下配置:
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop">
</activity>
并且在 res
目录下创建 xml
文件夹,添加 file_paths.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
3. 初始化支付插件
在你的 Flutter 应用中,初始化 flutter_flexible_pay
插件:
import 'package:flutter_flexible_pay/flutter_flexible_pay.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterFlexiblePay.init(
aliPayScheme: 'your_alipay_scheme', // 支付宝 Scheme
wxPayAppId: 'your_wxpay_appid', // 微信支付 AppId
);
runApp(MyApp());
}
4. 发起支付
使用 flutter_flexible_pay
发起支付请求。以下是一个简单的支付示例:
import 'package:flutter/material.dart';
import 'package:flutter_flexible_pay/flutter_flexible_pay.dart';
class PaymentPage extends StatelessWidget {
Future<void> _pay() async {
try {
final result = await FlutterFlexiblePay.aliPay(
orderInfo: 'your_order_info', // 支付宝订单信息
);
if (result['resultStatus'] == '9000') {
// 支付成功
print('支付成功');
} else {
// 支付失败
print('支付失败: ${result['memo']}');
}
} catch (e) {
print('支付异常: $e');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('支付'),
),
body: Center(
child: ElevatedButton(
onPressed: _pay,
child: Text('发起支付'),
),
),
);
}
}