|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Alert, StyleSheet, TextInput } from 'react-native'; |
| 3 | +import { |
| 4 | + useConfirmPayment, |
| 5 | + useConfirmSetupIntent, |
| 6 | +} from '@stripe/stripe-react-native'; |
| 7 | +import Button from '../components/Button'; |
| 8 | +import PaymentScreen from '../components/PaymentScreen'; |
| 9 | +import { API_URL } from '../Config'; |
| 10 | +import { colors } from '../colors'; |
| 11 | + |
| 12 | +export default function BilliePaymentScreen() { |
| 13 | + const [email, setEmail] = useState(''); |
| 14 | + const { confirmPayment, loading } = useConfirmPayment(); |
| 15 | + const { confirmSetupIntent, loading: setupLoading } = useConfirmSetupIntent(); |
| 16 | + |
| 17 | + const fetchClientSecret = async (intentType: 'setup' | 'payment') => { |
| 18 | + const response = await fetch(`${API_URL}/create-${intentType}-intent`, { |
| 19 | + method: 'POST', |
| 20 | + headers: { |
| 21 | + 'Content-Type': 'application/json', |
| 22 | + }, |
| 23 | + body: JSON.stringify({ |
| 24 | + email, |
| 25 | + currency: 'eur', |
| 26 | + items: ['id-1'], |
| 27 | + payment_method_types: ['billie'], |
| 28 | + }), |
| 29 | + }); |
| 30 | + const { clientSecret, error } = await response.json(); |
| 31 | + |
| 32 | + return { clientSecret, error }; |
| 33 | + }; |
| 34 | + |
| 35 | + const handlePayPress = async () => { |
| 36 | + const { clientSecret, error: clientSecretError } = |
| 37 | + await fetchClientSecret('payment'); |
| 38 | + |
| 39 | + if (clientSecretError) { |
| 40 | + Alert.alert(`Error`, clientSecretError); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const { error, paymentIntent } = await confirmPayment(clientSecret, { |
| 45 | + paymentMethodType: 'Billie', |
| 46 | + paymentMethodData: { |
| 47 | + billingDetails: { |
| 48 | + email: 'stripe@test.com', |
| 49 | + address: { |
| 50 | + country: 'US', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + if (error) { |
| 57 | + Alert.alert(`Error code: ${error.code}`, error.message); |
| 58 | + console.log('Payment confirmation error', error.message); |
| 59 | + } else if (paymentIntent) { |
| 60 | + Alert.alert( |
| 61 | + 'Success', |
| 62 | + `The payment was confirmed successfully! currency: ${paymentIntent.currency}` |
| 63 | + ); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const handleSetupPress = async () => { |
| 68 | + const { clientSecret, error: clientSecretError } = |
| 69 | + await fetchClientSecret('setup'); |
| 70 | + |
| 71 | + if (clientSecretError) { |
| 72 | + Alert.alert(`Error`, clientSecretError); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const { error, setupIntent } = await confirmSetupIntent(clientSecret, { |
| 77 | + paymentMethodType: 'Billie', |
| 78 | + paymentMethodData: { |
| 79 | + mandateData: { |
| 80 | + customerAcceptance: { |
| 81 | + online: { |
| 82 | + ipAddress: '1.1.1.1', |
| 83 | + userAgent: 'my-agent', |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + shippingDetails: { |
| 88 | + address: { |
| 89 | + city: 'Houston', |
| 90 | + country: 'US', |
| 91 | + line1: '1459 Circle Drive', |
| 92 | + state: 'Texas', |
| 93 | + postalCode: '77063', |
| 94 | + }, |
| 95 | + email: 'myemail@s.com', |
| 96 | + name: 'John Doe', |
| 97 | + }, |
| 98 | + billingDetails: { |
| 99 | + email: 'stripe@test.com', |
| 100 | + address: { |
| 101 | + country: 'US', |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + if (error) { |
| 108 | + Alert.alert(`Error code: ${error.code}`, error.message); |
| 109 | + console.log('Setup confirmation error', error.message); |
| 110 | + } else if (setupIntent) { |
| 111 | + Alert.alert('Success', `Status: ${setupIntent.status}`); |
| 112 | + console.log('Success from promise', setupIntent); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + return ( |
| 117 | + <PaymentScreen> |
| 118 | + <TextInput |
| 119 | + autoCapitalize="none" |
| 120 | + placeholder="E-mail" |
| 121 | + keyboardType="email-address" |
| 122 | + onChange={(value) => setEmail(value.nativeEvent.text)} |
| 123 | + style={styles.input} |
| 124 | + /> |
| 125 | + |
| 126 | + <Button |
| 127 | + variant="primary" |
| 128 | + onPress={handlePayPress} |
| 129 | + title="Pay" |
| 130 | + accessibilityLabel="Pay" |
| 131 | + loading={loading} |
| 132 | + /> |
| 133 | + |
| 134 | + <Button |
| 135 | + variant="primary" |
| 136 | + onPress={handleSetupPress} |
| 137 | + title="Setup" |
| 138 | + accessibilityLabel="Setup" |
| 139 | + loading={setupLoading} |
| 140 | + /> |
| 141 | + </PaymentScreen> |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +const styles = StyleSheet.create({ |
| 146 | + cardField: { |
| 147 | + width: '100%', |
| 148 | + height: 50, |
| 149 | + marginVertical: 30, |
| 150 | + }, |
| 151 | + row: { |
| 152 | + flexDirection: 'row', |
| 153 | + alignItems: 'center', |
| 154 | + marginBottom: 20, |
| 155 | + }, |
| 156 | + text: { |
| 157 | + marginLeft: 12, |
| 158 | + }, |
| 159 | + input: { |
| 160 | + height: 44, |
| 161 | + borderBottomColor: colors.slate, |
| 162 | + borderBottomWidth: 1.5, |
| 163 | + marginBottom: 20, |
| 164 | + }, |
| 165 | +}); |
0 commit comments