How to handle Flutter In App Purchase Refund or Subscription Cancelled

25 Views Asked by At

I am using the flutter package in_app_purchase for implementing on time purchase for my app(iOS). It is working fine and purchases are going smoothly. When the purchaseDetails.purchased is called, I complete the purchase and save it to the shared preference. There are very little refunds so I am ignoring the refunds.

Now, I am switching to subscription model with weekly, monthly and yearly subscription. If I use shared preference to see if a user has subscribed to my pro version, the users who get a refund or cancel the subscription keep on using my pro version. There is no way for me to check if a user has refunded or cancelled the subscription.

Kindly guide me how to handle this situation and implement my code in such a way that the users who cancel the subscription or get a refund are reverted back to the non-premium version of my app

this is my code

@override
  void initState() {
    loading();

    final Stream purchaseUpdated = InAppPurchase.instance.purchaseStream;
    _subscription = purchaseUpdated.listen((purchaseDetailsList) async {
      _listenToPurchaseUpdated(purchaseDetailsList);
    }, onDone: () {
      log('On Done Called');
    }, onError: (error) {
      log('On Error Called');
    });
    super.initState();
  }


void _listenToPurchaseUpdated(
      List<PurchaseDetails> purchaseDetailsList) async {
    log('in App Purchase Listener Called');
    for (var purchaseDetails in purchaseDetailsList) {
      
      if (purchaseDetails.status == PurchaseStatus.pending) {
        // _showPendingUI();
      } else {
        if (purchaseDetails.status == PurchaseStatus.error) {
          log('Some Error While Purchasing ${purchaseDetails.error!}');
       
          //Handle Error Here
          // _handleError(purchaseDetails.error!);
        } else if (purchaseDetails.status == PurchaseStatus.canceled) {
          log('Purchase Cancelled');
      //Handle cancelled purchase
        } else if (purchaseDetails.status == PurchaseStatus.purchased ||
            purchaseDetails.status == PurchaseStatus.restored) {
          bool valid = await _verifyPurchase(purchaseDetails);
          if (valid) {
            //Deliver Product Here
            log('Product Purchased Or Restored');

          
            InAppPurchase.instance.completePurchase(purchaseDetails);
           //handle purchased product (basically storing reference in shared preference that user has purchased)

           
          } else {
            

            // _handleInvalidPurchase(purchaseDetails);
          }
        }
        if (purchaseDetails.pendingCompletePurchase) {
          await InAppPurchase.instance.completePurchase(purchaseDetails);
        }
      }
    }
  }
0

There are 0 best solutions below