forked from anchetaWern/RNPay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
108 lines (86 loc) · 2.35 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
import React, { Component } from 'react';
import { View, Alert } from 'react-native';
import stripe from 'tipsi-stripe';
import Config from 'react-native-config';
stripe.setOptions({
publishableKey: Config.STRIPE_PUBLISHABLE_KEY,
androidPayMode: 'test',
});
import pay from './src/helpers/pay';
import ItemBox from './src/components/ItemBox';
export default class App extends Component {
constructor(props) {
super(props);
this.access_token = 'AN ACCESS TOKEN FROM YOUR DB';
this.currency_code = 'USD';
this.item = {
title: 'Loop 720',
price: 1,
image: require('./src/images/loop720.jpg')
};
}
state = {
isPaying: false,
canPay: false
}
async componentDidMount() {
const device_supported = await stripe.deviceSupportsAndroidPay();
const can_make_android_pay_payments = await stripe.canMakeAndroidPayPayments();
if(device_supported && can_make_android_pay_payments){
this.setState({
canPay: true
});
}
}
payItem = async () => {
this.setState({
isPaying: true
});
try {
const token = await stripe.paymentRequestWithNativePay({
total_price: this.item.price.toFixed(2),
currency_code: this.currency_code,
shipping_address_required: true,
line_items: [
{
currency_code: this.currency_code,
description: this.item.title,
total_price: this.item.price.toFixed(2),
unit_price: this.item.price.toFixed(2),
quantity: '1',
}
],
});
const response = await pay(this.item.price, this.item.title, this.access_token, token.tokenId);
if (response) {
Alert.alert("Done!", "Payment successful");
} else {
Alert.alert("Error occurred", "Something went wrong while processing payment. Please try again.");
}
} catch (e) {
console.log("error getting token: ", e);
}
this.setState({
isPaying: false
});
}
render() {
return (
<View style={styles.container}>
<ItemBox
{...this.item}
canPay={this.state.canPay}
isPaying={this.state.isPaying}
payAction={this.payItem} />
</View>
);
}
}
const styles = {
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFF',
}
};