anjlab / android-inapp-billing-v3

A lightweight implementation of Android In-app Billing Version 3
Other
2.2k stars 536 forks source link

How can i find expiry time #514

Closed Emranul-Islam closed 1 year ago

Emranul-Islam commented 2 years ago

I want to know when my subscription time will expire is there any way in this library ??

shashi-patel commented 2 years ago

Hi @Emranul-Islam Please let me know if you found any way to get expiry date in this library.

Emranul-Islam commented 2 years ago

@shashi-patel here's how i handle this and save it on shareprefarance you can save your way

    bp!!.getSubscriptionsListingDetailsAsync(arrayListOf(productId),
        object : BillingProcessor.ISkuDetailsResponseListener {
            override fun onSkuDetailsResponse(products: MutableList<SkuDetails>?) {
                products?.forEach { skuDetais ->
                    saveDate(skuDetails.subscriptionPeriod, purchaseInfo!!.purchaseData.purchaseTime)
                }
            }

            override fun onSkuDetailsError(error: String?) {
                Log.d(TAG, "onSkuDetailsError: $error")
                subscriptionDone.value = true
            }
        })

private fun saveDate(subscriptionPeriod: String, purchaseTime: Date) {

    val simpleDateFormat = SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH)

    mSharePrefarence.saveStartDate(simpleDateFormat.format(purchaseTime))

    val calendar = Calendar.getInstance()
    calendar.time = purchaseTime
    when (subscriptionPeriod) {
        "P1W" -> {
            calendar.add(Calendar.WEEK_OF_MONTH, 1)
        }
        "P4W" -> {
            calendar.add(Calendar.WEEK_OF_MONTH, 4)
        }
        "P1M" -> {
            calendar.add(Calendar.MONTH, 1)
        }
        "P3M" -> {
            calendar.add(Calendar.MONTH, 3)
        }
        "P6M" -> {
            calendar.add(Calendar.MONTH, 3)
        }
        "P1Y" -> {
            calendar.add(Calendar.YEAR, 1)
        }
    }

    val time = calendar.timeInMillis

    mSharePrefarence.saveExpireDate(simpleDateFormat.format(Date(time)))

}
shashi-patel commented 1 year ago

Thanks for the update @Emranul-Islam