-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPaymentSheet.tsx
132 lines (120 loc) · 3.24 KB
/
PaymentSheet.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {StripeProvider, usePaymentSheet} from '@stripe/stripe-react-native';
import React, {useEffect, useState} from 'react';
import {Button, Image, Text, View, Alert, StyleSheet} from 'react-native';
import {MERCHANT_ID, API_URL} from './Constants';
const PaymentSheet = ({
goBack,
publishableKey,
}: {
goBack: () => void;
publishableKey: string;
}) => {
const [ready, setReady] = useState(false);
const {
initPaymentSheet,
presentPaymentSheet,
loading,
resetPaymentSheetCustomer,
} = usePaymentSheet();
useEffect(() => {
initialisePaymentSheet();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const initialisePaymentSheet = async () => {
const {paymentIntent} = await fetchPaymentSheetParams();
const {error} = await initPaymentSheet({
appearance: {
colors: {
primary: '#e06c75',
background: '#282c34',
componentBackground: '#abb2bf',
componentDivider: '#e5c07b',
primaryText: '#61afef',
secondaryText: '#c678dd',
componentText: '#282c34',
icon: '#e06c75',
placeholderText: '#ffffff',
},
shapes: {
borderRadius: 25,
},
},
paymentIntentClientSecret: paymentIntent,
merchantDisplayName: 'Example Inc.',
applePay: {
merchantCountryCode: 'US',
},
googlePay: {
merchantCountryCode: 'US',
testEnv: true,
currencyCode: 'usd',
},
allowsDelayedPaymentMethods: true,
returnURL: 'stripe-example://stripe-redirect',
});
if (error) {
Alert.alert(`Error code: ${error.code}`, error.message);
} else {
setReady(true);
}
};
const fetchPaymentSheetParams = async () => {
const response = await fetch(`${API_URL}/payment-sheet`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
const {paymentIntent, ephemeralKey, customer} = await response.json();
return {
paymentIntent,
ephemeralKey,
customer,
};
};
async function buy() {
const {error} = await presentPaymentSheet();
if (error) {
Alert.alert(`Error code: ${error.code}`, error.message);
} else {
Alert.alert('Success', 'The payment was confirmed successfully');
setReady(false);
}
}
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} />
<View style={styles.buttons}>
<Button title={'Go back'} onPress={goBack} />
<Button title={'Buy'} onPress={buy} disabled={loading || !ready} />
</View>
<Button
title={'Logout'}
onPress={async () => {
await resetPaymentSheetCustomer();
}}
/>
</StripeProvider>
</View>
);
};
export default PaymentSheet;
const styles = StyleSheet.create({
container: {
alignItems: 'center',
marginTop: 100,
},
image: {
height: 250,
width: 250,
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '50%',
},
});