-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
81 lines (71 loc) · 2 KB
/
App.jsx
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
import {PlatformPay, PlatformPayButton, StripeProvider, usePlatformPay} from '@stripe/stripe-react-native';
import React from 'react'
const API_URL = 'http://10.0.2.2:3000';
function App() {
const {
isPlatformPaySupported,
confirmPlatformPayPayment,
} = usePlatformPay();
React.useEffect(() => {
(async function () {
if (!(await isPlatformPaySupported({ googlePay: {testEnv: true} }))) {
Alert.alert('Google Pay is not supported.');
return;
}
})();
}, []);
const fetchPaymentIntentClientSecret = async () => {
// Fetch payment intent created on the server, see above
const response = await fetch(`${API_URL}/create-payment-intent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
currency: 'usd',
}),
});
const { client_secret } = await response.json();
return client_secret;
};
const pay = async () => {
const clientSecret = await fetchPaymentIntentClientSecret();
const { error } = await confirmPlatformPayPayment(
clientSecret,
{
googlePay: {
testEnv: true,
merchantName: 'My merchant name',
merchantCountryCode: 'US',
currencyCode: 'USD',
billingAddressConfig: {
format: PlatformPay.BillingAddressFormat.Full,
isPhoneNumberRequired: true,
isRequired: true,
},
},
}
);
if (error) {
Alert.alert(error.code, error.message);
// Update UI to prompt user to retry payment (and possibly another payment method)
return;
}
Alert.alert('Success', 'The payment was confirmed successfully.');
};
return (
<StripeProvider
publishableKey='stripe_publishable_Key'
>
<PlatformPayButton
type={PlatformPay.ButtonType.Pay}
onPress={pay}
style={{
width: '100%',
height: 50,
}}
/>
</StripeProvider>
);
}
export default App;