Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate billing to 5.0.0 #387

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ android {
}

dependencies {
implementation 'com.android.billingclient:billing-ktx:4.1.0'
implementation 'com.android.billingclient:billing:5.0.0'
implementation files('jars/in-app-purchasing-2.0.76.jar')
implementation 'androidx.annotation:annotation:1.3.0'
implementation 'androidx.annotation:annotation:1.4.0'
}

Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
) {
try {
val array = ArrayList<String>()
billingClient!!.queryPurchasesAsync(BillingClient.SkuType.INAPP)
val params = QueryPurchasesParams.newBuilder().apply { setProductType(BillingClient.ProductType.INAPP) }.build()
billingClient!!.queryPurchasesAsync(params)
{ billingResult, skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (skuDetailsList.size == 0) {
Expand Down Expand Up @@ -252,9 +253,10 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
call: MethodCall,
safeChannel: MethodResultWrapper
) {
val type = if(call.argument<String>("type") == "subs") BillingClient.SkuType.SUBS else BillingClient.SkuType.INAPP
val type = if(call.argument<String>("type") == "subs") BillingClient.ProductType.SUBS else BillingClient.ProductType.INAPP
val params = QueryPurchasesParams.newBuilder().apply { setProductType(type) }.build()
val items = JSONArray()
billingClient!!.queryPurchasesAsync(type) { billingResult, skuDetailsList ->
billingClient!!.queryPurchasesAsync(params) { billingResult, skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
for (purchase in skuDetailsList) {
val item = JSONObject()
Expand Down Expand Up @@ -355,10 +357,11 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
call: MethodCall,
safeChannel: MethodResultWrapper
) {
val type = if(call.argument<String>("type") == "subs") BillingClient.SkuType.SUBS else BillingClient.SkuType.INAPP
val type = if(call.argument<String>("type") == "subs") BillingClient.ProductType.SUBS else BillingClient.ProductType.INAPP
val params = QueryPurchaseHistoryParams.newBuilder().apply { setProductType(type) }.build()

billingClient!!.queryPurchaseHistoryAsync(
type,
params,
PurchaseHistoryResponseListener { billingResult, purchaseHistoryRecordList ->
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
val errorData = BillingError.getErrorFromResponseData(billingResult.responseCode)
Expand Down Expand Up @@ -391,21 +394,21 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
call: MethodCall,
safeChannel: MethodResultWrapper
) {
val skuArr = call.argument<ArrayList<String>>("skus")!!
val skuList = ArrayList<String>()
val skuArr : ArrayList<String> = call.argument<ArrayList<String>>("skus")!!
val skuList = ArrayList<QueryProductDetailsParams.Product>()
for (i in skuArr.indices) {
skuList.add(skuArr[i])
skuList.add(QueryProductDetailsParams.Product.newBuilder().setProductId(skuArr[i]).setProductType(skuType).build())
}

billingClient!!.querySkuDetailsAsync(
SkuDetailsParams.newBuilder().setSkusList(skuList).setType(skuType).build()
billingClient!!.queryProductDetailsAsync(
QueryProductDetailsParams.newBuilder().setProductList(skuList).build()
) { billingResult, skuDetailsList ->
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
val errorData = BillingError.getErrorFromResponseData(billingResult.responseCode)
safeChannel.error(call.method, errorData.code, errorData.message)
return@querySkuDetailsAsync
return@queryProductDetailsAsync
}
for (sku in skuDetailsList!!) {
for (sku in skuDetailsList) {
if (!skus.contains(sku)) {
skus.add(sku)
}
Expand All @@ -414,32 +417,40 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
val items = JSONArray()
for (skuDetails in skuDetailsList) {
val item = JSONObject()
item.put("productId", skuDetails.sku)
item.put("price", (skuDetails.priceAmountMicros / 1000000f).toString())
item.put("currency", skuDetails.priceCurrencyCode)
item.put("type", skuDetails.type)
item.put("localizedPrice", skuDetails.price)
item.put("productId", skuDetails.productId)
item.put("price", ((skuDetails.oneTimePurchaseOfferDetails?.priceAmountMicros ?: 0) / 1000000f).toString())
item.put("currency", skuDetails.oneTimePurchaseOfferDetails?.priceCurrencyCode)
item.put("type", skuDetails.productType)
item.put("localizedPrice", skuDetails.oneTimePurchaseOfferDetails?.formattedPrice)
item.put("title", skuDetails.title)
item.put("description", skuDetails.description)
item.put("introductoryPrice", skuDetails.introductoryPrice)
item.put("subscriptionPeriodAndroid", skuDetails.subscriptionPeriod)
item.put("freeTrialPeriodAndroid", skuDetails.freeTrialPeriod)
item.put("introductoryPriceCyclesAndroid", skuDetails.introductoryPriceCycles)
item.put("introductoryPricePeriodAndroid", skuDetails.introductoryPricePeriod)
// new
item.put("iconUrl", skuDetails.iconUrl)
item.put("originalJson", skuDetails.originalJson)
item.put("originalPrice", (skuDetails.priceAmountMicros / 1000000f).toDouble())
val subs = JSONArray()
if(skuDetails.subscriptionOfferDetails!=null) {
for (it in skuDetails.subscriptionOfferDetails!!) {
for (pricing in it.pricingPhases.pricingPhaseList) {
val subItem = JSONObject()
subItem.put("introductoryPrice", pricing.formattedPrice)
subItem.put("subscriptionPeriodAndroid", pricing.billingPeriod)
// item.put("freeTrialPeriodAndroid", pricing.getRecurrenceMode())
subItem.put("introductoryPriceCyclesAndroid", pricing.billingCycleCount)
// item.put("introductoryPricePeriodAndroid", pricing.p)
subs.put(subItem)
}

}
}
item.put("subscriptionOfferDetails", subs)
item.put("originalPrice", ((skuDetails.oneTimePurchaseOfferDetails?.priceAmountMicros ?: 0) / 1000000f).toDouble())
items.put(item)
}
safeChannel.success(items.toString())
return@querySkuDetailsAsync
return@queryProductDetailsAsync
} catch (je: JSONException) {
je.printStackTrace()
safeChannel.error(TAG, BillingError.E_BILLING_RESPONSE_JSON_PARSE_ERROR, je.message)
} catch (fe: FlutterException) {
safeChannel.error(call.method, fe.message, fe.localizedMessage)
return@querySkuDetailsAsync
return@queryProductDetailsAsync
}
}
}
Expand All @@ -448,16 +459,16 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
call: MethodCall,
safeChannel: MethodResultWrapper
) {
val type = if(call.argument<String>("type") == "subs") BillingClient.SkuType.SUBS else BillingClient.SkuType.INAPP
val type = if(call.argument<String>("type") == "subs") BillingClient.ProductType.SUBS else BillingClient.ProductType.INAPP
val obfuscatedAccountId = call.argument<String>("obfuscatedAccountId")
val obfuscatedProfileId = call.argument<String>("obfuscatedProfileId")
val sku = call.argument<String>("sku")
val prorationMode = call.argument<Int>("prorationMode")!!
val purchaseToken = call.argument<String>("purchaseToken")
val builder = newBuilder()
var selectedSku: SkuDetails? = null
var selectedSku: ProductDetails? = null
for (skuDetail in skus) {
if (skuDetail.sku == sku) {
if (skuDetail.productId == sku) {
selectedSku = skuDetail
break
}
Expand All @@ -468,11 +479,14 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
safeChannel.error(TAG, "buyItemByType", debugMessage)
return
}
builder.setSkuDetails(selectedSku)

var arr = ArrayList<ProductDetailsParams>()
arr.add(ProductDetailsParams.newBuilder().apply { setProductDetails(selectedSku) }.build())
builder.setProductDetailsParamsList(arr)

val params = SubscriptionUpdateParams.newBuilder()
if (purchaseToken != null) {
params.setOldSkuPurchaseToken(purchaseToken)
params.setOldPurchaseToken(purchaseToken)
}
if (obfuscatedAccountId != null) {
builder.setObfuscatedAccountId(obfuscatedAccountId)
Expand All @@ -484,8 +498,8 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
when (prorationMode) {
-1 -> {} //ignore
ProrationMode.IMMEDIATE_AND_CHARGE_PRORATED_PRICE -> {
params.setReplaceSkusProrationMode(ProrationMode.IMMEDIATE_AND_CHARGE_PRORATED_PRICE)
if (type != BillingClient.SkuType.SUBS) {
params.setReplaceProrationMode(ProrationMode.IMMEDIATE_AND_CHARGE_PRORATED_PRICE)
if (type != BillingClient.ProductType.SUBS) {
safeChannel.error(
TAG,
"buyItemByType",
Expand All @@ -498,8 +512,8 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
ProrationMode.DEFERRED,
ProrationMode.IMMEDIATE_WITH_TIME_PRORATION,
ProrationMode.IMMEDIATE_AND_CHARGE_FULL_PRICE ->
params.setReplaceSkusProrationMode(prorationMode)
else -> params.setReplaceSkusProrationMode(ProrationMode.UNKNOWN_SUBSCRIPTION_UPGRADE_DOWNGRADE_POLICY)
params.setReplaceProrationMode(prorationMode)
else -> params.setReplaceProrationMode(ProrationMode.UNKNOWN_SUBSCRIPTION_UPGRADE_DOWNGRADE_POLICY)
}

if (purchaseToken != null) {
Expand Down Expand Up @@ -574,6 +588,6 @@ class AndroidInappPurchasePlugin internal constructor() : MethodCallHandler,
companion object {
private const val TAG = "InappPurchasePlugin"
private const val PLAY_STORE_URL = "https://play.google.com/store/account/subscriptions"
private var skus: ArrayList<SkuDetails> = arrayListOf()
private var skus: ArrayList<ProductDetails> = arrayListOf()
}
}