android - 使用 Flutter 确认购买

标签 android flutter in-app-purchase

Flutter 的 in_app_purchase 插件已更新到版本 0.3.0,将 Google Play 库迁移到 2.0.3
根据 Google Play Library v2,所有购买都必须在 3 天内(测试订单为 5 分钟)内确认,否则将自动取消。
in_app_purchase 的最新提交中,一个名为 acknowledgePurchase 的方法被添加到 BillingClient 类中。 但是,当前记录的进行应用内购买的方式是通过 InAppPurchaseConnection.instance,它不提供任何确认购买的方法。

in_app_purchase 的通用实现如下所示:

// Listening for new purchases
final Stream purchaseUpdates = InAppPurchaseConnection.instance.purchaseUpdatedStream;
StreamSubscription<List<PurchaseDetails>> _subscription = purchaseUpdates.listen((purchases) {
    _handlePurchaseUpdates(purchases);
});

// Product Details
ProductDetailsResponse _productDetailsResponse = await InAppPurchaseConnection.instance.queryProductDetails(this.productIDs.toSet());

// Past Purchases
QueryPurchaseDetailsResponse _purchaseDetailsResponse = await InAppPurchaseConnection.instance.queryPastPurchases();

// Making an nonConsumable purchase
PurchaseParam param = PurchaseParam(
    productDetails: productDetails,
    applicationUserName: null,
    sandboxTesting: sandboxTesting
);
InAppPurchaseConnection.instance.buyNonConsumable(purchaseParam: param);

一旦购买并通过验证,产品就会交付。但是,我们如何在收听新购买的同时确认新购买并在获取过去购买的列表时确认旧的(如果未确认)?

最佳答案

我也在寻找解决方案,看起来它在发行说明中有所描述。 https://pub.dev/packages/in_app_purchase#-changelog-tab-

例如,在获取过去的购买时:


// at top class level (i.e. App wrapper)
InAppPurchaseConnection.enablePendingPurchases();

// inside class handling the purchases
final InAppPurchaseConnection _connection = InAppPurchaseConnection.instance;

// example use

final QueryPurchaseDetailsResponse purchaseResponse = await _connection.queryPastPurchases();

if (purchaseResponse.error != null) {
  // (handle error)
} else {
  // (purge local store)

  await Future.forEach<PurchaseDetails>(
      purchaseResponse.pastPurchases, (purchaseDetails) async {
          if (await _verifyPurchase(purchaseDetails)) {
            await //(insert purchase to local store);

            final pending = Platform.isIOS
                ? purchaseDetails.pendingCompletePurchase
                : !purchaseDetails.billingClientPurchase.isAcknowledged;

            if (pending) {
              await _connection.completePurchase(purchaseDetails);
            }
          }
        }
    );
}

以前,步骤 _connection.competePurchase 仅适用于 iOS,现在它也适用于 Android。

purchaseDetails.pendingCompletePurchase 如果需要完成购买(iOS 和 Android)则为真。

purchaseDetails.billingClientPurchase.isAcknowledged 添加了针对 Android 的额外检查,以确保在需要时执行 completePurchase

0.3.0

Other Updates to the "billing_client_wrappers": Updates to the PurchaseWrapper: Add developerPayload, purchaseState and isAcknowledged fields.

Updates to the "InAppPurchaseConnection": [Breaking Change]: InAppPurchaseConnection.completePurchase now returns a Future instead of Future. A new optional parameter {String developerPayload} has also been added to the API. On Android, this API does not throw an exception anymore, it instead acknowledge the purchase. If a purchase is not completed within 3 days on Android, the user will be refunded.

[Breaking Change]: InAppPurchaseConnection.consumePurchase now returns a Future instead of Future. A new optional parameter {String developerPayload} has also been added to the API. A new boolean field pendingCompletePurchase has been added to the PurchaseDetails class. Which can be used as an indicator of whether to call InAppPurchaseConnection.completePurchase on the purchase.

[Breaking Change]: Added enablePendingPurchases in InAppPurchaseConnection. The application has to call this method when initializing the InAppPurchaseConnection on Android. See enablePendingPurchases for more information.

关于android - 使用 Flutter 确认购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59754136/

相关文章:

ios - [NSBundle appStoreReceiptURL] 的收据在应用程序更新后是否仍然存在

android - 如何从 Kotlin DSL build.gradle 中的所有依赖项中排除库?

java - 如何在android中为搜索框设置动画

dart - Flutter 中的透明粘性页脚

iphone - 我可以更改应用内购买登录 View 的方向吗?

ios - 在我的iOS应用中恢复应用内购买方式

android - Firebase 用户更改电子邮件或电话号码

java - 如何以编程方式检查应用程序是否在 Debug模式下运行?

flutter : Is provider an alternative to the BLoC pattern?

flutter - 文字输入值消失