- https://developer.android.com/google/play/billing/billing_library.html
- https://developer.android.com/training/play-billing-library/index.html
- https://github.com/googlecodelabs/play-billing-codelab
- https://github.com/googlesamples/android-play-billing/tree/master/TrivialDrive_v2
- https://codelabs.developers.google.com/codelabs/play-billing-codelab
Play Billing Library 1.0 では自動で com.android.vending.BILLING Permission が追加されるので手動で追加する必要はありません。
ライブラリの設定
- compile 'com.android.billingclient:billing:1.0'
- or
- implementation 'com.android.billingclient:billing:1.0'
- val billingClient : BillingClient = BillingClient.newBuilder(context)
- .setListener(this) // PurchasesUpdatedListener
- .build()
IabHelper にあった enableDebugLogging() に相当するメソッドは BillingClient にはありません。
開始と終了
IabHelper の startSetup() に相当するのが BillingClient.startConnection() です。 startConnection() で開始して endConnection() で終了します。- private lateinit var billingClient: BillingClient
- private var isBillingClientConnected: Boolean = false
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- billingClient = BillingClient.newBuilder(this)
- .setListener(this)
- .build()
- startServiceConnection(null)
- }
- private fun startServiceConnection(executeOnSuccess: Runnable?) {
- billingClient.startConnection(object : BillingClientStateListener {
- override fun onBillingSetupFinished(@BillingClient.BillingResponse response: Int) {
- when (response) {
- OK -> {
- isBillingClientConnected = true
- executeOnSuccess?.run()
- }
- else -> {
- ...
- }
- }
- }
- override fun onBillingServiceDisconnected() {
- isBillingClientConnected = false
- }
- })
- }
- override fun onDestroy() {
- billingClient.endConnection()
- super.onDestroy()
- }
購入
購入処理は BillingClient の launchBillingFlow() で開始します。- val params = BillingFlowParams.newBuilder()
- .setType(BillingClient.SkuType.INAPP)
- .setSku(SKU_INAPP)
- .build()
- val responseCode = billingClient.launchBillingFlow(activity, params)
- val params = BillingFlowParams.newBuilder()
- .setType(BillingClient.SkuType.SUBS)
- .setSku(SKU_SUBS)
- .addOldSku(SKU_OLD_SUBS) // replace するときは必須
- .setReplaceSkusProration(true) // Optional : デフォルトは false
- .setAccountId(hashedUserAccountId) // Optional : 難読化されたユーザー別の文字列
- .setVrPurchaseFlow(false) // Optional : デフォルトは false
- .build()
- val responseCode = billingClient.launchBillingFlow(activity, params)
購入処理の結果は PurchasesUpdatedListener の onPurchasesUpdated() で通知されます。
launchBillingFlow() の戻り値と PurchasesUpdatedListener.onPurchasesUpdated() の第1引数は @BillingClient.BillingResponse です。
- override fun onPurchasesUpdated(@BillingClient.BillingResponse responseCode: Int, purchases: List<Purchase>?) {
- when (responseCode) {
- OK -> {
- if (purchases != null) {
- purchases.forEach {
- handlePurchase(it)
- }
- } else {
- ...
- }
- }
- ITEM_ALREADY_OWNED -> {
- ...
- }
- USER_CANCELED -> {
- // do nothing
- }
- ...
- else -> {
- ...
- }
- }
- }
IabHelper のときは com.android.vending.billing.PURCHASES_UPDATED を受け取るために IabBroadcastReceiver を用意していましたが、その必要はなくなりました。代わりに、アプリからの購入だけでなく Play Store で開始された購入のときも onPurchasesUpdated() に通知されます。
購入済みアイテムの問い合わせ
BillingClient の queryPurchases() で行います。 このメソッドは Google Play Store アプリが提供するキャッシュから結果(PurchasesResult)を受け取ります。ネットワーク処理は開始されません。- val result : PurchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
購入可能なアイテムの問い合わせ
BillingClient の querySkuDetailsAsync() で行います。 問い合わせるアイテムは SkuDetailsParams で設定します。- fun querySkuDetailsAsync() {
- val executeOnConnectedService = Runnable {
- val params = SkuDetailsParams.newBuilder()
- .setType(BillingClient.SkuType.INAPP)
- .setSkusList(listOf("gas", "premium"))
- .build()
- billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->
- when (responseCode) {
- OK -> {
- skuDetailsList?.forEach {
- ...
- }
- }
- else -> {
- ...
- }
- }
- }
- }
- if (isBillingClientConnected) {
- executeOnConnectedService.run()
- } else {
- startServiceConnection(executeOnConnectedService)
- }
- }
注意
追記: https://codelabs.developers.google.com/codelabs/play-billing-codelab も更新されました。