Search code examples
flutterdartgoogle-playin-app-purchase

"You Already Have This Item" Flutter In App Purchase


I use in-app purchase in my app. Hassle free purchase. But when I want to buy the same product for the second time, this Play Store shows me "You Already Have This Item". I want my products to be bought again and again. I don't have any idea how I can solve it. Thank you very much for the help. I left my source codes below.

      InAppPurchase iap = InAppPurchase.instance;

      bool available = true;

      List<ProductDetails> products = [];

      List<PurchaseDetails> purchases = [];

      StreamSubscription subscription;

      int credits = 0;

      @override
      void initState() {
        initialize();
        super.initState();
      }

      @override
      void dispose() {
        subscription.cancel();
        super.dispose();
      }

      void initialize() async {
        available = await iap.isAvailable();

        if (available) {
          await getProducts();


          verifyPurchase();

          subscription = iap.purchaseStream.listen((data) => setState(() {
                print('New Purchase');
                purchases.addAll(data);
                verifyPurchase();
              }));
        }
      }

      Future<void> getProducts() async {
        Set<String> ids = Set.from(['buy_300_wecoin']);
        ProductDetailsResponse response = await iap.queryProductDetails(ids);

        setState(() {
          products = response.productDetails;
        });
      }

      PurchaseDetails hasPurchased(String productID) {
        return purchases.firstWhere((purchase) => purchase.productID == productID,
            orElse: () => null);
      }

      bool verifyPurchase() {
        PurchaseDetails purchase = hasPurchased('buy_300_wecoin');
        if (purchase != null && purchase.status == PurchaseStatus.purchased) {
          return true;
        }
      }

      void buyProduct(ProductDetails prod) {
        final PurchaseParam purchaseParam = PurchaseParam(productDetails: prod);
        iap.buyConsumable(purchaseParam: purchaseParam, autoConsume: false);
      }

Solution

  • you should use Consumable In-App Purchase

    A consumable product is one that a user consumes to receive in-app content, such as in-game currency. When a user consumes the product, your app dispenses the associated content, and the user can then purchase the item again.

    Note that the App Store does not have any APIs for querying consumable products, and Google Play considers consumable products to no longer be owned once they're marked as consumed and fails to return them here. For restoring these across devices you'll need to persist them on your own server and query that as well.

    Try in_app_purchase library and read about consumable IAP using this library.