handlePurchase method

  1. @protected
void handlePurchase(
  1. List<PurchaseDetails> purchases
)

Implementation

@protected
void handlePurchase(List<PurchaseDetails> purchases) async {
  for (final PurchaseDetails purchase in purchases) {
    if (_verifyPurchaseStatus(purchase)) {
      await _notifyPurchase(purchase);
    } else if (purchase.status == PurchaseStatus.error) {
      final productId = purchase.productID;
      final message = 'An error occured when purchasing an item: $productId';
      errorReporter?.recordError(
        purchase.error,
        StackTrace.current,
        reason: message,
      );

      _errorController.sink.add(purchase);
    } else {
      // other status: pending, cancelled
      _eventController.sink.add(purchase);
    }

    if (purchase.pendingCompletePurchase) {
      await _iapService.completePurchase(purchase);
    }

    if (purchase.status != PurchaseStatus.pending) {
      _isRestoringPurchases = false;
    }
  }

  if (_isRestoringPurchases) {
    if (purchases.isEmpty) {
      _errorController.sink.add(
        const FastIapError(FastIapError.noPurchasesFound),
      );
    }

    _isRestoringPurchases = false;
  }
}