|
|
|
@ -4,27 +4,36 @@ import {
@@ -4,27 +4,36 @@ import {
|
|
|
|
|
Product, |
|
|
|
|
requestPurchase, |
|
|
|
|
purchaseUpdatedListener, |
|
|
|
|
getPurchaseHistory, |
|
|
|
|
finishTransaction, |
|
|
|
|
} from 'react-native-iap' |
|
|
|
|
import { ProductsEnum } from '../../common' |
|
|
|
|
import { ProductsEnum, StorageKey, appEvents } from '../../common' |
|
|
|
|
import { purchasesConfig } from '../config' |
|
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage' |
|
|
|
|
import { Alert } from 'react-native' |
|
|
|
|
|
|
|
|
|
const ID_PRODUCTS = ['ALL', 'Crz', 'un18'] |
|
|
|
|
const ID_PRODUCTS = [ProductsEnum.All, ProductsEnum.Crazy, ProductsEnum.Under18] |
|
|
|
|
|
|
|
|
|
interface ProductItem { |
|
|
|
|
productId: ProductsEnum |
|
|
|
|
price: string |
|
|
|
|
name: string |
|
|
|
|
icon: string |
|
|
|
|
isPurchased: boolean |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export class PurchasesService { |
|
|
|
|
public products: ProductItem[] = [] |
|
|
|
|
public purchasedProducts: ProductsEnum[] |
|
|
|
|
|
|
|
|
|
public init() { |
|
|
|
|
this.initializeIAP() |
|
|
|
|
this.loadProducts() |
|
|
|
|
this.getPurchasedProducts() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async getProducts() { |
|
|
|
|
const res = await AsyncStorage.getItem(StorageKey.Products) |
|
|
|
|
return JSON.parse(res) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async initializeIAP() { |
|
|
|
@ -36,49 +45,76 @@ export class PurchasesService {
@@ -36,49 +45,76 @@ export class PurchasesService {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async loadProducts() { |
|
|
|
|
public async loadProducts() { |
|
|
|
|
try { |
|
|
|
|
const products: Product[] = await getProducts({ skus: ID_PRODUCTS }) |
|
|
|
|
this.products = this.transformProductsData(products) |
|
|
|
|
const purchaseHistory = await getPurchaseHistory() |
|
|
|
|
const productss = this.transformProductsData(products) |
|
|
|
|
|
|
|
|
|
console.log('products', this.products) |
|
|
|
|
console.log('purchaseHistory', purchaseHistory) |
|
|
|
|
this.products = productss |
|
|
|
|
await this.saveProductInStore(productss) |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('Error loading products:', error) |
|
|
|
|
throw error |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async saveProductInStore(products: ProductItem[]) { |
|
|
|
|
await AsyncStorage.setItem( |
|
|
|
|
StorageKey.Products, |
|
|
|
|
JSON.stringify(products), |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async purchaseProduct(productId: ProductsEnum) { |
|
|
|
|
try { |
|
|
|
|
const purchase = await requestPurchase({ sku: productId }) |
|
|
|
|
await requestPurchase({ |
|
|
|
|
sku: productId, |
|
|
|
|
}) |
|
|
|
|
this.purchaseListener() |
|
|
|
|
|
|
|
|
|
console.log('Purchase successful:', purchase) |
|
|
|
|
await this.savePurchase(productId) |
|
|
|
|
await purchasesService.loadProducts() |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('Purchase error:', error) |
|
|
|
|
throw error |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async savePurchase(productId: ProductsEnum) { |
|
|
|
|
const newProductsId = [...this.purchasedProducts, productId] |
|
|
|
|
const newProducts = JSON.stringify(newProductsId) |
|
|
|
|
|
|
|
|
|
this.purchasedProducts = newProductsId |
|
|
|
|
|
|
|
|
|
await AsyncStorage.setItem(StorageKey.Purchases, newProducts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected async getPurchasedProducts() { |
|
|
|
|
const response = await AsyncStorage.getItem(StorageKey.Purchases) |
|
|
|
|
|
|
|
|
|
this.purchasedProducts = response ? JSON.parse(response) : [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private purchaseListener() { |
|
|
|
|
purchaseUpdatedListener(purchase => { |
|
|
|
|
finishTransaction({ purchase }) |
|
|
|
|
console.log('purchaseListener', purchase) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private transformProductsData = (products: Product[]) => { |
|
|
|
|
console.log('transformProductsData', this.purchasedProducts) |
|
|
|
|
|
|
|
|
|
return products |
|
|
|
|
.map(product => { |
|
|
|
|
const isPurchased = this.purchasedProducts.some( |
|
|
|
|
it => product.productId === it, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
...purchasesConfig[product.productId], |
|
|
|
|
productId: product.productId, |
|
|
|
|
price: product.price, |
|
|
|
|
isPurchased, |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.sort() |
|
|
|
|
.sort((a, b) => a.productId.localeCompare(b.productId)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|