Flutter如何集成Google钱包支付

在Flutter项目中如何集成Google钱包支付功能?目前官方文档看起来不太清晰,有没有具体的实现步骤或示例代码可以参考?主要遇到的问题是不知道如何配置Android端的依赖和正确处理支付回调,希望有经验的大佬能分享一下完整的集成流程和注意事项。

2 回复

Flutter集成Google钱包支付主要通过调用原生API实现,具体步骤如下:

  1. 添加依赖 在pubspec.yaml中引入google_wallet或相关支付插件:
dependencies:
  google_wallet: ^最新版本
  1. 配置Android
  • AndroidManifest.xml添加权限和Google Pay配置
  • 设置minSdkVersion至少为19
  • 配置Google Pay API密钥
  1. 实现支付逻辑
  • 初始化钱包SDK
  • 创建支付请求(金额、货币类型等)
  • 调用presentPaymentMethodSelector触发支付界面
  • 通过onPaymentSuccess回调处理支付结果
  1. 注意事项
  • 需在Google Cloud平台创建项目并配置商家信息
  • 测试时使用Sandbox环境
  • 确保应用签名与Google Console中注册的一致

建议参考官方google_pay插件文档和示例代码,注意遵守Google Pay政策要求。

更多关于Flutter如何集成Google钱包支付的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中集成Google钱包支付,可以通过以下步骤实现:

1. 添加依赖

pubspec.yaml 中添加 google_pay 插件:

dependencies:
  google_pay: ^2.0.0

2. 配置Android清单

android/app/src/main/AndroidManifest.xml 中添加元数据:

<meta-data
    android:name="com.google.android.gms.wallet.api.enabled"
    android:value="true" />

3. 实现支付逻辑

使用 GooglePay 按钮和支付请求:

import 'package:google_pay/google_pay.dart';

class PaymentPage extends StatefulWidget {
  @override
  _PaymentPageState createState() => _PaymentPageState();
}

class _PaymentPageState extends State<PaymentPage> {
  void _onGooglePayPressed() {
    final request = PaymentRequest(
      apiVersion: 2,
      apiVersionMinor: 0,
      allowedPaymentMethods: [
        AllowedPaymentMethod(
          type: 'CARD',
          parameters: CardParameters(
            allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
            allowedCardNetworks: ['VISA', 'MASTERCARD'],
          ),
        ),
      ],
      transactionInfo: TransactionInfo(
        totalPrice: '10.00',
        totalPriceStatus: 'FINAL',
        currencyCode: 'USD',
      ),
      merchantInfo: MerchantInfo(
        merchantId: 'your_merchant_id', // 替换为实际商户ID
        merchantName: 'Your Store Name',
      ),
    );

    GooglePay.paymentRequest(request).then((result) {
      if (result.status == PaymentStatus.success) {
        // 处理支付成功
        print('Payment successful: ${result.paymentData}');
      } else {
        // 处理支付失败
        print('Payment failed: ${result.status}');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GooglePayButton(
          onPressed: _onGooglePayPressed,
        ),
      ),
    );
  }
}

4. 注意事项

  • 商户ID:需在Google Pay Business Console注册获取。
  • 环境配置:测试时使用测试环境,上线前切换至生产环境。
  • 合规性:确保应用符合Google Pay的使用政策。

5. 测试

  • 在真机上测试,确保设备已安装Google Play服务并登录Google账户。
  • 使用测试卡片进行支付验证。

通过以上步骤,即可在Flutter应用中集成Google钱包支付功能。

回到顶部