Skip to content

Commit

Permalink
Use Sk2 if avaiable (#1929)
Browse files Browse the repository at this point in the history
* Use Sk2 if avaiable

* sk2 val optional

Co-authored-by: Andres Aguilar <andres.aguilar@nfl.com>
  • Loading branch information
andresesfm and Andres Aguilar authored Sep 7, 2022
1 parent 363b972 commit 3a9ae6c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
2 changes: 0 additions & 2 deletions IapExample/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react';
import {storeKit2} from 'react-native-iap';
import {NavigationContainer} from '@react-navigation/native';

import {StackNavigator} from './navigators';
storeKit2();
export const App = () => (
<NavigationContainer>
<StackNavigator />
Expand Down
18 changes: 12 additions & 6 deletions IapExample/src/components/State.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import {Box} from './Box';

interface StateProps {
connected: boolean;
storekit2?: boolean;
}

export const State = ({connected}: StateProps) => (
<Box>
<Text style={theme.L1}>State</Text>
<Text style={theme.P1}>{connected ? 'connected' : 'not connected'}</Text>
</Box>
);
export const State = ({connected, storekit2}: StateProps) => {
const stateText =
(connected ? 'connected' : 'not connected') +
(storekit2 ? ' | Storekit 2' : '');
return (
<Box>
<Text style={theme.L1}>State</Text>
<Text style={theme.P1}>{stateText}</Text>
</Box>
);
};
2 changes: 1 addition & 1 deletion IapExample/src/screens/Products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const Products = () => {

return (
<ScrollView contentContainerStyle={contentContainerStyle}>
<State connected={connected} />
<State connected={connected} storekit2={isIosStorekit2()} />

{initConnectionError && (
<Box>
Expand Down
9 changes: 7 additions & 2 deletions IapExample/src/screens/Subscriptions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, {useEffect, useState} from 'react';
import {Platform, ScrollView, StyleSheet, Text, View} from 'react-native';
import {PurchaseError, requestSubscription, useIAP} from 'react-native-iap';
import {
isIosStorekit2,
PurchaseError,
requestSubscription,
useIAP,
} from 'react-native-iap';

import {Box, Button, Heading, Row, State} from '../components';
import {constants, contentContainerStyle, errorLog} from '../utils';
Expand Down Expand Up @@ -74,7 +79,7 @@ export const Subscriptions = () => {

return (
<ScrollView contentContainerStyle={contentContainerStyle}>
<State connected={connected} />
<State connected={connected} storekit2={isIosStorekit2()} />

<Box>
<View style={styles.container}>
Expand Down
46 changes: 23 additions & 23 deletions src/iap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ export const getInstallSourceAndroid = (): InstallSourceAndroid => {

let androidNativeModule = RNIapModule;

let iosNativeModule = RNIapIos;
let iosNativeModule = RNIapIosSk2;

export const isIosStorekit2 = () => iosNativeModule === RNIapIosSk2;

export const storeKit2 = () => {
iosNativeModule = RNIapIosSk2;
};

export const setAndroidNativeModule = (
nativeModule: typeof RNIapModule,
): void => {
Expand Down Expand Up @@ -82,7 +78,11 @@ const checkNativeIOSAvailable = (): void => {
export const getIosModule = (): typeof RNIapIos => {
checkNativeIOSAvailable();

return iosNativeModule ? iosNativeModule : RNIapIos ? RNIapIos : RNIapIosSk2;
return iosNativeModule
? iosNativeModule
: RNIapIosSk2
? RNIapIosSk2
: RNIapIos;
};

export const getNativeModule = ():
Expand Down Expand Up @@ -452,6 +452,23 @@ export const acknowledgePurchaseAndroid = ({
return getAndroidModule().acknowledgePurchase(token, developerPayload);
};

/**
* Deep link to subscriptions screen on Android. No-op on iOS.
* @param {string} sku The product's SKU (on Android)
* @returns {Promise<void>}
*/
export const deepLinkToSubscriptionsAndroid = async ({
sku,
}: {
sku: Sku;
}): Promise<void> => {
checkNativeAndroidAvailable();

return Linking.openURL(
`https://play.google.com/store/account/subscriptions?package=${await RNIapModule.getPackageName()}&sku=${sku}`,
);
};

/**
* Should Add Store Payment (iOS only)
* Indicates the the App Store purchase should continue from the app instead of the App Store.
Expand Down Expand Up @@ -551,23 +568,6 @@ export const validateReceiptIos = async ({
return await enhancedFetch<Apple.ReceiptValidationResponse>(url);
};

/**
* Deep link to subscriptions screen on Android. No-op on iOS.
* @param {string} sku The product's SKU (on Android)
* @returns {Promise<void>}
*/
export const deepLinkToSubscriptionsAndroid = async ({
sku,
}: {
sku: Sku;
}): Promise<void> => {
checkNativeAndroidAvailable();

return Linking.openURL(
`https://play.google.com/store/account/subscriptions?package=${await RNIapModule.getPackageName()}&sku=${sku}`,
);
};

/**
* Validate receipt for Android. NOTE: This method is here for debugging purposes only. Including
* your access token in the binary you ship to users is potentially dangerous.
Expand Down

0 comments on commit 3a9ae6c

Please sign in to comment.