Flutter iOS如何实现微信支付

在Flutter中集成微信支付时,iOS端一直无法调起支付界面,返回错误码-1。已按照官方文档配置了URL Types和Universal Links,并在AppDelegate中注册了微信SDK。请问还需要哪些特殊配置?如何排查iOS端微信支付失败的具体原因?测试环境下是否需要额外的证书或权限?

2 回复

Flutter iOS实现微信支付步骤:

  1. 在微信开放平台注册应用并获取AppID
  2. 集成fluwx插件
  3. 配置URL Types(weixin)
  4. 调用API发起支付请求
  5. 处理支付回调

需注意:iOS必须通过App Store分发,且支付金额与实际商品一致。

更多关于Flutter iOS如何实现微信支付的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter iOS 应用中集成微信支付,可以通过以下步骤实现:

1. 注册微信开放平台并获取 AppID

  • 微信开放平台注册应用,获取 AppID。
  • 配置应用的 Universal Links(iOS 必需)。

2. 添加依赖

  • pubspec.yaml 中添加 fluwx 插件(推荐):
    dependencies:
      fluwx: ^3.0.0
    
  • 运行 flutter pub get 安装依赖。

3. 配置 iOS 项目

  • Info.plist 中添加 URL Scheme
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>weixin</string>
        <key>CFBundleURLSchemes</key>
        <array>
          <string>wxYOUR_APP_ID</string> <!-- 替换为你的 AppID -->
        </array>
      </dict>
    </array>
    
  • 配置 Universal Links
    • 在微信开放平台填写 Associated Domains(如 applinks:yourdomain.com)。
    • 在 Xcode 中启用 Associated Domains,并添加 applinks:yourdomain.com

4. 初始化 Fluwx

  • main.dart 中初始化:
    import 'package:fluwx/fluwx.dart' as fluwx;
    
    void main() {
      runApp(MyApp());
      _initWeChat();
    }
    
    void _initWeChat() async {
      await fluwx.registerWxApi(
        appId: "YOUR_APP_ID", // 替换为你的 AppID
        universalLink: "https://yourdomain.com/universal_link/" // 你的 Universal Link
      );
    }
    

5. 发起微信支付

  • 从服务器获取支付参数(如 appIdpartnerIdprepayIdnonceStrtimeStamppackagesign)。
  • 调用 Fluwx 支付接口:
    import 'package:fluwx/fluwx.dart' as fluwx;
    
    void payWithWeChat() {
      final response = await fluwx.payWithWeChat(
        appId: "YOUR_APP_ID",
        partnerId: "PARTNER_ID",
        prepayId: "PREPAY_ID",
        packageValue: "Sign=WXPay",
        nonceStr: "NONCE_STR",
        timeStamp: "TIMESTAMP",
        sign: "SIGN",
      );
      
      // 处理支付结果
      if (response.isSuccessful) {
        // 支付成功
      } else {
        // 支付失败
      }
    }
    

6. 处理支付结果

  • 通过 fluwx.weChatResponseEventHandler 监听支付回调:
    fluwx.weChatResponseEventHandler.listen((event) {
      if (event is fluwx.WeChatPaymentResponse) {
        if (event.isSuccessful) {
          // 支付成功
        } else {
          // 支付失败
        }
      }
    });
    

注意事项:

  • 参数校验:确保所有参数(如 timeStamp 为字符串格式)与服务器生成的一致。
  • Universal Links:必须正确配置,否则微信无法回调应用。
  • 测试:使用微信支付沙箱环境进行测试,避免正式环境出错。

完成以上步骤后,即可在 Flutter iOS 应用中实现微信支付功能。

回到顶部