StephenVinouze / KinApp

A Kotlin In App Purchase library that lets you easily manage your billing process in Android
Apache License 2.0
81 stars 13 forks source link

How do we use the launch(ui) lines? #9

Open ghost opened 5 years ago

ghost commented 5 years ago

launch(UI) { val products = billingManager.fetchProductsAsync(<your_products_id_here>, KinAppProductType.INAPP).await() } will allow me to get a price for a donation (an IAP) but no way to call it

I tried:

suspend fun getProducts(){ val products = billingManager.fetchProductsAsync(skuList, KinAppProductType.INAPP).await() val price = products?.get(0)?.price }

but I can not actually call the function this may be due to my novice level coding, never really delt with suspended functions (yeah I know I would define price elsewhere and then give it the value in the function, need to learn to call the function first lol)

also

suspend fun consumeDonation(){ val success = billingManager.consumePurchaseAsync("my.purchse.id").await() } again no idea how to call but also "my.purchse.id" or skuList[0] wont work as they are type string and it wants KinnAppPurchase .... how do I get that? (need to consume the item as its a donation only so can be done again if the user wants)

ghost commented 5 years ago

I am not one to sit around waiting for a response...

The first part for getting price I have resolved like this:

override fun onBillingReady() { // From this point you can use the Manager to fetch/purchase/consume/restore items Log.d("IAP","READY") GlobalScope.launch(Dispatchers.Main, CoroutineStart.DEFAULT) { products = billingManager.fetchProductsAsync(skuList, KinAppProductType.INAPP).await()?.toMutableList() donateTest.text="Donate :"+ (products?.get(0)?.price ?:"unknown Amount" ) Toast.makeText(applicationContext, "Fetched " + products?.size + " products", Toast.LENGTH_LONG).show() if (products?.isNotEmpty() == true){ Log.d("IAP","list not empty") } } }

I am sure this is not the right way lol but it does work and the button is updated with the price ! yay!

still no idea on how to get the KinAppPurchase to be able to consume it

Edit I think I have sussed the consume issue, my code now reads:

KinAppPurchaseResult.SUCCESS -> { // Purchase successful with a non-null KinAppPurchase object. // You may choose to consume this item right now if you want to be able to re-buy it purchase?.let { purchases?.add(purchase) GlobalScope.launch(Dispatchers.Main, CoroutineStart.DEFAULT) { if(purchase.productId==skuList[0]){ val success = billingManager.consumePurchaseAsync(purchase).await() toast("consume :"+success) } } } }

I have my developer payload set so I can make test purchases (the test id provided stopped working for some reason but was able to test with mine) I was able to buy and have it consumed and be able to re-buy.

Shall not close the issue as I am curious if there is a better way (got a feeling I have done it somewhat hacky) if this is perfectly fine though then please close the issue