flutter IAP 如何 "Restore Purchases"

flutter IAP how to "Restore Purchases"

我正在使用 flutter_inapp_purchase 作为我的 flutter 应用程序的非消耗品 IAP。对于 IOS,它确实需要包含一个 "Restore Purchases" 功能以允许用户恢复之前购买的应用内购买。我可以知道如何创建 "Restore" 按钮并在用户点击 "Restore" 按钮时启动恢复过程吗?

  Future<Null> _buyProduct(IAPItem item) async {
    try {
      PurchasedItem purchased= await 
  FlutterInappPurchase.buyProduct(item.productId);
      print('purcuased - ${purchased.toString()}');
    } catch (error) {
      print('$error');
    }
  }

  Future<Null> _getProduct() async {
    List<IAPItem> items = await FlutterInappPurchase.getProducts(_productLists);
    for (var item in items) {
      print('${item.toString()}');
      this._items.add(item);
    }

    setState(() {
      this._items = items;
    });
  }

根据插件 documentation 你可以使用 API

getAvailablePurchases

那个看库native代码,这个方法是link到

// getAvailablePurchases
- (void)getAvailableItems:(FlutterResult)result {
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    flutterResult = result;
}

来自 Apple docs

Declaration

func restoreCompletedTransactions()

Discussion

Use this method to restore finished transactions—that is, transactions for which you have already called finishTransaction(_:). You call this method in one of the following situations: To install purchases on additional devices To restore purchases for an application that the user deleted and reinstalled

所以添加一个 Widget Button(你最喜欢的那个)由你决定,你可以把它放在任何你想放的地方。

然后在onClick事件中调用getAvailableItems并管理用户之前购买的商品并重新安装到用户的设备中。

对于使用官方 flutter 开发包的开发者 https://pub.dev/packages/in_app_purchase:

恢复购买(iOS 非消耗品、自动续订订阅或 非续订订阅)只是调用

Future<QueryPurchaseDetailsResponse> queryPastPurchases

在后台调用

_observer.getRestoredTransactions

您在这里应该做的是迭代所有状态为 PurchaseStatus.purchased 或 Purchase.error 的购买并调用

InAppPurchaseConnection.instance.completePurchase(purchase);

然后通过 Apple 验证收据并交付购买的商品

taken from class InAppPurchaseConnection:

/// You are responsible for completing every [PurchaseDetails] whose /// [PurchaseDetails.status] is [PurchaseStatus.purchased] or /// [[PurchaseStatus.error]. Completing a [PurchaseStatus.pending] purchase /// will cause an exception. /// /// This throws an [UnsupportedError] on Android.

  • If a purchase is not completed ([completePurchase] is not called on the purchase object) from the last app session. Purchase updates will happen when a new app session starts instead.

您可能会在应用启动时执行此操作,但 Apple 现在要求为所有非消耗品和自动更新的应用内产品恢复购买选项,否则您的应用将在审核过程中被拒绝。

https://developer.apple.com/documentation/storekit/in-app_purchase/offering_completing_and_restoring_in-app_purchases

If your app sells non-consumable, auto-renewable subscription, or non-renewing subscription products, you must provide a UI that allows them to be restored. Your customers expect your content to be available on all their devices and indefinitely; see In-App Purchase for more information.