import React, { useEffect } from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
SafeAreaView
} from 'react-native';
import {
TonderProvider,
useTonder,
SDKType,
CardHolderInput,
CardNumberInput,
CardExpirationMonthInput,
CardExpirationYearInput,
CardCVVInput
} from '@tonder.io/rn-sdk';
function PaymentForm() {
const { create, payment } = useTonder<SDKType.LITE>();
useEffect(() => {
const initialize = async () => {
const secureToken = await getSecureToken();
const paymentData = {
customer: {
firstName: "Juan",
email: "juan@example.com"
},
cart: {
total: 399,
items: [
{
name: 'Product',
amount_total: 399,
description: 'Product description',
price_unit: 399,
quantity: 1,
}
]
},
currency: "mxn"
};
await create({
secureToken,
paymentData,
callbacks: {
onFinishPayment: (response) => {
console.log('Payment completed:', response);
}
}
});
};
initialize();
}, []);
const handlePayment = async () => {
const { response, error } = await payment();
if (error) {
console.error('Payment error:', error);
return;
}
console.log('Payment success:', response);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.formContainer}>
<Text style={styles.title}>Payment Information</Text>
<View style={styles.fieldContainer}>
<Text style={styles.label}>Cardholder Name</Text>
<CardHolderInput style={styles.input} />
</View>
<View style={styles.fieldContainer}>
<Text style={styles.label}>Card Number</Text>
<CardNumberInput style={styles.input} />
</View>
<View style={styles.row}>
<View style={styles.halfField}>
<Text style={styles.label}>Expiry Month</Text>
<CardExpirationMonthInput style={styles.input} />
</View>
<View style={styles.halfField}>
<Text style={styles.label}>Expiry Year</Text>
<CardExpirationYearInput style={styles.input} />
</View>
</View>
<View style={styles.fieldContainer}>
<Text style={styles.label}>CVV</Text>
<CardCVVInput style={styles.smallInput} />
</View>
<TouchableOpacity style={styles.button} onPress={handlePayment}>
<Text style={styles.buttonText}>Pay $399.00</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5F5F5',
},
formContainer: {
margin: 16,
padding: 20,
backgroundColor: '#FFFFFF',
borderRadius: 12,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 8,
elevation: 3,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#1A1A1A',
marginBottom: 24,
},
fieldContainer: {
marginBottom: 16,
},
label: {
fontSize: 14,
fontWeight: '600',
color: '#4A4A4A',
marginBottom: 8,
},
input: {
borderWidth: 1,
borderColor: '#D1D1D1',
borderRadius: 8,
padding: 14,
fontSize: 16,
backgroundColor: '#FAFAFA',
color: '#1A1A1A',
},
row: {
flexDirection: 'row',
gap: 12,
marginBottom: 16,
},
halfField: {
flex: 1,
},
smallInput: {
borderWidth: 1,
borderColor: '#D1D1D1',
borderRadius: 8,
padding: 14,
fontSize: 16,
backgroundColor: '#FAFAFA',
width: '50%',
},
button: {
backgroundColor: '#007AFF',
paddingVertical: 16,
borderRadius: 10,
alignItems: 'center',
marginTop: 8,
},
buttonText: {
color: '#FFFFFF',
fontSize: 18,
fontWeight: '600',
},
});
// Wrap with TonderProvider
export default function App() {
return (
<TonderProvider
config={{
type: SDKType.LITE,
mode: 'development',
apiKey: 'YOUR_API_KEY',
}}
>
<PaymentForm />
</TonderProvider>
);
}