Isaac_E
Isaac_E

Reputation: 131

Trouble with queryProductDetails() in flutter package in_app_purchase

I am trying to add subscriptions to my app for IOS. The code I am using works on android but not for IOS whenever I use final ProductDetailsResponse response = await InAppPurchase.instance .queryProductDetails({'standard', 'family'}); it always returns an empty array when it shouldn't. I think it has something to do with Apple having subscription groups but I'm not to sure. Here's the full function for a better understanding:

  Future<bool> makePurchaseIos(String productId) async {

    late ProductDetails standardPlan;
    late ProductDetails familyPlan;

    final ProductDetailsResponse response = await InAppPurchase.instance
        .queryProductDetails({'standard', 'family'});

    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
              content: Text("Got the following products: ${response.productDetails}"),
            ));

    for (ProductDetails product in response.productDetails) {
      if (product.id == 'standard') {
        standardPlan = product;
      } else if (product.id == 'family') {
        familyPlan = product;
      }
    }

    ///

    ProductDetails selectedProduct;
    if (productId == "standard") {
      selectedProduct = standardPlan;
    } else {
      selectedProduct = familyPlan;
    }

    final PurchaseParam purchaseParam =
        PurchaseParam(productDetails: selectedProduct);
    await InAppPurchase.instance.buyConsumable(purchaseParam: purchaseParam);
    print(selectedProduct);
    return true;
  }

Whenever I try to press subscribe it returns the empty array which means the response from queryProductDetails() function is nothing. I do not know why this would be happening though as I am definitely giving it the correct subscription ID's

Upvotes: 4

Views: 1206

Answers (3)

Tiến Đinh
Tiến Đinh

Reputation: 1

When setting the Product ID as an integer with leading zeros (e.g., 01, 02), the response.productDetails always returns an empty array when querying with IDs in string format such as '01', "01", '02', or "02". This issue occurs because numeric values with leading zeros are typically interpreted as standard integers (e.g., 01 and 1 are considered the same in many programming languages). However, when querying with a string representation of the ID, it does not match the stored integer value, resulting in no matching records being found. I also tested changing the Product ID to a string, such as "test", and queried it using 'test', which worked correctly. This suggests that the issue is specifically related to how the system handles numeric values with leading zeros. It is possible that when storing or processing IDs, the system automatically converts 01 and 02 to 1 and 2, causing a mismatch when querying with the original string representation ('01' or '02'). Meanwhile, non-numeric string values like "test" are handled correctly without conversion.

Upvotes: -1

TrungNguyen
TrungNguyen

Reputation: 11

Let check and make sure that you agreed to the all apple purchasing agreement, tax, banking... on appstoreconnect.

Upvotes: 0

Isaac_E
Isaac_E

Reputation: 131

On the clients computer they had told me they had set up the banking, taxes and agreed to the terms and conditions. It turned out they hadn't so that's where it was going wrong however Apple does not give you an actual error message to inform you of this. If you have the same error double check the steps I have mentioned are done.

Upvotes: 4

Related Questions