close

上一篇我們講到取得商品列表

Android Kotlin APP 應用程式內購買 In-App Billing with Google Play Billing Library 實作教學、範例 example ( similar iOS In App Purchase ) ( 一 ) 』Willy's Fish教學筆記

 

這篇我們就要來談怎麼啟動購買流程

啟動沒什麼好提的,用 launchBillingFlow 這個 method 即可

直上 code 

billingLife.launchBillingFlow(
    requireActivity(),
    BillingFlowParams.newBuilder().setSkuDetails(it).build()
)
fun launchBillingFlow(
    activity: Activity, 
    params: BillingFlowParams): Int {
    if (!billingClient.isReady) {
        PLog.e("$TAG: launchBillingFlow: 
                 BillingClient is not ready")
    }
    val billingResult = billingClient
                        .launchBillingFlow(activity, params)
    val responseCode = billingResult.responseCode
    val debugMessage = billingResult.debugMessage
    PLog.d("$TAG: launchBillingFlow: 
            BillingResponse $responseCode $debugMessage")
    return responseCode
}

 

這樣就會以 bottom sheet 的 UI 形式

show 系統的付款畫面了

這時候無論成不成功,我們都會得到一個 BillingResponseCode

這裡要 implement PurchasesUpdatedListener.onPurchasesUpdated

讓 Google Play 可以回傳給果給我們

如下:

override fun onPurchasesUpdated(
    billingResult: BillingResult,
    purchases: MutableList<Purchase>?
) {
    val responseCode = billingResult.responseCode
    val debugMessage = billingResult.debugMessage
    PLog.d("$TAG: onPurchasesUpdated:
                    $responseCode $debugMessage")
    when (responseCode) {
        // Google 已確認付款成功
        BillingClient.BillingResponseCode.OK -> {
            if (purchases == null) {
                PLog.d("$TAG: onPurchasesUpdated:
                                null purchase list")
            } else {
                processPurchases(purchases)
            }
        }
        BillingClient.BillingResponseCode.USER_CANCELED -> {
            PLog.d("$TAG: onPurchasesUpdated: 
                    User canceled the purchase")
        }
        BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED -> {
            PLog.d("$TAG: onPurchasesUpdated: 
                    The user already owns this item")
        }
        BillingClient.BillingResponseCode.DEVELOPER_ERROR -> {
            PLog.e(
                "$TAG: 有可能 Google Play Console 設定有誤"
            )
        }
    }
}

 

順利的話我們就會取得 Pruchase 了

在 BillingResponseCode OK 的情況下取得 Pruchase 後

這個時間點的 User 已經付款成功了

我們就可以把其購買的商品設定給他

通常會和自家 server 溝通,寫入 DB

 

這裡要注意一下

會不會因為網路不穩…等問題造成 API 溝通失敗

所以我會做 retry 的動作

 

好的,在給完商品之後還有一件事要做

就是和 Google 確認我們已經給商品了

這個步驟很重要

因為一個 Purchase 要在 3 天內進入 acknowledged 狀態

否則我們就會收不到錢

所以這邊我也會做 retry 的動作

 

商品分兩種,一個是不可消耗,一個是可消耗

我們可以分別用 consumePurchase / acknowledgePurchase

來向 Google 做確認

前者除了已確認之外,還會多標記已消耗

這樣子 User 下次才能再次購買此商品

以下是 acknowledge 的作法

consume 同理

val params = AcknowledgePurchaseParams.newBuilder()
    .setPurchaseToken(purchaseToken)
    .build()
billingClient.acknowledgePurchase(params) { billingResult ->
    val responseCode = billingResult.responseCode
    val debugMessage = billingResult.debugMessage
    PLog.d("$TAG: acknowledgePurchase: $responseCode $debugMessage")
}

 

至此,我們已經成功讓消費者取得商品

而且也向 Google 領錢了

但我們還需要做一此例外處理

比如說,當 User 在金流的過程中有電話打來之類的中斷該怎麼辦?

 

所以這時候我會利用 queryPurchases 來檢查

如下

fun queryPurchases() {
    if (!billingClient.isReady) {
        PLog.e("$TAG: queryPurchases: BillingClient is not ready")
        return
    }

    PLog.d("$TAG: queryPurchases: IN-APP")
    val result = billingClient.queryPurchases(BillingClient.SkuType.INAPP)

    if (result == null) {
        PLog.d("$TAG: queryPurchases: null purchase result")
        return
    }

    if (result.purchasesList == null) {
        PLog.d("$TAG: queryPurchases: null purchase list")
        return
    }

    processPurchases(result.purchasesList!!)
}

 

在取得 Purchase List 之後

我們會去檢查每個 Purchase.isAcknowledged 是否為 true

沒有的話就要向自家 server 確認是否有給予商品

然後再向 Google 提交確認

 

這樣就算完成整個的金流作業了

想發大財的朋友們,還在等什麼呢

趕快實作看看吧!!

 

 

 

資料來源:

https://developer.android.com/google/play/billing

https://github.com/android/play-billing-samples


 

 


arrow
arrow

    顏澤偉 發表在 痞客邦 留言(1) 人氣()