How I can know if the user cancel the subscription from google play store using in_app_purchase Flutter?

33 views Asked by At

In internal testing if I make a subscription and then unsubscribe it from the play store the product is still showing purchased. I want to know, how can I check if the user unsubscribe the product from play store ?

This is the initialization function

  Future initiazlize() async {
    final Stream<List<PurchaseDetails>> purchaseUpdated = _iap.purchaseStream;

    _subscription = purchaseUpdated.listen((purchaseDetailsList) async {
      _purchases.addAll(purchaseDetailsList);

      _listenToPurchaseUpdated(purchaseDetailsList);
      notifyListeners();
    },
    availble = await _iap.isAvailable();
    notifyListeners();
    if (availble == true) {
      print("Is store availble ${availble}");
      await _getProducts();
    }
  }

this is the buy subscription function

  buySubscription(String id) async{
    PurchaseParam purchaseParam = PurchaseParam(
        productDetails: _products.firstWhere((element) => element.id == id));
    try {
      await _iap.buyConsumable(purchaseParam: purchaseParam);
    } catch (e) {
      print('Error while buying ${e}');
    }
  }

This is the purchase Update function

  void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
    purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
      switch (purchaseDetails.status) {
        case PurchaseStatus.pending:

        //  _showPendingUI();
          break;
        case PurchaseStatus.purchased:
          if (purchaseDetails.productID == subscriptionId) {
            setPurchases(fASubscribed: true);
          }

          break;
        case PurchaseStatus.restored:
          if (purchaseDetails.productID == subscriptionId) {
            setPurchases(fASubscribed: true);
          }

          break;
        case PurchaseStatus.error:
          print(purchaseDetails.error);
          break;
        default:
          break;
      }

      if (purchaseDetails.pendingCompletePurchase) {
        await _iap.completePurchase(purchaseDetails);
      }
    });
  }

  }
}

1

There are 1 answers

1
Manoj Reddy On

You cannot determine if the person unsubscribes just by using your mobile app with the connection to the store. As far as I know, whenever a purchase is made, you need to store the purchased item in your database, and you have to call the completePurchase function in the mobile app after verifying the purchase with the Play Store/App Store in your server. Regarding your question on how to detect when a user unsubscribes from a purchase, you have to connect your backend server to the Play Store using Firebase, and you also need to configure webhooks in the case of the App Store. End-to-end in-app purchase configuration is explained in this guide, check it out here: flutter-in-app-purchases