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

Version 3 Implementation #510

Merged
merged 8 commits into from
Jun 9, 2019
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Changelogs
- **[3.0.+]**
+ Major migration and breaking changes done in [#510](https://github.com/dooboolab/react-native-iap/pull/510)
- **[2.5.+]**
+ Fix flow type [#482](https://github.com/dooboolab/react-native-iap/pull/482)
+ Ugrade gradle to `3.2.1` [#488](https://github.com/dooboolab/react-native-iap/pull/488)
Expand Down
110 changes: 75 additions & 35 deletions IapExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
StyleSheet,
} from 'react-native';
import NativeButton from 'apsl-react-native-button';
import * as RNIap from 'react-native-iap';
import RNIap, {
Product,
ProductPurchase,
purchaseUpdatedListener,
} from 'react-native-iap';

// App Bundle > com.dooboolab.test

Expand All @@ -17,7 +21,8 @@ const itemSkus = Platform.select({
'com.cooni.point1000', 'com.cooni.point5000', // dooboolab
],
android: [
'android.test.purchased',
'android.test.purchased', 'android.test.canceled', 'android.test.refunded', 'android.test.item_unavailable',
// 'point_1000', '5000_point', // dooboolab
],
});

Expand All @@ -30,6 +35,8 @@ const itemSubs = Platform.select({
],
});

let purchaseUpdateSubscription;

class Page extends Component {
constructor(props) {
super(props);
Expand All @@ -44,22 +51,32 @@ class Page extends Component {
async componentDidMount() {
try {
const result = await RNIap.initConnection();
await RNIap.consumeAllItems();
await RNIap.consumeAllItemsAndroid();
console.log('result', result);
} catch (err) {
console.warn(err.code, err.message);
}
}

goToNext = () => {
this.props.navigation.navigate('Second', {
receipt: this.state.receipt,
purchaseUpdateSubscription = purchaseUpdatedListener((purchase: ProductPurchase) => {
console.log('purchaseUpdatedListener', purchase);
this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext());
});
}

componentWillMount() {
if (purchaseUpdateSubscription) {
purchaseUpdateSubscription.remove();
purchaseUpdateSubscription = null;
}
}

goNext = () => {
Alert.alert('Receipt', this.state.receipt);
}

getItems = async() => {
try {
const products = await RNIap.getProducts(itemSkus);
const products: Product[] = await RNIap.getProducts(itemSkus);
// const products = await RNIap.getSubscriptions(itemSkus);
console.log('Products', products);
this.setState({ productList: products });
Expand All @@ -70,26 +87,63 @@ class Page extends Component {

getSubscriptions = async() => {
try {
const products = await RNIap.getSubscriptions(itemSubs);
const products: Product[] = await RNIap.getSubscriptions(itemSubs);
console.log('Products', products);
this.setState({ productList: products });
} catch (err) {
console.warn(err.code, err.message);
}
}

getAvailablePurchases = async() => {
try {
console.info('Get available purchases (non-consumable or unconsumed consumable)');
const purchases = await RNIap.getAvailablePurchases();
console.info('Available purchases :: ', purchases);
if (purchases && purchases.length > 0) {
this.setState({
availableItemsMessage: `Got ${purchases.length} items.`,
receipt: purchases[0].transactionReceipt,
});
}
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
}
}

// Version 3 apis
requestPurchase = async(sku) => {
try {
RNIap.requestPurchase(sku);
} catch (err) {
console.warn(err.code, err.message);
}
}

requestSubscription = async(sku) => {
try {
RNIap.requestSubscription(sku);
} catch (err) {
Alert.alert(err.message);
}
}

// Deprecated apis
buyItem = async(sku) => {
console.info('buyItem: ' + 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);
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
const purchase: ProductPurchase = 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.goToNext());
this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext());
subscription.remove();
});
}
Expand All @@ -100,24 +154,7 @@ class Page extends Component {
console.log('buySubscribeItem: ' + sku);
const purchase = await RNIap.buySubscription(sku);
console.info(purchase);
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
}
}

getAvailablePurchases = async() => {
try {
console.info('Get available purchases (non-consumable or unconsumed consumable)');
const purchases = await RNIap.getAvailablePurchases();
console.info('Available purchases :: ', purchases);
if (purchases && purchases.length > 0) {
this.setState({
availableItemsMessage: `Got ${purchases.length} items.`,
receipt: purchases[0].transactionReceipt,
});
}
this.setState({ receipt: purchase.transactionReceipt }, () => this.goNext());
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
Expand All @@ -131,7 +168,7 @@ class Page extends Component {
return (
<View style={ styles.container }>
<View style={ styles.header }>
<Text style={ styles.headerTxt} >react-native-iap</Text>
<Text style={ styles.headerTxt} >react-native-iap V3</Text>
</View>
<View style={ styles.content }>
<ScrollView
Expand Down Expand Up @@ -170,11 +207,14 @@ class Page extends Component {
paddingHorizontal: 20,
}} >{JSON.stringify(product)}</Text>
<NativeButton
onPress={() => this.buyItem(product.productId)}
onPress={() => this.requestPurchase(product.productId)}
// onPress={() => this.requestSubscription(product.productId)}
// onPress={() => this.buyItem(product.productId)}
// onPress={() => this.buySubscribeItem(product.productId)}
activeOpacity={0.5}
style={styles.btn}
textStyle={styles.txt}
>Buy Above Product</NativeButton>
>Request purchase for above product</NativeButton>
</View>
);
})
Expand Down Expand Up @@ -210,7 +250,7 @@ const styles = StyleSheet.create({
alignItems: 'center',
},
headerTxt: {
fontSize: 32,
fontSize: 26,
color: 'green',
},
content: {
Expand Down
10 changes: 10 additions & 0 deletions IapExample/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ android {
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
v2SigningEnabled true
}
}
splits {
abi {
reset()
Expand All @@ -120,6 +129,7 @@ android {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
}
}
// applicationVariants are e.g. debug, release
Expand Down
Binary file added IapExample/android/app/dooboo_test.jks
Binary file not shown.
Binary file added IapExample/android/app/release/app.aab
Binary file not shown.
5 changes: 5 additions & 0 deletions IapExample/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

MYAPP_RELEASE_STORE_FILE=dooboo_test.jks
MYAPP_RELEASE_KEY_ALIAS=key0
MYAPP_RELEASE_STORE_PASSWORD=dooboolab
MYAPP_RELEASE_KEY_PASSWORD=dooboolab
Loading