Skip to content

Spring Boot 안드로이드 인앱 구매 서버 사이드 검증

Published: at 오후 12:28

Table of contents

Open Table of contents

결제 검증

다음과 같은 순서로 결제 검증을 수행합니다.

  1. 사용자가 안드로이드 앱에서 구매를 시도합니다.
  2. 구매가 성공하면, 구매 정보를 앱에서 서버로 전송합니다.
  3. 구매 토큰을 구글 서버로 전송하여 구매가 유효한지 확인합니다.
  4. 구매가 유효하면, 서버에서 구매 정보 및 상태를 저장합니다.
  5. 구매 검증이 완료되고 사용자에게 구매 완료 후 처리를 수행합니다.

Google Play Developer API 요청 인증

먼저 Google Play 개발 API를 사용하기 위해선 Google Cloud Platform에서 프로젝트를 생성하고 연동해야합니다.

  1. Google Cloud Platform에 접속하여 프로젝트를 생성합니다.
  2. Google Play Developer API를 활성화합니다.
  3. API 및 서비스 > 사용자 인증 정보에서 서비스 계정을 생성합니다.
  4. 생성된 서비스 계정을 다운로드하고, json 파일을 프로젝트에 추가합니다.
  5. Google Play Developer에 대한 권한을 부여합니다.

일반적으로 키를 생성한 후 24시간이상을 기다려야 한다고 합니다. 또는 인앱상품 또는 구독에 대한 설명을 업데이트하기만 하면즉시 키가 활성화됩니다.

Google Play Developer API - “The current user has insufficient permissions to perform the requested operation.”

Spring Boot 프로젝트에서 resources 디렉토리에 다운로드 받은 json 파일을 추가한 후 config 파일을 생성합니다.

@Configuration
class GoogleCredentialsConfig {
    @Value("\${google.credentials.path}")
    lateinit var credentialsPath: String

    @Value("\${google.credentials.projectId}")
    lateinit var projectId: String

    @Bean
    fun androidPublisher(): AndroidPublisher {
        val inputStream = ClassPathResource(credentialsPath).inputStream

        val credentials = GoogleCredentials.fromStream(inputStream)
            .createScoped(listOf(AndroidPublisherScopes.ANDROIDPUBLISHER))
        return AndroidPublisher.Builder(
            GoogleNetHttpTransport.newTrustedTransport(),
            GsonFactory.getDefaultInstance(),
            HttpCredentialsAdapter(credentials)
        )
            .setApplicationName(projectId)
            .build()
    }
}

구매 검증

인앱 상품의 구매 및 소비 상태를 확인하기 위해 purchases.products.get API를 사용합니다. purchases.products.get

@Service
class PurchaseService(
    private val androidPublisher: AndroidPublisher
) {
    fun verifyPurchase(purchaseToken: String, productId: String): Boolean {
        val request = androidPublisher.purchases().products().get(
            "com.example", // 인앱 상품이 판매된 애플리케이션의 패키지 이름입니다 (예: 'com.some.thing').
            productId, // 인앱 상품 SKU입니다 (예: 'product1').
            purchaseToken // 구매 토큰
        )
        val response = request.execute()
        return response.purchaseState == 0
    }
}

참조

Google Cloud Platform 연동 설정

일회성 구매 수명 주기  |  Google Play’s billing system  |  Android Developers

REST Resource: purchases.subscriptions  |  Google Play Developer API  |  Google for Developers

Pub/Sub client libraries  |  Cloud Pub/Sub Documentation  |  Google Cloud

Android 인앱 구매, 5부: 서버 측 구매 검증

인앱결제 서버 개발 (영수증 검증)