Android/Android 일반

2019 인앱결제(IAP, In app purchase)

lipnus 2019. 7. 25. 00:16
반응형




Build.gradle(Project)

buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
mavenCentral()
}



Build.gradle(Module)

implementation 'com.anjlab.android.iab.v3:library:1.0.44'



AndroidMenifest.xml

<uses-permission android:name="com.android.vending.BILLING" />




PlayConsole

1. App을 하나 등록

2. 출시관리 > 서비스 및 API 가서 키를 저장해놓음

3. 앱정보 > 인앱상품에서 상품등록



KeyInfo.kt

//인앱결제 라이센스 키
const val INAPP_PURCHAGE_KET = "API키"

키들은 따로 모아서 관리



IapTestActivity.kt

package com.empo.android.skct

import android.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.anjlab.android.iab.v3.BillingProcessor
import com.anjlab.android.iab.v3.TransactionDetails
import com.empo.android.skct.util.INAPP_PURCHAGE_KET
import kotlinx.android.synthetic.main.activity_iap_test.*

class IapTestActivity : AppCompatActivity(), BillingProcessor.IBillingHandler {

//IAP
private val productIds = arrayOf("skct_play_package")
private lateinit var bp: BillingProcessor //인앱결제


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_iap_test)

initIap()
initLayout()
}


private fun initLayout(){
button.setOnClickListener {
purchaseItem( productIds[0] )
}
}


private fun initIap() {
bp = BillingProcessor(this, INAPP_PURCHAGE_KET, this).apply {
initialize()
}
}


private fun showBasicDialog(msg: Int){
val builder = AlertDialog.Builder(this)

builder.run {
setMessage( getText(msg) )
setPositiveButton( "확인" ) { dialog, which -> }
setCancelable(false)
}
builder.create().show()
}


private fun purchaseItem(productId: String){

//인앱결제 초기화 실패
if( !bp.isInitialized){
showBasicDialog( R.string.iap_init_error )
return
}

if( !bp.isPurchased(productId)){ //구매
bp.purchase( this, productId )
}else{
//구매했으나 소비처리가 되지 않음
}
}




override fun onBillingInitialized() {
}

override fun onPurchaseHistoryRestored() {
}

override fun onProductPurchased(productId: String, details: TransactionDetails?) {
}

override fun onBillingError(errorCode: Int, error: Throwable?) {
}

}


반응형