Flutter支付功能插件mpayment的使用
Flutter支付功能插件mpayment的使用
Midtrans支付网关插件
请在使用此库之前阅读Midtrans文档,并确保你已经拥有Midtrans账户以访问仪表板。
Android设置
仅支持AndroidX且最低编译目标为28。
在你的android/app/src/main/res/values/styles.xml
文件中添加样式:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
完整的styles.xml
文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- 显示活动上的启动屏幕。当Flutter绘制其第一帧时会自动移除 -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
然后在你的AndroidManifest.xml文件的应用标签中添加样式:
android:theme="@style/AppTheme"
iOS设置
无需特定设置。
示例用法
确保你已经准备了这些变量:
YOUR_CLIENT_ID
: Midtrans客户端IDYOUR_URL_BASE
: 用于处理支付的后端URL基础API
示例代码如下:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mpayment/mpayment.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isMakePayment = false;
final mpayment = Mpayment();
[@override](/user/override)
void initState() {
super.initState();
mpayment.init("YOUR_CLIENT_ID", "YOUR_URL_BASE");
mpayment.setFinishCallback(_callback);
}
_makePayment() {
setState(() {
isMakePayment = true;
});
mpayment
.makePayment(
MidtransTransaction(
7500,
MidtransCustomer(
"Apin", "Prastya", "apin.klas@gmail.com", "085235419949"),
[
MidtransItem(
"5c18ea1256f67560cb6a00cdde3c3c7a81026c29",
7500,
2,
"USB FlashDisk",
)
],
skipCustomer: true,
customField1: "ANYCUSTOMFIELD"),
)
.catchError((err) => print("ERROR $err"));
}
Future<void> _callback(TransactionFinished finished) async {
setState(() {
isMakePayment = false;
});
return Future.value(null);
}
[@override](/user/override)
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: isMakePayment
? CircularProgressIndicator()
: RaisedButton(
child: Text("Make Payment"),
onPressed: () => _makePayment(),
),
),
),
);
}
}
更多关于Flutter支付功能插件mpayment的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付功能插件mpayment的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
mpayment
是一个用于在 Flutter 应用中集成支付功能的插件。它支持多种支付方式,如支付宝、微信支付等。以下是如何在 Flutter 项目中使用 mpayment
插件的步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 mpayment
插件的依赖:
dependencies:
flutter:
sdk: flutter
mpayment: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取插件。
2. 配置支付平台
Android 配置
在 android/app/src/main/AndroidManifest.xml
文件中添加以下权限和配置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
iOS 配置
在 ios/Runner/Info.plist
文件中添加以下配置:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
3. 初始化支付插件
在你的 Dart 代码中,首先导入 mpayment
插件:
import 'package:mpayment/mpayment.dart';
然后在应用启动时初始化支付插件:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Mpayment.init();
runApp(MyApp());
}
4. 调用支付功能
你可以通过 Mpayment
类提供的 API 来调用支付功能。以下是一个简单的示例,展示如何使用支付宝支付:
void payWithAlipay() async {
try {
// 替换为你的支付订单信息
String orderInfo = "your_order_info_here";
// 调用支付宝支付
final result = await Mpayment.alipay(orderInfo);
// 处理支付结果
if (result == '9000') {
print("支付成功");
} else {
print("支付失败");
}
} catch (e) {
print("支付异常: $e");
}
}
5. 处理支付结果
支付结果会通过 Mpayment.alipay
方法返回。你可以根据返回的状态码来判断支付是否成功。
6. 其他支付方式
mpayment
插件还支持其他支付方式,如微信支付。你可以使用类似的方式调用这些支付方式:
void payWithWeChat() async {
try {
// 替换为你的支付订单信息
String orderInfo = "your_order_info_here";
// 调用微信支付
final result = await Mpayment.wechatPay(orderInfo);
// 处理支付结果
if (result == '0') {
print("支付成功");
} else {
print("支付失败");
}
} catch (e) {
print("支付异常: $e");
}
}