Flutter支付插件mi_pay的使用

Flutter支付插件mi_pay的使用

mi_pay

Android小米联运支付SDK

初始化SDK

Future<void> initSDK({
  required String appId,
  required String appKey,
  bool isDebug = false,
});

示例代码:

MiPay().initSDK(
  appId: '2882303761517996496', 
  appKey: '5641799683496', 
  isDebug: true
);

SDK状态回调

mixin SDKCallback {

  void onInitSuccess(String info); // 初始化成功回调
  void onInitFailed(String info); // 初始化失败回调

  void onLoginSuccess(String accountInfo); // 登录成功回调
  void onLoginFailed(int code); // 登录失败回调

  void onPaySuccess(int type); // 支付成功回调
  void onPayFailed(int type, int code, String info); // 支付失败回调
  void onPayCancel(int type); // 支付取消回调
}

示例代码:

class _MyAppState extends State<MyApp> with SDKCallback {
  
  @override
  void onInitSuccess(String info) {
    print("初始化成功: $info");
  }

  @override
  void onInitFailed(String info) {
    print("初始化失败: $info");
  }

  @override
  void onLoginSuccess(String accountInfo) {
    print("登录成功: $accountInfo");
  }

  @override
  void onLoginFailed(int code) {
    print("登录失败: $code");
  }

  @override
  void onPaySuccess(int type) {
    print("支付成功: $type");
  }

  @override
  void onPayFailed(int type, int code, String info) {
    print("支付失败: 类型: $type, 错误码: $code, 信息: $info");
  }

  @override
  void onPayCancel(int type) {
    print("支付取消: $type");
  }
}

获取登录状态

Future<bool> isLogin() ;

示例代码:

bool isLogin = await MiPay().isLogin();
print("当前是否已登录: $isLogin");

登录(自动登录,如果未登录则手动登录)

Future<void> login();

示例代码:

await MiPay().login();

支付(非消耗类商品)

Future<void> payProduct({
  required String productCode,
  required int amount,
  String? orderId,
});

示例代码:

await MiPay().payProduct(
  productCode: 'product_12345',
  amount: 100,
  orderId: 'order_001'
);

支付(消耗型商品)

Future<void> pay({
  required int feeValue,
  String? orderId,
});

示例代码:

await MiPay().pay(
  feeValue: 1000,
  orderId: 'consume_order_001'
);

订阅(非消耗型商品)

Future<void> subscribeProduct({
  required String productCode,
  required int amount,
  String? orderId,
});

示例代码:

await MiPay().subscribeProduct(
  productCode: 'subscription_product_123',
  amount: 50,
  orderId: 'sub_order_001'
);

订阅(消耗型商品)

Future<void> subscribe({required int feeValue, String? orderId})

示例代码:

await MiPay().subscribe(
  feeValue: 2000,
  orderId: 'consume_sub_order_001'
);

更多关于Flutter支付插件mi_pay的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter支付插件mi_pay的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


mi_pay 是一个用于 Flutter 的支付插件,主要集成了小米支付(Mi Pay)的功能。使用该插件,开发者可以轻松地在 Flutter 应用中集成小米支付功能。以下是使用 mi_pay 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 mi_pay 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  mi_pay: ^1.0.0  # 请根据实际版本号填写

然后运行 flutter pub get 来获取依赖。

2. 配置 Android 项目

由于 mi_pay 主要面向 Android 平台,因此需要在 Android 项目中进行一些配置。

2.1 配置 AndroidManifest.xml

android/app/src/main/AndroidManifest.xml 文件中添加以下权限和配置:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2.2 配置 build.gradle

android/app/build.gradle 文件中,确保 minSdkVersion 至少为 16:

android {
    ...
    defaultConfig {
        ...
        minSdkVersion 16
        ...
    }
    ...
}

3. 初始化 Mi Pay 插件

在 Flutter 项目中,首先需要在应用的入口处初始化 mi_pay 插件。

import 'package:mi_pay/mi_pay.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MiPay.init();
  runApp(MyApp());
}

4. 发起支付请求

使用 MiPay.pay 方法发起支付请求。你需要提供订单信息、支付方式等参数。

void payWithMiPay() async {
  try {
    final result = await MiPay.pay(
      orderId: 'your_order_id', // 订单ID
      amount: '100', // 金额,单位为分
      productName: 'Product Name', // 商品名称
      productDesc: 'Product Description', // 商品描述
    );

    if (result['resultCode'] == 'SUCCESS') {
      // 支付成功
      print('Payment successful');
    } else {
      // 支付失败
      print('Payment failed: ${result['resultCode']}');
    }
  } catch (e) {
    // 处理异常
    print('Error: $e');
  }
}

5. 处理支付结果

支付结果会通过 MiPay.pay 方法的返回值返回。你可以根据 resultCode 来判断支付是否成功,并进行相应的处理。

6. 其他功能

mi_pay 插件可能还提供了其他功能,例如查询支付状态、退款等。你可以参考插件的文档或源码来使用这些功能。

7. 注意事项

  • mi_pay 插件主要针对小米设备和小米支付服务,因此在非小米设备上可能无法使用。
  • 确保在小米设备上已安装并配置了小米支付服务。
  • 支付功能涉及到敏感信息,确保在正式环境中使用 HTTPS 进行通信。

8. 示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 中使用 mi_pay 插件进行支付:

import 'package:flutter/material.dart';
import 'package:mi_pay/mi_pay.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MiPay.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Mi Pay Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: payWithMiPay,
            child: Text('Pay with Mi Pay'),
          ),
        ),
      ),
    );
  }
}

void payWithMiPay() async {
  try {
    final result = await MiPay.pay(
      orderId: 'your_order_id',
      amount: '100',
      productName: 'Product Name',
      productDesc: 'Product Description',
    );

    if (result['resultCode'] == 'SUCCESS') {
      print('Payment successful');
    } else {
      print('Payment failed: ${result['resultCode']}');
    }
  } catch (e) {
    print('Error: $e');
  }
}
回到顶部