---
url: https://developers.webzenk.kr/sdk/html5/iap-ads.md
---
# 결제·광고

## 결제

플랫폼 결제(현재 삼성 IAP)를 서버에서 검증합니다.

```js
const products = await api.iap.products(['gem_100'])   // priceString: 표시 가격
try {
  const p = await api.iap.purchase('gem_100', grantItem) // 검증 → grantItem → 완료 보고 → 플랫폼 소진
} catch (e) {
  if (e.code === 'CANCELED') return                      // 유저 취소
  throw e
}

async function grantItem(purchase) {    // 같은 결제로 두 번 호출되지 않습니다
  addGems(100)
  await api.storage.update('save', (s) => ({ ...s, gems: s.gems + 100 }))
}

await api.iap.restore(grantItem)        // 게임 시작마다 — 지난번 미지급분 복구
```

* `grantItem`이 예외를 던지거나 지급 중 페이지가 닫히면 소진하지 않고 남겨 둡니다. 다음 실행의 `restore()`가 다시 지급합니다.
* 결제 요청에는 `wk:<player_id>`가 들어가서, 다른 유저의 영수증은 서버가 거부합니다.

## 광고

```js
const r = await api.ads.showRewarded()
if (r.rewarded) giveReward()           // 끝까지 본 경우만
await api.ads.showInterstitial()
api.ads.preload('rewarded')            // 미리 로드하면 바로 재생
```

| 결과 | 설명 |
|---|---|
| `shown` | 광고가 재생됐는지 |
| `rewarded` | 보상형을 끝까지 봤는지 (스킵·오류면 false) |
| `reason` | `no_fill` · `show_error` · `busy` · `not_supported` · `timeout` |

HTML5 플랫폼 광고는 서버 콜백이 없어서 `rewarded`로 게임이 판단합니다. 하루 횟수 같은 제한은 게임에서 관리하세요.
