buyer data

This commit is contained in:
root
2025-12-21 11:12:02 +01:00
parent 67646c75a4
commit 514e04f43d
6 changed files with 359 additions and 23 deletions

View File

@@ -32,6 +32,9 @@ export default function Drop() {
const [selectedCurrency, setSelectedCurrency] = useState<string>('btc')
const [availableCurrencies, setAvailableCurrencies] = useState<string[]>([])
const [loadingCurrencies, setLoadingCurrencies] = useState(false)
const [buyerFullname, setBuyerFullname] = useState<string>('')
const [buyerAddress, setBuyerAddress] = useState<string>('')
const [buyerPhone, setBuyerPhone] = useState<string>('')
const [showConfirmModal, setShowConfirmModal] = useState(false)
const [showAuthModal, setShowAuthModal] = useState(false)
const [showPaymentModal, setShowPaymentModal] = useState(false)
@@ -192,29 +195,82 @@ export default function Drop() {
}
}
const fetchBuyerData = async () => {
try {
const response = await fetch('/api/buyer-data', {
credentials: 'include',
})
if (response.ok) {
const data = await response.json()
if (data.buyer_data) {
// Autofill form fields with existing buyer data
setBuyerFullname(data.buyer_data.fullname || '')
setBuyerAddress(data.buyer_data.address || '')
setBuyerPhone(data.buyer_data.phone || '')
}
}
} catch (error) {
console.error('Error fetching buyer data:', error)
}
}
const handleJoinDrop = () => {
// Check if user is logged in
if (!user) {
setShowAuthModal(true)
return
}
// Fetch available currencies when opening confirm modal
// Fetch available currencies and buyer data when opening confirm modal
fetchAvailableCurrencies()
fetchBuyerData()
setShowConfirmModal(true)
}
const handleLogin = (loggedInUser: User) => {
setUser(loggedInUser)
setShowAuthModal(false)
// After login, show the confirmation modal
// After login, fetch buyer data and show the confirmation modal
fetchAvailableCurrencies()
fetchBuyerData()
setShowConfirmModal(true)
}
const handleConfirmPurchase = async () => {
if (!drop) return
// Validate buyer data fields
if (!buyerFullname.trim() || !buyerAddress.trim() || !buyerPhone.trim()) {
setErrorMessage('Please fill in all delivery information (full name, address, and phone)')
setShowErrorModal(true)
return
}
setProcessing(true)
try {
// First, get or create buyer_data
const buyerDataResponse = await fetch('/api/buyer-data/get-or-create', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify({
fullname: buyerFullname.trim(),
address: buyerAddress.trim(),
phone: buyerPhone.trim(),
}),
})
if (!buyerDataResponse.ok) {
const error = await buyerDataResponse.json()
setErrorMessage(error.error || 'Failed to save delivery information')
setShowErrorModal(true)
setProcessing(false)
return
}
const buyerData = await buyerDataResponse.json()
// Create NOWPayments payment
const response = await fetch('/api/payments/create-invoice', {
method: 'POST',
@@ -226,6 +282,7 @@ export default function Drop() {
drop_id: drop.id,
size: selectedSize, // Size in grams
pay_currency: selectedCurrency, // Selected payment currency
buyer_data_id: buyerData.buyer_data_id, // Buyer delivery data ID
}),
})
@@ -481,9 +538,87 @@ export default function Drop() {
<p style={{ marginBottom: '12px', color: 'var(--muted)' }}>
<strong>Price per {drop.unit}:</strong> {(drop.ppu / 1000).toFixed(2)} CHF
</p>
{/* Delivery Information */}
<div style={{ marginTop: '24px', marginBottom: '16px' }}>
<h3 style={{ marginBottom: '16px', fontSize: '16px', color: 'var(--text)' }}>
Delivery Information
</h3>
<div style={{ marginBottom: '12px' }}>
<label style={{ display: 'block', marginBottom: '6px', fontSize: '14px', color: 'var(--muted)' }}>
<strong>Full Name *</strong>
</label>
<input
type="text"
value={buyerFullname}
onChange={(e) => setBuyerFullname(e.target.value)}
placeholder="Enter your full name"
required
style={{
width: '100%',
padding: '12px',
background: 'var(--bg-soft)',
border: '1px solid var(--border)',
borderRadius: '8px',
fontSize: '14px',
color: 'var(--text)',
boxSizing: 'border-box',
}}
/>
</div>
<div style={{ marginBottom: '12px' }}>
<label style={{ display: 'block', marginBottom: '6px', fontSize: '14px', color: 'var(--muted)' }}>
<strong>Address *</strong>
</label>
<textarea
value={buyerAddress}
onChange={(e) => setBuyerAddress(e.target.value)}
placeholder="Enter your delivery address"
required
rows={3}
style={{
width: '100%',
padding: '12px',
background: 'var(--bg-soft)',
border: '1px solid var(--border)',
borderRadius: '8px',
fontSize: '14px',
color: 'var(--text)',
fontFamily: 'inherit',
resize: 'vertical',
boxSizing: 'border-box',
}}
/>
</div>
<div style={{ marginBottom: '12px' }}>
<label style={{ display: 'block', marginBottom: '6px', fontSize: '14px', color: 'var(--muted)' }}>
<strong>Phone Number *</strong>
</label>
<input
type="tel"
value={buyerPhone}
onChange={(e) => setBuyerPhone(e.target.value)}
placeholder="Enter your phone number"
required
style={{
width: '100%',
padding: '12px',
background: 'var(--bg-soft)',
border: '1px solid var(--border)',
borderRadius: '8px',
fontSize: '14px',
color: 'var(--text)',
boxSizing: 'border-box',
}}
/>
</div>
</div>
{/* Currency Selection */}
<div style={{ marginBottom: '16px' }}>
<div style={{ marginTop: '24px', marginBottom: '16px' }}>
<label style={{ display: 'block', marginBottom: '8px', fontSize: '14px', color: 'var(--muted)' }}>
<strong>Payment Currency:</strong>
</label>