forked from charliecruzan-stripe/react-native-video-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElements.tsx
105 lines (96 loc) · 2.51 KB
/
Elements.tsx
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
import React from 'react';
import {Button, Image, Text, View, Alert, StyleSheet} from 'react-native';
import {
StripeProvider,
useConfirmPayment,
CardField,
CardForm,
} from '@stripe/stripe-react-native';
import {API_URL, MERCHANT_ID} from './Constants';
const Elements = ({
goBack,
publishableKey,
}: {
goBack: () => void;
publishableKey: string;
}) => {
const [isReady, setIsReady] = React.useState(false);
const {confirmPayment, loading} = useConfirmPayment();
const fetchPaymentIntentClientSecret = async () => {
const response = await fetch(`${API_URL}/create-payment-intent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
currency: 'usd',
}),
});
const {clientSecret} = await response.json();
return clientSecret;
};
const buy = async () => {
const clientSecret = await fetchPaymentIntentClientSecret();
const {error, paymentIntent} = await confirmPayment(clientSecret, {
paymentMethodType: 'Card',
});
if (error) {
Alert.alert(`Error code: ${error.code}`, error.localizedMessage);
} else if (paymentIntent) {
Alert.alert(
'Success',
`The payment was confirmed successfully! currency: ${paymentIntent.currency}`,
);
}
};
return (
<View style={styles.container}>
<StripeProvider
publishableKey={publishableKey}
merchantIdentifier={MERCHANT_ID}>
<Text>1 kg of Sweet Potatoes</Text>
<Image source={require('./potato.jpeg')} style={styles.image} />
<CardField
style={styles.cardField}
placeholders={{number: '4242 for testing'}}
onCardChange={details => {
if (details.complete) {
setIsReady(true);
}
}}
/>
{/* <CardForm style={styles.cardForm} /> */}
<View style={styles.buttons}>
<Button title={'Go back'} onPress={goBack} />
<Button title={'Buy'} onPress={buy} disabled={loading || !isReady} />
</View>
</StripeProvider>
</View>
);
};
export default Elements;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
image: {
height: 250,
width: 250,
},
cardField: {
height: 35,
width: '90%',
marginBottom: 20,
},
cardForm: {
height: 270,
width: '80%',
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '50%',
},
});