Added puschase listeners

This commit is contained in:
Mustafa Buyukcelebi 2019-07-31 12:20:26 +03:00
parent 1bf9b5b857
commit 996921c488

View File

@ -1,13 +1,7 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Platform, Alert } from 'react-native';
import RNIap, {
purchaseErrorListener,
purchaseUpdatedListener,
ProductPurchase,
PurchaseError,
} from 'react-native-iap';
// import { Alert } from 'react-native';
import RNIap, { purchaseErrorListener, purchaseUpdatedListener } from 'react-native-iap';
import { injectIntl } from 'react-intl';
// import { toastNotification } from '../../../redux/actions/uiAction';
@ -22,12 +16,6 @@ import { injectIntl } from 'react-intl';
// Component
import BoostScreen from '../screen/boostScreen';
/*
* Props Name Description Value
*@props --> props name here description here Value Type Here
*
*/
const ITEM_SKUS = Platform.select({
ios: ['099points', '199points', '499points', '999points', '4999points', '9999points'],
android: ['099points', '199points', '499points', '999points', '4999points', '9999points'],
@ -37,31 +25,47 @@ class BoostContainer extends Component {
constructor(props) {
super(props);
this.state = {
receipt: '',
productList: [],
};
}
// Component Life Cycle Functions
// Component Functions
async componentDidMount() {
componentDidMount() {
this._getItems();
this._purchaseUpdatedListener();
}
_getAvailablePurchases = async () => {
try {
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);
componentWillUnmount() {
if (this.purchaseUpdateSubscription) {
this.purchaseUpdateSubscription.remove();
this.purchaseUpdateSubscription = null;
}
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 () => {
@ -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() {
const { productList } = this.state;