From 7d1d955dec9c2f59e9972d07221b4aa7daceb843 Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Wed, 31 Jul 2019 12:20:26 +0300 Subject: [PATCH 1/2] Added puschase listeners --- src/screens/boost/container/boostContainer.js | 81 ++++++++----------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/src/screens/boost/container/boostContainer.js b/src/screens/boost/container/boostContainer.js index 4ec1aa368..9943ebbde 100644 --- a/src/screens/boost/container/boostContainer.js +++ b/src/screens/boost/container/boostContainer.js @@ -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; From 3cc6dafec36a0bb0877301fea22c0d219322fbfc Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Mon, 5 Aug 2019 14:42:23 +0300 Subject: [PATCH 2/2] Added esteem backend after puchase action --- App.js | 6 +-- package.json | 2 +- src/config/bugsnag.js | 5 ++ src/providers/esteem/esteem.js | 2 + src/screens/boost/container/boostContainer.js | 54 ++++++++++++------- yarn.lock | 8 +-- 6 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 src/config/bugsnag.js diff --git a/App.js b/App.js index 870c3eda8..53ecd9a1f 100644 --- a/App.js +++ b/App.js @@ -1,7 +1,5 @@ -import { Client } from 'bugsnag-react-native'; -import Config from 'react-native-config'; +// eslint-disable-next-line +import bugsnag from './src/config/bugsnag'; import App from './src/index'; -const bugsnag = new Client(Config.BUGSNAG_API_KEY); - export default App; diff --git a/package.json b/package.json index be646cac7..1931fd3af 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "react-native-datepicker": "^1.7.2", "react-native-extended-stylesheet": "^0.10.0", "react-native-fast-image": "^4.0.14", - "react-native-iap": "^3.3.0", + "react-native-iap": "^3.3.8", "react-native-image-crop-picker": "^0.24.1", "react-native-keyboard-aware-scroll-view": "^0.8.0", "react-native-linear-gradient": "^2.4.2", diff --git a/src/config/bugsnag.js b/src/config/bugsnag.js new file mode 100644 index 000000000..573707a03 --- /dev/null +++ b/src/config/bugsnag.js @@ -0,0 +1,5 @@ +import { Client } from 'bugsnag-react-native'; +import Config from 'react-native-config'; + +const client = new Client(Config.BUGSNAG_API_KEY); +export default client; diff --git a/src/providers/esteem/esteem.js b/src/providers/esteem/esteem.js index 0872ca7e1..b20843e73 100644 --- a/src/providers/esteem/esteem.js +++ b/src/providers/esteem/esteem.js @@ -311,3 +311,5 @@ export const getSCAccessToken = code => }); export const getPromotePosts = () => api.get(`/promoted-posts`).then(resp => resp.data); + +export const purchaseOrder = data => api.post('/purchase-order', data).then(resp => resp.data); diff --git a/src/screens/boost/container/boostContainer.js b/src/screens/boost/container/boostContainer.js index 9943ebbde..a1410e3ba 100644 --- a/src/screens/boost/container/boostContainer.js +++ b/src/screens/boost/container/boostContainer.js @@ -4,12 +4,9 @@ import { Platform, Alert } from 'react-native'; import RNIap, { purchaseErrorListener, purchaseUpdatedListener } from 'react-native-iap'; import { injectIntl } from 'react-intl'; -// import { toastNotification } from '../../../redux/actions/uiAction'; - -// Middleware - -// Constants -// import { default as ROUTES } from '../../../constants/routeNames'; +// Services +import { purchaseOrder } from '../../../providers/esteem/esteem'; +import bugsnag from '../../../config/bugsnag'; // Utilities @@ -49,17 +46,35 @@ class BoostContainer extends Component { // Component Functions _purchaseUpdatedListener = () => { + const { + currentAccount: { name }, + } = this.props; + 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); - // } - // }); + const data = { + platform: Platform.OS === 'android' ? 'play_store' : 'app_store', + product: purchase.productId, + receipt: Platform.OS === 'android' ? purchase.purchaseToken : purchase.transactionReceipt, + user: name, + }; + + purchaseOrder(data) + .then(() => { + if (Platform.OS === 'ios') { + RNIap.finishTransactionIOS(purchase.transactionId); + } else if (Platform.OS === 'android') { + RNIap.consumePurchaseAndroid(purchase.purchaseToken); + } + }) + .catch(err => + bugsnag.notify(err, report => { + report.metadata = { + data, + }; + }), + ); } }); @@ -71,19 +86,22 @@ class BoostContainer extends Component { _getItems = async () => { try { const products = await RNIap.getProducts(ITEM_SKUS); - console.log('Products', products); products.sort((a, b) => parseFloat(a.price) - parseFloat(b.price)).reverse(); this.setState({ productList: products }); } catch (err) { - console.warn(err.code, err.message); + bugsnag.notify(err); } }; _buyItem = async sku => { try { - RNIap.requestPurchase(sku); + RNIap.requestPurchase(sku, false); } catch (err) { - console.warn(err.code, err.message); + bugsnag.notify(err, report => { + report.metadata = { + sku, + }; + }); } }; diff --git a/yarn.lock b/yarn.lock index de50a16ca..dfd890476 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7670,10 +7670,10 @@ react-native-fast-image@^4.0.14: dependencies: prop-types "^15.5.10" -react-native-iap@^3.3.0: - version "3.3.0" - resolved "https://registry.yarnpkg.com/react-native-iap/-/react-native-iap-3.3.0.tgz#e8aae960072dffb926dad52cb2215e1edbb00caf" - integrity sha512-C/e/LjiYKgWJuPr1LTr3Tt5Bl4nwcb4WLbUF5QbMAXqsWKpyHd8qocqdww0bsBd3gCqtWoexFKPeKQ9ZqQVHYg== +react-native-iap@^3.3.8: + version "3.3.8" + resolved "https://registry.yarnpkg.com/react-native-iap/-/react-native-iap-3.3.8.tgz#4db012e60f4fd8473122fb48d79adb498befc7fa" + integrity sha512-AP+8VqXcAs48GUmqJPgRWcVpuCT2dWJMm6LQQPNw8B3jYV8BOTgpOQ4Q0Um92u04z2C9nAubyvjR+3tPbxSZag== dependencies: dooboolab-welcome "^1.1.0"