Added puschase listeners

This commit is contained in:
Mustafa Buyukcelebi 2019-07-31 12:20:26 +03:00
parent 7db333212d
commit 7d1d955dec

View File

@ -1,13 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Platform, Alert } from 'react-native'; import { Platform, Alert } from 'react-native';
import RNIap, { import RNIap, { purchaseErrorListener, purchaseUpdatedListener } from 'react-native-iap';
purchaseErrorListener,
purchaseUpdatedListener,
ProductPurchase,
PurchaseError,
} from 'react-native-iap';
// import { Alert } from 'react-native';
import { injectIntl } from 'react-intl'; import { injectIntl } from 'react-intl';
// import { toastNotification } from '../../../redux/actions/uiAction'; // import { toastNotification } from '../../../redux/actions/uiAction';
@ -22,12 +16,6 @@ import { injectIntl } from 'react-intl';
// Component // Component
import BoostScreen from '../screen/boostScreen'; import BoostScreen from '../screen/boostScreen';
/*
* Props Name Description Value
*@props --> props name here description here Value Type Here
*
*/
const ITEM_SKUS = Platform.select({ const ITEM_SKUS = Platform.select({
ios: ['099points', '199points', '499points', '999points', '4999points', '9999points'], ios: ['099points', '199points', '499points', '999points', '4999points', '9999points'],
android: ['099points', '199points', '499points', '999points', '4999points', '9999points'], android: ['099points', '199points', '499points', '999points', '4999points', '9999points'],
@ -37,31 +25,47 @@ class BoostContainer extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
receipt: '',
productList: [], productList: [],
}; };
} }
// Component Life Cycle Functions // Component Life Cycle Functions
componentDidMount() {
// Component Functions
async componentDidMount() {
this._getItems(); this._getItems();
this._purchaseUpdatedListener();
} }
_getAvailablePurchases = async () => { componentWillUnmount() {
try { if (this.purchaseUpdateSubscription) {
const purchases = await RNIap.getAvailablePurchases(); this.purchaseUpdateSubscription.remove();
console.info('Available purchases :: ', purchases); this.purchaseUpdateSubscription = null;
if (purchases && purchases.length > 0) {
this.setState({
receipt: purchases[0].transactionReceipt,
});
}
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
} }
if (this.purchaseErrorSubscription) {
this.purchaseErrorSubscription.remove();
this.purchaseErrorSubscription = null;
}
}
// Component Functions
_purchaseUpdatedListener = () => {
this.purchaseUpdateSubscription = purchaseUpdatedListener(purchase => {
console.log('purchaseUpdatedListener', purchase);
const receipt = purchase.transactionReceipt;
if (receipt) {
// yourAPI.deliverOrDownloadFancyInAppPurchase(receipt).then(deliveryResult => {
// if (Platform.OS === 'ios') {
// RNIap.finishTransactionIOS(purchase.transactionId);
// } else if (Platform.OS === 'android') {
// RNIap.consumePurchaseAndroid(purchase.purchaseToken);
// }
// });
}
});
this.purchaseErrorSubscription = purchaseErrorListener(error => {
Alert.alert('Warning', error);
});
}; };
_getItems = async () => { _getItems = async () => {
@ -83,25 +87,6 @@ class BoostContainer extends Component {
} }
}; };
// _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() { render() {
const { productList } = this.state; const { productList } = this.state;