LoginSignup
43
35

More than 5 years have passed since last update.

簡単なGoogle Play Billing Libraryの課金の流れ

Posted at

概要

課金今まで触れてこなかったので、メモです。

基本的にはBillingClientに対して以下をするだけです。

  • 接続する。
  • 課金アイテムの情報を取得する。
  • アイテムの情報を渡して課金するだけ。

image.png

このコードラボやりました。
https://codelabs.developers.google.com/codelabs/play-billing-codelab/

(この記事のコードミスっててヤバイことになっても保証はできないです)
何かコメントあればください。

接続する

image.png

val billingClient: BillingClient = BillingClient
            .newBuilder(context)
            .setListener(this)
            .build()

    init {
        billingClient.startConnection(object : BillingClientStateListener {
            override fun onBillingSetupFinished(@BillingResponse billingResponse: Int) {
                if (billingResponse == BillingResponse.OK) {
                    Log.i(TAG, "onBillingSetupFinished() response: $billingResponse")
                } else {
                    Log.w(TAG, "onBillingSetupFinished() error code: $billingResponse")
                }
            }

            override fun onBillingServiceDisconnected() {
                Log.w(TAG, "onBillingServiceDisconnected()")
            }
        })
    }

課金アイテム(SKU)の情報を取得する

image.png

SKUの名前はGoogle Play上でつけるっぽい。

        val itemType = SkuType.INAPP
        val skuList = listOf("gas", "premium")
        val skuDetailsParams = SkuDetailsParams.newBuilder()
                .setSkusList(skuList).setType(itemType).build()
        billingClient.querySkuDetailsAsync(skuDetailsParams,
                { responseCode, skuDetailsList: MutableList<SkuDetails> -> 
                    // ここで取得完了。(RecyclerViewとかでSkuDetailsの情報を表示できる) 
                })
SkuDetailsの例
{"productId":"gas","type":"inapp","price":"¥113","price_amount_micros":113088351,"price_currency_code":"JPY","title":"Gas (Play Billing Codelab)","description":"Buy gasoline to ride!"}

課金する

image.png

取得した情報にあるSkuDetailsのインスタンスを渡すだけで課金できる

        val billingFlowParams = BillingFlowParams.newBuilder()
                .setSkuDetails(skuDetails).build()
        billingClient.launchBillingFlow(activity, billingFlowParams)

接続の時に渡したリスナーで課金が検知できる。

    override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) {
        Log.d(TAG, "onPurchasesUpdated() response: $responseCode");
    }

queryPurchases(SkuType.INAPP)などを呼ぶと、今の課金の状態とか取得できるみたい。
https://developer.android.com/google/play/billing/billing_library_overview

接続のリトライのベストプラクティス

ここに載っているので見てみると良さそう
https://codelabs.developers.google.com/codelabs/play-billing-codelab/#7

Next Step

https://developer.android.com/google/play/billing/billing_library_overview
ここに載っていたものそのままですが、Next Stepとして以下が良さそう

アイテム課金などの都度課金
https://developer.android.com/google/play/billing/billing_onetime
月額課金などのサブスクリプション
https://developer.android.com/google/play/billing/billing_subscriptions

43
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
35