如何在 Flutter 中验证应用内购买?

问题描述 投票:0回答:1

我在我的 Flutter 应用程序中使用 in_app_purchase。产品为消耗品。

我已经按照包的官方例子。但是 verifyingPurchases 没有逻辑。 现在我正在通过以下检查来验证产品。

bool _verifyPurchase(PurchaseDetails purchaseDetails)  {
if (purchaseDetails.status == PurchaseStatus.purchased &&
    _productIDs.contains(purchaseDetails.productID) &&
    purchaseDetails.transactionDate != null) {
  return true;
} else {
  _handleInvalidPurchase(purchaseDetails);
  return false;
}
}

但是作为verifyingPurchases是一件很关键的事情。所以我对此有疑问。我在互联网上查了一下,但没有发现关于如何在 Flutter 中验证产品的好方法。唯一的解决方案是使用 RevenueCart。但这对于订阅或非消费产品更为重要。有什么逻辑可以让我们在应用程序中轻松verifyPurchases

flutter dart in-app-purchase app-store google-play-console
1个回答
0
投票

验证购买的逻辑特定于您的应用程序。在验证之前,您应该检查内容是否已交付给用户。这是一种故障保护,可确保用户不会为因未知(可能是设计特定)问题而未交付的购买请求退款。

比如,如果你有一个产品可以去除广告,那么在验证之前,你应该检查一下广告是否被去除了。

监听器示例代码:

 void __updatePurchases(List<PurchaseDetails> data) async {

if (data.isNotEmpty) {
  // Add purchase data
  _purchases.addAll(data);

  // Handle any new purchases
  for (PurchaseDetails purchaseDetails in data) {
    if (purchaseDetails.status == PurchaseStatus.purchased ||
        purchaseDetails.status == PurchaseStatus.restored) {
      // Success, check delivery and complete purchase if purchase is not complete.
      if (purchaseDetails.pendingCompletePurchase && *your content delivery check here*) {
        _log.d("Completing purchase '${purchaseDetails.productID}'.");
        _iap.completePurchase(purchaseDetails);
      }
    } else if (purchaseDetails.status == PurchaseStatus.error) {
      // Failed, log error.
      _log.i("Purchase of '${purchaseDetails.productID}' failed.");
    }
  }

  // Query all products to get the details.
  ProductDetailsResponse response =
  await _iap.queryProductDetails(_availableProducts);

  _products = response.productDetails;

  // Notify listeners.
  notifyListeners();
}

} }

© www.soinside.com 2019 - 2024. All rights reserved.