WT1.1.5 In-app-purchase

This commit is contained in:
bossanyit
2021-01-24 14:34:13 +01:00
parent d44fefe9af
commit 71efd3f349
52 changed files with 847 additions and 367 deletions
+49 -19
View File
@@ -1,16 +1,19 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'dart:math' as math;
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/product.dart';
import 'package:aitrainer_app/model/product_test.dart';
import 'package:aitrainer_app/model/purchase.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/service/product_test_service.dart';
import 'package:aitrainer_app/util/platform_purchase.dart';
import 'package:aitrainer_app/service/purchase.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/purchases.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flurry/flurry.dart';
import 'package:purchases_flutter/offering_wrapper.dart';
part 'sales_event.dart';
part 'sales_state.dart';
@@ -28,15 +31,34 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
try {
if (event is SalesLoad) {
yield SalesLoading();
//Flurry.logEvent("SalesPageOpen");
// await PlatformPurchaseApi().initPurchasePlatformState();
// this.getProductSet();
log("Load Sales");
Common.sendMessage("Salespage Load");
Flurry.logEvent("SalesPageOpen");
//await PlatformPurchaseApi().initPurchasePlatform();
await RevenueCatPurchases().getOfferings();
this.getProductSet();
yield SalesReady();
} else if (event is SalesPurchase) {
if (Cache().hasPurchased) {
throw Exception("You have already a successfull subscription");
}
yield SalesLoading();
final int productId = event.productId;
trace("Requesting purchase for" + productId.toString());
//Flurry.logEvent("PurchaseRequest");
// PlatformPurchaseApi().purchase(getSelectedProduct(productId));
log("Requesting purchase for: " + productId.toString());
Flurry.logEvent("PurchaseRequest");
final Product selectedProduct = this.getSelectedProduct(productId);
log("SelectedProduct for purchase " + selectedProduct.toString());
await RevenueCatPurchases().makePurchase(selectedProduct);
if (Cache().hasPurchased) {
Purchase purchase = Purchase(customerId: Cache().userLoggedIn.customerId, productId: productId);
purchase.dateAdd = DateTime.now();
purchase.purchaseSum = 0;
purchase.currency = "EUR";
await PurchaseApi().savePurchase(purchase);
Flurry.logEvent("PurchaseSuccessful");
Common.sendMessage("Purchase: " + purchase.toJson().toString());
}
yield SalesSuccessful();
}
} on Exception catch (ex) {
yield SalesError(message: ex.toString());
@@ -53,18 +75,24 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
return prod;
}
/* String getLocalizedPrice(String productId) {
String getLocalizedPrice(String productId) {
String price = "";
for (var product in PlatformPurchaseApi().getIAPItems()) {
if (Platform.isAndroid) {
print("PlatformProduct " + product.toString());
if (productId == product.productId) {
price = product.localizedPrice;
Offering offering = RevenueCatPurchases().offering;
if (offering != null) {
for (var package in offering.availablePackages) {
log("PlatformProduct " + package.toString());
if (productId == package.product.identifier) {
price = package.product.priceString;
if (price.contains(r'HUF')) {
price = price.replaceAll(RegExp(r'HUF'), 'Ft');
price = price.replaceAll(RegExp(r',00'), "");
}
break;
}
}
} else {
log(" !!! No Offering");
}
return price;
}
@@ -73,7 +101,7 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
this.tests = Cache().productTests;
if (tests.isEmpty) {
var rand = Random.secure();
var rand = math.Random.secure();
productSet = rand.nextInt(5) + 1;
} else {
trace("Previous ProductTest: " + tests[0].toJson().toString());
@@ -88,7 +116,9 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
}
ProductTest productTest = ProductTest();
trace("ProductSet: " + productSet.toString());
productSet = 2;
log("ProductSet: " + productSet.toString());
for (var elem in Cache().products) {
Product product = elem as Product;
@@ -109,5 +139,5 @@ class SalesBloc extends Bloc<SalesEvent, SalesState> with Logging {
productTest.dateView = DateTime.now();
//ProductTestApi().saveProductTest(productTest);
//Cache().productTests.add(productTest);
} */
}
}
+4
View File
@@ -19,6 +19,10 @@ class SalesReady extends SalesState {
const SalesReady();
}
class SalesSuccessful extends SalesState {
const SalesSuccessful();
}
class SalesError extends SalesState {
final String message;
const SalesError({this.message});