aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/iab/IAB.kt
blob: e047e2ffb469321e98a2b4e9e730c214d4e8dddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.pitchedapps.frost.utils.iab

import android.app.Activity
import android.content.Context
import ca.allanwang.kau.utils.isFromGooglePlay
import com.crashlytics.android.answers.PurchaseEvent
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.SettingsActivity
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.frostAnswers
import com.pitchedapps.frost.utils.frostAnswersCustom

/**
 * Created by Allan Wang on 2017-06-23.
 */
object IAB {

    var helper: IabHelper? = null

    fun setupAsync(activity: Activity) {
        if (helper == null) {
            L.d("IAB setup async")
            if (!activity.isFromGooglePlay && !BuildConfig.DEBUG) return L.d("IAB not from google play")
            try {
                helper = IabHelper(activity.applicationContext, PUBLIC_BILLING_KEY)
                helper!!.enableDebugLogging(BuildConfig.DEBUG, "Frost:")
                helper!!.startSetup {
                    result ->
                    L.d("IAB result ${result.message}")
                    if (!result.isSuccess) L.eThrow("IAB Setup error: $result")
                }
            } catch (e: Exception) {
                L.e(e, "IAB error")
                activity.playStoreNoLongerPro()
            }
        }
    }

    /**
     * If user has pro, check if it's valid and destroy the helper
     */
    fun validatePro(activity: Activity) {

    }
}

private const val FROST_PRO = "frost_pro"

val IS_FROST_PRO: Boolean
    get() = (BuildConfig.DEBUG && Prefs.debugPro) || Prefs.previouslyPro

private val Context.isFrostPlay: Boolean
    get() = isFromGooglePlay || BuildConfig.DEBUG

fun SettingsActivity.restorePurchases() {

}

fun Activity.openPlayProPurchase(code: Int) = openPlayPurchase(FROST_PRO, code) {
    Prefs.previouslyPro = true
}

fun Activity.openPlayPurchase(key: String, code: Int, onSuccess: (key: String) -> Unit) {
    L.d("Open play purchase $key $code")
    if (!isFrostPlay) return playStoreNotFound()
    frostAnswersCustom("PLAY_PURCHASE") {
        putCustomAttribute("Key", key)
    }
    L.d("IAB flag end async")
    IAB.helper?.flagEndAsync() ?: return playStoreGenericError("Null flag end async")
    L.d("IAB query inv async")
    try {
        IAB.helper!!.queryInventoryAsync {
            res, inv ->
            if (res.isFailure) return@queryInventoryAsync playStoreGenericError("Query res error")
            if (inv?.getSkuDetails(key) != null) return@queryInventoryAsync playStoreAlreadyPurchased(key)
            L.d("IAB: inventory ${inv.allOwnedSkus}")
            IAB.helper!!.launchPurchaseFlow(this@openPlayPurchase, key, code) {
                result, _ ->
                if (result.isSuccess) {
                    onSuccess(key)
                    playStorePurchasedSuccessfully(key)
                }
                frostAnswers {
                    logPurchase(PurchaseEvent()
                            .putItemId(key)
                            .putCustomAttribute("result", result.message)
                            .putSuccess(result.isSuccess))
                }
            }
        }
    } catch(e: IabHelper.IabAsyncInProgressException) {
        L.e(e, "IAB query dup")
    }
}