Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 13x 5x 5x 13x 13x 13x 10x 50x 13x 3x 3x 2x 2x 2x 2x 2x 2x 3x 13x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 1x 1x 2x 2x 3x 13x 13x 1x 4x 3x | import React, { useMemo, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import {
Alert,
Box,
Button,
Checkbox,
Container,
Divider,
FormControl,
FormControlLabel,
InputLabel,
MenuItem,
Paper,
Select,
Stack,
TextField,
Typography,
} from '@mui/material';
import LockIcon from '@mui/icons-material/Lock';
import { useLanguage } from '../contexts/LanguageContext';
import { createOrderPayment } from '../db/api';
import CardBrandChips from './payment/CardBrandChips';
import PaymentMethodSelector from './payment/PaymentMethodSelector';
const METHOD_OPTIONS = ['credit', 'debit', 'pix', 'boleto'];
function detectCardBrand(cardNumber = '') {
const n = String(cardNumber).replace(/\D/g, '');
Iif (/^4/.test(n)) return 'visa';
Iif (/^(5[1-5]|2[2-7])/.test(n)) return 'mastercard';
Iif (/^3[47]/.test(n)) return 'amex';
Iif (/^(636297|636368|438935|504175|451416|506699|6362|5066|5090|6500|6516|6550)/.test(n)) return 'elo';
Iif (/^637601/.test(n)) return 'cabal';
Iif (/^5899/.test(n)) return 'verdecard';
Iif (/^637095/.test(n)) return 'hiper';
Iif (/^(6062|3841|637568)/.test(n)) return 'hipercard';
Iif (/^62/.test(n)) return 'unionpay';
Iif (/^(30[0-5]|36|38|39)/.test(n)) return 'diners';
return null;
}
function formatCardNumber(value = '') {
const digits = String(value).replace(/\D/g, '').slice(0, 19);
return digits.replace(/(.{4})/g, '$1 ').trim();
}
function formatExpiry(value = '') {
const digits = String(value).replace(/\D/g, '').slice(0, 4);
if (digits.length <= 2) return digits;
return `${digits.slice(0, 2)}/${digits.slice(2)}`;
}
const defaultCardData = {
holderName: '',
cardNumber: '',
expiry: '',
cvv: '',
installments: 1,
};
const PaymentsPage = ({ clearCart = () => {} }) => {
const { t } = useLanguage();
const navigate = useNavigate();
const location = useLocation();
const order = location.state?.order;
const cartItems = Array.isArray(location.state?.cartItems) ? location.state.cartItems : [];
const total = Number(order?.grand_total ?? 0);
const [loading, setLoading] = useState(false);
const [method, setMethod] = useState('credit');
const [splitEnabled, setSplitEnabled] = useState(false);
const [secondMethod, setSecondMethod] = useState('pix');
const [firstAmount, setFirstAmount] = useState(total > 0 ? Number((total / 2).toFixed(2)) : 0);
const [cardData, setCardData] = useState(defaultCardData);
const activeBrand = useMemo(
() => detectCardBrand(cardData.cardNumber),
[cardData.cardNumber]
);
const remaining = useMemo(() => {
const value = Number(total) - Number(firstAmount || 0);
return Number(value.toFixed(2));
}, [firstAmount, total]);
const ctaLabel = useMemo(() => {
Iif (method === 'pix') return t('payments.cta.generate_pix');
if (method === 'boleto') return t('payments.cta.generate_boleto');
return t('payments.cta.pay_now');
}, [method, t]);
const handleCardChange = (field) => (event) => {
let nextValue = event.target.value;
if (field === 'cardNumber') {
nextValue = formatCardNumber(nextValue);
}
if (field === 'expiry') {
nextValue = formatExpiry(nextValue);
}
if (field === 'cvv') {
nextValue = String(nextValue).replace(/\D/g, '').slice(0, 4);
}
setCardData((prev) => ({ ...prev, [field]: nextValue }));
};
const buildPaymentPayload = (methodValue, amountValue) => {
const payload = {
method: methodValue,
amount: amountValue,
};
if (methodValue === 'credit' || methodValue === 'debit') {
payload.holderName = cardData.holderName;
payload.cardNumber = cardData.cardNumber;
payload.expiry = cardData.expiry;
payload.cvv = cardData.cvv;
payload.installments = Number(cardData.installments || 1);
payload.cardBrand = activeBrand;
}
return payload;
};
const handleSubmit = async () => {
Iif (!order?.id) {
toast.error(t('payments.error.no_order'));
navigate('/cart');
return;
}
Iif (splitEnabled) {
if (!firstAmount || firstAmount <= 0 || remaining <= 0) {
toast.error(t('payments.error.invalid_split'));
return;
}
if (method === secondMethod) {
toast.error(t('payments.error.same_method'));
return;
}
}
try {
setLoading(true);
const payments = [];
const firstPayment = await createOrderPayment(order.id, buildPaymentPayload(method, splitEnabled ? firstAmount : total));
payments.push(firstPayment);
if (firstPayment.status === 'failed') {
toast.error('Pagamento negado. Tente outro método.');
return;
}
Iif (splitEnabled) {
const secondPayment = await createOrderPayment(order.id, buildPaymentPayload(secondMethod, remaining));
payments.push(secondPayment);
if (secondPayment.status === 'failed') {
toast.error('Segundo pagamento negado. Ajuste os métodos e tente novamente.');
return;
}
}
const hasPending = payments.some((p) => p.status === 'pending');
if (hasPending) {
toast.info(t('payments.pending'));
} else {
toast.success(t('payments.success'));
}
clearCart();
navigate('/thank-you', {
state: {
cartItems,
order: {
...order,
paymentStatus: hasPending ? 'pending' : 'authorized',
payments,
},
},
});
} catch (err) {
toast.error(err?.message || 'Falha ao processar pagamento');
} finally {
setLoading(false);
}
};
Iif (!order?.id) {
return (
<Container maxWidth="md">
<Alert severity="warning" sx={{ mt: 4 }}>
{t('payments.error.no_order')}
</Alert>
</Container>
);
}
return (
<Container maxWidth="lg" sx={{ mt: 2 }}>
<Stack direction={{ xs: 'column', md: 'row' }} spacing={2} alignItems="flex-start">
<Paper sx={{ flex: 1, p: 3, borderRadius: 3 }}>
<Typography variant="h4" fontWeight={700} gutterBottom>
{t('payments.title')}
</Typography>
<Typography color="text.secondary" sx={{ mb: 2 }}>
{t('payments.subtitle')}
</Typography>
<Typography variant="h6" sx={{ mb: 1 }}>{t('payments.method')}</Typography>
<PaymentMethodSelector value={method} onChange={setMethod} />
<FormControlLabel
sx={{ mt: 2 }}
control={<Checkbox checked={splitEnabled} onChange={(e) => setSplitEnabled(e.target.checked)} />}
label={t('payments.split.enable')}
/>
{splitEnabled && (
<Stack spacing={1.5} sx={{ mt: 1 }}>
<FormControl fullWidth size="small">
<InputLabel>{t('payments.split.second_method')}</InputLabel>
<Select
value={secondMethod}
label={t('payments.split.second_method')}
onChange={(e) => setSecondMethod(e.target.value)}
>
{METHOD_OPTIONS.filter((m) => m !== method).map((m) => (
<MenuItem key={m} value={m}>{m.toUpperCase()}</MenuItem>
))}
</Select>
</FormControl>
<TextField
label={t('payments.split.first_amount')}
type="number"
size="small"
value={firstAmount}
onChange={(e) => setFirstAmount(Number(e.target.value))}
inputProps={{ min: '0.01', step: '0.01', max: total }}
/>
<Typography variant="body2" color="text.secondary">
{t('payments.split.remaining')}: <strong>R$ {remaining.toFixed(2)}</strong>
</Typography>
</Stack>
)}
{(method === 'credit' || method === 'debit') && (
<Stack spacing={1.5} sx={{ mt: 2 }}>
<TextField
id="payments-card-holder-input"
label={t('payments.card.holder')}
size="small"
value={cardData.holderName}
onChange={handleCardChange('holderName')}
inputProps={{ autoComplete: 'cc-name' }}
/>
<TextField
id="payments-card-number-input"
label={t('payments.card.number')}
size="small"
value={cardData.cardNumber}
onChange={handleCardChange('cardNumber')}
inputProps={{ inputMode: 'numeric', autoComplete: 'cc-number' }}
helperText={cardData.cardNumber ? 'A bandeira é detectada automaticamente.' : 'Digite os números do cartão para identificar a bandeira.'}
/>
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={1.5}>
<TextField
id="payments-card-expiry-input"
label={t('payments.card.expiry')}
size="small"
value={cardData.expiry}
onChange={handleCardChange('expiry')}
inputProps={{ inputMode: 'numeric', autoComplete: 'cc-exp', placeholder: 'MM/AA' }}
/>
<TextField
id="payments-card-cvv-input"
label={t('payments.card.cvv')}
size="small"
value={cardData.cvv}
onChange={handleCardChange('cvv')}
inputProps={{ inputMode: 'numeric', autoComplete: 'cc-csc' }}
/>
{method === 'credit' && (
<TextField
id="payments-card-installments-input"
label={t('payments.card.installments')}
type="number"
size="small"
inputProps={{ min: 1, max: 12 }}
value={cardData.installments}
onChange={handleCardChange('installments')}
/>
)}
</Stack>
<CardBrandChips
activeBrand={activeBrand}
visible={Boolean(String(cardData.cardNumber).replace(/\D/g, '').length)}
/>
</Stack>
)}
{method === 'pix' && (
<Alert severity="info" sx={{ mt: 2 }}>{t('payments.pix.info')}</Alert>
)}
{method === 'boleto' && (
<Alert severity="info" sx={{ mt: 2 }}>{t('payments.boleto.info')}</Alert>
)}
<Divider sx={{ my: 2 }} />
<Button
fullWidth
variant="contained"
color="primary"
onClick={handleSubmit}
disabled={loading}
>
{loading ? 'Processando...' : ctaLabel}
</Button>
</Paper>
<Paper sx={{ width: { xs: '100%', md: 320 }, p: 3, borderRadius: 3, position: { md: 'sticky' }, top: { md: 88 } }}>
<Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 1 }}>
<LockIcon fontSize="small" color="success" />
<Typography fontWeight={700}>{t('payments.secure')}</Typography>
</Stack>
<Typography variant="h6" sx={{ mb: 1 }}>{t('payments.summary')}</Typography>
<Stack spacing={0.75}>
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
<Typography color="text.secondary">Pedido</Typography>
<Typography>#{order.id}</Typography>
</Box>
<Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
<Typography color="text.secondary">{t('payments.amount')}</Typography>
<Typography fontWeight={700}>R$ {total.toFixed(2)}</Typography>
</Box>
</Stack>
</Paper>
</Stack>
</Container>
);
};
export default PaymentsPage;
|