In-App-Käufe integrieren: von Website zu Flutter-App
Diese Anleitung erklärt native In-App-Käufe in einer Wrapply-App: Store-Produkte, Product IDs, Kaufaufrufe, Wiederherstellung und serverseitige Belegprüfung.
Bridge-Befehle für In-App-Käufe
Wenn das In-App-Kauf-Modul aktiviert ist, stellt die generierte App echte JavaScript-Befehle in der WebView bereit. Die Website ruft sie auf, danach sollte das Backend den Kauf prüfen und die bezahlte Funktion freischalten.
| Befehl | Verwendung | Payload | Typische Antwort |
|---|---|---|---|
window.Wrapply.iap.getProducts(payload) | Liest Produkte aus Google Play oder App Store | { productIds: ['premium_monthly'] } | { ok, available, products, notFoundIds } |
window.Wrapply.iap.purchase(payload) | Startet den nativen Kaufablauf | { productId, consumable, applicationUserName } | { ok, purchaseStarted, product } |
window.Wrapply.iap.restorePurchases(payload) | Stellt nicht konsumierbare Käufe oder Abos wieder her | { applicationUserName } | { ok, restoreStarted, latestPurchases } |
window.Wrapply.iap.getStatus(payload) | Prüft Store-Billing-Verfügbarkeit und letzte Ereignisse | {} | { ok, available, latestPurchases } |
Erforderliche Store-Konfiguration
Erstellen Sie die Produkte in Google Play Console und App Store Connect mit denselben Product IDs wie auf der Website. Testen Sie auf echten Geräten und validieren Sie Belege serverseitig, bevor Abos, Credits oder Premium-Inhalte freigeschaltet werden.
if (window.Wrapply?.__nativeBridgeReady) return callback(window.Wrapply);
window.addEventListener('wrapplyNativeBridgeReady', () => callback(window.Wrapply), { once: true });
}
onWrapplyReady(async (Wrapply) => {
if (!Wrapply.iap) return;
const products = await Wrapply.iap.getProducts({ productIds: ['premium_monthly', 'credits_10'] });
console.log(products);
});
if (!window.Wrapply?.iap) return showWebCheckoutFallback();
const result = await window.Wrapply.iap.purchase({
productId: 'premium_monthly',
consumable: false,
applicationUserName: currentUser.id
});
await fetch('/api/iap/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result)
});
}
if (!window.Wrapply?.iap) return;
const restored = await window.Wrapply.iap.restorePurchases({
applicationUserName: currentUser.id
});
await fetch('/api/iap/restore', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(restored)
});
}
Integrationsbeispiele
Das gleiche Bridge-Muster funktioniert in JavaScript, React, Vue, Angular, PHP/Laravel, WordPress oder jQuery. Wichtig ist, auf die native Bridge zu warten.
JavaScript
if (!window.Wrapply?.iap) return;
const result = await window.Wrapply.iap.purchase({
productId: 'premium_monthly',
consumable: false,
applicationUserName: currentUser.id
});
await fetch('/api/wrapply-event', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result)
});
}
React Hook
export function useWrapplyBridge() {
const [ready, setReady] = useState(false);
useEffect(() => {
if (window.Wrapply?.__nativeBridgeReady) return setReady(true);
const onReady = () => setReady(true);
window.addEventListener('wrapplyNativeBridgeReady', onReady);
return () => window.removeEventListener('wrapplyNativeBridgeReady', onReady);
}, []);
return { ready, Wrapply: ready ? window.Wrapply : null };
}
Vue Composable
export function useWrapplyBridge() {
const ready = ref(false);
onMounted(() => {
if (window.Wrapply?.__nativeBridgeReady) ready.value = true;
else window.addEventListener('wrapplyNativeBridgeReady', () => ready.value = true, { once: true });
});
return { ready };
}
Angular Service
export class WrapplyBridgeService {
async callNative(payload = {}) {
if (!window.Wrapply?.iap) return { ok: false, error: 'Bridge unavailable' };
return window.Wrapply.iap.getStatus(payload);
}
}
jQuery
if (!window.Wrapply?.iap) return;
const result = await window.Wrapply.iap.getStatus({});
$('#wrapply-output').text(JSON.stringify(result, null, 2));
});
CMS / Backend Template
<script>
window.addEventListener('wrapplyNativeBridgeReady', function () {
document.body.classList.add('wrapply-native-app');
});
</script>