Flutter支付插件uppay的使用

Flutter支付插件uppay的使用

开始

本项目是一个新的Flutter插件项目,专门包含Android和/或iOS平台的特定实现代码。

获取开始

对于Flutter开发的帮助,可以查看在线文档,其中包括教程、示例、移动开发指南和完整的API引用。

示例代码

以下是使用uppay插件的基本示例代码:

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

import 'package:flutter/services.dart';
import 'package:uppay/uppay.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _uppayPlugin = Uppay();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,所以我们初始化在一个异步方法中。
  Future<void> initPlatformState() async {
    String platformVersion;
    // 平台消息可能会失败,所以我们使用try/catch来捕获PlatformException。
    // 我们还处理了消息可能返回null的情况。
    try {
      platformVersion =
          await _uppayPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // 如果小部件在异步平台消息飞行时从树中移除,我们希望丢弃回复而不是调用setState来更新我们的不存在的外观。
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  _isPaymentAppInstalled() async {
    bool? installed = await _uppayPlugin.isPaymentAppInstalled('00', '123456');
    if (installed != null) {
      print(installed == true ? '已安装' : '未安装');
    } else {
      print('安装未知');
    }
  }

  _startPay() async {
    // scheme 可选参数 用于iOS,安卓可不填
    bool? success = await _uppayPlugin.startPay('123456', '01',
        scheme: 'baomoonLotteryUppay');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('插件示例应用'),
          ),
          body: Container(
            alignment: Alignment.center,
            child: Column(
              children: [
                const SizedBox(
                  height: 30,
                ),
                Center(
                  child: Text('运行于: $_platformVersion\n'),
                ),
                const SizedBox(
                  height: 10,
                ),
                TextButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all<Color>(
                        const Color(0xFFf2f2f2)),
                  ),
                  onPressed: () {
                    _isPaymentAppInstalled();
                  },
                  child: const Text('查询是否安装云闪付'),
                ),
                const SizedBox(
                  height: 10,
                ),
                TextButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all<Color>(
                        const Color(0xFFf2f2f2)),
                  ),
                  onPressed: () {
                    _startPay();
                  },
                  child: const Text('调用云闪付'),
                )
              ],
            ),
          )),
    );
  }
}

以上代码展示了如何初始化uppay插件,并检查云闪付是否已安装以及如何启动支付流程。请根据您的实际需求进行调整。


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

1 回复

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


uppay 是 Flutter 中用于集成银联支付的插件。通过该插件,开发者可以方便地在 Flutter 应用中实现银联支付功能。以下是如何在 Flutter 项目中使用 uppay 插件的步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  uppay: ^0.0.1  # 请使用最新的版本号

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

2. 配置 Android 项目

在 Android 项目中,需要添加银联支付的 SDK 和必要的配置。

2.1 添加 SDK 依赖

android/app/build.gradle 文件中添加银联支付的 SDK 依赖:

dependencies {
    implementation 'com.unionpay:uppay:1.0.0'  // 请使用最新的版本号
}

2.2 配置 AndroidManifest.xml

android/app/src/main/AndroidManifest.xml 文件中,添加银联支付所需的权限和 activity 配置:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.your_app">

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

    <application
        ...>
        <activity
            android:name="com.unionpay.uppay.PaymentActivity"
            android:configChanges="orientation|keyboardHidden|navigation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    </application>
</manifest>

3. 配置 iOS 项目

在 iOS 项目中,需要通过 CocoaPods 添加银联支付的 SDK。

3.1 添加 SDK 依赖

ios/Podfile 文件中添加银联支付的 SDK 依赖:

target 'Runner' do
  use_frameworks!
  pod 'UPPaymentControl', '~> 1.0.0'  # 请使用最新的版本号
end

然后运行 pod install 来安装依赖。

3.2 配置 Info.plist

ios/Runner/Info.plist 文件中,添加银联支付所需的 URL Scheme:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>your_app_scheme</string>  <!-- 替换为你的应用 URL Scheme -->
        </array>
    </dict>
</array>

4. 在 Flutter 中使用 uppay

在 Flutter 代码中,你可以使用 uppay 插件发起支付请求。

import 'package:uppay/uppay.dart';

void pay() async {
  try {
    String tn = "你的交易流水号";  // 从服务器获取的交易流水号
    String mode = "00";  // 00: 正式环境, 01: 测试环境
    String scheme = "your_app_scheme";  // 与 Info.plist 中的 URL Scheme 一致

    bool result = await Uppay.startPay(tn, mode, scheme);
    if (result) {
      print("支付成功");
    } else {
      print("支付失败");
    }
  } catch (e) {
    print("支付异常: $e");
  }
}
回到顶部