android / play-billing-samples

Samples for Google Play In-app Billing
Apache License 2.0
2.38k stars 1.34k forks source link

How to get email in users data in google play subscriptions an additional call to the Google Play Developer API. The subscription purchase #671

Open PongPloy2016 opened 1 month ago

PongPloy2016 commented 1 month ago

hi How to get Email Playstore when use subscription an additional call to the Google Play Developer API. The subscription purchase

i have use get in code

` private suspend fun checkSubscription() {

    var  subscriptionPurchase = SubscriptionPurchase()
    try {
        val credential = subscriptionPurchase.getGoogleCredential(requireContext())
        val androidPublisher = subscriptionPurchase.getAndroidPublisher(credential)
        val subscriptionPurchase = subscriptionPurchase.getSubscriptionPurchase(androidPublisher, PACKAGE_NAME, SUBSCRIPTION_ID, PURCHASE_TOKEN)
        Log.d("SubscriptionChecker", "Subscription Status: ${subscriptionPurchase?.emailAddress}")

        // Check if email is present
        if (subscriptionPurchase?.emailAddress != null) {
            Log.d("SubscriptionChecker", "User Email: ${subscriptionPurchase.emailAddress}")
        } else {
            Log.d("SubscriptionChecker", "Email address not available for this purchase.")
        }
    } catch (e: Exception) {
        e.printStackTrace()
        Log.d("SubscriptionChecker Error", e.message.toString())

    }
} 

public fun getAndroidPublisher(credential: GoogleCredential): AndroidPublisher {
    return AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
       // .setApplicationName(APPLICATION_NAME)
        .build()
}

public suspend fun getSubscriptionPurchase(
    androidPublisher: AndroidPublisher,
    packageName: String,
    subscriptionId: String,
    token: String
): SubscriptionPurchase? {
    return withContext(Dispatchers.IO) {
        androidPublisher.purchases().subscriptions().get(packageName, subscriptionId, token).execute()
    }
}

public fun getGoogleCredential(context: Context): GoogleCredential { val assetManager = context.assets val inputStream: InputStream = assetManager!!.open(SERVICE_ACCOUNT_FILE_PATH) return GoogleCredential.fromStream(inputStream) .createScoped(listOf(AndroidPublisherScopes.ANDROIDPUBLISHER)) }`

and i. debug code in show detail. and why. not get email. please help

Screenshot 2567-07-01 at 13 32 52
matale commented 1 month ago

You cant get email through the Google Play Developer API for most (practically all) users. If you look at the docs the emailAddress The email address of the user when the subscription was purchased. Only present for purchases made with 'Subscribe with Google'. Subscribe with Google was a very specific service that Google tried to introduce in 2018 that no one really uses anymore. https://blog.google/outreach-initiatives/google-news-initiative/introducing-subscribe-google/