Files
2nd/10_Wiki/Topic_Programming/Coding/Android_BillingClient_IAP.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

5.1 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
android-billingclient-iap Android Billing Client — IAP / Subscription Coding draft B conceptual 2026-05-09 2026-05-09
android
billing
iap
subscription
vibe-coding
language applicable_to
Kotlin / com.android.billingclient
Android
Google Play Billing
Purchase
acknowledgePurchase
server-side validation

Android Billing Client (IAP)

Google Play 결제. acknowledge / consume 안 하면 사용자 환불. 서버 검증 (Real-Time Developer Notifications + Subscription API) 필수.

📖 핵심 개념

  • ProductDetails: Google Play Console 등록 상품.
  • Purchase: 구매 결과. acknowledged / consumed 상태.
  • Acknowledge: 3일 안 안 하면 자동 환불.
  • Server: Pub/Sub RTDN 으로 갱신 / 환불 / 만료 통지.

💻 코드 패턴

의존성

implementation("com.android.billingclient:billing-ktx:7.0.0")

Setup

class BillingManager(ctx: Context) : PurchasesUpdatedListener {

    private val client = BillingClient.newBuilder(ctx)
        .setListener(this)
        .enablePendingPurchases()
        .build()

    suspend fun startConnection(): BillingResult = suspendCancellableCoroutine { cont ->
        client.startConnection(object : BillingClientStateListener {
            override fun onBillingSetupFinished(result: BillingResult) { cont.resume(result) }
            override fun onBillingServiceDisconnected() { /* retry later */ }
        })
    }

    suspend fun queryProducts(ids: List<String>): List<ProductDetails> {
        val params = QueryProductDetailsParams.newBuilder()
            .setProductList(ids.map {
                QueryProductDetailsParams.Product.newBuilder()
                    .setProductId(it)
                    .setProductType(BillingClient.ProductType.SUBS)
                    .build()
            })
            .build()
        return client.queryProductDetails(params).productDetailsList ?: emptyList()
    }

    fun launchPurchase(activity: Activity, product: ProductDetails) {
        val flow = BillingFlowParams.newBuilder()
            .setProductDetailsParamsList(listOf(
                BillingFlowParams.ProductDetailsParams.newBuilder()
                    .setProductDetails(product)
                    .setOfferToken(product.subscriptionOfferDetails!!.first().offerToken)
                    .build()
            ))
            .setObfuscatedAccountId(currentUserId) // server matching
            .build()
        client.launchBillingFlow(activity, flow)
    }

    override fun onPurchasesUpdated(result: BillingResult, purchases: List<Purchase>?) {
        if (result.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
            for (p in purchases) handlePurchase(p)
        }
    }

    private fun handlePurchase(purchase: Purchase) {
        if (purchase.purchaseState != Purchase.PurchaseState.PURCHASED) return

        // 1) 서버 검증
        scope.launch {
            val verified = api.verify(purchase.purchaseToken, purchase.products.first(), currentUserId)
            if (!verified) return@launch

            // 2) acknowledge (subscription) or consume (consumable)
            if (!purchase.isAcknowledged) {
                val params = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.purchaseToken)
                    .build()
                client.acknowledgePurchase(params) {}
            }
        }
    }
}

서버 검증 (Subscription API)

// Backend
import { google } from 'googleapis';

const auth = new google.auth.GoogleAuth({ keyFile: serviceAccountPath, scopes: ['https://www.googleapis.com/auth/androidpublisher'] });
const pub = google.androidpublisher({ version: 'v3', auth });

const r = await pub.purchases.subscriptionsv2.get({
  packageName: 'com.example.app',
  token: purchaseToken,
});

if (r.data.subscriptionState === 'SUBSCRIPTION_STATE_ACTIVE') {
  await grantEntitlement(userId, productId);
}

RTDN (Real-Time Developer Notifications)

  • Google Cloud Pub/Sub 구독.
  • 갱신 / 환불 / 만료 / 환경 변경 notify.
  • 서버가 사용자 entitlement 갱신.

🤔 의사결정 기준

상품 type
1회 영구 (광고 제거) INAPP
사용 후 소진 (코인) INAPP + consume
월/년 구독 SUBS + offer
무료 trial SUBS + introductoryPrice offer

안티패턴

  • acknowledge 안 함: 3일 후 자동 환불.
  • client-side validation 만: 우회 가능. 서버 + signature 검증.
  • consume vs acknowledge 헷갈림: consumable 만 consume, 그 외 acknowledge.
  • obfuscatedAccountId 미설정: 결제 ↔ 사용자 매핑 어려움.
  • RTDN 없음: 환불 / 만료 모름.
  • client 가 entitlement 결정: 서버가 진실. 클라는 표시.
  • 동시에 여러 구독 가능: 의도와 다른 구독 활성. SkuDetails 하나만 active.

🤖 LLM 활용 힌트

  • BillingClient 7+ + obfuscatedAccountId + 서버 검증 + RTDN 4종 세트.
  • acknowledge / consume 분리.

🔗 관련 문서