m7c: Play Billing and a complete deletion path

- billing module with a testable seam: models (product ids, purchase events),
  StoreClient + PlayStoreClient over in_app_purchase, Entitlements persisted in
  SharedPreferences, BillingService orchestrating query/buy/restore/grant.
- rules encoded: grant before completePurchase (Play refunds unacknowledged
  purchases after three days), pending purchases are shown but never granted,
  restorePurchases on start so a reinstall keeps the purchase, and Remove Ads
  only hides interstitials - rewarded video stays available.
- ads gate: AdsService.adsRemoved stops both the counter and the preload, so an
  owner never requests an interstitial.
- settings sheet: shop section (price from the store, never hardcoded),
  "restore purchases", and "delete local data" with confirmation; the existing
  mail-based deletion request stays the account-deletion path since the app has
  no accounts.
- 9 new tests with a fake store cover purchase, restore, pending, cancel,
  error, restart persistence, missing store and a store that throws. 17 total.
- verified on device: the app talks to the real Play Billing service
  (IInAppBillingService in logcat) and degrades to "no products created"
  without a crash; com.android.vending.BILLING is in the merged manifest.
- docs: monetization 7c (rules + verification), decision D6 (local entitlement
  until purchases affect the server), compliance rows for billing/restore/
  deletion, blocked.md 3c (create remove_ads in the Console).
- release 0.9.0 (13).
This commit is contained in:
2026-10-25 04:06:44 +01:00
parent 82bd496cc9
commit a26e0b93b5
15 changed files with 868 additions and 6 deletions
+126
View File
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flame/game.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
@@ -6,6 +8,10 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'src/ads/ads_service.dart';
import 'src/billing/billing_service.dart';
import 'src/billing/entitlements.dart';
import 'src/billing/models.dart';
import 'src/billing/store_client.dart';
import 'src/game/iron_arc_game.dart';
import 'src/native/native_match.dart';
import 'src/net/api_client.dart';
@@ -66,6 +72,9 @@ class _MatchScreenState extends State<MatchScreen> {
IronArcGame? _game;
bool _splashGone = false;
Entitlements? _entitlements;
BillingService? _billing;
@override
void initState() {
super.initState();
@@ -96,11 +105,30 @@ class _MatchScreenState extends State<MatchScreen> {
setState(() => _splashGone = true);
debugPrint('iron_arc: ready after ${_bootWatch.elapsedMilliseconds} ms');
// Store first: entitlements gate the interstitials that ads would load.
final entitlements = await Entitlements.load();
if (!mounted) return;
_entitlements = entitlements;
entitlements.addListener(_syncAdsGate);
_syncAdsGate();
final billing = BillingService(
entitlements: entitlements,
client: PlayStoreClient(),
);
_billing = billing;
unawaited(billing.init());
_ads.initialize();
}
/// Owners of "Remove Ads" never see an interstitial.
void _syncAdsGate() => _ads.adsRemoved = _entitlements?.adsRemoved ?? false;
@override
void dispose() {
_entitlements?.removeListener(_syncAdsGate);
_billing?.dispose();
_ads.dispose();
_api.close();
_session.dispose();
@@ -199,6 +227,7 @@ class _MatchScreenState extends State<MatchScreen> {
if (!ok && mounted) _toast('Keine App zum Öffnen gefunden');
},
),
_shopSection(sheetContext),
ListTile(
leading: const Icon(Icons.campaign_outlined),
title: const Text('Werbe-Status'),
@@ -228,6 +257,36 @@ class _MatchScreenState extends State<MatchScreen> {
if (!ok && mounted) _toast('Keine E-Mail-App gefunden');
},
),
ListTile(
leading: const Icon(Icons.cleaning_services_outlined),
title: const Text('Lokale Daten löschen'),
subtitle: const Text('Verlauf, Warteschlange, Geräte-ID'),
onTap: () async {
Navigator.pop(sheetContext);
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Lokale Daten löschen?'),
content: const Text(
'Entfernt Offline-Warteschlange, Replays und die Geräte-ID '
'von diesem Gerät. Käufe bleiben erhalten.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: const Text('Abbrechen')),
FilledButton(
onPressed: () => Navigator.pop(ctx, true),
child: const Text('Löschen')),
],
),
);
if (confirmed != true || !mounted) return;
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
if (!mounted) return;
_toast('Lokale Daten gelöscht – App neu starten');
},
),
if (kDebugMode)
ListTile(
leading: const Icon(Icons.restart_alt),
@@ -245,6 +304,73 @@ class _MatchScreenState extends State<MatchScreen> {
);
}
/// Store section of the settings sheet (M7c).
Widget _shopSection(BuildContext sheetContext) {
final billing = _billing;
final ent = _entitlements;
if (billing == null || ent == null) {
return const ListTile(
leading: Icon(Icons.shopping_bag_outlined),
title: Text('Werbung entfernen'),
subtitle: Text('Store wird geladen …'),
enabled: false,
);
}
return ListenableBuilder(
listenable: Listenable.merge([billing, ent]),
builder: (context, _) {
final owned = ent.adsRemoved;
final price = billing.product(ProductIds.removeAds)?.price;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const Divider(height: 1, color: Color(0xFF1B2C44)),
ListTile(
leading: Icon(
owned ? Icons.verified : Icons.block,
color: owned ? const Color(0xFFFFB347) : null,
),
title: const Text('Werbung entfernen'),
subtitle: Text(owned
? 'Gekauft - keine Interstitials mehr'
: billing.busy
? 'Einen Moment …'
: price ?? billing.error ?? 'Preis wird geladen …'),
trailing: owned
? null
: const Icon(Icons.shopping_bag_outlined, size: 18),
enabled: !owned && billing.canPurchase && !billing.busy,
onTap: owned
? null
: () async {
Navigator.pop(sheetContext);
final started = await billing.buy(ProductIds.removeAds);
if (!mounted) return;
_toast(started
? 'Kauf gestartet'
: billing.error ?? 'Kauf nicht möglich');
},
),
if (!owned)
ListTile(
leading: const Icon(Icons.restore),
title: const Text('Käufe wiederherstellen'),
enabled: billing.available && !billing.busy,
onTap: () async {
Navigator.pop(sheetContext);
await billing.restore();
if (!mounted) return;
_toast(ent.adsRemoved
? 'Käufe wiederhergestellt'
: 'Keine Käufe gefunden');
},
),
],
);
},
);
}
Future<void> _showRewardedAd() async {
if (!_ads.isAdsAllowed) {
_toast('Anzeigen erst nach Zustimmung');