Flutter中如何集成接入google play 实现支付

Flutter中如何集成接入google play 实现支付

在Flutter中集成Google Play支付功能,通常使用google_mobile_adsin_app_purchase插件。以下是实现步骤:

1. 添加依赖

pubspec.yaml中添加in_app_purchase依赖:

dependencies:
  flutter:
    sdk: flutter
  in_app_purchase: ^3.0.6

然后运行flutter pub get安装依赖。

2. 配置Google Play控制台

  1. 在Google Play控制台创建应用。
  2. 在“ monetize > products > in-app products”中添加商品,设置商品ID、价格等信息。

3. 初始化In-App Purchase

在Flutter中初始化InAppPurchase

import 'package:in_app_purchase/in_app_purchase.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final InAppPurchase _inAppPurchase = InAppPurchase.instance;

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

  Future<void> _initialize() async {
    final bool available = await _inAppPurchase.isAvailable();
    if (!available) {
      // 处理设备不支持应用内购买的情况
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Google Play支付'),
        ),
        body: Center(
          child: Text('集成Google Play支付'),
        ),
      ),
    );
  }
}

4. 获取商品信息

通过queryProductDetails获取商品信息:

Future<void> _fetchProducts() async {
  const Set<String> _kProductIds = {'product_id_1', 'product_id_2'};
  final ProductDetailsResponse response = await _inAppPurchase.queryProductDetails(_kProductIds);
  if (response.notFoundIDs.isNotEmpty) {
    // 处理未找到的商品
  }
  List<ProductDetails> products = response.productDetails;
  // 显示商品信息
}

5. 发起购买

使用buyNonConsumablebuyConsumable发起购买:

Future<void> _buyProduct(ProductDetails productDetails) async {
  final PurchaseParam purchaseParam = PurchaseParam(productDetails: productDetails);
  await _inAppPurchase.buyNonConsumable(purchaseParam: purchaseParam);
}

6. 处理购买结果

监听购买状态变化:

StreamSubscription<List<PurchaseDetails>> _subscription;

@override
void initState() {
  super.initState();
  _subscription = _inAppPurchase.purchaseStream.listen((List<PurchaseDetails> purchaseDetailsList) {
    _handlePurchase(purchaseDetailsList);
  }, onDone: () {
    _subscription.cancel();
  }, onError: (error) {
    // 处理错误
  });
}

void _handlePurchase(List<PurchaseDetails> purchaseDetailsList) {
  for (PurchaseDetails purchaseDetails in purchaseDetailsList) {
    if (purchaseDetails.status == PurchaseStatus.purchased) {
      // 处理购买成功
    } else if (purchaseDetails.status == PurchaseStatus.error) {
      // 处理购买失败
    }
  }
}

@override
void dispose() {
  _subscription.cancel();
  super.dispose();
}

7. 验证购买

建议在服务器端验证购买凭证,确保安全性。

8. 处理订阅

如果是订阅商品,使用buyConsumable方法,并处理订阅状态。

9. 测试

在Google Play控制台上传测试版本,使用测试账号进行支付测试。

10. 发布

测试完成后,发布应用到Google Play。

注意事项

  • 遵守Google Play的支付政策。
  • 确保正确处理购买状态,避免重复购买或未完成购买。

通过这些步骤,你可以在Flutter应用中集成Google Play支付功能。


更多关于Flutter中如何集成接入google play 实现支付的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter中如何集成接入google play 实现支付的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中集成Google Play以实现应用内购买(In-App Purchase, IAP),你需要遵循一系列步骤来配置你的项目和代码。下面是一个简化的指南,基于你提供的参考资料。

前期准备

  1. 创建Google Play开发者账号:如果你还没有Google Play开发者账号,需要先注册一个。
  2. 创建应用程序:登录Google Play Console并为你的应用创建一个新的条目。
  3. 设置支付功能:在Google Play Console中启用Google Play Billing,并完成所有必要的设置。

配置商品

  1. 在Google Play Console中添加你的IAP商品。这可能包括一次性购买的商品或订阅服务。
  2. 设置定价模板(如果需要),以便统一管理不同地区的定价策略。

应用程序发布与测试

  1. 提交至少一个封闭测试版本的应用包(APK/AAB),以便能够创建应用内购买的商品。
  2. 配置内部测试轨道或封闭测试轨道,并添加测试用户账户。

Flutter端集成

  1. 添加依赖:在pubspec.yaml文件中添加in_app_purchase插件的依赖项:

    dependencies:
      in_app_purchase: ^3.1.5
    
  2. 初始化插件:在你的Dart代码中,使用InAppPurchase.instance来获取插件实例,并监听购买更新流。

    final InAppPurchase _inAppPurchase = InAppPurchase.instance;
    Stream<List<PurchaseDetails>> purchaseUpdated =
        _inAppPurchase.purchaseStream;
    StreamSubscription<List<PurchaseDetails>> _subscription;
    
    @override
    void initState() {
      super.initState();
      _subscription = purchaseUpdated.listen((purchaseDetailsList) {
        _listenToPurchaseUpdated(purchaseDetailsList);
      });
    }
    
  3. 查询商品详情:使用queryProductDetails方法来获取商品详情列表。

    final ProductDetailsResponse response = await _inAppPurchase.queryProductDetails(_kIds.toSet());
    
  4. 处理购买流程:当用户选择购买时,调用buyNonConsumablebuyConsumable方法来发起购买请求。

  5. 验证购买:确保从Google Play收到的购买确认是有效的,并根据结果解锁相应的内容。

  6. 消耗商品(如果是消耗型商品):使用consumePurchase方法通知Google Play该商品已被消耗,允许用户再次购买。

服务器端验证(可选)

为了提高安全性,你可能还需要在服务器端验证购买的有效性,通过检查Google Play的签名和订单ID等信息。

注意事项

  • 确保你的应用已经过充分的测试,特别是在不同的设备和Google Play环境条件下。
  • 根据Google Play的要求,正确处理退款、取消订阅等情况。
  • 对于实际的支付流程,你还需要考虑如何处理错误情况以及用户体验优化等问题。

以上步骤提供了一个基本框架,具体实施细节可能会随着Google Play Console的变化而有所不同,请参考最新的官方文档进行操作。此外,由于涉及敏感的操作如支付,务必仔细阅读相关API和服务条款,确保符合Google Play的规定。

回到顶部