-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
122 lines (112 loc) · 2.98 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
Button,
TextInput
} from "react-native";
import InAppBilling from "react-native-billing";
const defaultState = {
productDetails: null,
transactionDetails: null,
consumed: false,
error: null
};
export default class App extends Component {
state = {
productId: "vipuser",
...defaultState
};
resetState = () => {
this.setState(defaultState);
};
getProductDetails = async () => {
try {
this.resetState();
await InAppBilling.open();
const details = await InAppBilling.getProductDetails(
this.state.productId
);
await InAppBilling.close();
this.setState({ productDetails: JSON.stringify(details) });
} catch (err) {
this.setState({ error: JSON.stringify(err) });
await InAppBilling.close();
}
};
purchaseProduct = async () => {
try {
this.resetState();
await InAppBilling.open();
const details = await InAppBilling.purchase(this.state.productId);
await InAppBilling.close();
this.setState({ transactionDetails: JSON.stringify(details) });
} catch (err) {
this.setState({ error: JSON.stringify(err) });
await InAppBilling.close();
}
};
consumePurchase = async () => {
try {
this.resetState();
await InAppBilling.open();
const details = await InAppBilling.consumePurchase(this.state.productId);
await InAppBilling.close();
this.setState({ consumed: true });
} catch (err) {
this.setState({ error: JSON.stringify(err) });
await InAppBilling.close();
}
};
updateProductId = productId => {
this.setState({ productId });
};
render() {
return (
<View style={styles.container}>
<TextInput
onChangeText={this.updateProductId}
value={this.state.productId}
/>
<Button onPress={this.getProductDetails} title="Get product details" />
{this.state.productDetails && (
<Text style={styles.text}>{this.state.productDetails}</Text>
)}
<Button
onPress={this.purchaseProduct}
title={"Purchase " + this.state.productId}
/>
{this.state.transactionDetails && (
<Text style={styles.text}>{this.state.transactionDetails}</Text>
)}
<Button
onPress={this.consumePurchase}
title={"Consume " + this.state.productId}
/>
{this.state.consumed && (
<Text style={styles.text}>Purchase consumed</Text>
)}
{this.state.error && (
<Text style={[styles.text, { color: "red", marginTop: 10 }]}>
Error:{"\n"}
{this.state.error}
</Text>
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
text: {
textAlign: "center",
color: "#333333",
marginBottom: 5
}
});