dooboolab-community / react-native-iap

In App Purchase module for React Native!
https://react-native-iap.dooboolab.com
MIT License
2.74k stars 633 forks source link

getSubscriptions returns an empty array on Android #2720

Open gkpo opened 3 months ago

gkpo commented 3 months ago

Environment:

I managed to get the subscriptions list on iOS but now I'm trying to do so on android and the getSubscriptions method is not returning anything.

const {
  connected,
  currentPurchaseError,
  initConnectionError,
  getSubscriptions,
  subscriptions,
} = useIAP();

const SUBSCRIPTIONS_SKUS = Platform.select({
  ios: ["premium_monthly", "premium_yearly"],
  android: ["monthly", "test"], // <-- monthly is a Subscription id, test is a Product id just doing some tests at this point...
});

const handleGetSubscriptions = async () => {
    try {
      await getSubscriptions({ skus: SUBSCRIPTIONS_SKUS as string[] });
    } catch (error) {
      console.log({ message: "handleGetSubscriptions", error });
    }
  };

 // This is executed when getSubscriptions returns
 // ios I have a list of subs. On android, an empty array
 useEffect(() => {
    console.log("subscriptions changed: ", subscriptions);
 }, [subscriptions]);

// In the return function I have:
<Button text="Get subscriptions" onPress={handleGetSubscriptions} />

How does the library even work ?

In fact I'm a bit lost about how it even works. How does react-native-iap know which Google Play project is associated with my mobile app and where to fetch the products and subscriptions from? Unlike other libraries, there is no API key or anything that I had to set in my project.

I have uploaded my .apk (.aab actually) to my play store app, and now the play store project is aware of the bundle ID. Is that how it works ?

Regardless of how it works.. why it doesn't work !!! 😭😭 any help would be much appreciated !!!

RickYangzz commented 2 months ago

hhhhhhh, I have the same problem. hhhh, God maybe help me.

gkpo commented 2 months ago

I got it working in the end!!! After so many attempts and a little bit of luck as well i guess. There were so many parameters to take into account! I'm going to try to list them here:

  1. Use the provided example

Use the "subscriptions" screen from the iapExample app (https://github.com/dooboolab-community/react-native-iap/blob/main/IapExample/src/screens/Subscriptions.tsx). Leave it as is, except specify your own product_id in the subscriptions skus section.

  1. App signing:

I think I finally understood how the library works. You first need to build your app (I built with expo) then download the .aab file and on your play console account, in the internal testing section, you can upload your .aab build. Once uploaded, go to the "downloads" tab of the release you just created, you can then download the "signed apk". This is the version that you install on your device. I think this signature is what links the app to the play store account.

  1. Physical device or emulator:

I had it working once in a simulator but then I couldn't get it working (empty subscriptions again) so I had to install the Signed APK on a physical device instead. So maybe try that.

  1. Propagation time on google servers:

When you create subscriptions and pricing plans, they won't be visible straight away from the subscriptions screen. In fact the first time I saw something being displayed was the day after the day I had coded it (that's why I say it was a bit of luck). So count at least a few hours before hoping to see your changes reflected on the device.

  1. Publish your app:

Even if it is in internal testing, you should publish your app. I'm not sure but I think it is necessary to have the subscriptions available through the API.

  1. License testing:

Add the google account email that is linked to the device to the license testing email list (https://blog.logrocket.com/implement-react-native-in-app-purchases-android-apps/#add-license-testers). Not sure this is mandatory either but I did it regardless. Also this will allow you to be in test mode and not be charged with your bank card.

That's all I can recall for now. But yeah, google ecosystem is crazy confusing and there is not much documentation about this library either.

Hope this helps!

RickYangzz commented 2 months ago

Thank you, I will try those steps.

RickYangzz commented 2 months ago
image

I'd like to know if this release's status is ok for me to test react-native-iap?

RickYangzz commented 2 months ago

Luckily, I have solved this problem finally.

gkpo commented 2 months ago

Post your solution here as it might help others :)

Le ven. 3 mai 2024 à 06:45, Rick @.***> a écrit :

Luckily, I have solved this problem finally.

— Reply to this email directly, view it on GitHub https://github.com/dooboolab-community/react-native-iap/issues/2720#issuecomment-2092113857, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHII5EVRX6JS7TFEFPFUD3ZAMI5LAVCNFSM6AAAAABFXWIX46VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJSGEYTGOBVG4 . You are receiving this because you authored the thread.Message ID: @.***>

bharathy-kannan commented 3 weeks ago

I am still getting an empty array after pushing my app to internal testing as well as closed testing. Can someone let me know how to solve this problem in Android? I have done all the things mentioned above. When I download the closed testing version, I still do not see any subscriptions.

bharathy-kannan commented 2 weeks ago

Luckily, I have solved this problem finally.

Can you please post your solution here?