Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listion to purchase events on startup of the app #198

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ApplicationRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { HEADER_HEIGHT } from './constants/Layout';
import Entypo from 'react-native-vector-icons/Entypo';
import { t } from 'i18next';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import useIAPConnection from './hooks/useIAPConnection';
const Stack = createNativeStackNavigator();

/*import { LogBox } from 'react-native'; // enabled for recording demos
Expand All @@ -40,6 +41,7 @@ export default function App() {
setIsPalettesLoaded(true);
})();
}, []);
useIAPConnection();

const spinner = (
<View style={{ flex: 1, marginTop: '20%' }}>
Expand Down
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
ndkVersion = "21.4.7075529"
androidXAnnotation = "1.1.0"
androidXBrowser = "1.0.0"
supportLibVersion = "28.0.0"
}
repositories {
google()
Expand Down
File renamed without changes.
69 changes: 69 additions & 0 deletions hooks/useIAPConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useEffect } from 'react';
import {
initConnection,
purchaseErrorListener,
purchaseUpdatedListener,
type SubscriptionPurchase,
type ProductPurchase,
type PurchaseError,
flushFailedPurchasesCachedAsPendingAndroid,
finishTransaction,
} from 'react-native-iap';
import { notifyMessage } from '../libs/Helpers';
// https://react-native-iap.dooboolab.com/docs/guides/purchases
// TODO: We need to implement this properly with server side validation.
const useIAPConnection = () => {
useEffect(() => {
let purchaseUpdateSubscription = null;
let purchaseErrorSubscription = null;

initConnection().then(() => {
flushFailedPurchasesCachedAsPendingAndroid()
.catch(() => {
// exception can happen here if:
// - there are pending purchases that are still pending (we can't consume a pending purchase)
// in any case, you might not want to do anything special with the error
})
.then(() => {
purchaseUpdateSubscription = purchaseUpdatedListener(
async (purchase: SubscriptionPurchase | ProductPurchase) => {
console.log('purchaseUpdatedListener', purchase);
const receipt = purchase.transactionReceipt;
if (receipt) {

// Tell the store that you have delivered what has been paid for.
// Failure to do this will result in the purchase being refunded on Android and
// the purchase event will reappear on every relaunch of the app until you succeed
// in doing the below. It will also be impossible for the user to purchase consumables
// again until you do this.

// If consumable (can be purchased again)
//await finishTransaction({purchase, isConsumable: true});
// If not consumable

await finishTransaction({purchase, isConsumable: false});
notifyMessage('Purchase successful');
}
},
);
purchaseErrorSubscription = purchaseErrorListener(
(error: PurchaseError) => {
console.warn('purchaseErrorListener', error);
},
);
});
});

return () => {
if (purchaseUpdateSubscription) {
purchaseUpdateSubscription.remove();
}

if (purchaseErrorSubscription) {
purchaseErrorSubscription.remove();
}
};
}, []);
};

export default useIAPConnection;
1 change: 0 additions & 1 deletion libs/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const purchase = async function (setPurchase, productSKU) {
andDangerouslyFinishTransactionAutomatically: true
});
await setPurchase(details);
RNIap.finishTransaction(details, false);
logEvent('purchase_successful');
notifyMessage('Congrats, You are now a pro user!');
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion screens/ChatSessionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { logEvent } from '../libs/Helpers';
import { createChatSession, followUpChatSession, getChatSession } from '../network/chat_session';
import ChatCard from '../components/ChatCard';
import LoginScreen from './LoginScreen';
import useUserData from '../Hooks/getUserData';
import useUserData from '../hooks/getUserData';

// eslint-disable-next-line no-undef
const bgImage = require('../assets/images/colorful_background.jpg');
Expand Down
Loading