import { View, Text, Image, TouchableOpacity, Alert, ScrollView } from 'react-native'
import React, { useState, useEffect } from 'react'
import TabBar from '../themes/TabBar'
import { ChevronLeftIcon, CreditCardIcon, CheckCircleIcon, TrashIcon } from 'react-native-heroicons/outline'
import { TextInput } from 'react-native'
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
import { useNavigation } from '@react-navigation/native'
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Linking } from 'react-native';
// Import local assets
const esewaLogo = require('../../assets/images/esewa.jpg');
const khaltiLogo = require('../../assets/images/khalti.jpg');
const imePayLogo = require('../../assets/images/imepay.png');
export default function PaymentMethodScreen() {
const navigator = useNavigation()
const [cardNumber, setCardNumber] = useState('')
const [cardName, setCardName] = useState('')
const [expiryDate, setExpiryDate] = useState('')
const [cvv, setCvv] = useState('')
const [paypalEmail, setPaypalEmail] = useState('')
const [paypalPassword, setPaypalPassword] = useState('')
const [esewaPhone, setEsewaPhone] = useState('')
const [esewaPin, setEsewaPin] = useState('')
const [khaltiPhone, setKhaltiPhone] = useState('')
const [khaltiPin, setKhaltiPin] = useState('')
const [imePhone, setImePhone] = useState('')
const [imePin, setImePin] = useState('')
const [showCardDetails, setShowCardDetails] = useState(false)
const [showPaypalDetails, setShowPaypalDetails] = useState(false)
const [showApplePayDetails, setShowApplePayDetails] = useState(false)
const [showEsewaDetails, setShowEsewaDetails] = useState(false)
const [showKhaltiDetails, setShowKhaltiDetails] = useState(false)
const [showImeDetails, setShowImeDetails] = useState(false)
const [linkedMethods, setLinkedMethods] = useState({
card: false,
paypal: false,
applePay: false,
esewa: false,
khalti: false,
ime: false
})
// Load saved payment methods on component mount
useEffect(() => {
loadSavedPaymentMethods();
}, []);
const loadSavedPaymentMethods = async () => {
try {
const savedMethods = await AsyncStorage.getItem('linkedPaymentMethods');
if (savedMethods) {
setLinkedMethods(JSON.parse(savedMethods));
}
} catch (error) {
console.error('Error loading saved payment methods:', error);
}
};
const saveLinkedMethods = async (methods) => {
try {
await AsyncStorage.setItem('linkedPaymentMethods', JSON.stringify(methods));
} catch (error) {
console.error('Error saving payment methods:', error);
}
};
const handleUnlink = (method) => {
Alert.alert(
'Unlink Account',
'Are you sure you want to unlink this account?',
[
{
text: 'Cancel',
style: 'cancel'
},
{
text: 'Unlink',
style: 'destructive',
onPress: () => {
const updatedMethods = { ...linkedMethods, [method]: false };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
]
)
}
const handleCardLink = () => {
if (!cardNumber.trim() || !cardName.trim() || !expiryDate.trim() || !cvv.trim()) {
Alert.alert('Error', 'Please fill in all card details')
return
}
// Here you would typically validate the card details
// For now, we'll just show a success message
Alert.alert('Success', 'Card linked successfully', [
{
text: 'OK',
onPress: () => {
setShowCardDetails(false)
setCardNumber('')
setCardName('')
setExpiryDate('')
setCvv('')
const updatedMethods = { ...linkedMethods, card: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const handlePaypalLink = () => {
if (!paypalEmail.trim() || !paypalPassword.trim()) {
Alert.alert('Error', 'Please fill in all PayPal details')
return
}
// Here you would typically validate the PayPal credentials
Alert.alert('Success', 'PayPal account linked successfully', [
{
text: 'OK',
onPress: () => {
setShowPaypalDetails(false)
setPaypalEmail('')
setPaypalPassword('')
const updatedMethods = { ...linkedMethods, paypal: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const handleApplePayLink = () => {
// Apple Pay typically uses the device's built-in authentication
Alert.alert('Success', 'Apple Pay account linked successfully', [
{
text: 'OK',
onPress: () => {
setShowApplePayDetails(false)
const updatedMethods = { ...linkedMethods, applePay: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const handleEsewaLink = () => {
if (!esewaPhone.trim() || !esewaPin.trim()) {
Alert.alert('Error', 'Please fill in all eSewa details')
return
}
// Here you would typically validate the eSewa credentials
Alert.alert('Success', 'eSewa account linked successfully', [
{
text: 'OK',
onPress: () => {
setShowEsewaDetails(false)
setEsewaPhone('')
setEsewaPin('')
const updatedMethods = { ...linkedMethods, esewa: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const handleKhaltiLink = () => {
if (!khaltiPhone.trim() || !khaltiPin.trim()) {
Alert.alert('Error', 'Please fill in all Khalti details')
return
}
// Here you would typically validate the Khalti credentials
Alert.alert('Success', 'Khalti account linked successfully', [
{
text: 'OK',
onPress: () => {
setShowKhaltiDetails(false)
setKhaltiPhone('')
setKhaltiPin('')
const updatedMethods = { ...linkedMethods, khalti: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const handleImeLink = () => {
if (!imePhone.trim() || !imePin.trim()) {
Alert.alert('Error', 'Please fill in all IME Pay details')
return
}
// Here you would typically validate the IME Pay credentials
Alert.alert('Success', 'IME Pay account linked successfully', [
{
text: 'OK',
onPress: () => {
setShowImeDetails(false)
setImePhone('')
setImePin('')
const updatedMethods = { ...linkedMethods, ime: true };
setLinkedMethods(updatedMethods);
saveLinkedMethods(updatedMethods);
}
}
])
}
const openEsewaApp = async () => {
try {
const esewaUrl = 'esewa://';
const supported = await Linking.canOpenURL(esewaUrl);
if (supported) {
await Linking.openURL(esewaUrl);
} else {
Alert.alert(
'eSewa App Not Found',
'Please install eSewa app to continue',
[
{
text: 'Install',
onPress: () => Linking.openURL('https://play.google.com/store/apps/details?id=com.f1soft.esewa&hl=en')
},
{
text: 'Cancel',
style: 'cancel'
}
]
);
}
} catch (error) {
Alert.alert('Error', 'Could not open eSewa app');
}
};
const openKhaltiApp = async () => {
try {
const khaltiUrl = 'khalti://';
const supported = await Linking.canOpenURL(khaltiUrl);
if (supported) {
await Linking.openURL(khaltiUrl);
} else {
Alert.alert(
'Khalti App Not Found',
'Please install Khalti app to continue',
[
{
text: 'Install',
onPress: () => Linking.openURL('https://play.google.com/store/apps/details?id=com.khalti&hl=en')
},
{
text: 'Cancel',
style: 'cancel'
}
]
);
}
} catch (error) {
Alert.alert('Error', 'Could not open Khalti app');
}
};
const openImeApp = async () => {
try {
const imeUrl = 'imepay://';
const supported = await Linking.canOpenURL(imeUrl);
if (supported) {
await Linking.openURL(imeUrl);
} else {
Alert.alert(
'IME Pay App Not Found',
'Please install IME Pay app to continue',
[
{
text: 'Install',
onPress: () => Linking.openURL('https://play.google.com/store/apps/details?id=com.swifttechnology.imepay&hl=en')
},
{
text: 'Cancel',
style: 'cancel'
}
]
);
}
} catch (error) {
Alert.alert('Error', 'Could not open IME Pay app');
}
};
const renderLinkStatus = (isLinked) => {
if (isLinked) {
return (
Linked
)
}
return Link
}
const renderLinkedCardDetails = () => (
Card Details
handleUnlink('card')}>
Card ending in 1234
Expires: 12/25
John Doe
)
const renderLinkedPaypalDetails = () => (
PayPal Account
handleUnlink('paypal')}>
user@example.com
)
const renderLinkedApplePayDetails = () => (
Apple Pay
handleUnlink('applePay')}>
Using device's Apple Pay
)
const renderLinkedEsewaDetails = () => (
eSewa Account
handleUnlink('esewa')}>
Phone: 98XXXXXXXX
Open eSewa App
)
const renderLinkedKhaltiDetails = () => (
Khalti Account
handleUnlink('khalti')}>
Phone: 98XXXXXXXX
Open Khalti App
)
const renderLinkedImeDetails = () => (
IME Pay Account
handleUnlink('ime')}>
Phone: 98XXXXXXXX
Open IME Pay App
)
return (
}
titleStyle={{
color: "black",
fontWeight: "bold",
fontSize: 20
}}
prefixStyle={{
backgroundColor: "transparent"
}}
suffixStyle={{
backgroundColor: "white",
borderWidth: 1,
borderColor: "black"
}}
containerStyle=""
suffixAction={() => navigator.goBack()}
/>
Credit & Debit Card
setShowCardDetails(!showCardDetails)}
className={`mx-4 flex-row items-center mt-2 justify-between p-2 border border-gray-300 rounded-2xl`}
>
Add New Card
{renderLinkStatus(linkedMethods.card)}
{showCardDetails && !linkedMethods.card && (
Link Card
)}
{showCardDetails && linkedMethods.card && renderLinkedCardDetails()}
More Payment Options
setShowPaypalDetails(!showPaypalDetails)}
className={`flex-row items-center mt-2 justify-between p-4 border-b border-gray-300 gap-4`}
>
Paypal
{renderLinkStatus(linkedMethods.paypal)}
{showPaypalDetails && !linkedMethods.paypal && (
Link PayPal
)}
{showPaypalDetails && linkedMethods.paypal && renderLinkedPaypalDetails()}
setShowApplePayDetails(!showApplePayDetails)}
className={`flex-row items-center justify-between p-4 border-b border-gray-300 gap-4`}
>
Apple Pay
{renderLinkStatus(linkedMethods.applePay)}
{showApplePayDetails && !linkedMethods.applePay && (
Apple Pay will use your device's built-in authentication to securely link your account.
Link Apple Pay
)}
{showApplePayDetails && linkedMethods.applePay && renderLinkedApplePayDetails()}
setShowEsewaDetails(!showEsewaDetails)}
className={`flex-row items-center justify-between p-4 gap-4`}
>
eSewa
{renderLinkStatus(linkedMethods.esewa)}
{showEsewaDetails && !linkedMethods.esewa && (
Link eSewa
Open eSewa App
)}
{showEsewaDetails && linkedMethods.esewa && renderLinkedEsewaDetails()}
setShowKhaltiDetails(!showKhaltiDetails)}
className={`flex-row items-center justify-between p-4 border-b border-gray-300 gap-4`}
>
Khalti
{renderLinkStatus(linkedMethods.khalti)}
{showKhaltiDetails && !linkedMethods.khalti && (
Link Khalti
Open Khalti App
)}
{showKhaltiDetails && linkedMethods.khalti && renderLinkedKhaltiDetails()}
setShowImeDetails(!showImeDetails)}
className={`flex-row items-center justify-between p-4 gap-4`}
>
IME Pay
{renderLinkStatus(linkedMethods.ime)}
{showImeDetails && !linkedMethods.ime && (
Link IME Pay
Open IME Pay App
)}
{showImeDetails && linkedMethods.ime && renderLinkedImeDetails()}
)
}