gpt4 book ai didi

android - 如何在 Android 上使用 Flutter 成功完成应用内购买?

转载 作者:行者123 更新时间:2023-12-04 15:11:50 24 4
gpt4 key购买 nike

我想要的是
我想将 Google Play 在应用购买中集成到我的 Flutter 应用中,我要销售的产品是消耗品。现在我正在使用 in_app_purchase: (0.3.4+16)包裹。
我做了什么

  • 在 google play 开发者控制台中创建产品
  • 根据in_app_purchase包的文档设置项目
  • 实现了一些购买消耗品的逻辑(见下文)
  • 构建了一个 alpha 版本并将其上传到 alpha 测试轨道以进行测试
  • 在我的开发者手机上创建了一个新的谷歌帐户,将其注册为测试者并下载了 alpha 版本
  • 购买时带有“测试卡,始终认可”
  • 使用我的 PayPal 帐户购买

  • 预期和实际结果
    我希望付款能够正常工作,并且所有 api 调用都能正常返回。
    当我在手机上开始购买时,购买流程开始,我可以选择所需的付款方式。在我接受付款后 _listenToPurchaseUpdated(...)方法被调用,正如预期的那样。
    但是,调用 InAppPurchaseConnection.instance.completePurchase(p)返回 BillingResponse.developerError我收到以下调试消息:
    W/BillingHelper: Couldn't find purchase lists, trying to find single data.
    I/flutter: result: BillingResponse.developerError (Purchase is in an invalid state.)
    此错误伴随着“测试卡,始终批准”以及当我使用 PayPal 开始实际交易时。对于 PayPal 购买,我收到了一封确认电子邮件,表明交易成功。
    在文档中它说:

    Warning! Failure to call this method and get a successful response within 3 days of the purchase will result a refund on Android.


    总结问题
    我怎样才能接到 InAppPurchaseConnection.instance.completePurchase(p) 的电话返回一个成功的结果?
    采购实现
    在应用程序购买中设置的代码如文档所示实现:
    InAppPurchaseConnection.enablePendingPurchases();

    Stream<List<PurchaseDetails>> purchaseUpdated = InAppPurchaseConnection.instance.purchaseUpdatedStream;

    _subscription = purchaseUpdated.listen(_listenToPurchaseUpdated, onDone: () {
    _subscription.cancel();
    }, onError: (error) {
    // handle error here.
    });

    ...

    Future<void> _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) async {
    for (var p in purchaseDetailsList) {

    // Code to validate the payment

    if (!p.pendingCompletePurchase) continue;

    var result = await InAppPurchaseConnection.instance.completePurchase(p);

    if (result.responseCode != BillingResponse.ok) {
    print("result: ${result.responseCode} (${result.debugMessage})");
    }
    }
    }
    要购买消耗品,我有这种查询产品详细信息并调用 buyConsumable(...) 的方法
    Future<bool> _buyConsumableById(String id) async {
    final ProductDetailsResponse response = await InAppPurchaseConnection
    .instance
    .queryProductDetails([id].toSet());

    if (response.notFoundIDs.isNotEmpty || response.productDetails.isEmpty) {
    return false;
    }

    List<ProductDetails> productDetails = response.productDetails;

    final PurchaseParam purchaseParam = PurchaseParam(
    productDetails: productDetails[0],
    );

    return await InAppPurchaseConnection.instance.buyConsumable(
    purchaseParam: purchaseParam,
    );
    }

    最佳答案

    解决方法是不要调用 completePurchase(...)购买消耗品的方法。默认情况下,图书馆会为您消费购买,这隐含地充当对 completePurchase(...) 的调用。 .
    背景
    调用 InAppPurchaseConnection.instance.buyConsumable(...)有一个可选的 bool 参数 autoConsume始终为 true .这意味着,在 android 上,购买是在回调到 purchaseUpdatedStream 之前消耗的。 .completePurchase 的文档方法说如下:

    The [consumePurchase] acts as an implicit [completePurchase] on Android


    解决问题的代码
    Future<void> _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) async {
    for (var p in purchaseDetailsList) {

    // Code to validate the payment

    if (!p.pendingCompletePurchase) continue;
    if (_isConsumable(p.productID)) continue; // Determine if the item is consumable. If so do not consume it

    var result = await InAppPurchaseConnection.instance.completePurchase(p);

    if (result.responseCode != BillingResponse.ok) {
    print("result: ${result.responseCode} (${result.debugMessage})");
    }
    }
    }

    关于android - 如何在 Android 上使用 Flutter 成功完成应用内购买?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65088898/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com