본문으로 건너뛰기

인앱 결제 예시

KPlayAPI 클래스를 사용한 상점 구현 예시입니다.

const kplay = new KPlayAPI();

const shopItems = [
{ id: 'boost-1', name: 'Click Boost x2', price: 5, icon: '👆', apply: () => { coinsPerClick *= 2; } },
{ id: 'auto-1', name: 'Auto Clicker', price: 10, icon: '🤖', apply: () => { coinsPerSecond += 1; } },
{ id: 'skin-gold', name: 'Gold Skin', price: 100, icon: '✨', apply: () => { player.skin = 'gold'; } },
];

let pendingItem = null;

kplay.on('purchaseResult', (result) => {
if (result.success && pendingItem) {
pendingItem.apply();
showToast(`${pendingItem.name} 구매 완료!`);
} else if (!result.success) {
showToast(`${result.error}`);
}
pendingItem = null;
renderShop();
});

function buyItem(item) {
pendingItem = item;
kplay.purchase(item.id, item.name, item.price);
}

function renderShop() {
document.getElementById('shop').innerHTML = shopItems.map((item, i) => `
<button onclick="buyItem(shopItems[${i}])">
${item.icon} ${item.name}${item.price} 크레딧
</button>
`).join('');
}

결제 테스트 게임 플레이하기