From 0919190de304c48e2f1684d8509e872df4c1c749 Mon Sep 17 00:00:00 2001 From: u-e Date: Tue, 23 Jul 2019 22:11:06 +0300 Subject: [PATCH] wip on iap --- src/screens/boost/container/boostContainer.js | 129 +++++++++++++----- src/screens/boost/screen/boostScreen.js | 21 +-- 2 files changed, 95 insertions(+), 55 deletions(-) diff --git a/src/screens/boost/container/boostContainer.js b/src/screens/boost/container/boostContainer.js index a0b0b5706..5d9bac516 100644 --- a/src/screens/boost/container/boostContainer.js +++ b/src/screens/boost/container/boostContainer.js @@ -28,59 +28,114 @@ import BoostScreen from '../screen/boostScreen'; * */ +const ITEM_SKUS = Platform.select({ + ios: ['099points', '199points', '499points', '999points', '4999points', '9999points'], + android: ['099points', '199points', '499points', '999points', '4999points', '9999points'], +}); + +const BOOST_DATA = Platform.select({ + ios: [ + { + id: '099points', + name: '10000 ESTM', + priceText: '100$', + price: 100, + description: 'BEST DEAL!', + }, + { id: '199points', name: '5000 ESTM', quantity: 500, price: 50, description: 'POPULAR' }, + { id: '499points', name: '1000 ESTM', quantity: 10000, price: 10, description: '' }, + { id: '999points', name: '500 ESTM', quantity: 500, price: 5, description: '' }, + { id: '4999points', name: '200 ESTM', quantity: 200, price: 2, description: '' }, + { id: '9999points', name: '100 ESTM', quantity: 100, price: 1, description: '' }, + ], + android: [ + { + id: '099points', + name: '10000 ESTM', + priceText: '100$', + price: 100, + description: 'BEST DEAL!', + }, + { id: '199points', name: '5000 ESTM', quantity: 500, price: 50, description: 'POPULAR' }, + { id: '499points', name: '1000 ESTM', quantity: 10000, price: 10, description: '' }, + { id: '999points', name: '500 ESTM', quantity: 500, price: 5, description: '' }, + { id: '4999points', name: '200 ESTM', quantity: 200, price: 2, description: '' }, + { id: '9999points', name: '100 ESTM', quantity: 100, price: 1, description: '' }, + ], +}); + class BoostContainer extends Component { constructor(props) { super(props); - this.state = {}; + this.state = { + receipt: '', + // productList: '', + }; } // Component Life Cycle Functions async componentDidMount() { - try { - const itemSkus = Platform.select({ - ios: ['app.esteem.mobile.ios.099_points'], - android: ['com.example.099points'], - }); - // await RNIap.prepare(); - const products = await RNIap.getProducts([ - '099points', - '199points', - '499points', - '999points', - '4999points', - '9999points', - ]); - // this.setState({ items }); - console.log(products); - } catch (err) { - console.warn(err); - } - - this.purchaseUpdateSubscription = purchaseUpdatedListener((purchase: ProductPurchase) => { - console.log('purchaseUpdatedListener', purchase); - const receipt = purchase.transactionReceipt; - if (receipt) { - alert('done'); - } - }); - - this.purchaseErrorSubscription = purchaseErrorListener((error: PurchaseError) => { - console.warn('purchaseErrorListener', error); - }); + this._getItems(); } - // Component Functions - - _purchase = async sku => { + _getAvailablePurchases = async () => { try { - await RNIap.requestPurchase('099points', false); + const purchases = await RNIap.getAvailablePurchases(); + console.info('Available purchases :: ', purchases); + if (purchases && purchases.length > 0) { + this.setState({ + receipt: purchases[0].transactionReceipt, + }); + } + } catch (err) { + console.warn(err.code, err.message); + Alert.alert(err.message); + } + }; + + _getItems = async () => { + try { + console.log(ITEM_SKUS); + const products = await RNIap.getProducts(ITEM_SKUS); + // const products = await RNIap.getSubscriptions(itemSkus); + console.log('Products', products); + // this.setState({ productList: products }); } catch (err) { console.warn(err.code, err.message); } }; + // Component Functions + + _buyItem = async sku => { + try { + RNIap.requestPurchase(sku); + } catch (err) { + console.warn(err.code, err.message); + } + }; + + // _buyItem = async sku => { + // console.info('buyItem', sku); + // // const purchase = await RNIap.buyProduct(sku); + // // const products = await RNIap.buySubscription(sku); + // // const purchase = await RNIap.buyProductWithoutFinishTransaction(sku); + // try { + // const purchase = await RNIap.buyProduct(sku); + // // console.log('purchase', purchase); + // // await RNIap.consumePurchaseAndroid(purchase.purchaseToken); + // this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext()); + // } catch (err) { + // console.warn(err.code, err.message); + // const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async purchase => { + // this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext()); + // subscription.remove(); + // }); + // } + // }; + render() { - return ; + return ; } } diff --git a/src/screens/boost/screen/boostScreen.js b/src/screens/boost/screen/boostScreen.js index 414a181c4..d030928ed 100644 --- a/src/screens/boost/screen/boostScreen.js +++ b/src/screens/boost/screen/boostScreen.js @@ -12,21 +12,6 @@ import { Icon } from '../../../components/icon'; import globalStyles from '../../../globalStyles'; import styles from './boostScreenStyles'; -const BOOST_DATA = [ - { - id: 0, - name: '10000 ESTM', - priceText: '100$', - price: 100, - description: 'BEST DEAL!', - }, - { id: 1, name: '5000 ESTM', quantity: 500, price: 50, description: 'POPULAR' }, - { id: 2, name: '1000 ESTM', quantity: 10000, price: 10, description: '' }, - { id: 3, name: '500 ESTM', quantity: 500, price: 5, description: '' }, - { id: 4, name: '200 ESTM', quantity: 200, price: 2, description: '' }, - { id: 5, name: '100 ESTM', quantity: 100, price: 1, description: '' }, -]; - class BoostScreen extends PureComponent { /* Props * ------------------------------------------------ @@ -45,7 +30,7 @@ class BoostScreen extends PureComponent { // Component Functions render() { - const { intl, purchase } = this.props; + const { intl, buyItem, boostData } = this.props; const { selectedBoost } = this.state; return ( @@ -56,7 +41,7 @@ class BoostScreen extends PureComponent { })} /> - {BOOST_DATA.map(item => ( + {boostData.map(item => ( {!!get(item, 'description', null) && ( @@ -69,7 +54,7 @@ class BoostScreen extends PureComponent { purchase()} + onPress={() => buyItem(item.id)} height={50} text={intl.formatMessage({ id: 'boost.buy',