신뢰할 수 있는 웹 활동에서 Google Play 결제 사용

이 가이드는 신뢰할 수 있는 웹 활동으로 패키징된 웹 앱을 대상으로 합니다. Kotlin 또는 자바로 작성된 Android 앱의 경우 Google Play 결제 문서를 참고하세요.

디지털 상품 또는 정기 결제를 판매하는 Google Play 앱은 Google Play 결제를 사용해야 합니다. 신뢰할 수 있는 웹 활동에서는 Android 및 ChromeOS의 Chrome 101 이상에서 사용할 수 있는 다음 웹 API를 사용하여 통합합니다.

요구사항

Google Play 결제를 설정하려면 다음이 필요합니다.

Play 스토어에서 앱을 다운로드하는 사용자에게 요금을 청구하지 않습니다. PWA는 웹에서 계속 사용할 수 있으므로 유료 다운로드로 인해 액세스가 제한되지 않습니다. 대신 인앱 구매 또는 정기 결제를 통해 기능이나 콘텐츠를 판매하세요.

Android 프로젝트에서 Play 결제 사용 설정

Bubblewrap으로 앱을 빌드하는 경우 프로젝트 루트의 twa-manifest.json에서 playBilling 기능을 사용 설정합니다.

"features": {
  "playBilling": {
    "enabled": true
  }
}

그런 다음 bubblewrap update, bubblewrap build를 차례로 실행하고 새 Android 패키지를 Play 스토어에 업로드합니다.

Android 스튜디오에서 앱을 빌드하는 경우 Android 브라우저 도우미 Play 결제 라이브러리를 추가합니다. 필수 DelegationService 및 AndroidManifest.xml 변경사항은 twa-play-billing 데모를 참고하세요.

Google Play 결제 지원 확인

Play 스토어 식별자 https://play.google.com/billing를 사용하여 getDigitalGoodsService()를 호출합니다. 메서드가 누락되었거나 호출이 실패하면 다른 결제 흐름을 사용합니다.

if ('getDigitalGoodsService' in window) {
  try {
    const service =
        await window.getDigitalGoodsService('https://play.google.com/billing');
    // Google Play Billing is available.
  } catch (error) {
    // Google Play Billing isn't available. Use another payment flow.
  }
}

제품 세부정보 표시

getDetails()를 사용하여 제품 ID의 제목, 설명, 가격을 가져오면 표시되는 가격이 Play 스토어와 일치합니다.

const details = await service.getDetails(['shiny_sword', 'gem']);
for (const item of details) {
  const localizedPrice = new Intl.NumberFormat(
      navigator.language,
      {style: 'currency', currency: item.price.currency}
  ).format(item.price.value);

  renderProductDetails(
      item.itemId, item.title, localizedPrice, item.description);
}

Digital Goods API에서 제품 ID를 나열할 수 없습니다. 백엔드에서 쿼리하려면 Google Play Developer API를 사용하세요.

구매

data에서 https://play.google.com/billing를 결제 수단으로, 제품 ID를 sku로 사용하여 PaymentRequest를 만듭니다. Google Play 결제는 Play Console의 가격을 사용하므로 total는 필수이지만 무시됩니다.

show()가 확인되면 구매 토큰을 백엔드로 전송하여 구매를 확인합니다. 3일 이내에 구매를 확인하지 않으면 Google Play에서 사용자에게 환불하고 구매를 취소합니다.

async function makePurchase(service, sku) {
  const paymentMethods = [{
    supportedMethods: 'https://play.google.com/billing',
    data: {sku},
  }];
  // Required by the Payment Request API, but ignored by Google Play Billing.
  const paymentDetails = {
    total: {label: 'Total', amount: {currency: 'USD', value: '0'}},
  };

  const request = new PaymentRequest(paymentMethods, paymentDetails);
  try {
    const response = await request.show();
    const {purchaseToken} = response.details;

    if (await acknowledgePurchaseOnBackend(purchaseToken, sku)) {
      // Optional: consume the purchase so the user can buy the item again.
      await service.consume(purchaseToken);
      await response.complete('success');
    } else {
      await response.complete('fail');
    }
  } catch (error) {
    // An AbortError usually means the user canceled the purchase.
  }
}

사용자의 구매 내역 확인하기

listPurchases()를 사용하여 활성 정기 결제, 소비되지 않은 구매, 사용된 프로모션 코드 등 사용자가 소유한 항목을 찾습니다. 백엔드에서 각 구매를 확인하고 확인하지 않은 구매를 확인합니다.

const purchases = await service.listPurchases();
for (const p of purchases) {
  await verifyOrAcknowledgePurchaseOnBackend(p.purchaseToken, p.itemId);
  // Update the UI with items the user is entitled to.
}

통합 테스트

Play 스토어에 게시하기 전에 Android 기기에서 테스트하려면 다음 단계를 따르세요.

  1. 개발자 옵션이 사용 설정된 Android 9 이상을 사용합니다.
  2. Chrome에서 chrome://flags로 이동하여 #enable-debug-for-store-billing를 사용 설정합니다.
  3. HTTPS를 통해 사이트를 제공합니다. Digital Goods API는 HTTP를 통해 사용할 수 없습니다.

앱이 Play 스토어에서 설치된 경우 플래그가 필요하지 않습니다. 테스트 계정 및 테스트 제품으로 테스트하려면 Google Play Billing Library 통합 테스트를 참고하세요.

다음 단계